File: Argus_esp.c

package info (click to toggle)
argus 1%3A2.0.6.fixes.1-16.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,016 kB
  • ctags: 4,025
  • sloc: ansic: 23,022; sh: 5,734; makefile: 380; yacc: 255; lex: 234
file content (162 lines) | stat: -rwxr-xr-x 4,419 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (c) 2000-2004 QoSient, LLC
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#ifndef ArgusEsp
#define ArgusEsp
#endif


#include <stdio.h>
#include <ArgusModeler.h>

#include <errno.h>
#include <string.h>


struct esphdr {
   unsigned int spi, seq;
};


int
ArgusCreateESPFlow (struct ip *ip)
{
   int retn = 0;
   struct ArgusESPFlow *espFlow = &ArgusThisFlow->esp_flow;
   struct esphdr *esp = (struct esphdr *) ArgusThisUpHdr;

   ArgusThisDir = 0;

   if (STRUCTCAPTURED(*esp)) {
      espFlow->ip_src = ip->ip_src.s_addr;
      espFlow->ip_dst = ip->ip_dst.s_addr;
      espFlow->ip_p   = ip->ip_p;
      espFlow->spi    = esp->spi;

#ifdef _LITTLE_ENDIAN
      espFlow->spi    = ntohl(espFlow->spi);
#endif

      if (espFlow->ip_src < espFlow->ip_dst) {
         espFlow->ip_src = ip->ip_dst.s_addr;
         espFlow->ip_dst = ip->ip_src.s_addr;
         ArgusThisDir = 1;
      }

      retn = 1;
   }

#ifdef ARGUSDEBUG
   ArgusDebug (4, "ArgusCreateESPFlow(0x%x) returning %d\n", ip, retn);
#endif 

   return (retn);
}


void
ArgusUpdateESPState (struct ArgusFlowStruct *flowstr, unsigned char *state)
{
   struct esphdr *esp = (struct esphdr *) ArgusThisUpHdr;
   struct ArgusESPStruct *espObject = NULL;
   struct ArgusESPObject *ArgusThisEsp;

   ArgusTallyTime (flowstr, *state);

   if (STRUCTCAPTURED(*esp)) {

#ifdef _LITTLE_ENDIAN
      esp->spi = ntohl(esp->spi);
      esp->seq = ntohl(esp->seq);
#endif 

      if (*state == ARGUS_START) {
         if (flowstr->TransportDSRBuffer) 
            ArgusFree(flowstr->TransportDSRBuffer);
   
         if ((flowstr->TransportDSRBuffer = (struct ArgusESPStruct *) ArgusCalloc (1,
                                                 sizeof (struct ArgusESPStruct))) == NULL) {
            ArgusLog (LOG_ERR, "%s: ArgusUpdateEspState: ArgusCalloc failed %s\n", ArgusProgramName, strerror(errno));
   
         } else {
            espObject = flowstr->TransportDSRBuffer;
            espObject->type = ARGUS_ESP_DSR;
            espObject->length = sizeof(*espObject);
            espObject->status = ARGUS_START;
         }
         
         espObject->src.spi = esp->spi;
         espObject->dst.spi = esp->spi;

         if (flowstr->state.rev == ArgusThisDir)
            ArgusThisEsp = &espObject->src;
         else
            ArgusThisEsp = &espObject->dst;
   
         ArgusThisEsp->lastseq = esp->seq;
         
      } else {
         flowstr->ArgusTimeout = ARGUS_IPTIMEOUT;

         if ((espObject = flowstr->TransportDSRBuffer) != NULL) {
            if (flowstr->state.rev == ArgusThisDir)
               ArgusThisEsp = &espObject->src;
            else
               ArgusThisEsp = &espObject->dst;
   
            if (esp->spi == ArgusThisEsp->spi) {
               if (ArgusThisEsp->lastseq && (esp->seq != (ArgusThisEsp->lastseq + 1))) {
                  ArgusThisEsp->lostseq++;
                  espObject->status |= ARGUS_SRC_PKTS_RETRANS;
               }
   
               ArgusThisEsp->lastseq = esp->seq;
            }
         }
      }
   }
   
#ifdef ARGUSDEBUG
   ArgusDebug (8, "ArgusUpdateESPState(0x%x, %d) returning\n", flowstr, *state);
#endif 
}


#include <argus_out.h>

void
ArgusESPFlowRecord (struct ArgusFlowStruct *flow, struct ArgusRecord *argus, unsigned char state)
{
   int length = 0;
   struct ArgusESPStruct *esp = (struct ArgusESPStruct *) flow->TransportDSRBuffer;

   if (esp && ((length = argus->ahdr.length) > 0)) {
      bcopy ((char *)esp, &((char *)argus)[argus->ahdr.length], sizeof(*esp));
      argus->ahdr.length += sizeof(*esp);

      esp->src.lostseq = 0;
      esp->dst.lostseq = 0;
   }


#ifdef ARGUSDEBUG
   ArgusDebug (4, "ArgusESPFlowRecord(0x%x, 0x%x, %d) returning\n", flow, argus, state);
#endif 
}