~ubuntu-branches/ubuntu/maverick/ntop/maverick

« back to all changes in this revision

Viewing changes to ntop/traffic.c

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 1998-2001 Luca Deri <deri@ntop.org>
 
3
 *                          Portions by Stefano Suin <stefano@ntop.org>
 
4
 *
 
5
 *                          http://www.ntop.org/
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "ntop.h"
 
23
 
 
24
 
 
25
/* ******************************* */
 
26
 
 
27
static void updateThptStats(int deviceToUpdate,
 
28
                            u_int topSentIdx, u_int secondSentIdx,  u_int thirdSentIdx,
 
29
                            u_int topHourSentIdx, u_int secondHourSentIdx,
 
30
                            u_int thirdHourSentIdx,
 
31
                            u_int topRcvdIdx, u_int secondRcvdIdx, u_int thirdRcvdIdx,
 
32
                            u_int topHourRcvdIdx, u_int secondHourRcvdIdx,
 
33
                            u_int thirdHourRcvdIdx) {
 
34
  int i;
 
35
 
 
36
#ifdef DEBUG
 
37
  traceEvent(TRACE_INFO, "updateThptStats(%d, %d, %d, %d, %d, %d)\n",
 
38
         topSentIdx, secondSentIdx, thirdSentIdx,
 
39
         topHourSentIdx, secondHourSentIdx,
 
40
         thirdHourSentIdx);
 
41
#endif
 
42
 
 
43
  /* We never check enough... */
 
44
  if(topSentIdx == NO_PEER) 
 
45
    return;
 
46
 
 
47
  if(topRcvdIdx == NO_PEER) 
 
48
    return;
 
49
 
 
50
  if(secondSentIdx == NO_PEER) 
 
51
    secondSentIdx = 0;
 
52
 
 
53
  if(thirdSentIdx == NO_PEER)
 
54
    thirdSentIdx = 0;
 
55
 
 
56
  if(secondRcvdIdx == NO_PEER)
 
57
    secondRcvdIdx = 0;
 
58
 
 
59
  if(thirdRcvdIdx == NO_PEER)
 
60
    thirdRcvdIdx = 0;
 
61
 
 
62
  for(i=58; i>=0; i--)
 
63
    memcpy(&device[deviceToUpdate].last60MinutesThpt[i+1],
 
64
           &device[deviceToUpdate].last60MinutesThpt[i], sizeof(ThptEntry));
 
65
 
 
66
  device[deviceToUpdate].last60MinutesThpt[0].trafficValue = device[deviceToUpdate].lastMinThpt;
 
67
 
 
68
#ifdef DEBUG
 
69
  traceEvent(TRACE_INFO, "LastMinThpt: %s", formatThroughput(device[deviceToUpdate].lastMinThpt));
 
70
#endif
 
71
 
 
72
  device[deviceToUpdate].last60MinutesThpt[0].topHostSentIdx = topSentIdx,
 
73
    device[deviceToUpdate].last60MinutesThpt[0].topSentTraffic = 
 
74
    device[deviceToUpdate].hash_hostTraffic[topSentIdx]->actualSentThpt;
 
75
  device[deviceToUpdate].last60MinutesThpt[0].secondHostSentIdx = secondSentIdx,
 
76
    device[deviceToUpdate].last60MinutesThpt[0].secondSentTraffic = 
 
77
    device[deviceToUpdate].hash_hostTraffic[secondSentIdx]->actualSentThpt;
 
78
  device[deviceToUpdate].last60MinutesThpt[0].thirdHostSentIdx = thirdSentIdx,
 
79
    device[deviceToUpdate].last60MinutesThpt[0].thirdSentTraffic = 
 
80
    device[deviceToUpdate].hash_hostTraffic[thirdSentIdx]->actualSentThpt;
 
81
 
 
82
  device[deviceToUpdate].last60MinutesThpt[0].topHostRcvdIdx = topRcvdIdx,
 
83
    device[deviceToUpdate].last60MinutesThpt[0].topRcvdTraffic = 
 
84
    device[deviceToUpdate].hash_hostTraffic[topRcvdIdx]->actualRcvdThpt;
 
85
  device[deviceToUpdate].last60MinutesThpt[0].secondHostRcvdIdx = secondRcvdIdx,
 
86
    device[deviceToUpdate].last60MinutesThpt[0].secondRcvdTraffic = 
 
87
    device[deviceToUpdate].hash_hostTraffic[secondRcvdIdx]->actualRcvdThpt;
 
88
  device[deviceToUpdate].last60MinutesThpt[0].thirdHostRcvdIdx = thirdRcvdIdx,
 
89
    device[deviceToUpdate].last60MinutesThpt[0].thirdRcvdTraffic = 
 
90
    device[deviceToUpdate].hash_hostTraffic[thirdRcvdIdx]->actualRcvdThpt;
 
91
 
 
92
  device[deviceToUpdate].last60MinutesThptIdx = (device[deviceToUpdate].last60MinutesThptIdx+1) % 60;
 
93
 
 
94
  if(topHourSentIdx != NO_PEER) { /* It wrapped -> 1 hour is over */
 
95
    float average=0;
 
96
    int i;
 
97
 
 
98
    if(topHourSentIdx == NO_PEER) return;
 
99
    if(topHourRcvdIdx == NO_PEER) return;
 
100
    if(secondHourSentIdx == NO_PEER) secondHourSentIdx = 0;
 
101
    if(thirdHourSentIdx == NO_PEER)  thirdHourSentIdx = 0;
 
102
    if(secondHourRcvdIdx == NO_PEER) secondHourRcvdIdx = 0;
 
103
    if(thirdHourRcvdIdx == NO_PEER)  thirdHourRcvdIdx = 0;
 
104
 
 
105
    for(i=0; i<60; i++) {
 
106
      average += device[deviceToUpdate].last60MinutesThpt[i].trafficValue;
 
107
    }
 
108
 
 
109
    average /= 60;
 
110
 
 
111
    for(i=22; i>=0; i--)
 
112
      memcpy(&device[deviceToUpdate].last24HoursThpt[i+1], 
 
113
             &device[deviceToUpdate].last24HoursThpt[i], sizeof(ThptEntry));
 
114
 
 
115
    device[deviceToUpdate].last24HoursThpt[0].trafficValue = average;
 
116
 
 
117
    device[deviceToUpdate].last24HoursThpt[0].topHostSentIdx = topHourSentIdx,
 
118
      device[deviceToUpdate].last24HoursThpt[0].topSentTraffic = 
 
119
      device[deviceToUpdate].hash_hostTraffic[topHourSentIdx]->lastHourSentThpt;
 
120
    device[deviceToUpdate].last24HoursThpt[0].secondHostSentIdx = secondHourSentIdx,
 
121
      device[deviceToUpdate].last24HoursThpt[0].secondSentTraffic = 
 
122
      device[deviceToUpdate].hash_hostTraffic[secondHourSentIdx]->lastHourSentThpt;
 
123
    device[deviceToUpdate].last24HoursThpt[0].thirdHostSentIdx = thirdHourSentIdx,
 
124
      device[deviceToUpdate].last24HoursThpt[0].thirdSentTraffic = 
 
125
      device[deviceToUpdate].hash_hostTraffic[thirdHourSentIdx]->lastHourSentThpt;
 
126
 
 
127
    device[deviceToUpdate].last24HoursThpt[0].topHostRcvdIdx = topHourRcvdIdx,
 
128
      device[deviceToUpdate].last24HoursThpt[0].topRcvdTraffic = 
 
129
      device[deviceToUpdate].hash_hostTraffic[topHourRcvdIdx]->lastHourRcvdThpt;
 
130
    device[deviceToUpdate].last24HoursThpt[0].secondHostRcvdIdx = secondHourRcvdIdx,
 
131
      device[deviceToUpdate].last24HoursThpt[0].secondRcvdTraffic = 
 
132
      device[deviceToUpdate].hash_hostTraffic[secondHourRcvdIdx]->lastHourRcvdThpt;
 
133
    device[deviceToUpdate].last24HoursThpt[0].thirdHostRcvdIdx = thirdHourRcvdIdx,
 
134
      device[deviceToUpdate].last24HoursThpt[0].thirdRcvdTraffic = 
 
135
      device[deviceToUpdate].hash_hostTraffic[thirdHourRcvdIdx]->lastHourRcvdThpt;
 
136
 
 
137
    device[deviceToUpdate].last24HoursThptIdx = 
 
138
      (device[deviceToUpdate].last24HoursThptIdx + 1) % 24;
 
139
 
 
140
    if(device[deviceToUpdate].last24HoursThptIdx == 0) {
 
141
      average=0;
 
142
 
 
143
      for(i=0; i<24; i++) {
 
144
        average += device[deviceToUpdate].last24HoursThpt[i].trafficValue;
 
145
      }
 
146
 
 
147
      average /= 24;
 
148
 
 
149
      for(i=28; i>=0; i--) {
 
150
        device[deviceToUpdate].last30daysThpt[i+1] = 
 
151
          device[deviceToUpdate].last30daysThpt[i];
 
152
      }
 
153
 
 
154
      device[deviceToUpdate].last30daysThpt[0] = average;
 
155
      device[deviceToUpdate].last30daysThptIdx = 
 
156
        (device[deviceToUpdate].last30daysThptIdx + 1) % 30;
 
157
    }
 
158
  }
 
159
 
 
160
  device[deviceToUpdate].numThptSamples++;
 
161
  
 
162
#ifdef DEBUG
 
163
  traceEvent(TRACE_INFO, "updateThptStats() completed.\n");
 
164
#endif
 
165
}
 
