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

« back to all changes in this revision

Viewing changes to ntop/sql.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
#include "globals-report.h"
 
24
 
 
25
static int sqlSocket = -1;
 
26
 
 
27
/* Global */
 
28
static struct sockaddr_in dest;
 
29
 
 
30
/* **************************************** */
 
31
 
 
32
static void openSQLsocket(char* dstHost, int dstPort) {
 
33
  struct hostent *hostAddr = gethostbyname(dstHost);
 
34
  
 
35
  if(hostAddr == NULL) {
 
36
    traceEvent(TRACE_INFO, "Unable to resolve address '%s'\n", dstHost);
 
37
    exit(-1);
 
38
  }
 
39
        
 
40
  memcpy(&dest.sin_addr.s_addr, hostAddr->h_addr_list[0],
 
41
         hostAddr->h_length);
 
42
  dest.sin_family      = AF_INET;
 
43
  dest.sin_port        = (int)htons((unsigned short int)dstPort);
 
44
 
 
45
  sqlSocket = socket (AF_INET, SOCK_DGRAM, 0);
 
46
  
 
47
  if(sqlSocket <= 0) {
 
48
    traceEvent(TRACE_INFO, "Unable to open SQLsocket\n");
 
49
      exit(-1);
 
50
  } else {
 
51
    traceEvent(TRACE_INFO, "Open channel with ntop SQL client running @ %s:%d\n", dstHost, dstPort);
 
52
  }
 
53
}
 
54
 
 
55
/* **************************************** */
 
56
 
 
57
void handleDbSupport(char* addr /* host:port */, int* enableDBsupport) {
 
58
  char *hostName, *strtokState;
 
59
  int  portNumber;
 
60
 
 
61
  if((addr == NULL) || (addr[0] == '\0'))
 
62
    return;
 
63
 
 
64
  hostName = strtok_r(addr, ":", &strtokState);
 
65
  portNumber = atoi(strtok_r(NULL, ":", &strtokState));
 
66
 
 
67
  if((hostName == NULL) || (portNumber == 0)) {
 
68
    traceEvent(TRACE_WARNING, "WARNING: invalid value specified for '-b' parameter. \n"
 
69
          "         It should be host:port.\n");
 
70
    return;
 
71
  } else {
 
72
    (*enableDBsupport) = 1;
 
73
    openSQLsocket(hostName, portNumber); /* *** SQL Engine *** */
 
74
  }
 
75
}
 
76
 
 
77
/* **************************************** */
 
78
 
 
79
void closeSQLsocket(void) {
 
80
#ifndef WIN32
 
81
  close(sqlSocket);
 
82
#else
 
83
  closesocket(sqlSocket);
 
84
#endif     
 
85
}
 
86
 
 
87
/* **************************************** */
 
88
 
 
89
void updateHostNameInfo(unsigned long numeric, char* symbolic) {
 
90
  char *hostName;
 
91
  struct in_addr addr;
 
92
  char buf[32];
 
93
  char sqlBuf[BUF_SIZE];
 
94
  u_int idx;
 
95
 
 
96
  if(!capturePackets) return;
 
97
 
 
98
  addr.s_addr = numeric;
 
99
  hostName = _intoa(addr, buf, sizeof(buf));
 
100
 
 
101
  /* Search the instance and update its name */
 
102
 
 
103
#ifdef MULTITHREADED
 
104
  accessMutex(&addressResolutionMutex, "updateHostNameInfo");
 
105
#endif
 
106
    
 
107
  idx = findHostIdxByNumIP(addr);
 
108
 
 
109
  if(idx != NO_PEER) {
 
110
    if(device[actualDeviceId].hash_hostTraffic[idx] != NULL) {
 
111
 
 
112
      if(strlen(symbolic) >= (MAX_HOST_SYM_NAME_LEN-1)) 
 
113
        symbolic[MAX_HOST_SYM_NAME_LEN-2] = '\0';
 
114
      strcpy(device[actualDeviceId].hash_hostTraffic[idx]->hostSymIpAddress, symbolic);
 
115
    }
 
116
  }
 
117
 
 
118
#ifdef MULTITHREADED
 
119
    releaseMutex(&addressResolutionMutex);
 
120
#endif
 
121
 
 
122
  if(sqlSocket != -1) {
 
123
    if(strcmp(hostName, symbolic) != 0) {  
 
124
      /* [1] Delete */
 
125
      if(snprintf(sqlBuf, sizeof(sqlBuf), "DELETE FROM NameMapper WHERE IPaddress = '%s'", hostName) < 0) 
 
126
        traceEvent(TRACE_ERROR, "Buffer overflow!");
 
127
      sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, 
 
128
             (struct sockaddr *)&dest, sizeof(dest));
 
129
      
 
130
      /* [2] Insert */
 
131
      if(snprintf(sqlBuf, sizeof(sqlBuf), "INSERT INTO NameMapper (IPaddress, Name)"
 
132
                  " VALUES ('%s', '%s')", hostName, symbolic) < 0)
 
133
        traceEvent(TRACE_ERROR, "Buffer overflow!");
 
134
      sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, 
 
135
             (struct sockaddr *)&dest, sizeof(dest));
 
136
    }
 
