Hello all!

Unfortantly, I have not followed the development of ntop, but last week I
got this assigment from my employer: "Make ntop write direct to a mySQL
database, so we do not need the mySQLserver-perlscript."

So today I give you this patch. It is not pretty, it is not working 100%,
but it will write direct to a mySQL database.
I hope you can have a look at it, and give me suggestions, bugfixes and so
on. And I hope some of you find some use for it :-)

Quick install notes:
1. Get the latest ntop-current snapshot.
2. Unpack and apply th patch:
  % tar zxvf ntop-current.tgz
  % patch -p0 < ntop-mysql.patch
3. configure && make in libpng, gd, zlib and gdchart (nothing new)
4. Run configure and make nn ntop-current/ntop/ :
  % LIBS="-lmysqlclient" ./configure <options>
  % make

Create a database, according to the README.mySQL file, but do not start
the perl script. Remeber to password protect your NTOP-database.

Then start ntop:
./ntop -v username:password:ntop

Example:
If the database's name is "NTOP", the user is "root", and the passord is
"wordpass", and you want ntop to start with a webserver at port 80:
./ntop -w 80 -v root:wordpass:NTOP

Enjoy.
Vidar Hoel
[EMAIL PROTECTED]
diff -u --recursive --new-file ntop-current/ntop/Makefile.am 
ntop-mysql/ntop/Makefile.am
--- ntop-current/ntop/Makefile.am       Wed May  2 08:18:44 2001
+++ ntop-mysql/ntop/Makefile.am Wed Jun 13 14:12:44 2001
@@ -125,7 +125,7 @@
                      regex.c         rules.c       sql.c        \
                      ssl.c           term.c        traffic.c    \
                      util.c          vendor.c      version.c    \
-                     vendortable.h
+                     vendortable.h   mysql.c
 
 libntop_la_DEPENDENCIES = config.h
 libntop_la_LIBADD       = $(CORELIBS)
diff -u --recursive --new-file ntop-current/ntop/Makefile.in 
ntop-mysql/ntop/Makefile.in
--- ntop-current/ntop/Makefile.in       Wed May  2 08:18:44 2001
+++ ntop-mysql/ntop/Makefile.in Wed Jun 13 14:13:04 2001
@@ -221,7 +221,7 @@
 libntop_la_OBJECTS =  address.lo dataFormat.lo event.lo globals-core.lo \
 hash.lo initialize.lo leaks.lo logger.lo ntop.lo pbuf.lo plugin.lo \
 qsort.lo regex.lo rules.lo sql.lo ssl.lo term.lo traffic.lo util.lo \
-vendor.lo version.lo
+vendor.lo version.lo mysql.lo
 libntopreport_la_OBJECTS =  admin.lo emitter.lo globals-report.lo \
 graph.lo http.lo report.lo reportUtils.lo webInterface.lo
 PROGRAMS =  $(bin_PROGRAMS)
diff -u --recursive --new-file ntop-current/ntop/address.c ntop-mysql/ntop/address.c
--- ntop-current/ntop/address.c Fri May 11 18:54:13 2001
+++ ntop-mysql/ntop/address.c   Wed Jun 13 14:14:51 2001
@@ -131,6 +131,7 @@
 #endif
 
     updateHostNameInfo(addr, data_data.dptr);
+    mySQLupdateHostNameInfo(addr, data_data.dptr);
     free(data_data.dptr);
     numResolvedOnCacheAddresses++;
 #ifdef DEBUG
@@ -302,6 +303,7 @@
   data_data.dsize = strlen(symAddr)+1;
 
   updateHostNameInfo(addr, symAddr);
+  mySQLupdateHostNameInfo(addr, symAddr);
 
 #ifdef MULTITHREADED
   accessMutex(&gdbmMutex, "resolveAddress-4");
@@ -575,6 +577,7 @@
     traceEvent(TRACE_INFO, "Fetched data (1): %s [%s]\n", data_data.dptr, tmpBuf);
 #endif
     updateHostNameInfo(hostIpAddress.s_addr, data_data.dptr);
