~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to plugins/pdaPlugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovico Cavedon, Jordan Metzmeier, Ludovico Cavedon
  • Date: 2010-12-15 20:06:19 UTC
  • mfrom: (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101215200619-0ojz3iak95ihibun
Tags: 3:4.0.3+dfsg1-1
[ Jordan Metzmeier ]
* New upstream release (Closes: #522042)
* Move data files to /usr/share/ntop (Closes: #595450).
* Package architecture independent data in a separate ntop-data package.
* Use debhelper 7.
* Update Standards-Version to 3.9.1.
* Depend on python-mako.
* Do not include ntop.txt in binary packages as it is a copy of the man
  page.
* Do not include NEWS, as it is outdated.
* Switch to package source version 3.0 (quilt).
* Add password creation to debconf
* Changed init script to fix localization problems (thanks to Alejandro
  Varas <alej0varas@gmail.com>, LP: #257466)
* Remove manual update-rc.d calls from postrm and postinst. debhelper adds
  this for us.
* Add pre-depends on adduser for postinst script.
* Fix errors in the manpages: fix-manpage-errors.patch.
* Added fixes for matching active interfaces.
* Added a watch file.

[ Ludovico Cavedon ]
* Remove direct changes to upstream tree, and move them into specific patch
  files:
  - fix-manpage-errors.patch: fix typos in ntop.8.
  - dot-path.patch: fix path of /usr/bin/dot executable
* Add patches:
  - reduce-autogen-purged-files.patch: prevent agutogen.sh from reamoving
  too many files during cleanup.
  - Add build-without-ntop-darwin.patch, to fix compilation without
  ntop_darwin.c.
* No longer add faq.html, as it is not distributed in the upstream tarball.
* Use ${source:Version} in control file. Have ntop-data recommend
  ntop.
* Rename dirs to ntop.dirs and keep only empty directories that need
  to be created.
* Remove var/lib from ntop.install file, as it is empty (keeping it in
  ntop.dirs).
* Update po files.
* Breaks and Replaces instead of Conflitcs for ntop-data.
* Use a longer package description.
* Remove useless configure options from debian/rules.
* Move private shared libraries libraries in /usr/lib/ntop.
* Add change-plugin-dir.patch for adjusting plugin directory.
* Remove development files.
* Use system library for MochiKit.js.
* Rewrite DEP5 copyright file.
* Repackage upstream tarball in order to remove non-DFSG-compliant code. Add
  get-orig-source.sh script and get-orig-source target in debian/rules.
* Add explanation to README.Debian why geolocation is no longer working.
* Add avoid-copy-maxmind-db.patch to prevent copying of Geo*.dat
  files.
* Remove old unused patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * 
3
 
 *  Copyright (C) 2001 Luca Deri <deri@ntop.org>
4
 
 *  Copyright (C) 2002 Walter Brock <walterbrock@netscape.net>
5
 
 *                      
6
 
 *                 http://www.ntop.org/
7
 
 *                      
8
 
 *  This program is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License as published by
10
 
 *  the Free Software Foundation; either version 2 of the License, or
11
 
 *  (at your option) any later version.
12
 
 *
13
 
 *  This program is distributed in the hope that it will be useful,
14
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 *  GNU General Public License for more details.
17
 
 *
18
 
 *  You should have received a copy of the GNU General Public License
19
 
 *  along with this program; if not, write to the Free Software
20
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
 
 */
22
 
 
23
 
#include "ntop.h"
24
 
#include "globals-report.h"
25
 
 
26
 
/* ***** f o r w a r d ***** */
27
 
 
28
 
void printPdaData(HostTraffic* tmpTable[MAX_PDA_HOST_TABLE], u_int numEntries);
29
 
void printPdaSummaryData(void);
30
 
static void termPdaFunct(u_char termNtop /* 0=term plugin, 1=term ntop */);
31
 
static void handlePDArequest(char* url);
32
 
 
33
 
/* ****************************** */
34
 
 
35
 
static PluginInfo PDAPluginInfo[] = {
36
 
  {
37
 
    VERSION, /* current ntop version */
38
 
    "PDA",
39
 
    "This plugin produces a minimal ntop report, suitable for display on a pda",
40
 
    "2.2a",            /* version */
41
 
    "<a class=mailto href=\"mailto:&#119;&#097;&#108;&#116;&#101;&#114;&#098;&#114;&#111;&#099;&#107;&#064;&#110;&#101;&#116;&#115;&#099;&#097;&#112;&#101;&#046;&#110;&#101;&#116;\">W. Brock</A>", 
42
 
    "PDAPlugin",      /* http://<host>:<port>/plugins/PDAPlugin */
43
 
    0,                /* Active by default */
44
 
    ViewOnly,
45
 
    0,                /* Inactive setup */
46
 
    NULL,             /* no special startup after init */
47
 
    termPdaFunct,     /* TermFunc   */
48
 
    NULL,             /* PluginFunc */
49
 
    handlePDArequest, /* http request handler */
50
 
    NULL,             /* no host creation/deletion handle */
51
 
    NULL,             /* BPF Filter */
52
 
    NULL,             /* no status */
53
 
    NULL              /* no extra pages */
54
 
  }
55
 
};
56
 
 
57
 
/* ****************************** */
58
 
/* Plugin entry fctn */
59
 
#ifdef MAKE_STATIC_PLUGIN
60
 
PluginInfo* wapPluginEntryFctn(void)
61
 
#else
62
 
  PluginInfo* PluginEntryFctn(void)
63
 
#endif
64
 
{
65
 
  traceEvent(CONST_TRACE_ALWAYSDISPLAY, "PDA: Welcome to %s. (C) 2001-2005 by L.Deri and W.Brock",  
66
 
             PDAPluginInfo->pluginName);
67
 
  
68
 
  return(PDAPluginInfo);
69
 
}
70
 
 
71
 
/* ********************** */
72
 
 
73
 
static void printHtmlNotFoundError(void) {
74
 
    sendString("Unknown request");
75
 
}
76
 
 
77
 
/* ********************** */
78
 
 
79
 
static int cmpPdaFctnSent(const void *_a, const void *_b) {
80
 
  HostTraffic **a = (HostTraffic **)_a;
81
 
  HostTraffic **b = (HostTraffic **)_b;
82
 
  TrafficCounter a_, b_;
83
 
 
84
 
  if((a == NULL) && (b != NULL)) {
85
 
    traceEvent(CONST_TRACE_WARNING, "cmpPdaFctnSent() (1)");
86
 
    return(1);
87
 
  } else if((a != NULL) && (b == NULL)) {
88
 
    traceEvent(CONST_TRACE_WARNING, "cmpPdaFctnSent() (2)");
89
 
    return(-1);
90
 
  } else if((a == NULL) && (b == NULL)) {
91
 
    traceEvent(CONST_TRACE_WARNING, "cmpPdaFctnSent() (3)");
92
 
    return(0);
93
 
  }
94
 
 
95
 
  /* Data Sent */
96
 
  a_ = (*a)->bytesSent;
97
 
  b_ = (*b)->bytesSent;
98
 
 
99
 
  if(a_.value < b_.value)
100
 
    return(1);
101
 
  else if (a_.value > b_.value)
102
 
    return(-1);
103
 
  else
104
 
    return(0);
105
 
}
106
 
 
107
 
static int cmpPdaFctnRcvd(const void *_a, const void *_b) {
108
 
  HostTraffic **a = (HostTraffic **)_a;
109
 
  HostTraffic **b = (HostTraffic **)_b;
110
 
  TrafficCounter a_, b_;
111
 
 
112
 
  if((a == NULL) && (b != NULL)) {
113
 
    traceEvent(CONST_TRACE_WARNING, "cmpPdaFctnRcvd() (1)");
114
 
    return(1);
115
 
  } else if((a != NULL) && (b == NULL)) {
116
 
    traceEvent(CONST_TRACE_WARNING, "cmpPdaFctnRcvd() (2)");
117
 
    return(-1);
118
 
  } else if((a == NULL) && (b == NULL)) {
119
 
    traceEvent(CONST_TRACE_WARNING, "cmpPdaFctnRcvd() (3)");
120
 
    return(0);
121
 
  }
122
 
 
123
 
  /* Data Rcvd */
124
 
  a_ = (*a)->bytesRcvd;
125
 
  b_ = (*b)->bytesRcvd;
126
 
 
127
 
  if(a_.value < b_.value)
128
 
    return(1);
129
 
  else if (a_.value > b_.value)
130
 
    return(-1);
131
 
  else
132
 
    return(0);
133
 
}
134
 
 
135
 
/* ********************** */
136
 
 
137
 
void printPdaData(HostTraffic* tmpTable[MAX_PDA_HOST_TABLE], u_int numEntries) {
138
 
  u_int idx;
139
 
  int i;
140
 
  HostTraffic *el;
141
 
  char *tmpName;
142
 
  char hostLinkBuf[LEN_GENERAL_WORK_BUFFER];
143
 
  char linkName[256];
144
 
  char buf[LEN_GENERAL_WORK_BUFFER];
145
 
  char formatBuf[32];
146
 
 
147
 
  sendString("  <table ");
148
 
  sendString((myGlobals.runningPref.w3c == TRUE) ? "" : "columns=\"2\" ");
149
 
  sendString("align=\"left\">\n");
150
 
  sendString("      <tr><td><b><u>Top Sending Hosts</u></b></td><td><b><u>Total</u></b></td></tr>\n");
151
 
  
152
 
  /* Data Sent */
153
 
  qsort(tmpTable, numEntries, sizeof(HostTraffic*), cmpPdaFctnSent); 
154
 
  
155
 
  for(idx=0; idx<numEntries; idx++) {
156
 
    if(idx == 5) break;
157
 
 
158
 
    el = tmpTable[idx];
159
 
    tmpName = getHostName(el, 0, hostLinkBuf, sizeof(hostLinkBuf)); 
160
 
    tmpName = el->hostNumIpAddress;
161
 
    strncpy(linkName, el->hostNumIpAddress, sizeof(linkName));
162
 
    
163
 
    if((strcmp(tmpName, "0.0.0.0") == 0) || (tmpName[0] == '\0')){
164
 
      tmpName = el->ethAddressString;
165
 
      strncpy(linkName, el->ethAddressString, sizeof(linkName));
166
 
      for(i=0; linkName[i] != '\0'; i++)
167
 
        if(linkName[i] == ':')
168
 
          linkName[i] = '_';
169
 
    }
170
 
      
171
 
    safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
172
 
                "<tr><td><a href=\"/%s.html\">%s</a></td>"
173
 
                "<td>%s</td></tr>\n",
174
 
                linkName, tmpName,      
175
 
                formatBytes(el->bytesSent.value, 1, formatBuf, sizeof(formatBuf)));
176
 
    sendString(buf);
177
 
  }
178
 
 
179
 
  sendString("</table>\n"
180
 
             "<br><br>\n"
181
 
             "</td></tr>\n"
182
 
             " <tr><td>\n");
183
 
  sendString("  <table ");
184
 
  sendString((myGlobals.runningPref.w3c == TRUE) ? "" : "columns=\"2\" ");
185
 
  sendString("align=\"left\">\n");
186
 
  sendString("    <tr><td><b><u>Top Receiving Hosts</u></b></td><td><b><u>Total</u></b></td></tr>\n");
187
 
 
188
 
  /* Data Rcvd */
189
 
  qsort(tmpTable, numEntries, sizeof(HostTraffic*), cmpPdaFctnRcvd); 
190
 
 
191
 
  for(idx=0; idx<numEntries; idx++) {
192
 
    if(idx == 5) break;
193
 
 
194
 
    el = tmpTable[idx];
195
 
    tmpName = getHostName(el, 0, hostLinkBuf, sizeof(hostLinkBuf)); 
196
 
    tmpName = el->hostNumIpAddress;
197
 
    strncpy(linkName, el->hostNumIpAddress, sizeof(linkName));
198
 
 
199
 
    if((strcmp(tmpName, "0.0.0.0") == 0) || (tmpName[0] == '\0')){
200
 
      tmpName = el->ethAddressString;
201
 
      strncpy(linkName, el->ethAddressString, sizeof(linkName));
202
 
      for(i=0; linkName[i] != '\0'; i++)
203
 
        if(linkName[i] == ':') 
204
 
          linkName[i] = '_';
205
 
    }   
206
 
    safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), 
207
 
                "<tr><td><a href=\"/%s.html\">%s</a></td>"
208
 
                "<td>%s</td></tr>\n",
209
 
                linkName, tmpName,  
210
 
                formatBytes(el->bytesRcvd.value, 1, formatBuf, sizeof(formatBuf)));
211
 
    sendString(buf);
212
 
  }
213
 
 
214
 
  sendString("</table>\n");
215
 
  sendString("<br><br>\n");
216
 
}
217
 
 
218
 
/* ********************** */
219
 
 
220
 
void printPdaSummaryData(void) {
221
 
 
222
 
  char formatBuf[32];
223
 
  Counter diff;    
224
 
  char buf[LEN_GENERAL_WORK_BUFFER];
225
 
  Counter unicastPkts=0;
226
 
 
227
 
  sendString("  <table ");
228
 
  sendString((myGlobals.runningPref.w3c == TRUE) ? "" : "columns=\"2\" ");
229
 
  sendString("align=\"left\">\n");
230
 
  sendString("  <tr><td><b><u>Stats</u></b></td><td><b><u>Total</u></b></td></tr>\n");
231
 
 
232
 
  /** **/
233
 
 
234
 
  safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),"<tr><td>Sampling Time</td>"
235
 
              "<td>%s</td></tr>\n",
236
 
              formatSeconds((long)(myGlobals.actTime-myGlobals.initialSniffTime), formatBuf, sizeof(formatBuf)));
