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

« back to all changes in this revision

Viewing changes to ntop/plugins/wapPlugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2001 Luca Deri <deri@ntop.org>
3
 
 *                      
4
 
 *                     http://www.ntop.org/
5
 
 *                                      
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 
 */
20
 
 
21
 
#include "ntop.h"
22
 
#include "globals-report.h"
23
 
 
24
 
#ifndef MICRO_NTOP
25
 
static int wapColumnSort = 0;
26
 
 
27
 
/* ****************************** */
28
 
 
29
 
void printWMLheader(void)  {
30
 
  sendHTTPHeader(HTTP_TYPE_NONE, HTTP_FLAG_NO_CACHE_CONTROL | HTTP_FLAG_MORE_FIELDS);
31
 
  sendString("Content-Type: text/vnd.wap.wml;charset=UTF-8\n\n"); 
32
 
}
33
 
 
34
 
/* ********************** */
35
 
 
36
 
static void printWmlNotFoundError(void) {
37
 
 
38
 
}
39
 
 
40
 
/* ********************** */
41
 
 
42
 
static void printWmlNoDataYet(void) {
43
 
 
44
 
}
45
 
 
46
 
/* ********************** */
47
 
 
48
 
static int cmpWapFctn(const void *_a, const void *_b) {
49
 
  HostTraffic **a = (HostTraffic **)_a;
50
 
  HostTraffic **b = (HostTraffic **)_b;
51
 
  TrafficCounter a_, b_;
52
 
 
53
 
  if((a == NULL) && (b != NULL)) {
54
 
    traceEvent(TRACE_WARNING, "WARNING (1)\n");
55
 
    return(1);
56
 
  } else if((a != NULL) && (b == NULL)) {
57
 
    traceEvent(TRACE_WARNING, "WARNING (2)\n");
58
 
    return(-1);
59
 
  } else if((a == NULL) && (b == NULL)) {
60
 
    traceEvent(TRACE_WARNING, "WARNING (3)\n");
61
 
    return(0);
62
 
  }
63
 
 
64
 
  if(wapColumnSort == 0) {
65
 
    /* Data Sent */
66
 
    a_ = (*a)->bytesSent;
67
 
    b_ = (*b)->bytesSent;
68
 
  } else  {
69
 
    /* Data Rcvd */
70
 
    a_ = (*a)->bytesReceived;
71
 
    b_ = (*b)->bytesReceived;
72
 
  }
73
 
 
74
 
  if(a_ < b_)
75
 
    return(1);
76
 
  else if (a_ > b_)
77
 
    return(-1);
78
 
  else
79
 
    return(0);
80
 
}
81
 
 
82
 
/* ********************** */
83
 
 
84
 