166
 
 
167
/* ******************************* */
 
168
 
 
169
static void updateDeviceThpt(int deviceToUpdate) {
 
170
  time_t timeDiff, timeMinDiff, timeHourDiff=0, totalTime;
 
171
  u_int idx;
 
172
  HostTraffic *el;
 
173
 
 
174
  timeDiff = actTime-device[deviceToUpdate].lastThptUpdate;
 
175
 
 
176
  if(timeDiff > 10 /* secs */) {
 
177
    u_int topSentIdx=NO_PEER, secondSentIdx=NO_PEER, thirdSentIdx=NO_PEER;
 
178
    u_int topHourSentIdx=NO_PEER, secondHourSentIdx=NO_PEER, thirdHourSentIdx=NO_PEER;
 
179
    u_int topRcvdIdx=NO_PEER, secondRcvdIdx=NO_PEER, thirdRcvdIdx=NO_PEER;
 
180
    u_int topHourRcvdIdx=NO_PEER, secondHourRcvdIdx=NO_PEER, thirdHourRcvdIdx=NO_PEER;
 
181
    short updateMinThpt, updateHourThpt;
 
182
    
 
183
    totalTime = actTime-initialSniffTime;
 
184
 
 
185
    updateHourThpt = 0;
 
186
    updateMinThpt = 0;
 
187
 
 
188
    if((timeMinDiff = actTime-device[deviceToUpdate].lastMinThptUpdate) >= 60 /* 1 minute */) {
 
189
      updateMinThpt = 1;
 
190
      device[deviceToUpdate].lastMinThptUpdate = actTime;
 
191
      if((timeHourDiff = actTime-device[deviceToUpdate].lastHourThptUpdate) >= 60*60 /* 1 hour */) {
 
192
        updateHourThpt = 1;
 
193
        device[deviceToUpdate].lastHourThptUpdate = actTime;
 
194
      }
 
195
    }
 
196
 
 
197
    for(idx=1; idx<device[deviceToUpdate].actualHashSize; idx++) {
 
198
      if((el = device[deviceToUpdate].hash_hostTraffic[idx]) != NULL) {
 
199
 
 
200
        if(broadcastHost(el))
 
201
          continue;
 
202
 
 
203
        el->actualRcvdThpt       = (float)(el->bytesReceived-el->lastBytesReceived)/timeDiff;
 
204
        if(el->peakRcvdThpt      < el->actualRcvdThpt) el->peakRcvdThpt = el->actualRcvdThpt;
 
205
        if(el->peakSentThpt      < el->actualSentThpt) el->peakSentThpt = el->actualSentThpt;
 
206
        el->actualSentThpt       = (float)(el->bytesSent-el->lastBytesSent)/timeDiff;
 
207
        el->lastBytesSent        = el->bytesSent;
 
208
        el->lastBytesReceived    = el->bytesReceived;
 
209
 
 
210
        /* ******************************** */
 
211
 
 
212
        el->actualRcvdPktThpt    = (float)(el->pktReceived-el->lastPktReceived)/timeDiff;
 
213
        if(el->peakRcvdPktThpt   < el->actualRcvdPktThpt) el->peakRcvdPktThpt = el->actualRcvdPktThpt;
 
214
        if(el->peakSentPktThpt   < el->actualSentPktThpt) el->peakSentPktThpt = el->actualSentPktThpt;
 
215
        el->actualSentPktThpt    = (float)(el->pktSent-el->lastPktSent)/timeDiff;
 
216
        el->lastPktSent          = el->pktSent;
 
217
        el->lastPktReceived      = el->pktReceived;
 
218
 
 
219
        /* ******************************** */
 
220
 
 
221
        if(updateMinThpt) {
 
222
          el->averageRcvdThpt    = ((float)el->bytesReceived)/totalTime;
 
223
          el->averageSentThpt    = ((float)el->bytesSent)/totalTime;
 
224
          el->averageRcvdPktThpt = ((float)el->pktReceived)/totalTime;
 
225
          el->averageSentPktThpt = ((float)el->pktSent)/totalTime;
 
226
 
 
227
          if(topSentIdx == NO_PEER) {
 
228
            topSentIdx = idx;
 
229
          } else {
 
230
            if(el->actualSentThpt > device[deviceToUpdate].hash_hostTraffic[topSentIdx]->actualSentThpt) {
 
231
              secondSentIdx = topSentIdx;
 
232
              topSentIdx = idx;
 
233
            } else {
 
234
              if(secondSentIdx == NO_PEER)
 
235
                secondSentIdx = idx;
 
236
              else {
 
237
                if(el->actualSentThpt > device[deviceToUpdate].hash_hostTraffic[secondSentIdx]->actualSentThpt) {
 
238
                  thirdSentIdx = secondSentIdx;
 
239
                  secondSentIdx = idx;
 
240
                } else {
 
241
                  if(thirdSentIdx == NO_PEER)
 
242
                    thirdSentIdx = idx;
 
243
                  else {
 
244
                    if(el->actualSentThpt > device[deviceToUpdate].hash_hostTraffic[thirdSentIdx]->actualSentThpt) {
 
245
                      thirdSentIdx = idx;
 
246
                    }
 
247
                  }
 
248
                }
 
249
              }
 
250
            }
 
251
          }
 
252
 
 
253
          if(topRcvdIdx == NO_PEER) {
 
254
            topRcvdIdx = idx;
 
255
          } else {
 
256
            if(el->actualRcvdThpt > device[deviceToUpdate].hash_hostTraffic[topRcvdIdx]->actualRcvdThpt) {
 
257
              secondRcvdIdx = topRcvdIdx;
 
258
              topRcvdIdx = idx;
 
259
            } else {
 
260
              if(secondRcvdIdx == NO_PEER)
 
261
                secondRcvdIdx = idx;
 
262
              else {
 
263
                if(el->actualRcvdThpt > device[deviceToUpdate].hash_hostTraffic[secondRcvdIdx]->actualRcvdThpt) {
 
264
                  thirdRcvdIdx = secondRcvdIdx;
 
265
                  secondRcvdIdx = idx;
 
266
                } else {
 
267
                  if(thirdRcvdIdx == NO_PEER)
 
268
                    thirdRcvdIdx = idx;
 
269
                  else {
 
270
                    if(el->actualRcvdThpt > device[deviceToUpdate].
 
271
                       hash_hostTraffic[thirdRcvdIdx]->actualRcvdThpt) {
 
272
                      thirdRcvdIdx = idx;
 
273
                    }
 
274
                  }
 
275
                }
 
276
              }
 
277
            }
 
278
          }
 
279
 
 
280
          if(updateHourThpt) {
 
281
            el->lastHourRcvdThpt = (float)(el->bytesReceived-el->lastHourBytesReceived)/timeHourDiff;
 
282
            el->lastHourSentThpt = (float)(el->bytesSent-el->lastHourBytesSent)/timeHourDiff;
 
283
            el->lastHourBytesReceived = el->bytesReceived;
 
284
            el->lastHourBytesSent = el->bytesSent;
 
285
 
 
286
            if(topHourSentIdx == NO_PEER) {
 
287
              topHourSentIdx = idx;
 
288
            } else {
 
289
              if(el->lastHourSentThpt > device[deviceToUpdate].
 
290
                 hash_hostTraffic[topHourSentIdx]->lastHourSentThpt) {
 
291
                secondHourSentIdx = topHourSentIdx;
 
292
                topHourSentIdx = idx;
 
293
              } else {
 
294
                if(secondHourSentIdx == NO_PEER)
 
295
                  secondHourSentIdx = idx;
 
296
                else {
 
297
                  if(el->lastHourSentThpt > device[deviceToUpdate].
 
298
                     hash_hostTraffic[secondHourSentIdx]->lastHourSentThpt) {
 
299
                    thirdHourSentIdx = secondHourSentIdx;
 
300
                    secondHourSentIdx = idx;
 
301
                  } else {
 
302
                    if(thirdHourSentIdx == NO_PEER)
 
303
                      thirdHourSentIdx = idx;
 
304
                    else {
 
305
                      if(el->lastHourSentThpt > device[deviceToUpdate].
 
306
                         hash_hostTraffic[thirdHourSentIdx]->lastHourSentThpt) {
 
307
                        thirdHourSentIdx = idx;
 
308
                      }
 
309
                    }
 
310
                  }
 
311
                }
 
312
              }
 
313
            }
 
314
 
 
315
            if(topHourRcvdIdx == NO_PEER) {
 
316
              topHourRcvdIdx = idx;
 
317
            } else {
 
318
              if(el->lastHourRcvdThpt > device[deviceToUpdate].
 
319
                 hash_hostTraffic[topHourRcvdIdx]->lastHourRcvdThpt) {
 
320
                secondHourRcvdIdx = topHourRcvdIdx;
 
321
                topHourRcvdIdx = idx;
 
322
              } else {
 
323
                if(secondHourRcvdIdx == NO_PEER)
 
324
                  secondHourRcvdIdx = idx;
 
325
                else {
 
326
                  if(el->lastHourRcvdThpt > device[deviceToUpdate].
 
327
                     hash_hostTraffic[secondHourRcvdIdx]->lastHourRcvdThpt) {
 
328
                    thirdHourRcvdIdx = secondHourRcvdIdx;
 
329
                    secondHourRcvdIdx = idx;
 
330
                  } else {
 
331
                    if(thirdHourRcvdIdx == NO_PEER)
 
332
                      thirdHourRcvdIdx = idx;
 
333
                    else {
 
334
                      if(el->lastHourRcvdThpt > device[deviceToUpdate].
 
335
                         hash_hostTraffic[thirdHourRcvdIdx]->lastHourRcvdThpt) {
 
336
                        thirdHourRcvdIdx = idx;
 
337
                      }
 
338
                    }
 
339
                  }
 
340
                }
 
341
              }
 
342
            }
 
343
          }
 
344
        }
 
345
      }
 
346
    }
 
347
 
 
348
    /* ******************************** */
 
349
 
 
350
    device[deviceToUpdate].throughput =
 
351
      device[deviceToUpdate].ethernetBytes-device[deviceToUpdate].throughput;
 
352
    device[deviceToUpdate].packetThroughput = device[deviceToUpdate].ethernetPkts-
 
353
      device[deviceToUpdate].lastNumEthernetPkts;
 
354
    device[deviceToUpdate].lastNumEthernetPkts = device[deviceToUpdate].ethernetPkts;
 
355
 
 
356
    /* timeDiff++; */
 
357
    device[deviceToUpdate].actualThpt = (float)device[deviceToUpdate].throughput/(float)timeDiff;
 
358
    device[deviceToUpdate].actualPktsThpt = 
 
359
      (float)device[deviceToUpdate].packetThroughput/(float)timeDiff;
 
360
 
 
361
    if(device[deviceToUpdate].actualThpt > device[deviceToUpdate].peakThroughput)
 
362
      device[deviceToUpdate].peakThroughput = device[deviceToUpdate].actualThpt;
 
363
 
 
364
    if(device[deviceToUpdate].actualPktsThpt > device[deviceToUpdate].peakPacketThroughput)
 
365
      device[deviceToUpdate].peakPacketThroughput = device[deviceToUpdate].actualPktsThpt;
 
366
 
 
367
    device[deviceToUpdate].throughput = device[deviceToUpdate].ethernetBytes;
 
368
    device[deviceToUpdate].packetThroughput = device[deviceToUpdate].ethernetPkts;
 
369
 
 
370
    if(updateMinThpt) {
 
371
      device[deviceToUpdate].lastMinEthernetBytes = device[deviceToUpdate].ethernetBytes-
 
372
        device[deviceToUpdate].lastMinEthernetBytes;
 
373
      device[deviceToUpdate].lastMinThpt = 
 
374
        (float)(device[deviceToUpdate].lastMinEthernetBytes)/(float)timeMinDiff;
 
375
      device[deviceToUpdate].lastMinEthernetBytes = device[deviceToUpdate].ethernetBytes;
 
376
      /* ******************* */
 
377
      device[deviceToUpdate].lastMinEthernetPkts = device[deviceToUpdate].ethernetPkts-
 
378
        device[deviceToUpdate].lastMinEthernetPkts;
 
379
      device[deviceToUpdate].lastMinPktsThpt = 
 
380
        (float)device[deviceToUpdate].lastMinEthernetPkts/(float)timeMinDiff;
 
381
      device[deviceToUpdate].lastMinEthernetPkts = device[deviceToUpdate].ethernetPkts;
 
382
      device[deviceToUpdate].lastMinThptUpdate = actTime;
 
383
    }
 
384
 
 
385
    if((timeMinDiff = actTime-device[deviceToUpdate].lastFiveMinsThptUpdate) > 300 /* 5 minutes */) {
 
386
      device[deviceToUpdate].lastFiveMinsEthernetBytes = 
 
387
        device[deviceToUpdate].ethernetBytes - device[deviceToUpdate].lastFiveMinsEthernetBytes;
 
388
      device[deviceToUpdate].lastFiveMinsThptUpdate = timeMinDiff;
 
389
      device[deviceToUpdate].lastFiveMinsThpt = 
 
390
        (float)device[deviceToUpdate].lastFiveMinsEthernetBytes/(float)device[deviceToUpdate].lastFiveMinsThptUpdate;
 
391
      device[deviceToUpdate].lastFiveMinsEthernetBytes = device[deviceToUpdate].ethernetBytes;
 
392
      /* ******************* */
 
393
      device[deviceToUpdate].lastFiveMinsEthernetPkts = 
 
394
        device[deviceToUpdate].ethernetPkts - device[deviceToUpdate].lastFiveMinsEthernetPkts;
 
395
      device[deviceToUpdate].lastFiveMinsPktsThpt = 
 
396
        (float)device[deviceToUpdate].lastFiveMinsEthernetPkts/(float)device[deviceToUpdate].lastFiveMinsThptUpdate;
 
397
      device[deviceToUpdate].lastFiveMinsEthernetPkts = device[deviceToUpdate].ethernetPkts;
 
398
      device[deviceToUpdate].lastFiveMinsThptUpdate = actTime;
 
399
    }
 
400
 
 
401
    if((updateMinThpt || updateHourThpt) 
 
402
       && ((topSentIdx        != NO_PEER) 
 
403
           || (topHourSentIdx != NO_PEER)
 
404
           || (topRcvdIdx     != NO_PEER)
 
405
           || (topHourRcvdIdx != NO_PEER)))
 
406
      updateThptStats(deviceToUpdate,
 
407
                      topSentIdx, secondSentIdx, thirdSentIdx,
 
408
                      topHourSentIdx, secondHourSentIdx, thirdHourSentIdx,
 
409
                      topRcvdIdx, secondRcvdIdx, thirdRcvdIdx,
 
410
                      topHourRcvdIdx, secondHourRcvdIdx, thirdHourRcvdIdx);
 
411
 
 
412
    device[deviceToUpdate].lastThptUpdate = actTime;
 
413
  }
 
414
}
 