237
 
  sendString(buf);
238
 
 
239
 
  /** **/
240
 
  
241
 
  diff = myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value - myGlobals.device[myGlobals.actualReportDeviceId].broadcastPkts.value -
242
 
    myGlobals.device[myGlobals.actualReportDeviceId].multicastPkts.value;
243
 
 
244
 
  if(diff > 0)
245
 
    unicastPkts = 0; /* It shouldn't happen */
246
 
  else 
247
 
    unicastPkts = diff; 
248
 
 
249
 
  if(myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value <= 0) 
250
 
    myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value = 1;
251
 
    
252
 
  safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),"<tr><td>Total</td><td>%s</td></tr>\n",
253
 
              formatPkts(myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value, formatBuf, sizeof(formatBuf)));
254
 
  sendString(buf);
255
 
 
256
 
  safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),"<tr><td>Unicast</td>"
257
 
              "<td>%s [%.1f%%]</td></tr>\n", 
258
 
              formatPkts(unicastPkts, formatBuf, sizeof(formatBuf)),
259
 
              (float)(100*unicastPkts)/(float)myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value);
260
 
  sendString(buf);
261
 
  safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),"<tr><td>Broadcast</td>"
262
 
              "<td>%s [%.1f%%]</td></tr>\n", 
263
 
              formatPkts(myGlobals.device[myGlobals.actualReportDeviceId].broadcastPkts.value, formatBuf, sizeof(formatBuf)),
