|
@@ -0,0 +1,2887 @@
+/*
+ * nProbe - a Netflow v5/v9/IPFIX probe for IPv4/v6
+ *
+ * Copyright (C) 2002-10 Luca Deri <deri@ntop.org>
+ *
+ * http://www.ntop.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "nprobe.h"
+
+#ifdef sun
+extern char *strtok_r(char *, const char *, char **);
+#endif
+
+#ifdef WIN32
+#define strtok_r(a, b, c) strtok(a, b)
+#endif
+
+#ifdef HAVE_SQLITE
+extern void sqlite_exec_sql(char* sql);
+#endif
+
+/* ********************** */
+
+static char *port_mapping[0xFFFF] = { NULL };
+static char *proto_mapping[0xFF] = { NULL };
+
+static u_int32_t localNetworks[MAX_NUM_NETWORKS][CONST_NETWORK_SIZE];
+static u_int32_t blacklistNetworks[MAX_NUM_NETWORKS][CONST_NETWORK_SIZE];
+
+/* ********************** */
+
+#define CUSTOM_FIELD_LEN 16
+
+/* ************************************ */
+
+void traceEvent(const int eventTraceLevel, const char* file,
+ const int line, const char * format, ...) {
+ va_list va_ap;
+
+ if(eventTraceLevel <= readOnlyGlobals.traceLevel) {
+ char buf[2048], out_buf[640];
+ char theDate[32], *extra_msg = "";
+ time_t theTime = time(NULL);
+
+ va_start (va_ap, format);
+
+ /* We have two paths - one if we're logging, one if we aren't
+ * Note that the no-log case is those systems which don't support it (WIN32),
+ * those without the headers !defined(USE_SYSLOG)
+ * those where it's parametrically off...
+ */
+
+ memset(buf, 0, sizeof(buf));
+ strftime(theDate, 32, "%d/%b/%Y %H:%M:%S", localtime(&theTime));
+
+ vsnprintf(buf, sizeof(buf)-1, format, va_ap);
+
+ if(eventTraceLevel == 0 /* TRACE_ERROR */)
+ extra_msg = "ERROR: ";
+ else if(eventTraceLevel == 1 /* TRACE_WARNING */)
+ extra_msg = "WARNING: ";
+
+ while(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
+
+ snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate,
+#ifdef WIN32
+ strrchr(file, '\\')+1,
+#else
+ file,
+#endif
+ line, extra_msg, buf);
+
+#ifndef WIN32
+ if(readOnlyGlobals.useSyslog) {
+ if(!readWriteGlobals->syslog_opened) {
+ openlog(readOnlyGlobals.nprobeId, LOG_PID, LOG_DAEMON);
+ readWriteGlobals->syslog_opened = 1;
+ }
+
+ syslog(LOG_INFO, "%s", out_buf);
+ } else
+ printf("%s\n", out_buf);
+#else
+ printf("%s\n", out_buf);
+#endif
+ }
+
+ fflush(stdout);
+ va_end(va_ap);
+}
+
+
+/* ************************************ */
+
+#ifdef WIN32
+unsigned long waitForNextEvent(unsigned long ulDelay /* ms */) {
+ unsigned long ulSlice = 1000L; /* 1 Second */
+
+ while(ulDelay > 0L) {
+ if(ulDelay < ulSlice)
+ ulSlice = ulDelay;
+ Sleep(ulSlice);
+ ulDelay -= ulSlice;
+ }
+
+ return ulDelay;
+}
+
+/* ******************************* */
+
+void initWinsock32() {
+ WORD wVersionRequested;
+ WSADATA wsaData;
+ int err;
+
+ wVersionRequested = MAKEWORD(2, 0);
+ err = WSAStartup( wVersionRequested, &wsaData );
+ if( err != 0 ) {
+ /* Tell the user that we could not find a usable */
+ /* WinSock DLL. */
+ traceEvent(TRACE_ERROR, "FATAL ERROR: unable to initialise Winsock 2.x.");
+ exit(-1);
+ }
+}
+
+/* ******************************** */
+
+short isWinNT() {
+ DWORD dwVersion;
+ DWORD dwWindowsMajorVersion;
+
+ dwVersion=GetVersion();
+ dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
+ if(!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
+ return 1;
+ else
+ return 0;
+}
+
+/* ****************************************************** */
+/*
+ int snprintf(char *string, size_t maxlen, const char *format, ...) {
+ int ret=0;
+ va_list args;
+
+ va_start(args, format);
+ vsprintf(string,format,args);
+ va_end(args);
+ return ret;
+ }
+*/
+#endif /* Win32 */
+
+/* ******************************************************************* */
+
+u_int8_t ip2mask(IpAddress ip) {
+ if((readOnlyGlobals.numLocalNetworks == 0) || (ip.ipVersion != 4))
+ return(0);
+ else {
+ int i;
+ u_int32_t addr = htonl(ip.ipType.ipv4);
+
+ for(i=0; i<readOnlyGlobals.numLocalNetworks; i++) {
+ if((addr & localNetworks[i][CONST_NETMASK_ENTRY]) == localNetworks[i][CONST_NETWORK_ENTRY]) {
+ // traceEvent(TRACE_INFO, "--> %d", localNetworks[i][CONST_NETMASK_V6_ENTRY]);
+ return(localNetworks[i][CONST_NETMASK_V6_ENTRY]);
+ }
+ }
+ }
+
+ return(0); /* Unknown */
+}
+
+/* ******************************************************************* */
+
+static ip_to_AS _ip_to_AS;
+static fillASinfo _fillASinfo;
+
+void initAS() {
+ _ip_to_AS = NULL;
+ _fillASinfo = NULL;
+}
+
+
|