Lines 12-17
Link Here
|
12 |
#include <time.h> |
12 |
#include <time.h> |
13 |
#include <setjmp.h> |
13 |
#include <setjmp.h> |
14 |
|
14 |
|
|
|
15 |
#ifdef WITH_MYSQL |
16 |
#include <mysql/mysql.h> |
17 |
#include "sql.h" |
18 |
#endif |
19 |
|
15 |
#ifdef WIN32 |
20 |
#ifdef WIN32 |
16 |
#include "winsock2.h" |
21 |
#include "winsock2.h" |
17 |
#include "fcntl.h" |
22 |
#include "fcntl.h" |
Lines 116-121
Link Here
|
116 |
u_int32_t linktype; /* data link type (DLT_*) */ |
121 |
u_int32_t linktype; /* data link type (DLT_*) */ |
117 |
}; |
122 |
}; |
118 |
|
123 |
|
|
|
124 |
#ifdef WITH_MYSQL |
125 |
/* mysql db information */ |
126 |
typedef struct _db_settings { |
127 |
char *username; |
128 |
char *password; |
129 |
char *host; |
130 |
char *database; |
131 |
char *type; // "MySQL" for the moment |
132 |
} db_settings; |
133 |
#endif |
134 |
|
119 |
struct pcap_pkthdr { |
135 |
struct pcap_pkthdr { |
120 |
struct timeval ts; /* time stamp */ |
136 |
struct timeval ts; /* time stamp */ |
121 |
u_int32_t caplen; /* length of portion present */ |
137 |
u_int32_t caplen; /* length of portion present */ |
Lines 137-143
Link Here
|
137 |
int sock; |
153 |
int sock; |
138 |
} SFForwardingTarget; |
154 |
} SFForwardingTarget; |
139 |
|
155 |
|
140 |
typedef enum { SFLFMT_FULL=0, SFLFMT_PCAP, SFLFMT_LINE, SFLFMT_NETFLOW, SFLFMT_FWD } EnumSFLFormat; |
156 |
typedef enum { SFLFMT_FULL=0, SFLFMT_PCAP, SFLFMT_LINE, SFLFMT_LINE_EXTENDED, SFLFMT_NETFLOW, SFLFMT_FWD } EnumSFLFormat; |
141 |
|
157 |
|
142 |
typedef struct _SFConfig { |
158 |
typedef struct _SFConfig { |
143 |
/* sflow(R) options */ |
159 |
/* sflow(R) options */ |
Lines 157-162
Link Here
|
157 |
u_int32_t tcpdumpHdrPad; |
173 |
u_int32_t tcpdumpHdrPad; |
158 |
u_char zeroPad[100]; |
174 |
u_char zeroPad[100]; |
159 |
int pcapSwap; |
175 |
int pcapSwap; |
|
|
176 |
#ifdef WITH_MYSQL |
177 |
/* db settings */ |
178 |
struct _db_settings db_settings; |
179 |
#endif |
160 |
|
180 |
|
161 |
#ifdef SPOOFSOURCE |
181 |
#ifdef SPOOFSOURCE |
162 |
int spoofSource; |
182 |
int spoofSource; |
Lines 507-512
Link Here
|
507 |
sample->meanSkipCount); |
527 |
sample->meanSkipCount); |
508 |
} |
528 |
} |
509 |
|
529 |
|
|
|
530 |
/*_________________---------------------------__________________ |
531 |
* _________________ writeFlowLineExtended __________________ |
532 |
* -----------------___________________________------------------ |
533 |
*/ |
534 |
|
535 |
static void writeFlowLineExtended(SFSample *sample) |
536 |
{ |
537 |
|
538 |
char agentIP[51], srcIP[51], dstIP[51]; |
539 |
// source |
540 |
printf("FLOW,%s,%d,%d,", |
541 |
printAddress(&sample->agent_addr, agentIP, 50), |
542 |
sample->inputPort, |
543 |
sample->outputPort); |
544 |
// layer 2 |
545 |
|
546 |
printf("%02x%02x%02x%02x%02x%02x,%02x%02x%02x%02x%02x%02x,0x%04x,%d,%d", |
547 |
sample->eth_src[0], |
548 |
sample->eth_src[1], |
549 |
sample->eth_src[2], |
550 |
sample->eth_src[3], |
551 |
sample->eth_src[4], |
552 |
sample->eth_src[5], |
553 |
sample->eth_dst[0], |
554 |
sample->eth_dst[1], |
555 |
sample->eth_dst[2], |
556 |
sample->eth_dst[3], |
557 |
sample->eth_dst[4], |
558 |
sample->eth_dst[5], |
559 |
sample->eth_type, |
560 |
sample->in_vlan, |
561 |
sample->out_vlan); |
562 |
// layer 3/4 |
563 |
printf(",%s,%s,%d,0x%02x,%d,%d,%d,0x%02x", |
564 |
IP_to_a(sample->ipsrc.address.ip_v4.s_addr, srcIP), |
565 |
IP_to_a(sample->ipdst.address.ip_v4.s_addr, dstIP), |
566 |
sample->dcd_ipProtocol, |
567 |
sample->dcd_ipTos, |
568 |
sample->dcd_ipTTL, |
569 |
sample->dcd_sport, |
570 |
sample->dcd_dport, |
571 |
sample->dcd_tcpFlags); |
572 |
// bytes |
573 |
printf(",%d,%d,%d", |
574 |
sample->sampledPacketSize, |
575 |
sample->sampledPacketSize - sample->stripped - |
576 |
sample->offsetToIPV4, |
577 |
sample->meanSkipCount); |
578 |
// Extended Information |
579 |
// Colin |
580 |
//printf(",%d,",sample->my_as); |
581 |
|
582 |
printf(",%d,%d,%d,%d,%d,%d,%d,%d\n", |
583 |
sample->my_as, |
584 |
sample->srcMask, |
585 |
sample->dstMask, |
586 |
sample->src_as, |
587 |
sample->src_peer_as, |
588 |
sample->dst_as_path_len, |
589 |
sample->dst_peer_as, |
590 |
sample->dst_as); |
591 |
} |
592 |
|
593 |
|
594 |
#ifdef WITH_MYSQL |
595 |
/*_________________---------------------------__________________ |
596 |
_________________ writeMySQLRecord __________________ |
597 |
-----------------___________________________------------------ |
598 |
*/ |
599 |
|
600 |
static void writeMySQLRecord(SFSample *sample) |
601 |
{ |
602 |
char agentIP[51], srcIP[51], dstIP[51]; |
603 |
/* do mysql insert */ |
604 |
char buf[2048]; |
605 |
sprintf(buf, sflow_to_mysql, |
606 |
printAddress(&sample->agent_addr, agentIP, 50), |
607 |
sample->inputPort, |
608 |
sample->outputPort, |
609 |
sample->eth_src[0], |
610 |
sample->eth_src[1], |
611 |
sample->eth_src[2], |
612 |
sample->eth_src[3], |
613 |
sample->eth_src[4], |
614 |
sample->eth_src[5], |
615 |
sample->eth_dst[0], |
616 |
sample->eth_dst[1], |
617 |
sample->eth_dst[2], |
618 |
sample->eth_dst[3], |
619 |
sample->eth_dst[4], |
620 |
sample->eth_dst[5], |
621 |
sample->eth_type, |
622 |
sample->in_vlan, |
623 |
sample->out_vlan, |
624 |
IP_to_a(sample->ipsrc.address.ip_v4.s_addr, srcIP), |
625 |
IP_to_a(sample->ipdst.address.ip_v4.s_addr, dstIP), |
626 |
sample->dcd_ipProtocol, |
627 |
sample->dcd_ipTos, |
628 |
sample->dcd_ipTTL, |
629 |
sample->dcd_sport, |
630 |
sample->dcd_dport, |
631 |
sample->dcd_tcpFlags, |
632 |
sample->sampledPacketSize, |
633 |
sample->sampledPacketSize - sample->stripped - sample->offsetToIPV4, |
634 |
sample->meanSkipCount, |
635 |
sample->my_as, |
636 |
sample->srcMask, |
637 |
sample->dstMask, |
638 |
sample->src_as, |
639 |
sample->src_peer_as, |
640 |
sample->dst_as_path_len, |
641 |
sample->dst_peer_as, |
642 |
sample->dst_as); |
643 |
|
644 |
if(mysql_real_query(&mysql, buf, strlen(buf))){ |
645 |
fprintf(stderr, "mysql_real_query() failed\n"); |
646 |
fprintf(stderr, "%s\n", mysql_error(&mysql)); |
647 |
} |
648 |
} |
649 |
#endif |
650 |
|
651 |
|
510 |
/*_________________---------------------------__________________ |
652 |
/*_________________---------------------------__________________ |
511 |
_________________ writeCountersLine __________________ |
653 |
_________________ writeCountersLine __________________ |
512 |
-----------------___________________________------------------ |
654 |
-----------------___________________________------------------ |
Lines 1988-1995
Link Here
|
1988 |
case INMEXTENDED_SWITCH: readExtendedSwitch(sample); break; |
2130 |
case INMEXTENDED_SWITCH: readExtendedSwitch(sample); break; |
1989 |
case INMEXTENDED_ROUTER: readExtendedRouter(sample); break; |
2131 |
case INMEXTENDED_ROUTER: readExtendedRouter(sample); break; |
1990 |
case INMEXTENDED_GATEWAY: |
2132 |
case INMEXTENDED_GATEWAY: |
1991 |
if(sample->datagramVersion == 2) readExtendedGateway_v2(sample); |
2133 |
if(sample->datagramVersion == 2) { |
1992 |
else readExtendedGateway(sample); |
2134 |
readExtendedGateway_v2(sample); |
|
|
2135 |
} else readExtendedGateway(sample); |
1993 |
break; |
2136 |
break; |
1994 |
case INMEXTENDED_USER: readExtendedUser(sample); break; |
2137 |
case INMEXTENDED_USER: readExtendedUser(sample); break; |
1995 |
case INMEXTENDED_URL: readExtendedUrl(sample); break; |
2138 |
case INMEXTENDED_URL: readExtendedUrl(sample); break; |
Lines 2012-2022
Link Here
|
2012 |
/* or line-by-line output... */ |
2155 |
/* or line-by-line output... */ |
2013 |
writeFlowLine(sample); |
2156 |
writeFlowLine(sample); |
2014 |
break; |
2157 |
break; |
|
|
2158 |
case SFLFMT_LINE_EXTENDED: |
2159 |
/* or line-by-line extended output... */ |
2160 |
writeFlowLineExtended(sample); |
2161 |
break; |
2015 |
case SFLFMT_FULL: |
2162 |
case SFLFMT_FULL: |
2016 |
default: |
2163 |
default: |
2017 |
/* if it was full-detail output then it was done as we went along */ |
2164 |
/* if it was full-detail output then it was done as we went along */ |
2018 |
break; |
2165 |
break; |
2019 |
} |
2166 |
} |
|
|
2167 |
#ifdef WITH_MYSQL |
2168 |
if(sfConfig.db_settings.type == "MySQL") |
2169 |
writeMySQLRecord(sample); |
2170 |
#endif |
2020 |
} |
2171 |
} |
2021 |
} |
2172 |
} |
2022 |
|
2173 |
|
Lines 2133-2143
Link Here
|
2133 |
/* or line-by-line output... */ |
2284 |
/* or line-by-line output... */ |
2134 |
writeFlowLine(sample); |
2285 |
writeFlowLine(sample); |
2135 |
break; |
2286 |
break; |
|
|
2287 |
case SFLFMT_LINE_EXTENDED: |
2288 |
/* or line-by-line extended output... */ |
2289 |
writeFlowLineExtended(sample); |
2290 |
break; |
2136 |
case SFLFMT_FULL: |
2291 |
case SFLFMT_FULL: |
2137 |
default: |
2292 |
default: |
2138 |
/* if it was full-detail output then it was done as we went along */ |
2293 |
/* if it was full-detail output then it was done as we went along */ |
2139 |
break; |
2294 |
break; |
2140 |
} |
2295 |
} |
|
|
2296 |
#ifdef WITH_MYSQL |
2297 |
if(sfConfig.db_settings.type == "MySQL") |
2298 |
writeMySQLRecord(sample); |
2299 |
#endif |
2141 |
} |
2300 |
} |
2142 |
} |
2301 |
} |
2143 |
|
2302 |
|
Lines 2723-2728
Link Here
|
2723 |
return YES; |
2882 |
return YES; |
2724 |
} |
2883 |
} |
2725 |
|
2884 |
|
|
|
2885 |
#ifdef WITH_MYSQL |
2886 |
/*________________---------------------------__________________ |
2887 |
_________________ db connect __________________ |
2888 |
-----------------___________________________------------------ |
2889 |
*/ |
2890 |
int db_connect() |
2891 |
{ |
2892 |
printf("Conecting to Database!\n"); |
2893 |
printf("%s\n",sfConfig.db_settings.host); |
2894 |
int result; |
2895 |
int store_data; |
2896 |
store_data = 1; |
2897 |
|
2898 |
if(store_data == 1){ |
2899 |
/* |
2900 |
* Connect MySQL |
2901 |
*/ |
2902 |
mysql_init(&mysql); |
2903 |
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"sflow"); |
2904 |
result = (int)mysql_real_connect(&mysql, sfConfig.db_settings.host, sfConfig.db_settings.username, sfConfig.db_settings.password, sfConfig.db_settings.database, 0, NULL, 0); |
2905 |
if (result == (int)NULL){ |
2906 |
fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&mysql)); |
2907 |
exit(1); |
2908 |
} |
2909 |
} |
2910 |
|
2911 |
return 0; |
2912 |
} |
2913 |
#endif |
2914 |
|
2726 |
/*_________________---------------------------__________________ |
2915 |
/*_________________---------------------------__________________ |
2727 |
_________________ instructions __________________ |
2916 |
_________________ instructions __________________ |
2728 |
-----------------___________________________------------------ |
2917 |
-----------------___________________________------------------ |
Lines 2742-2748
Link Here
|
2742 |
fprintf(stderr, " - ...repeat for multiple collectors)\n"); |
2931 |
fprintf(stderr, " - ...repeat for multiple collectors)\n"); |
2743 |
fprintf(stderr,"\n"); |
2932 |
fprintf(stderr,"\n"); |
2744 |
fprintf(stderr,"csv output:\n"); |
2933 |
fprintf(stderr,"csv output:\n"); |
2745 |
fprintf(stderr, " -l - (output in line-by-line format)\n"); fprintf(stderr,"\n"); |
2934 |
fprintf(stderr, " -l - (output in line-by-line format)\n"); |
|
|
2935 |
fprintf(stderr, " -a - (output in extended line-by-line format)\n"); fprintf(stderr,"\n"); |
2746 |
fprintf(stderr,"tcpdump output:\n"); |
2936 |
fprintf(stderr,"tcpdump output:\n"); |
2747 |
fprintf(stderr, " -t - (output in binary tcpdump(1) format)\n"); |
2937 |
fprintf(stderr, " -t - (output in binary tcpdump(1) format)\n"); |
2748 |
fprintf(stderr, " -r file - (read binary tcpdump(1) format)\n"); |
2938 |
fprintf(stderr, " -r file - (read binary tcpdump(1) format)\n"); |
Lines 2758-2763
Link Here
|
2758 |
#ifdef SPOOFSOURCE |
2948 |
#ifdef SPOOFSOURCE |
2759 |
fprintf(stderr, " -S - spoof source of netflow packets to input agent IP\n"); |
2949 |
fprintf(stderr, " -S - spoof source of netflow packets to input agent IP\n"); |
2760 |
#endif |
2950 |
#endif |
|
|
2951 |
#ifdef WITH_MYSQL |
2952 |
fprintf(stderr,"\n"); |
2953 |
fprintf(stderr,"Mysql database connection:\n"); |
2954 |
fprintf(stderr, " -H host - MySQL server host IP address\n"); |
2955 |
fprintf(stderr, " -D database - Database name (table name is assumed to be 'sflow')\n"); |
2956 |
fprintf(stderr, " -U username - Database Username\n"); |
2957 |
fprintf(stderr, " -P password - Database Password\n"); |
2958 |
#endif |
2761 |
fprintf(stderr,"\n"); |
2959 |
fprintf(stderr,"\n"); |
2762 |
fprintf(stderr,"Filters:\n"); |
2960 |
fprintf(stderr,"Filters:\n"); |
2763 |
fprintf(stderr, " +v <vlans> - include vlans (e.g. +v 0-20,4091)\n"); |
2961 |
fprintf(stderr, " +v <vlans> - include vlans (e.g. +v 0-20,4091)\n"); |
Lines 2794-2799
Link Here
|
2794 |
case 'p': sfConfig.sFlowInputPort = atoi(argv[arg++]); break; |
2992 |
case 'p': sfConfig.sFlowInputPort = atoi(argv[arg++]); break; |
2795 |
case 't': sfConfig.outputFormat = SFLFMT_PCAP; break; |
2993 |
case 't': sfConfig.outputFormat = SFLFMT_PCAP; break; |
2796 |
case 'l': sfConfig.outputFormat = SFLFMT_LINE; break; |
2994 |
case 'l': sfConfig.outputFormat = SFLFMT_LINE; break; |
|
|
2995 |
case 'a': sfConfig.outputFormat = SFLFMT_LINE_EXTENDED; break; |
2797 |
case 'r': sfConfig.readPcapFileName = strdup(argv[arg++]); break; |
2996 |
case 'r': sfConfig.readPcapFileName = strdup(argv[arg++]); break; |
2798 |
case 'x': sfConfig.removeContent = YES; break; |
2997 |
case 'x': sfConfig.removeContent = YES; break; |
2799 |
case 'z': sfConfig.tcpdumpHdrPad = atoi(argv[arg++]); break; |
2998 |
case 'z': sfConfig.tcpdumpHdrPad = atoi(argv[arg++]); break; |
Lines 2814-2819
Link Here
|
2814 |
break; |
3013 |
break; |
2815 |
case 'e': sfConfig.netFlowPeerAS = YES; break; |
3014 |
case 'e': sfConfig.netFlowPeerAS = YES; break; |
2816 |
case 's': sfConfig.disableNetFlowScale = YES; break; |
3015 |
case 's': sfConfig.disableNetFlowScale = YES; break; |
|
|
3016 |
#ifdef WITH_MYSQL |
3017 |
/* db_settings part */ |
3018 |
case 'H': sfConfig.db_settings.host = strdup(argv[arg++]); break; |
3019 |
case 'D': sfConfig.db_settings.database = strdup(argv[arg++]); break; |
3020 |
case 'U': sfConfig.db_settings.username = strdup(argv[arg++]); break; |
3021 |
case 'P': sfConfig.db_settings.password = strdup(argv[arg++]); break; |
3022 |
#endif |
2817 |
#ifdef SPOOFSOURCE |
3023 |
#ifdef SPOOFSOURCE |
2818 |
case 'S': sfConfig.spoofSource = YES; break; |
3024 |
case 'S': sfConfig.spoofSource = YES; break; |
2819 |
#endif |
3025 |
#endif |
Lines 2842-2847
Link Here
|
2842 |
default: instructions(*argv); |
3048 |
default: instructions(*argv); |
2843 |
} |
3049 |
} |
2844 |
} |
3050 |
} |
|
|
3051 |
#ifdef WITH_MYSQL |
3052 |
if(sfConfig.db_settings.host && sfConfig.db_settings.username && sfConfig.db_settings.database){ |
3053 |
/* Assume "" as default password */ |
3054 |
sfConfig.db_settings.password = ""; |
3055 |
db_connect(); |
3056 |
sfConfig.db_settings.type = "MySQL"; |
3057 |
} |
3058 |
else { |
3059 |
fprintf(stderr, "ERROR: Database settings incorrect.\n"); |
3060 |
instructions(*argv); |
3061 |
} |
3062 |
#endif |
2845 |
} |
3063 |
} |
2846 |
|
3064 |
|
2847 |
/*_________________---------------------------__________________ |
3065 |
/*_________________---------------------------__________________ |