static void printWmlIndex(void) {
85
 
  int diff;
86
 
  u_int idx, numEntries=0;
87
 
  HostTraffic *el;
88
 
  HostTraffic* tmpTable[HASHNAMESIZE];
89
 
  char *tmpName, buf[BUF_SIZE];
90
 
  TrafficCounter unicastPkts=0;
91
 
 
92
 
  printWMLheader();
93
 
 
94
 
  for(idx=1; idx<device[actualDeviceId].actualHashSize; idx++)
95
 
    if(((el = device[actualDeviceId].hash_hostTraffic[idx]) != NULL) 
96
 
       && (!broadcastHost(el)))
97
 
      tmpTable[numEntries++]=el;
98
 
  
99
 
  if(numEntries == 0) {
100
 
    printWmlNoDataYet();
101
 
    return;
102
 
  }
103
 
 
104
 
  sendString("<?xml version=\"1.0\"?>\n");
105
 
  sendString("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http:///www.wapforum.org/DTD/wml_1.1.xml\">\n\n");
106
 
  sendString("<wml>\n");
107
 
  sendString("  <card id=\"home\" title=\"ntop for Wap\">\n");
108
 
  sendString("  <p>\n");
109
 
  sendString("          <a href=\"#senders\">Top Senders</a><br/>\n");
110
 
  sendString("          <a href=\"#receivers\">Top Receivers</a><br/>\n");
111
 
  sendString("          <a href=\"#stats\">Stats</a><br/>\n");
112
 
  sendString("  </p>\n");
113
 
  sendString("  </card>\n");
114
 
  sendString("  <card id=\"senders\" title=\"Top Senders\"> \n");
115
 
  sendString("  <p>\n");
116
 
  sendString("  <table columns=\"2\" align=\"LCC\">\n");
117
 
  sendString("          <tr><td><b>Host</b></td><td><b>Total</b></td></tr>\n");
118
 
  
119
 
  /* Data Sent */
120
 
  wapColumnSort = 0;  
121
 
  quicksort(tmpTable, numEntries, sizeof(HostTraffic*), cmpWapFctn); 
122
 
  
123
 
  for(idx=0; idx<numEntries; idx++) {
124
 
    if(idx == 5) break;
125
 
 
126
 
    el = tmpTable[idx];
127
 
    tmpName = getHostName(el, 0);
128
 
 
129
 
    if((strcmp(tmpName, "0.0.0.0") == 0) || (tmpName[0] == '\0'))
130
 
      tmpName = el->ethAddressString;
131
 
            
132
 
    if(snprintf(buf, sizeof(buf),
133
 
            "<tr><td><a href=\"/ntop/host.wml?%s\">%s</a></td>"
134
 
            "<td>%s</td></tr>\n",
135
 
            tmpName, tmpName,       
136
 
            formatBytes(el->bytesSent, 1)) < 0) 
137
 
      traceEvent(TRACE_ERROR, "Buffer overflow!");
138
 
    sendString(buf);
139
 
  }
140
 
 
141
 
  sendString("</table>\n");
142
 
  sendString("  <a href=\"#home\">[Back Home]</a><br/>\n");
143
 
  sendString("  </p>\n");
144
 
  sendString("  </card>\n");
145
 
  sendString("  <card id=\"receivers\" title=\"Top Receivers\">\n");
146
 
  sendString("  <p>\n");
147
 
  sendString("  <table columns=\"2\" align=\"LCC\">\n");
148
 
  sendString("  <tr><td><b>Host</b></td><td><b>Total</b></td></tr>\n");
149
 
 
150
 
  /* Data Rcvd */
151
 
  wapColumnSort = 1;  
152
 
  quicksort(tmpTable, numEntries, sizeof(HostTraffic*), cmpFctn); 
153
 
 
154
 
  for(idx=0; idx<numEntries; idx++) {
155
 
    if(idx == 5) break;
156
 
 
157
 
    el = tmpTable[idx];
158
 
    tmpName = getHostName(el, 0);
159
 
 
160
 
    if((strcmp(tmpName, "0.0.0.0") == 0) || (tmpName[0] == '\0'))
161
 
      tmpName = el->ethAddressString;
162
 
            
163
 
    if(snprintf(buf, sizeof(buf), 
164
 
            "<tr><td><a href=\"/ntop/host.wml?%s\">%s</a></td>"
165
 
            "<td>%s</td></tr>\n",
166
 
            tmpName, tmpName,       
167
 
            formatBytes(el->bytesReceived, 1)) < 0) 
168
 
      traceEvent(TRACE_ERROR, "Buffer overflow!");
169
 
    sendString(buf);
170
 
  }
171
 
 
172
 
  sendString("</table>\n");
173
 
  sendString("  <a href=\"#home\">[Back Home]</a><br/>\n");
174
 
  sendString("  </p>\n");
175
 
  sendString("  </card>\n");
176
 
  /* ************************* */
177
 
 
178
 
  sendString("  <card id=\"stats\" title=\"Traffic Stats\">\n");
179
 
  sendString("  <p>\n");
180
 
  sendString("  <table columns=\"2\" align=\"LCC\">\n");
181
 
 
182
 
  /** **/
183
 
 
184
 
  if(snprintf(buf, sizeof(buf),"<tr><td>Sampling&nbsp;Time</td>"
185
 
          "<td>%s</td></tr>\n",
186
 
          formatSeconds(actTime-initialSniffTime)) < 0) 
187
 
    traceEvent(TRACE_ERROR, "Buffer overflow!");
188
 
  sendString(buf);
189
 
 
190
 
  /** **/
191
 
  
192
 
  diff = (int)(device[actualDeviceId].ethernetPkts -
193
 
               device[actualDeviceId].broadcastPkts -
194
 
               device[actualDeviceId].multicastPkts);
195
 
  if(diff > 0)
196
 
    unicastPkts = 0; /* It shouldn't happen */
197
 
  else 
198
 
    unicastPkts = (TrafficCounter)diff; 
199
 
 
200
 
  if(device[actualDeviceId].ethernetPkts <= 0) device[actualDeviceId].ethernetPkts = 1;
201
 
        
202
 
  if(snprintf(buf, sizeof(buf),"<tr><td>Total</td><td>%s</td></tr>\n",
203
 
          formatPkts(device[actualDeviceId].ethernetPkts)) < 0) 
204
 
    traceEvent(TRACE_ERROR, "Buffer overflow!");
205
 
  sendString(buf);
206
 
 
207
 
  if(snprintf(buf, sizeof(buf),"<tr><td>Unicast</td>"
208
 
          "<td>%s [%.1f%%]</td></tr>\n", 
209
 
          formatPkts(unicastPkts),
210
 
          (float)(100*unicastPkts)/(float)device[actualDeviceId].ethernetPkts) < 0) 
211
 
    traceEvent(TRACE_ERROR, "Buffer overflow!");
212
 
  sendString(buf);
213
 
  if(snprintf(buf, sizeof(buf),"<tr><td>Broadcast</td>"
214
 
          "<td>%s [%.1f%%]</td></tr>\n", 
215
 
          formatPkts(device[actualDeviceId].broadcastPkts),
216
 
          (float)(100*device[actualDeviceId].broadcastPkts)
217
 
           /(float)device[actualDeviceId].ethernetPkts) < 0) 
218
 
    traceEvent(TRACE_ERROR, "Buffer overflow!");
219
 
  sendString(buf);
220
 
 
221
 
  if(device[actualDeviceId].multicastPkts > 0) {
222
 
    if(snprintf(buf, sizeof(buf),"<tr><td>Multicast</td>"
223
 
            "<td>%s [%.1f%%]</td></tr>\n", 
224
 
            formatPkts(device[actualDeviceId].multicastPkts),
225
 
            (float)(100*device[actualDeviceId].multicastPkts)
226
 
             /(float)device[actualDeviceId].ethernetPkts) < 0) 
227
 
      traceEvent(TRACE_ERROR, "Buffer overflow!");
228
 
    sendString(buf);
229
 
  }
230
 
 
231
 
  /** **/
232
 
  sendString("</table>\n");
233
 
  sendString("  <a href=\"#home\">[Back Home]</a><br/>\n");
234
 
  sendString("  </p>\n");
235
 
  sendString("  </card>\n");
236
 
 
237
 
  /* ************************* */
238
 
 
239
 
  sendString("</wml>\n");
240
 
}
241
 
 
242
 