415
 
 
416
/* ******************************* */
 
417
 
 
418
void updateThpt(void) {
 
419
  int i;
 
420
 
 
421
  if(mergeInterfaces)
 
422
    updateDeviceThpt(0);
 
423
  else {
 
424
    for(i=0; i<numDevices; i++)
 
425
      updateDeviceThpt(i);  
 
426
  }
 
427
}
 
428
 
 
429
/* ******************************* */
 
430
 
 
431
static void updateHostThpt(HostTraffic *el, int hourId) {
 
432
 
 
433
  if(broadcastHost(el))
 
434
    return;
 
435
  
 
436
  el->lastCounterBytesSent = el->bytesSent;
 
437
  el->lastCounterBytesRcvd = el->bytesReceived;
 
438
 
 
439
  if(hourId == 0) {
 
440
    el->lastDayBytesSent = el->bytesSent;
 
441
      el->lastDayBytesRcvd = el->bytesReceived;
 
442
  }
 
443
}
 
444
 
 
445
/* ******************************* */
 
446
 
 
447
static void updateHostsDeviceThpt(int deviceToUpdate, int hourId) {
 
448
  u_int idx;
 
449
  HostTraffic *el;
 
450
  
 
451
  for(idx=1; idx<device[deviceToUpdate].actualHashSize; idx++) {
 
452
    if((el = device[deviceToUpdate].hash_hostTraffic[idx]) != NULL) {
 
453
      updateHostThpt(el, hourId);
 
454
    }
 
455
  }
 
456
}
 