137
  }
 
138
 
 
139
#ifdef HAVE_MYSQL
 
140
  mySQLupdateHostNameInfo(numeric, symbolic);
 
141
#endif
 
142
}
 
143
 
 
144
/* **************************************** */
 
145
 
 
146
void updateHostTraffic(HostTraffic *el) {
 
147
  char theDate[32], theDate2[32];
 
148
  char sqlBuf[2*BUF_SIZE];
 
149
  struct tm t;
 
150
 
 
151
  if((sqlSocket == -1)
 
152
     || (broadcastHost(el))
 
153
     || (el->hostNumIpAddress[0] == '\0'))
 
154
    return;
 
155
 
 
156
  /* Fixes below courtesy of Andreas Pfaller <a.pfaller@pop.gun.de> */
 
157
  strftime(theDate2, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&el->firstSeen, &t));
 
158
 
 
159
  /* Added by David Moore <davem@mitre.org> */
 
160
  strftime(theDate, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&el->lastSeen, &t));  
 
161
 
 
162
  /* ****************************** */
 
163
 
 
164
  if(snprintf(sqlBuf, sizeof(sqlBuf), "UPDATE Hosts SET "
 
165
              "PktSent = %llu, "
 
166
              "PktRcvd = %llu, "
 
167
              "PktMulticastSent = %llu, "
 
168
              "PktBroadcastSent = %llu, "
 
169
              "DataSent = %llu, "
 
170
              "DataRcvd = %llu, "
 
171
              "DataMulticastSent = %llu, "
 
172
              "DataBroadcastSent = %llu, "
 
173
              "FirstSeen = '%s', "
 
174
              "LastSeen = '%s'"
 
175
              " WHERE IPaddress = '%s'",
 
176
              (el->pktSent),
 
177
              (el->pktReceived),
 
178
              (el->pktMulticastSent),
 
179
              (el->pktBroadcastSent),
 
180
              (el->bytesSent),
 
181
              (el->bytesReceived),
 
182
              (el->bytesMulticastSent),
 
183
              (el->bytesBroadcastSent),
 
184
              theDate2, theDate,
 
185
              el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer overflow!");
 
186
  
 
187
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
188
    
 
189
  /* ****************************** */
 
190
 
 
191
  if(snprintf(sqlBuf, sizeof(sqlBuf), "UPDATE IPtraffic SET "
 
192
          "TCPSentLocally = %llu, "
 
193
          "TCPSentRemotely = %llu, "
 
194
          "TCPrcvdLocally = %llu, "
 
195
          "TCPrcvdFromRemote = %llu, "
 
196
          "UDPSentLocally = %llu, "
 
197
          "UDPSentRemotely = %llu, "
 
198
          "UDPrcvdLocally = %llu, "
 
199
          "UDPrcvdFromRemote = %llu, "
 
200
          "ICMPsent = %llu, "
 
201
          "ICMPrcvd = %llu, "
 
202
          "OSPFsent = %llu, "
 
203
          "OSPFrcvd = %llu, "
 
204
          "IGMPsent = %llu, "
 
205
          "IGMPrcvd = %llu "
 
206
          " WHERE IPaddress = '%s'",
 
207
          (el->tcpSentLocally),
 
208
          (el->tcpSentRemotely),
 
209
          (el->tcpReceivedLocally),
 
210
          (el->tcpReceivedFromRemote),
 
211
          (el->udpSentLocally),
 
212
          (el->udpSentRemotely),
 
213
          (el->udpReceivedLocally),
 
214
          (el->udpReceivedFromRemote),
 
215
          (el->icmpSent),
 
216
          (el->icmpReceived),
 
217
          (el->ospfSent),
 
218
          (el->ospfReceived),
 
219
          (el->igmpSent),
 
220
          (el->igmpReceived),
 
221
          el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer overflow!");
 
222
 
 
223
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
224
 
 
225
  /* ****************************** */
 
226
 
 
227
  if(snprintf(sqlBuf, sizeof(sqlBuf), "UPDATE NonIPTraffic SET "
 
228
          "IPXsent = %llu, "
 
229
          "IPXrcvd = %llu, "
 
230
          "OSIsent = %llu, "
 
231
          "OSIrcvd = %llu, "
 
232
          "DLCsent = %llu, "
 
233
          "DLCrcvd = %llu, "
 
234
          "ARPsent = %llu, "
 
235
          "ARPrcvd = %llu, "
 
236
          "DECNETsent = %llu, "
 
237
          "DECNETrcvd = %llu, "
 
238
          "ATALKsent = %llu, "
 
239
          "ATALKrcvd = %llu, "
 
240
          "NBIOSsent = %llu, "
 
241
          "NBIOSrcvd = %llu, "
 
242
          "OtherSent = %llu, "
 
243
          "OtherRcvd = %llu "
 
244
          " WHERE IPaddress = '%s'",
 
245
          (el->ipxSent),
 
246
          (el->ipxReceived),
 
247
          (el->osiSent),
 
248
          (el->osiReceived),
 
249
          (el->dlcSent),
 
250
          (el->dlcReceived),
 
251
          (el->arp_rarpSent),
 
252
          (el->arp_rarpReceived),
 
253
          (el->decnetSent),
 
254
          (el->decnetReceived),
 
255
          (el->appletalkSent),
 
256
          (el->appletalkReceived),
 
257
          (el->netbiosSent),
 
258
          (el->netbiosReceived),
 
259
          (el->otherSent),
 
260
          (el->otherReceived),
 
261
          el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer overflow!");
 
262
 
 
263
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
264
}
 
265
 
 
266
/* **************************************** */
 
267
 
 
268
void notifyHostCreation(HostTraffic *el) {
 
269
  char sqlBuf[BUF_SIZE];
 
270
 
 
271
  if((sqlSocket == -1) || broadcastHost(el))
 
272
    return;
 
273
 
 
274
  /* [1] Delete */
 
275
 
 
276
  if(el->hostNumIpAddress[0] != '\0') {
 
277
    if(snprintf(sqlBuf, sizeof(sqlBuf), "DELETE FROM Hosts WHERE IPaddress = '%s'", 
 
278
                el->hostNumIpAddress) < 0) 
 
279
      traceEvent(TRACE_ERROR, "Buffer overflow!");
 
280
  } else {
 
281
    if(snprintf(sqlBuf, sizeof(sqlBuf), "DELETE FROM Hosts WHERE MACaddress = '%s'", 
 
282
                el->ethAddressString) < 0) 
 
283
      traceEvent(TRACE_ERROR, "Buffer overflow!");
 
284
  }
 
285
  
 
286
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
287
  
 
288
  /* [2] Insert */
 
289
  if(snprintf(sqlBuf, sizeof(sqlBuf), "INSERT INTO Hosts (IPaddress, MACaddress, NICvendor)"
 
290
          " VALUES ('%s', '%s', '%s')",
 
291
          el->hostNumIpAddress,
 
292
          el->ethAddressString,
 
293
          getVendorInfo(el->ethAddress, 0)) < 0) traceEvent(TRACE_ERROR, "Buffer overflow!");
 
294
 
 
295
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
296
 
 
297
  if(el->hostNumIpAddress[0] != '\0') {
 
298
    if(snprintf(sqlBuf, sizeof(sqlBuf), "DELETE FROM NonIPTraffic WHERE IPaddress = '%s'",
 
299
                el->hostNumIpAddress) < 0) 
 
300
      traceEvent(TRACE_ERROR, "Buffer overflow!");
 
301
    sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
302
 
 
303
    if(snprintf(sqlBuf, sizeof(sqlBuf), "INSERT INTO NonIPTraffic (IPaddress) VALUES ('%s')", 
 
304
                el->hostNumIpAddress) < 0) 
 
305
     traceEvent(TRACE_ERROR, "Buffer overflow!");
 
306
    sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
307
 
 
308
    if(snprintf(sqlBuf, sizeof(sqlBuf), "DELETE FROM IPtraffic WHERE IPaddress = '%s'", 
 
309
                el->hostNumIpAddress) < 0) 
 
310
      traceEvent(TRACE_ERROR, "Buffer overflow!");
 
311
    sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
312
 
 
313
    if(snprintf(sqlBuf, sizeof(sqlBuf), "INSERT INTO IPtraffic (IPaddress) VALUES ('%s')", 
 
314
                el->hostNumIpAddress) < 0) 
 
315
      traceEvent(TRACE_ERROR, "Buffer overflow!");
 
316
    sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
317
  }
 
318
 
 
319
  /* traceEvent(TRACE_INFO, "%s\n", buf); */
 
320
 
 
321
}
 
322
 
 
323
/* **************************************** */
 
324
 
 
325
void notifyTCPSession(IPSession *session) {
 
326
  HostTraffic *server, *client;
 
327
  char dt1[32], dt2[32];
 
328
  struct tm t;
 
329
  char sqlBuf[BUF_SIZE];
 
330
 
 
331
  if((sqlSocket == -1) 
 
332
     || (session->initiatorIdx == NO_PEER)
 
333
     || (session->remotePeerIdx == NO_PEER))
 
334
    return;
 
335
 
 
336
  client = device[actualDeviceId].hash_hostTraffic[checkSessionIdx(session->initiatorIdx)];
 
337
  server = device[actualDeviceId].hash_hostTraffic[checkSessionIdx(session->remotePeerIdx)];
 
338
 
 
339
  strftime(dt1, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&session->firstSeen, &t));  
 
340
  strftime(dt2, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&session->lastSeen, &t));  
 
341
 
 
342
  if(snprintf(sqlBuf, sizeof(sqlBuf), "INSERT INTO TCPsessions (Client, Server, ClientPort, "
 
343
          "ServerPort, DataSent, DataRcvd, FirstSeen, LastSeen)"
 
344
          " VALUES ('%s', '%s', '%d', '%d', '%llu', '%llu', '%s', '%s')",
 
345
          client->hostNumIpAddress,
 
346
          server->hostNumIpAddress,
 
347
          session->sport,
 
348
          session->dport,
 
349
          (session->bytesSent), 
 
350
          (session->bytesReceived),
 
351
          dt1, dt2) < 0) 
 
352
    traceEvent(TRACE_ERROR, "Buffer overflow!");
 
353
 
 
354
#ifndef DEBUG
 
355
  traceEvent(TRACE_INFO, "%s\n", sqlBuf);
 
356
#endif
 
357
 
 
358
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
359
}
 
360
 
 
361
/* **************************************** */
 
362
 
 
363
void updateDBOSname(HostTraffic *el) {
 
364
  char sqlBuf[BUF_SIZE];
 
365
 
 
366
  if((sqlSocket == -1) 
 
367
     || (el->osName == NULL)
 
368
     || (el->osName[0] == '\0'))
 
369
    return;
 
370
  
 
371
  /* traceEvent(TRACE_INFO, "%s@%s\n", el->osName, el->hostNumIpAddress); */
 
372
 
 
373
  if(snprintf(sqlBuf, sizeof(sqlBuf), "UPDATE Hosts SET "
 
374
          "OsName = '%s' WHERE IPaddress = '%s'",
 
375
          el->osName, el->hostNumIpAddress) < 0) 
 
376
    traceEvent(TRACE_ERROR, "Buffer overflow!");
 
377
  
 
378
  sendto(sqlSocket, sqlBuf, strlen(sqlBuf), 0, (struct sockaddr *)&dest, sizeof(dest));
 
379
}
 
380