+    mySQLupdateHostNameInfo(hostIpAddress.s_addr, data_data.dptr);
     strncpy(outBuf, data_data.dptr, outBufLen);
     return;
   } else {
diff -u --recursive --new-file ntop-current/ntop/globals-core.h 
ntop-mysql/ntop/globals-core.h
--- ntop-current/ntop/globals-core.h    Sat Jun  9 00:05:22 2001
+++ ntop-mysql/ntop/globals-core.h      Wed Jun 13 14:15:21 2001
@@ -356,6 +356,15 @@
 extern void notifyTCPSession(IPSession *session);
 extern void updateDBOSname(HostTraffic *el);
 
+/* mysql.c */
+extern void handlemySQLSupport(char* addr /* host:port */, int* enableDBsupport);
+extern void closemySQLSQLsocket(void);
+extern void mySQLupdateHostNameInfo(unsigned long numeric, char* symbolic);
+extern void mySQLupdateHostTraffic(HostTraffic *el);
+extern void mySQLnotifyHostCreation(HostTraffic *el);
+extern void mySQLnotifyTCPSession(IPSession *session);
+extern void mySQLupdateDBOSname(HostTraffic *el);
+
 /* ssl.c */
 #ifdef HAVE_OPENSSL
 extern int sslInitialized, sslPort;
diff -u --recursive --new-file ntop-current/ntop/hash.c ntop-mysql/ntop/hash.c
--- ntop-current/ntop/hash.c    Sat Jun  9 00:05:27 2001
+++ ntop-mysql/ntop/hash.c      Wed Jun 13 14:16:32 2001
@@ -573,6 +573,7 @@
         || (device[deviceToExtend].tcpSession[j]->remotePeerIdx == NO_PEER)) {
        /* Session to purge */
        notifyTCPSession(device[deviceToExtend].tcpSession[j]);
+       mySQLnotifyTCPSession(device[deviceToExtend].tcpSession[j]);
        free(device[deviceToExtend].tcpSession[j]); /* No inner pointers to free */
        device[deviceToExtend].numTcpSessions--;
        device[deviceToExtend].tcpSession[j] = NULL;
@@ -644,6 +645,7 @@
   /* FIXME (DL): checkSessionIdx() acts on actualDeviceId instead of theDevice */
 
   updateHostTraffic(host);
+  mySQLupdateHostTraffic(host);
 
   device[theDevice].hash_hostTraffic[hostIdx] = NULL;
   device[theDevice].hostsno--;
diff -u --recursive --new-file ntop-current/ntop/main.c ntop-mysql/ntop/main.c
--- ntop-current/ntop/main.c    Fri May 11 18:55:02 2001
+++ ntop-mysql/ntop/main.c      Wed Jun 13 14:17:31 2001
@@ -138,9 +138,9 @@
   initIPServices();
 
 #ifdef WIN32
-  theOpts = "ce:f:F:hr:p:i:nw:m:b:B:D:s:P:R:S:gt:a:W:12l:";
+  theOpts = "ce:f:F:hr:p:i:nw:m:b:v:B:D:s:P:R:S:gt:a:W:12l:";
 #else
-  theOpts = "cIde:f:F:hr:i:p:nNw:m:b:D:s:P:R:MS:gt:a:u:W:12l:";
+  theOpts = "cIde:f:F:hr:i:p:nNw:m:b:v:D:s:P:R:MS:gt:a:u:W:12l:";
 #endif
   
   while((op = getopt(argc, argv, theOpts)) != EOF)
@@ -198,6 +198,11 @@
       case 'b': /* host:port */
        stringSanityCheck(optarg);
        handleDbSupport(optarg, &enableDBsupport);
+       break;
+
+      case 'v': /* username:password:dbname */
+       stringSanityCheck(optarg);
+       handlemySQLSupport(optarg, &enableDBsupport);
        break;
 
       case 'D': /* domain */
diff -u --recursive --new-file ntop-current/ntop/mysql.c ntop-mysql/ntop/mysql.c
--- ntop-current/ntop/mysql.c   Thu Jan  1 01:00:00 1970
+++ ntop-mysql/ntop/mysql.c     Wed Jun 13 14:11:34 2001
@@ -0,0 +1,541 @@
+/**************************************************
+ * This file contains the code for NTOP to communicate direct to a
+ * mySQL database. It is very messy atm, but hopefully I'll make it
+ * cleaner..
+**************************************************/
+
+
+#include <mysql/mysql.h>
+#include <string.h>
+#include "ntop.h"
+#include "globals-report.h"    
+
+MYSQL  mysql, *mysql_sock;
+MYSQL_RES      *mysql_res;
+char qbuf[160];
+struct sockaddr_in dest;
+
+/*-----------------------------------------------*/
+
+void handlemySQLSupport(char* param /* host:port */, int* enableDBsupport) {
+  char *userName, *password, *dbName, *strtokState;
+
+/**************************************************
+ * Address is not set or in use atm. The user, passwd and name is
+ * hardcore programmed in this file..
+**************************************************/
+  printf("Param: %s\n", param);
+
+  if((param == NULL) || (param[0] == '\0'))
+    return;
+
+  userName = strtok_r(param, ":", &strtokState);
+  password = strtok_r(NULL, ":", &strtokState);
+  dbName = strtok_r(NULL, ":", &strtokState);
+
+  printf("User: %s\nPass: %s\ndb: %s\n", userName, password, dbName);
+
+  if ((userName == NULL) || (password == NULL) || (dbName == NULL)) {
+    traceEvent(TRACE_INFO, "WARNING: invalid value specified for '-v' parameter. \n"
+       "         It should be username:password:dbName.\n");
+    return;
+  } else {
+    (*enableDBsupport) = 1;
+    open_mySQLsocket(userName, password, dbName); 
+  }
+}
+
+/*-----------------------------------------------*/
+
+void open_mySQLsocket(char *userName, char* password, char* dbName) {
+  mysql_init(&mysql);
+  if (!(mysql_sock = 
+mysql_real_connect(&mysql,NULL,userName,password,dbName,0,NULL,0)))
+  {
+    printf("Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
+    perror("");
+  }
+}
+
+/*-----------------------------------------------*/
+
+void closemySQLsocket(void) {
+  mysql_close(mysql_sock);
+  printf("DEBUG: Socket is down...Socket is down...\n");
+}
+
+/*-----------------------------------------------*/
+
+void mySQLupdateHostNameInfo(unsigned long numeric, char* symbolic) {
+  char *hostName;
+  struct in_addr addr;
+  char buf[32];
+
+  addr.s_addr = numeric;
+
+  hostName = _intoa(addr, buf, sizeof(buf));
+
+/********************************************************************
+ * The code here is taken from sql.c, just translated to the mySQL C
+ * API interface.
+********************************************************************/
+
+  if(strcmp(hostName, symbolic) != 0) {  
+    /* [1] Delete */
+    if(sprintf(qbuf, "DELETE FROM NameMapper WHERE IPaddress = '%s'", hostName) < 0) 
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (UHI): %s\n", qbuf);
+    } else {
+      printf("Satt inn (UHI): %s\n", qbuf);
+    }
+
+    /* [2] Insert */ 
+    if(sprintf(qbuf,"INSERT INTO NameMapper (IPaddress, Name)"
+         " VALUES ('%s', '%s')", hostName, symbolic) < 0)
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt inn inn (UHI): %s\n", qbuf);
+    } else {
+      printf("Satt inn (UHI): %s\n", qbuf);
+    }
+  } 
+}
+
+/*-----------------------------------------------*/
+
+void mySQLupdateHostTraffic(HostTraffic *el) {
+  char theDate[32], theDate2[32], mySQLcmd[30], ip[15];
+  struct tm t;
+
+  /* Fixes below courtesy of Andreas Pfaller <[EMAIL PROTECTED]> */
+  strftime(theDate2, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&el->firstSeen, &t));
+
+  /* Added by David Moore <[EMAIL PROTECTED]> */
+  strftime(theDate, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&el->lastSeen, &t));  
+
+  /* ****************************** */
+
+  if ((strcmp(el->hostNumIpAddress, "")) == 0) {
+    printf("DEBUG: Empty IP, gonna do NOTHING\n");
+  } else {
+
+    sprintf(qbuf, "SELECT IPaddress FROM Hosts WHERE IPaddress = \"%s\"", 
+el->hostNumIpAddress);
+    printf("DEBUG: %s\n", qbuf);
+
+    if(mysql_query(mysql_sock,qbuf)) {
+      fprintf(stderr,"Query failed (%s)\n",mysql_error(mysql_sock));
+    }
+
+    if (!(mysql_res=mysql_store_result(mysql_sock))) {
+      printf("DEBUG: Couldn't get result from %s\n",
+         mysql_error(mysql_sock));
+    }
+
+/********************************************************************
+ * Code here is also taken from sql.c, but I had to make some changes.
+ * First of all, it is translated to the mySQL C API. Second, I
+ * discoverd that mySQL can not do a update if a entry is not there,
+ * so I had to check the if the given IP-adr. was already in the DB,
+ * and then do a UPDATE or INSERT accordingly..
+********************************************************************/
+    
+    if (mysql_num_rows(mysql_res) > 0) {
+      printf("DEBUG: IP exists, gonna do a UPDATE:\n");
+      if(sprintf(qbuf, "UPDATE Hosts SET "
+           "PktSent = %llu, "
+           "PktRcvd = %llu, "
+           "PktMulticastSent = %llu, "
+           "PktBroadcastSent = %llu, "
+           "DataSent = %llu, "
+           "DataRcvd = %llu, "
+           "DataMulticastSent = %llu, "
+           "DataBroadcastSent = %llu, "
+           "FirstSeen = '%s', "
+           "LastSeen = '%s' "
+           "WHERE IPaddress = '%s'",
+           (el->pktSent),
+           (el->pktReceived),
+           (el->pktMulticastSent),
+           (el->pktBroadcastSent),
+           (el->bytesSent),
+           (el->bytesReceived),
+           (el->bytesMulticastSent),
+           (el->bytesBroadcastSent),
+           theDate2, theDate,
+           el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer overflow!");
+
+           if (mysql_query(mysql_sock, qbuf)) {
+             printf("Satt ikke inn (UHT): %s\n", qbuf);
+           } else {
+             printf("Satt inn (UHT): %s\n", qbuf);
+           }
+    } else {
+      printf("DEBUG: IP DOES NOT exist, gonna do a INSERT:\n");
+      if(sprintf(qbuf, "INSERT Hosts SET "
+           "PktSent = %llu, "
+           "PktRcvd = %llu, "
+           "PktMulticastSent = %llu, "
+           "PktBroadcastSent = %llu, "
+           "DataSent = %llu, "
+           "DataRcvd = %llu, "
+           "DataMulticastSent = %llu, "
+           "DataBroadcastSent = %llu, "
+           "FirstSeen = '%s', "
+           "LastSeen = '%s', "
+           "IPaddress = '%s'",
+           (el->pktSent),
+           (el->pktReceived),
+           (el->pktMulticastSent),
+           (el->pktBroadcastSent),
+           (el->bytesSent),
+           (el->bytesReceived),
+           (el->bytesMulticastSent),
+           (el->bytesBroadcastSent),
+           theDate2, theDate,
+           el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer overflow!");
+
+           if (mysql_query(mysql_sock, qbuf)) {
+             printf("Satt ikke inn (UHT): %s\n", qbuf);
+           } else {
+             printf("Satt inn (UHT): %s\n", qbuf);
+           }
+
+           /* --- */
+
+           sprintf(qbuf, "SELECT IPaddress FROM IPtraffic WHERE IPaddress = \"%s\"", 
+el->hostNumIpAddress);
+           printf("DEBUG: %s\n", qbuf);
+
+           if(mysql_query(mysql_sock,qbuf)) {
+             fprintf(stderr,"Query failed (%s)\n",mysql_error(mysql_sock));
+           }
+
+           if (!(mysql_res=mysql_store_result(mysql_sock))) {
+             printf("DEBUG: Couldn't get result from %s\n",
+                 mysql_error(mysql_sock));
+           }
+
+           if (mysql_num_rows(mysql_res) > 0) {
+             printf("DEBUG: IP exists, gonna do a UPDATE:\n");
+             if(sprintf(qbuf, "UPDATE IPtraffic SET "
+                   "TCPSentLocally = %llu, "
+                   "TCPSentRemotely = %llu, "
+                   "TCPrcvdLocally = %llu, "
+                   "TCPrcvdFromRemote = %llu, "
+                   "UDPSentLocally = %llu, "
+                   "UDPSentRemotely = %llu, "
+                   "UDPrcvdLocally = %llu, "
+                   "UDPrcvdFromRemote = %llu, "
+                   "ICMPsent = %llu, "
+                   "ICMPrcvd = %llu, "
+                   "OSPFsent = %llu, "
+                   "OSPFrcvd = %llu, "
+                   "IGMPsent = %llu, "
+                   "IGMPrcvd = %llu WHERE "
+                   "IPaddress = '%s'",
+                   (el->tcpSentLocally),
+                   (el->tcpSentRemotely),
+                   (el->tcpReceivedLocally),
+                   (el->tcpReceivedFromRemote),
+                   (el->udpSentLocally),
+                   (el->udpSentRemotely),
+                   (el->udpReceivedLocally),
+                   (el->udpReceivedFromRemote),
+                   (el->icmpSent),
+                   (el->icmpReceived),
+                   (el->ospfSent),
+                   (el->ospfReceived),
+                   (el->igmpSent),
+                   (el->igmpReceived),
+                   el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer 
+overflow!");
+
+                   if (mysql_query(mysql_sock, qbuf)) {
+                     printf("Satt ikke inn (UHT): %s\n", qbuf);
+                   } else {
+                     printf("Satt inn (UHT): %s\n", qbuf);
+                   }
+
+           } else {
+
+             printf("DEBUG: IP DOES NOT exist, gonna do a INSERT:\n");
+             if(sprintf(qbuf, "INSERT IPtraffic SET "
+                   "TCPSentLocally = %llu, "
+                   "TCPSentRemotely = %llu, "
+                   "TCPrcvdLocally = %llu, "
+                   "TCPrcvdFromRemote = %llu, "
+                   "UDPSentLocally = %llu, "
+                   "UDPSentRemotely = %llu, "
+                   "UDPrcvdLocally = %llu, "
+                   "UDPrcvdFromRemote = %llu, "
+                   "ICMPsent = %llu, "
+                   "ICMPrcvd = %llu, "
+                   "OSPFsent = %llu, "
+                   "OSPFrcvd = %llu, "
+                   "IGMPsent = %llu, "
+                   "IGMPrcvd = %llu, "
+                   "IPaddress = '%s'",
+                   (el->tcpSentLocally),
+                   (el->tcpSentRemotely),
+                   (el->tcpReceivedLocally),
+                   (el->tcpReceivedFromRemote),
+                   (el->udpSentLocally),
+                   (el->udpSentRemotely),
+                   (el->udpReceivedLocally),
+                   (el->udpReceivedFromRemote),
+                   (el->icmpSent),
+                   (el->icmpReceived),
+                   (el->ospfSent),
+                   (el->ospfReceived),
+                   (el->igmpSent),
+                   (el->igmpReceived),
+                   el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer 
+overflow!");
+           }
+
+           /* --- */
+
+           sprintf(qbuf, "SELECT IPaddress FROM NonIPTraffic WHERE IPaddress = 
+\"%s\"", el->hostNumIpAddress);
+           printf("DEBUG: %s\n", qbuf);
+
+           if(mysql_query(mysql_sock,qbuf)) {
+             fprintf(stderr,"Query failed (%s)\n",mysql_error(mysql_sock));
+           }
+
+           if (!(mysql_res=mysql_store_result(mysql_sock))) {
+             printf("DEBUG: Couldn't get result from %s\n",
+                 mysql_error(mysql_sock));
+           }
+
+           if (mysql_num_rows(mysql_res) > 0) {
+             printf("DEBUG: IP exists, gonna do a UPDATE:\n");
+             if(sprintf(qbuf, "UPDATE NonIPTraffic SET "
+                   "IPXsent = %llu, "
+                   "IPXrcvd = %llu, "
+                   "OSIsent = %llu, "
+                   "OSIrcvd = %llu, "
+                   "DLCsent = %llu, "
+                   "DLCrcvd = %llu, "
+                   "ARPsent = %llu, "
+                   "ARPrcvd = %llu, "
+                   "DECNETsent = %llu, "
+                   "DECNETrcvd = %llu, "
+                   "ATALKsent = %llu, "
+                   "ATALKrcvd = %llu, "
+                   "NBIOSsent = %llu, "
+                   "NBIOSrcvd = %llu, "
+                   "OtherSent = %llu, "
+                   "OtherRcvd = %llu WHERE "
+                   "IPaddress = '%s'",
+                   (el->ipxSent),
+                   (el->ipxReceived),
+                   (el->osiSent),
+                   (el->osiReceived),
+                   (el->dlcSent),
+                   (el->dlcReceived),
+                   (el->arp_rarpSent),
+                   (el->arp_rarpReceived),
+                   (el->decnetSent),
+                   (el->decnetReceived),
+                   (el->appletalkSent),
+                   (el->appletalkReceived),
+                   (el->netbiosSent),
+                   (el->netbiosReceived),
+                   (el->otherSent),
+                   (el->otherReceived),
+                   el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer 
+overflow!");
+
+                   if (mysql_query(mysql_sock, qbuf)) {
+                     printf("Satt ikke inn (UHT): %s\n", qbuf);
+                   } else {
+                     printf("Satt inn (UHT): %s\n", qbuf);
+                   }
+                   
+           } else {
+             
+             printf("DEBUG: IP DOES NOT exist, gonna do a INSERT:\n");
+             if(sprintf(qbuf, "INSERT NonIPTraffic SET "
+                   "IPXsent = %llu, "
+                   "IPXrcvd = %llu, "
+                   "OSIsent = %llu, "
+                   "OSIrcvd = %llu, "
+                   "DLCsent = %llu, "
+                   "DLCrcvd = %llu, "
+                   "ARPsent = %llu, "
+                   "ARPrcvd = %llu, "
+                   "DECNETsent = %llu, "
+                   "DECNETrcvd = %llu, "
+                   "ATALKsent = %llu, "
+                   "ATALKrcvd = %llu, "
+                   "NBIOSsent = %llu, "
+                   "NBIOSrcvd = %llu, "
+                   "OtherSent = %llu, "
+                   "OtherRcvd = %llu, "
+                   "IPaddress = '%s'",
+                   (el->ipxSent),
+                   (el->ipxReceived),
+                   (el->osiSent),
+                   (el->osiReceived),
+                   (el->dlcSent),
+                   (el->dlcReceived),
+                   (el->arp_rarpSent),
+                   (el->arp_rarpReceived),
+                   (el->decnetSent),
+                   (el->decnetReceived),
+                   (el->appletalkSent),
+                   (el->appletalkReceived),
+                   (el->netbiosSent),
+                   (el->netbiosReceived),
+                   (el->otherSent),
+                   (el->otherReceived),
+                   el->hostNumIpAddress) < 0) traceEvent(TRACE_ERROR, "Buffer 
+overflow!");
+
+
+                   if (mysql_query(mysql_sock, qbuf)) {
+                     printf("Satt ikke inn (UHT): %s\n", qbuf);
+                   } else {
+                     printf("Satt inn (UHT): %s\n", qbuf);
+                   }
+           }
+    }
+  }
+}
+
+/*-----------------------------------------------------------------*/
+
+void mySQLnotifyHostCreation(HostTraffic *el) {
+
+  /* [1] Delete */
+
+  if(el->hostNumIpAddress[0] != '\0') {
+    if(sprintf(qbuf, "DELETE FROM Hosts WHERE IPaddress = '%s'", 
+               el->hostNumIpAddress) < 0) 
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+  } else {
+    if(sprintf(qbuf, "DELETE FROM Hosts WHERE MACaddress = '%s'", 
+               el->ethAddressString) < 0) 
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+  }
+  
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (HC): %s\n", qbuf);
+    } else {
+      printf("Satt inn (HC): %s\n", qbuf);
+    }
+  
+  /* [2] Insert */
+  if(sprintf(qbuf, "INSERT INTO Hosts (IPaddress, MACaddress, NICvendor)"
+         " VALUES ('%s', '%s', '%s')",
+         el->hostNumIpAddress,
+         el->ethAddressString,
+         getVendorInfo(el->ethAddress, 0)) < 0) traceEvent(TRACE_ERROR, "Buffer 
+overflow!");
+
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (HC): %s\n", qbuf);
+    } else {
+      printf("Satt inn (HC): %s\n", qbuf);
+    }
+
+  if(el->hostNumIpAddress[0] != '\0') {
+    if(sprintf(qbuf, "DELETE FROM NonIPTraffic WHERE IPaddress = '%s'",
+               el->hostNumIpAddress) < 0) 
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (HC): %s\n", qbuf);
+    } else {
+      printf("Satt inn (HC): %s\n", qbuf);
+    }
+
+    if(sprintf(qbuf, "INSERT INTO NonIPTraffic (IPaddress) VALUES ('%s')", 
+               el->hostNumIpAddress) < 0) 
+     traceEvent(TRACE_ERROR, "Buffer overflow!");
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (HC): %s\n", qbuf);
+    } else {
+      printf("Satt inn (HC): %s\n", qbuf);
+    }
+
+    if(sprintf(qbuf, "DELETE FROM IPtraffic WHERE IPaddress = '%s'", 
+               el->hostNumIpAddress) < 0) 
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (HC): %s\n", qbuf);
+    } else {
+      printf("Satt inn (HC): %s\n", qbuf);
+    }
+
+    if(sprintf(qbuf, "INSERT INTO IPtraffic (IPaddress) VALUES ('%s')", 
+               el->hostNumIpAddress) < 0) 
+      traceEvent(TRACE_ERROR, "Buffer overflow!");
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (HC): %s\n", qbuf);
+    } else {
+      printf("Satt inn (HC): %s\n", qbuf);
+    }
+  }
+
+  /* traceEvent(TRACE_INFO, "%s\n", buf); */
+
+}
+
+/*-----------------------------------------------------------------*/
+
+void mySQLnotifyTCPSession(IPSession *session) {
+  HostTraffic *server, *client;
+  char dt1[32], dt2[32];
+  struct tm t;
+
+  /*
+  if((sqlSocket == -1) 
+     || (session->initiatorIdx == NO_PEER)
+     || (session->remotePeerIdx == NO_PEER))
+    return;
+*/
+  client = 
+device[actualDeviceId].hash_hostTraffic[checkSessionIdx(session->initiatorIdx)];
+  server = 
+device[actualDeviceId].hash_hostTraffic[checkSessionIdx(session->remotePeerIdx)];
+
+  strftime(dt1, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&session->firstSeen, &t));  
+  strftime(dt2, 32, "%Y-%m-%d %H:%M:%S", localtime_r(&session->lastSeen, &t));  
+
+  if(sprintf(qbuf, "INSERT INTO TCPsessions (Client, Server, ClientPort, "
+         "ServerPort, DataSent, DataRcvd, FirstSeen, LastSeen)"
+         " VALUES ('%s', '%s', '%d', '%d', '%llu', '%llu', '%s', '%s')",
+         client->hostNumIpAddress,
+         server->hostNumIpAddress,
+         session->sport,
+         session->dport,
+         (session->bytesSent), 
+         (session->bytesReceived),
+         dt1, dt2) < 0) 
+    traceEvent(TRACE_ERROR, "Buffer overflow!");
+
+  /* traceEvent(TRACE_INFO, "%s\n", sqlBuf); */
+
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (TS): %s\n", qbuf);
+    } else {
+      printf("Satt inn (TS): %s\n", qbuf);
+    }
+}
+
+/*-----------------------------------------------------------------*/
+
+
+void mySQLupdateDBOSname(HostTraffic *el) {
+  /*
+  if((sqlSocket == -1) 
+     || (el->osName == NULL)
+     || (el->osName[0] == '\0'))
+    return;
+  */
+  /* traceEvent(TRACE_INFO, "%s@%s\n", el->osName, el->hostNumIpAddress); */
+
+  if(sprintf(qbuf, "UPDATE Hosts SET "
+         "OsName = '%s' WHERE IPaddress = '%s'",
+         el->osName, el->hostNumIpAddress) < 0) 
+    traceEvent(TRACE_ERROR, "Buffer overflow!");
+  
+    if (mysql_query(mysql_sock, qbuf)) {
+      printf("Satt ikke inn (DBOS): %s\n", qbuf);
+    } else {
+      printf("Satt inn (DBOS): %s\n", qbuf);
+    }
+}
+
diff -u --recursive --new-file ntop-current/ntop/ntop.c ntop-mysql/ntop/ntop.c
--- ntop-current/ntop/ntop.c    Fri Jun  1 18:26:51 2001
+++ ntop-mysql/ntop/ntop.c      Wed Jun 13 14:18:37 2001
@@ -823,8 +823,10 @@
        }
       }
 
-  if(enableDBsupport)
+  if(enableDBsupport) {
     closeSQLsocket(); /* *** SQL Engine *** */
+    closemySQLsocket(); /* *** SQL Engine *** */
+  }
 
 #ifdef WIN32
   termWinsock32();
diff -u --recursive --new-file ntop-current/ntop/pbuf.c ntop-mysql/ntop/pbuf.c
--- ntop-current/ntop/pbuf.c    Sat Jun  9 00:05:47 2001
+++ ntop-mysql/ntop/pbuf.c      Wed Jun 13 14:20:12 2001
@@ -736,6 +736,7 @@
            sessionToPurge->magic = 0;
 
            notifyTCPSession(sessionToPurge);
+           mySQLnotifyTCPSession(sessionToPurge);
            free(sessionToPurge); /* No inner pointers to free */
          }
       }