264
 
              (float)(100*myGlobals.device[myGlobals.actualReportDeviceId].broadcastPkts.value)
265
 
              /(float)myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value);
266
 
  sendString(buf);
267
 
 
268
 
  if(myGlobals.device[myGlobals.actualReportDeviceId].multicastPkts.value > 0) {
269
 
    safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),"<tr><td>Multicast</td>"
270
 
                "<td>%s [%.1f%%]</td></tr>\n", 
271
 
                formatPkts(myGlobals.device[myGlobals.actualReportDeviceId].multicastPkts.value, formatBuf, sizeof(formatBuf)),
272
 
                (float)(100*myGlobals.device[myGlobals.actualReportDeviceId].multicastPkts.value)
273
 
                /(float)myGlobals.device[myGlobals.actualReportDeviceId].ethernetPkts.value);
274
 
    sendString(buf);
275
 
  }
276
 
 
277
 
  /** **/
278
 
  sendString("</table>\n");
279
 
}
280
 
/* ********************** */
281
 
 
282
 
static void printHtmlIndex(void) {
283
 
  u_int idx, numEntries=0;
284
 
  HostTraffic *el;
285
 
  HostTraffic* tmpTable[MAX_PDA_HOST_TABLE];
286
 
 
287
 
  /* #ifdef WIN32
288
 
     deviceId = 0;
289
 
     #else
290
 
     deviceId = (int)_deviceId;
291
 
     #endif
292
 
 
293
 
     actualDeviceId = getActualInterface(deviceId); */
294
 
  
295
 
  for(idx=1; idx<myGlobals.device[myGlobals.actualReportDeviceId].actualHashSize; idx++)
296
 
    if(((el = myGlobals.device[myGlobals.actualReportDeviceId].hash_hostTraffic[idx]) != NULL) 
297
 
       && (!broadcastHost(el))
298
 
       && (numEntries < MAX_PDA_HOST_TABLE))
299
 
      tmpTable[numEntries++]=el;
300
 
  
301
 
  if(numEntries > 0)
302
 
    printPdaData(tmpTable, numEntries);
303
 
  else 
304
 
    sendString("&nbsp;\n");
305
 
 
306
 
  sendString("</td></tr>\n"
307
 
             "<tr><td>\n");
308
 
 
309
 
  printPdaSummaryData();
310
 
 
311
 
}
312
 
 
313
 
