Beispiel #1
0
int
main(int argc, char *argv[])
{
    sk = create_genl_socket();
    sk2 = create_genl_socket();

    /* Resolve generic netlink families. */
    ovs_datapath_family = genl_ctrl_resolve(sk, OVS_DATAPATH_FAMILY);
    ovs_packet_family = genl_ctrl_resolve(sk, OVS_PACKET_FAMILY);
    ovs_vport_family = genl_ctrl_resolve(sk, OVS_VPORT_FAMILY);
    ovs_flow_family = genl_ctrl_resolve(sk, OVS_FLOW_FAMILY);
    if (ovs_datapath_family < 0 || ovs_packet_family < 0 ||
        ovs_vport_family < 0 || ovs_flow_family < 0) {
        fprintf(stderr, "Failed to resolve Open vSwitch generic netlink families; module not loaded?\n");
        return 1;
    }

    parse_options(argc, argv);

    argc -= optind;
    argv += optind;

    if (argc < 1) {
        help();
        return 1;
    }

    const char *cmd = argv[0];

    if (!strcmp(cmd, "help")) {
        help();
    } else if (!strcmp(cmd, "show")) {
        show();
    } else if (!strcmp(cmd, "add-port") ||!strcmp(cmd, "add-if")) {
        if (argc != 2) {
            fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd);
            return 1;
        }
        add_port(datapath_name, argv[1]);
    } else if (!strcmp(cmd, "add-internal-port")) {
        if (argc != 2) {
            fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd);
            return 1;
        }
        add_internal_port(datapath_name, argv[1]);
    } else if (!strcmp(cmd, "del-port") ||!strcmp(cmd, "del-if")) {
        if (argc != 2) {
            fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd);
            return 1;
        }
        del_port(datapath_name, argv[1]);
    } else if (!strcmp(cmd, "del-br") ||!strcmp(cmd, "del-dp")) {
        if (argc != 1) {
            fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd);
            return 1;
        }
        del_dp(datapath_name);
    } else if (!strcmp(cmd, "cli")) {
        cli(argc-1, argv+1);
    } else if (!strcmp(cmd, "dump-flows")) {
        if (argc != 1) {
            fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd);
            return 1;
        }
        dump_flows(datapath_name);
    } else {
        fprintf(stderr, "Unknown command '%s' (try help)\n", cmd);
        return 1;
    }

    return 0;
}
Beispiel #2
0
int main(int argc, char **argv) {
	pcap_t *pcap;
	char errbuf[PCAP_ERRBUF_SIZE];
	const u_char *pkt;
	struct pcap_pkthdr hdr;
	struct ether_header *eptr;
	struct ip *ip_hdr;   
	struct tcphdr *tcp_hdr;   
	unsigned long nbp,nbip,nbtcp;
	unsigned long long totalSize;
	flow * flowrec;
	int c,optpkt;
	struct timeval t0,t1;
	uint32_t last_expiration,current_ts;

	// Default Paramaters for the classification
	labeling=clusterport;
	SSL_labeling=SSL_clusterport;
	sslparsing=0;
	threshold=-255;
	action=ACTION_LABEL;
	memory=NOOPTMEM;
	pktlimit=model.nbpackets;
	optpkt=pktlimit;

	stats.nbstats=0;
	bzero(stats.timeidx,sizeof(stats.timeidx));
	bzero(stats.creations,sizeof(stats.creations));
	bzero(stats.deletions,sizeof(stats.deletions));

	nbflows=0;
	last_expiration=0;

	// Parsing options
	while ((c = getopt (argc, argv, "m:hCDPLSt:p:")) != -1)
		switch (c) {
			case 'h':
				usage(argv);
				break;
			case 'P':
				action=ACTION_PARSE;
				break;
			case 'L':
				action=ACTION_LABEL;
				break;
			case 'D':
				labeling=dominant;
				SSL_labeling=dominant;
				break;
			case 'C':
				labeling=clusterport;
				SSL_labeling=SSL_clusterport;
				break;
			case 'm':
				if (atoi(optarg)>=0) {
					memory=atoi(optarg);
					break;
				} else {
					fprintf(stderr,"Invalid Memory option\n");
					usage(argv);
				}
				break;
			case 'S':
				sslparsing=1;
				break;
			case 't':
				if (atoi(optarg)>0) {
					threshold=-atoi(optarg);
					break;
				} else {
					fprintf(stderr,"Invalid threshold value\n");
					usage(argv);
				}
			case 'p':
				if (atoi(optarg)>0) {
					optpkt=atoi(optarg);
					break;
				} else {
					fprintf(stderr,"Invalid packet value\n");
					usage(argv);
				}

			case '?':
				if (isprint (optopt))
					fprintf (stderr, "Unknown option `-%c'.\n", optopt);
				else
					fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
				usage(argv);
			default:
				usage(argv);
		}


	if (action==ACTION_PARSE) pktlimit=optpkt;

	if (optind>=argc) {
		fprintf(stderr,"No file specified\n");
		usage(argv);
	}
	
	// Opening pcap file
	fprintf(stderr,"Opening file : %s\n",argv[optind]);
	if ((pcap = pcap_open_offline(argv[optind], errbuf)) == NULL) {
		fprintf(stderr,"Error opening pcap file : %s\n",errbuf);
		usage(argv);
	}

	init_hashes ();

	// Empty connection to start list
	first_flow=(flow*)malloc(sizeof(flow));
	first_flow->src.s_addr=0;
	first_flow->dst.s_addr=0;
	first_flow->sport=0;
	first_flow->dport=0;

	active_flows=first_flow;


	// Parse packets one by one
	nbp=0;nbip=0;nbtcp=0;
	totalSize=0;
	gettimeofday (&t0,NULL);
	while (pkt  = pcap_next( pcap,  &hdr )) {
		nbp++;
		totalSize+=hdr.len;

		current_ts=hdr.ts.tv_sec;
		if (last_expiration==0) last_expiration=current_ts;
		if (current_ts-last_expiration>CLEAN_TIME) {
			if (memory>=OPT_GARBAGE) clean_flows(first_flow->next,current_ts);
			statistics(current_ts-last_expiration);
			last_expiration=current_ts;
		}

		if (nbp % PRINTPKT== 0) {
			fprintf(stderr, "Pkt : %lu",nbp);
	    		fprintf(stderr, "\r");
			fflush(stderr);
		}

		eptr = (struct ether_header *) pkt;
		if (ntohs (eptr->ether_type) != ETHERTYPE_IP) {
			continue;
		} else {
			nbip++;
			ip_hdr=(struct ip *)(pkt+14);

			if (ip_hdr->ip_p==IPPROTO_TCP && ((ntohs (ip_hdr->ip_off) & IP_OFFMASK)==0)) {
				nbtcp++;
				count_flow(ip_hdr,hdr.caplen-14,hdr.ts.tv_sec);
			}
		}
	}

	if (memory>=OPT_GARBAGE) clean_flows(first_flow->next,current_ts);
	statistics(current_ts-last_expiration);
	last_expiration=current_ts;
	dump_flows(first_flow->next);
	statistics(1);

	gettimeofday (&t1,NULL);
	pcap_close(pcap);

	fprintf(stderr,"\n%lu Packets parsed in %.2fms\n(%lu non-ip / %lu non-tcp)\n"
			"TCP Connections with Syn: %lu\n"
			"Total Volume: %llu\n"
			"Duration: %lu\n",
			nbp,
			(float)((t1.tv_sec-t0.tv_sec)*1000000 + (t1.tv_usec-t0.tv_usec))/1000,
			nbp-nbip,nbp-nbtcp,flow_hash->total_insert,totalSize,stats.timeidx[stats.nbstats]);
	
	//dump_hashtab_stats(flow_hash);
	//print_statistics();
}