457
 
 
458
/* ******************************* */
 
459
 
 
460
void updateHostTrafficStatsThpt(int hourId) {
 
461
  int i;
 
462
 
 
463
  if(mergeInterfaces)
 
464
    updateHostsDeviceThpt(0, hourId);
 
465
  else {
 
466
    for(i=0; i<numDevices; i++)
 
467
      updateHostsDeviceThpt(i, hourId);  
 
468
  }
 
469
}
 
470
 
 
471
/* ******************************* */
 
472
 
 
473
void updateTrafficMatrix(HostTraffic *srcHost,
 
474
                         HostTraffic *dstHost,
 
475
                         TrafficCounter length) {
 
476
  if(subnetLocalHost(srcHost) && subnetLocalHost(dstHost)) {
 
477
    unsigned long a, b, id;    
 
478
 
 
479
    a = (unsigned long)(srcHost->hostIpAddress.s_addr) % device[actualDeviceId].numHosts;
 
480
    b = (unsigned long)(dstHost->hostIpAddress.s_addr) % device[actualDeviceId].numHosts;
 
481
 
 
482
    device[actualDeviceId].ipTrafficMatrixHosts[a] = srcHost, 
 
483
      device[actualDeviceId].ipTrafficMatrixHosts[b] = dstHost;
 
484
 
 
485
    id = a*device[actualDeviceId].numHosts+b;
 
486
    if(device[actualDeviceId].ipTrafficMatrix[id] == NULL)
 
487
      device[actualDeviceId].ipTrafficMatrix[id] = (TrafficEntry*)calloc(1, sizeof(TrafficEntry));
 
488
    device[actualDeviceId].ipTrafficMatrix[id]->bytesSent += length;
 
489
 
 
490
    id = b*device[actualDeviceId].numHosts+a;
 
491
    if(device[actualDeviceId].ipTrafficMatrix[id] == NULL)
 
492
      device[actualDeviceId].ipTrafficMatrix[id] = (TrafficEntry*)calloc(1, sizeof(TrafficEntry));
 
493
    device[actualDeviceId].ipTrafficMatrix[id]->bytesReceived += length;
 
494
  }
 
495
}
 