/* ********************** */
243
 
 
244
 
static void printWmlHostInfo(char *host _UNUSED_) {
245
 
 
246
 
}
247
 
 
248
 
/* ********************** */
249
 
 
250
 
static void handleWAPrequest(char* url) {
251
 
 
252
 
  if((url == NULL) 
253
 
     || (url[0] == 0) 
254
 
     || (strncmp(url, "index.wml", 
255
 
                 strlen("index.wml")) == 0))  {
256
 
    printWmlIndex();
257
 
  } else if(strncmp(url, "host.wml", strlen("host.wml")) == 0)
258
 
    printWmlHostInfo(&url[strlen("host.wml")+1]);
259
 
  else
260
 
    printWmlNotFoundError();
261
 
}
262
 
 
263
 
/* ****************************** */
264
 
 
265
 
static void termWapFunct(void) {
266
 
  traceEvent(TRACE_INFO, "Thanks for using wapWatch...\n");
267
 
  traceEvent(TRACE_INFO, "Done.\n");
268
 
}
269
 
 
270
 
#endif /* MICRO_NTOP */
271
 
 
272
 
/* ****************************** */
273
 
 
274
 
static PluginInfo WAPPluginInfo[] = {
275
 
  { "WAPPlugin",
276
 
    "ntop WAP (Wireless Application protocol) Interface",
277
 
    "1.0",           /* version */
278
 
    "<A HREF=http://luca.ntop.org/>L.Deri</A>", 
279
 
    "WAPPlugin",      /* http://<host>:<port>/plugins/WAPPlugin */
280
 
    0, /* Not Active */
281
 
    NULL, /* no special startup after init */
282
 
#ifndef MICRO_NTOP
283
 
    termWapFunct,    /* TermFunc   */
284
 
#else
285
 
    NULL,
286
 
#endif
287
 
    NULL,    /* PluginFunc */
288
 
#ifndef MICRO_NTOP
289
 
    handleWAPrequest,
290
 
#else
291
 
    NULL,
292
 
#endif
293
 
    NULL,
294
 
    NULL /* BPF Filter */
295
 
  }
296
 
};
297
 
 
298
 
/* Plugin entry fctn */
299
 
#ifdef STATIC_PLUGIN
300
 
PluginInfo* wapPluginEntryFctn(void) {
301
 
#else
302
 
PluginInfo* PluginEntryFctn(void) {
303
 
#endif
304
 
  traceEvent(TRACE_INFO, "Welcome to %s. (C) 2000 by Luca Deri.\n",  
305
 
         WAPPluginInfo->pluginName);
306
 
 
307
 
  return(WAPPluginInfo);
308
 
}