/* ********************** */
314
 
 
315
 
static void printHtmlHostInfo(char *host _UNUSED_) {
316
 
  sendString("Data for host ");
317
 
  sendString(host);
318
 
  sendString("<br>Currently this function is not available");
319
 
}
320
 
 
321
 
 
322
 
/* ********************** */
323
 
 
324
 
static void handlePDArequest(char* url) {
325
 
 
326
 
  sendHTTPHeader(FLAG_HTTP_TYPE_HTML, BITFLAG_HTTP_NO_CACHE_CONTROL | BITFLAG_HTTP_MORE_FIELDS, 1);
327
 
  sendString((myGlobals.runningPref.w3c == TRUE) ? CONST_W3C_DOCTYPE_LINE_32 "\n" : "");
328
 
  sendString("<html>\n"
329
 
             "<head>\n");
330
 
  sendString((myGlobals.runningPref.w3c == TRUE) ? CONST_W3C_CHARTYPE_LINE "\n" : "");
331
 
  sendString("<meta http-equiv=REFRESH content=\"240\">\n"
332
 
             "<title>ntop for PDAs</title>\n");
333
 
  /* sendString("<link rel=stylesheet href=\"/stylepda.css\" type=\"text/css\">\n"); */
334
 
  sendString("</head>\n"
335
 
             "<body>\n"
336
 
             "<b>ntop for PDAs</b>\n"
337
 
             "<br><br>\n");
338
 
  sendString("<table ");
339
 
  sendString((myGlobals.runningPref.w3c == TRUE) ? "" : "columns=\"1\" ");
340
 
  sendString("align=\"left\">\n");
341
 
  sendString("<tr><td>\n");
342
 
 
343
 
  if((url == NULL) 
344
 
     || (url[0] == 0) 
345
 
     || (strncmp(url, CONST_INDEX_HTML, 
346
 
                 strlen(CONST_INDEX_HTML)) == 0))  {
347
 
    printHtmlIndex();
348
 
  } else if(strncmp(url, CONST_HOST_HTML, strlen(CONST_HOST_HTML)) == 0)
349
 
    printHtmlHostInfo(&url[strlen(CONST_HOST_HTML)+1]);
350
 
  else
351
 
    printHtmlNotFoundError();
352
 
 
353
 
  sendString("</td></tr>\n"
354
 
             "</table>\n"
355
 
             "</body>\n"
356
 
             "</html>\n");
357
 
}
358
 
 
359
 
/* ****************************** */
360
 
 
361
 
static void termPdaFunct(u_char termNtop /* 0=term plugin, 1=term ntop */) {
362
 
  traceEvent(CONST_TRACE_INFO, "PDA: Thanks for using ntop PDA plugin");
363
 
  traceEvent(CONST_TRACE_ALWAYSDISPLAY, "PDA: Done");
364
 
}
365