496
 
 
497
/* *********************************** */
 
498
 
 
499
void updateDbHostsTraffic(int deviceToUpdate) {
 
500
  u_int i;
 
501
  HostTraffic *el;
 
502
 
 
503
#ifndef DEBUG
 
504
  traceEvent(TRACE_INFO, "updateDbHostsTraffic(device=%d)", deviceToUpdate);
 
505
#endif
 
506
 
 
507
  for(i=0; i<device[deviceToUpdate].actualHashSize; i++) {
 
508
    el = device[deviceToUpdate].hash_hostTraffic[i]; /* (**) */
 
509
 
 
510
    if((el != NULL)
 
511
       && (!broadcastHost(el))
 
512
       && (el->nextDBupdate < actTime)) {
 
513
 
 
514
      el->instanceInUse++;
 
515
 
 
516
      if(el->nextDBupdate == 0) {
 
517
        /* traceEvent(TRACE_INFO, "1"); */
 
518
        notifyHostCreation(el);
 
519
#ifdef HAVE_MYSQL
 
520
        mySQLnotifyHostCreation(el);
 
521
#endif
 
522
        /* traceEvent(TRACE_INFO, "2"); */
 
523
      } else if(el->nextDBupdate < actTime) {
 
524
        /* traceEvent(TRACE_INFO, "3"); */
 
525
        updateHostTraffic(el);
 
526
        /* traceEvent(TRACE_INFO, "4"); */
 
527
#ifdef HAVE_MYSQL
 
528
        mySQLupdateHostTraffic(el);
 
529
#endif
 
530
        /* traceEvent(TRACE_INFO, "5"); */
 
531
        if(el->osName == NULL) {
 
532
          /* traceEvent(TRACE_INFO, "6"); */
 
533
          updateOSName(el);
 
534
          /* traceEvent(TRACE_INFO, "7"); */
 
535
        }
 
536
      }
 
537
 
 
538
      el->nextDBupdate = actTime + DB_TIMEOUT_REFRESH_TIME;
 
539
      el->instanceInUse--;
 
540
    }
 
541
  }
 
542
}
 