@@ -4781,6 +4782,7 @@
       el->osName = strdup(theName);
 
       updateDBOSname(el);
+      mySQLupdateDBOSname(el);
 
 #ifdef HAVE_GDBM_H
       if(snprintf(tmpBuf, sizeof(tmpBuf), "@%s", el->hostNumIpAddress) < 0)
diff -u --recursive --new-file ntop-current/ntop/traffic.c ntop-mysql/ntop/traffic.c
--- ntop-current/ntop/traffic.c Mon Apr 30 11:48:13 2001
+++ ntop-mysql/ntop/traffic.c   Wed Jun 13 14:21:13 2001
@@ -503,10 +503,12 @@
 
       el->instanceInUse++;
 
-      if(el->nextDBupdate == 0)
+      if(el->nextDBupdate == 0) {
        notifyHostCreation(el);
-      else if(el->nextDBupdate < actTime) {
+       mySQLnotifyHostCreation(el);
+      } else if(el->nextDBupdate < actTime) {
        updateHostTraffic(el);
+       mySQLupdateHostTraffic(el);
        if(el->osName == NULL) {
          updateOSName(el);
        }
diff -u --recursive --new-file ntop-current/ntop/webInterface.c 
ntop-mysql/ntop/webInterface.c
--- ntop-current/ntop/webInterface.c    Mon May  7 11:10:14 2001
+++ ntop-mysql/ntop/webInterface.c      Wed Jun 13 14:21:44 2001
@@ -900,6 +900,7 @@
   traceEvent(TRACE_INFO, "    %s\n",   "[-s <max hash size (default 32768)>]");
   traceEvent(TRACE_INFO, "    %s\n",   "[-F <flow specs (see man page)>]");
   traceEvent(TRACE_INFO, "    %s\n",   "[-b <client:port (ntop DB client)>]");
+  traceEvent(TRACE_INFO, "    %s\n",   "[-v <username:password:dbName (ntop mySQL 
+client)>]");
   traceEvent(TRACE_INFO, "    %s\n",   "[-R <matching rules file>]");
   traceEvent(TRACE_INFO, "    %s\n",   "[-N <don't use nmap if installed>]");
   traceEvent(TRACE_INFO, "    %s\n",   "[-M <don't merge network interfaces (see man 
page)>]");

Reply via email to