543
 
 
544
/* ************************ */
 
545
 
 
546
int isInitialHttpData(char* packetData) {
 
547
  /* GET / HTTP/1.0 */
 
548
  if((strncmp(packetData,    "GET ",     4) == 0) /* HTTP/1.0 */
 
549
     || (strncmp(packetData, "HEAD ",    5) == 0)
 
550
     || (strncmp(packetData, "LINK ",    5) == 0)
 
551
     || (strncmp(packetData, "POST ",    5) == 0)
 
552
     || (strncmp(packetData, "OPTIONS ", 8) == 0) /* HTTP/1.1 */
 
553
     || (strncmp(packetData, "PUT ",     4) == 0)
 
554
     || (strncmp(packetData, "DELETE ",  7) == 0)
 
555
     || (strncmp(packetData, "TRACE ",   6) == 0)
 
556
     || (strncmp(packetData, "PROPFIND", 8) == 0) /* RFC 2518 */
 
557
     ) 
 
558
    return(1);
 
559
  else
 
560
    return(0);
 
561
}
 
562
 
 
563
/* ************************ */
 
564
 
 
565
int isInitialSshData(char* packetData) {
 
566
  /* SSH-1.99-OpenSSH_2.1.1 */
 
567
  if(strncmp(packetData, "SSH-", 4) == 0)
 
568
    return(1);
 
569
  else
 
570
    return(0);
 
571
}
 
572
 
 
573
/* ************************ */
 
574
 
 
575
int isInitialFtpData(char* packetData) {
 
576
  /* 220 linux.local FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.16) ready. */
 
577
  if((strncmp(packetData, "220 ", 4) == 0)
 
578
     || (strncmp(packetData, "530", 3) == 0))
 
579
    return(1);
 
580
  else
 
581
    return(0);
 
582
}