Пример #1
0
int main(int argc, char *argv[])
{
	/* This is essentially the same main function from readdemo.c */
	
	libtrace_t *trace = NULL;
	libtrace_packet_t *packet = NULL;

	/* Ensure we have at least one argument after the program name */
        if (argc < 2) {
                fprintf(stderr, "Usage: %s inputURI\n", argv[0]);
                return 1;
        }	
	
	packet = trace_create_packet();

	if (packet == NULL) {
		perror("Creating libtrace packet");
		libtrace_cleanup(trace, packet);
		return 1;
	}

	trace = trace_create(argv[1]);

	if (trace_is_err(trace)) {
		trace_perror(trace,"Opening trace file");
		libtrace_cleanup(trace, packet);
		return 1;
	}

	if (trace_start(trace) == -1) {
		trace_perror(trace,"Starting trace");
		libtrace_cleanup(trace, packet);
		return 1;
	}


	while (trace_read_packet(trace,packet)>0) {
		per_packet(packet);
	}


	if (trace_is_err(trace)) {
		trace_perror(trace,"Reading packets");
		libtrace_cleanup(trace, packet);
		return 1;
	}

	/* Print the stats for the final reporting period that was probably
	 * not complete when the trace finished */
	printf("%u\t", next_report);
	printf("%.2f\t\t", ((double)wire_count) / packet_count);
	printf("%.2f\n", ((double)capture_count) / packet_count);

	libtrace_cleanup(trace, packet);
	return 0;
}
Пример #2
0
int main(int argc, char *argv[])
{
        /* This is essentially the same main function from readdemo.c */

        libtrace_t *trace = NULL;
        libtrace_packet_t *packet = NULL;

        /* Ensure we have at least one argument after the program name */
        if (argc < 2) {
                fprintf(stderr, "Usage: %s inputURI\n", argv[0]);
                return 1;
        }

	packet = trace_create_packet();

        if (packet == NULL) {
                perror("Creating libtrace packet");
                libtrace_cleanup(trace, packet);
                return 1;
        }

        trace = trace_create(argv[1]);

        if (trace_is_err(trace)) {
                trace_perror(trace,"Opening trace file");
                libtrace_cleanup(trace, packet);
                return 1;
        }

        if (trace_start(trace) == -1) {
                trace_perror(trace,"Starting trace");
                libtrace_cleanup(trace, packet);
                return 1;
        }


        while (trace_read_packet(trace,packet)>0) {
                per_packet(packet);
        }


        if (trace_is_err(trace)) {
                trace_perror(trace,"Reading packets");
                libtrace_cleanup(trace, packet);
                return 1;
        }

	printf("Packet Count = %" PRIu64 "\n", count);

        libtrace_cleanup(trace, packet);
        return 0;
}
Пример #3
0
int main(int argc, char *argv[])
{
	libtrace_t *trace;
	libtrace_packet_t *packet;
	libtrace_filter_t *filter=NULL;
	int snaplen=-1;
	int promisc=-1;

	while(1) {
		int option_index;
		struct option long_options[] = {
			{ "filter",		1, 0, 'f' },
			{ "snaplen",		1, 0, 's' },
			{ "promisc",		1, 0, 'p' },
			{ "help",		0, 0, 'h' },
			{ "libtrace-help",	0, 0, 'H' },
			{ NULL,			0, 0, 0 }
		};

		int c= getopt_long(argc, argv, "f:s:p:hH",
				long_options, &option_index);

		if (c==-1)
			break;

		switch (c) {
			case 'f':
				filter=trace_create_filter(optarg);
				break;
			case 's':
				snaplen=atoi(optarg);
				break;
			case 'p':
				promisc=atoi(optarg);
				break;
			case 'H':
				trace_help();
				return 1;
			default:
				fprintf(stderr,"Unknown option: %c\n",c);
				/* FALL THRU */
			case 'h':
				usage(argv[0]);
				return 1;
		}
	}

	if (optind>=argc) {
		fprintf(stderr,"Missing input uri\n");
		usage(argv[0]);
		return 1;
	}

	while (optind<argc) {
		trace = trace_create(argv[optind]);
		++optind;

		if (trace_is_err(trace)) {
			trace_perror(trace,"Opening trace file");
			return 1;
		}

		if (snaplen>0)
			if (trace_config(trace,TRACE_OPTION_SNAPLEN,&snaplen)) {
				trace_perror(trace,"ignoring: ");
			}
		if (filter)
			if (trace_config(trace,TRACE_OPTION_FILTER,filter)) {
				trace_perror(trace,"ignoring: ");
			}
		if (promisc!=-1) {
			if (trace_config(trace,TRACE_OPTION_PROMISC,&promisc)) {
				trace_perror(trace,"ignoring: ");
			}
		}

		if (trace_start(trace)) {
			trace_perror(trace,"Starting trace");
			trace_destroy(trace);
			return 1;
		}

		packet = trace_create_packet();

		while (trace_read_packet(trace,packet)>0) {
			per_packet(packet);
		}

		trace_destroy_packet(packet);

		if (trace_is_err(trace)) {
			trace_perror(trace,"Reading packets");
		}

		trace_destroy(trace);
	}

	return 0;
}
Пример #4
0
int main(int argc, char *argv[])
{
	char *compress_type_str=NULL;
	struct libtrace_filter_t *filter=NULL;
	struct libtrace_t *input = NULL;
	struct libtrace_packet_t *packet = trace_create_packet();
	struct sigaction sigact;
	int i;	
	
	if (argc<2) {
		usage(argv[0]);
		return 1;
	}

	/* Parse command line options */
	while(1) {
		int option_index;
		struct option long_options[] = {
			{ "filter",	   1, 0, 'f' },
			{ "count",	   1, 0, 'c' },
			{ "bytes",	   1, 0, 'b' },
			{ "starttime",	   1, 0, 's' },
			{ "endtime",	   1, 0, 'e' },
			{ "interval",	   1, 0, 'i' },
			{ "libtrace-help", 0, 0, 'H' },
			{ "maxfiles", 	   1, 0, 'm' },
			{ "snaplen",	   1, 0, 'S' },
			{ "verbose",       0, 0, 'v' },
			{ "compress-level", 1, 0, 'z' },
			{ "compress-type", 1, 0, 'Z' },
			{ NULL, 	   0, 0, 0   },
		};

		int c=getopt_long(argc, argv, "f:c:b:s:e:i:m:S:Hvz:Z:",
				long_options, &option_index);

		if (c==-1)
			break;

		switch (c) {
			case 'f': filter=trace_create_filter(optarg);
				break;
			case 'c': count=atoi(optarg);
				break;
			case 'b': bytes=atoi(optarg);
				break;
			case 's': starttime=atof(optarg); /* FIXME: use getdate */
				  break;
			case 'e': endtime=atof(optarg);
				  break;
			case 'i': interval=atoi(optarg);
				  break;
			case 'm': maxfiles=atoi(optarg);
				  break;
			case 'S': snaplen=atoi(optarg);
				  break;
			case 'H':
				  trace_help();
				  exit(1);
				  break;
			case 'v':
				  verbose++;
				  break;
			case 'z':
				  compress_level=atoi(optarg);
				  if (compress_level<0 || compress_level>9) {
					usage(argv[0]);
				  	exit(1);
				  }
				  break;
			case 'Z':
				  compress_type_str=optarg;
				  break;	
			default:
				fprintf(stderr,"Unknown option: %c\n",c);
				usage(argv[0]);
				return 1;
		}
	}


	if (compress_type_str == NULL && compress_level >= 0) {
		fprintf(stderr, "Compression level set, but no compression type was defined, setting to gzip\n");
		compress_type = TRACE_OPTION_COMPRESSTYPE_ZLIB;
	}

	else if (compress_type_str == NULL) {
		/* If a level or type is not specified, use the "none"
		 * compression module */
		compress_type = TRACE_OPTION_COMPRESSTYPE_NONE;
	}

	/* I decided to be fairly generous in what I accept for the
	 * compression type string */
	else if (strncmp(compress_type_str, "gz", 2) == 0 ||
			strncmp(compress_type_str, "zlib", 4) == 0) {
		compress_type = TRACE_OPTION_COMPRESSTYPE_ZLIB;
	} else if (strncmp(compress_type_str, "bz", 2) == 0) {
		compress_type = TRACE_OPTION_COMPRESSTYPE_BZ2;
	} else if (strncmp(compress_type_str, "lzo", 3) == 0) {
		compress_type = TRACE_OPTION_COMPRESSTYPE_LZO;
	} else if (strncmp(compress_type_str, "no", 2) == 0) {
		compress_type = TRACE_OPTION_COMPRESSTYPE_NONE;
	} else {
		fprintf(stderr, "Unknown compression type: %s\n", 
			compress_type_str);
		return 1;
	}

	if (optind+2>argc) {
		fprintf(stderr,"missing inputuri or outputuri\n");
		usage(argv[0]);
	}

	output_base = argv[argc - 1];

	sigact.sa_handler = cleanup_signal;
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = SA_RESTART;

	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);

	output=NULL;

	signal(SIGINT,&cleanup_signal);
	signal(SIGTERM,&cleanup_signal);

	for (i = optind; i < argc - 1; i++) {


		input = trace_create(argv[i]);	
		
		if (trace_is_err(input)) {
			trace_perror(input,"%s",argv[i]);
			return 1;
		}

		if (filter && trace_config(input, TRACE_OPTION_FILTER, filter) == 1) {
			trace_perror(input, "Configuring filter for %s", 
					argv[i]);
			return 1;
		}

		if (trace_start(input)==-1) {
			trace_perror(input,"%s",argv[i]);
			return 1;
		}

		while (trace_read_packet(input,packet)>0) {
			if (per_packet(packet) < 1)
				done = 1;
			if (done)
				break;
		}

		if (done)
			break;
		
		if (trace_is_err(input)) {
			trace_perror(input,"Reading packets");
			trace_destroy(input);
			break;
		}

		trace_destroy(input);
	}

	if (verbose) {
		uint64_t f;
		f=trace_get_received_packets(input);
		if (f!=UINT64_MAX)
			fprintf(stderr,"%" PRIu64 " packets on input\n",f);
		f=trace_get_filtered_packets(input);
		if (f!=UINT64_MAX)
			fprintf(stderr,"%" PRIu64 " packets filtered\n",f);
		f=trace_get_dropped_packets(input);
		if (f!=UINT64_MAX)
			fprintf(stderr,"%" PRIu64 " packets dropped\n",f);
		f=trace_get_accepted_packets(input);
		if (f!=UINT64_MAX)
			fprintf(stderr,"%" PRIu64 " packets accepted\n",f);
	}
	
	if (output)
		trace_destroy_output(output);

	trace_destroy_packet(packet);

	return 0;
}
Пример #5
0
int main(int argc, char *argv[])
{
  libtrace_t *trace;
  libtrace_packet_t *packet;
  libtrace_filter_t *filter=NULL;
  session_manager_t *sm;
  uint64_t f;

  while(1) {
    int option_index;
    struct option long_options[] = {
      { "filter",	1, 0, 'f' },
      { "interval",	1, 0, 'i' },
      { "count",        1, 0, 'c' },
      { "exit",         1, 0, 'e' },			
      { "relative",     0, 0, 'r' },
      { "help",		0, 0, 'h' },
      { "libtrace-help",0, 0, 'H' },
      { NULL,		0, 0, 0 }
    };

    int c= getopt_long(argc, argv, "c:e:f:i:hHr",
		       long_options, &option_index);

    if (c==-1)
      break;

    switch (c) {
    case 'f':
      filter=trace_create_filter(optarg);
      break;
    case 'i':
      packet_interval=atof(optarg);
      break;
    case 'c':
      packet_count=atoi(optarg);
      packet_interval=UINT32_MAX; // make sure only one is defined
      break;
    case 'e':
      report_periods=atoi(optarg);
      break;      
    case 'r':
      report_rel_time = 1;
      break;
    case 'H':
      trace_help();
      return 1;
    default:
      fprintf(stderr,"Unknown option: %c\n",c);
      /* FALL THRU */
    case 'h':
      usage(argv[0]);
      return 1;
    }
  }

  if (optind>=argc) {
    fprintf(stderr,"Missing input uri\n");
    usage(argv[0]);
    return 1;
  }

  while (optind<argc) {
    // create tcp session manager
    sm = session_manager_create();
    // create and register the data-ack based RTT module
    session_manager_register_module(sm,rtt_n_sequence_module()); 

    fprintf(stderr, "Processing %s\n",argv[optind]);
    trace = trace_create(argv[optind]);
    ++optind;

    if (trace_is_err(trace)) {
      trace_perror(trace,"Opening trace file");
      return 1;
    }

    if (filter && trace_config(trace,TRACE_OPTION_FILTER,filter)==1) {
	trace_perror(trace,"Configuring filter");
    }

    if (trace_start(trace)) {
      trace_perror(trace,"Starting trace");
      trace_destroy(trace);
      return 1;
    }

    packet = trace_create_packet();

    print_report_hdr();
    reset_report();
    last_report_ts = 0;
    last_packet_ts = 0;

    while (trace_read_packet(trace,packet)>0) {      
      per_packet(packet,session_manager_update(sm,packet));
      if (report_periods != UINT64_MAX && reported >= report_periods) {
	break;
      }
    }

    // remaining pkts (or all if no period set)
    if ((reports[OUT].count+reports[IN].count) > 0 || 
	(packet_interval == UINT32_MAX && packet_count == UINT64_MAX)) {
      double ts = trace_get_seconds(packet);
      if (report_rel_time) {
	print_report(ts-last_report_ts);
      } else {
	print_report(ts);
      }
    }

    trace_destroy_packet(packet);

    if (trace_is_err(trace)) {
      trace_perror(trace,"Reading packets");
    }

    // some stats
    f=trace_get_received_packets(trace);
    if (f!=UINT64_MAX)
      fprintf(stderr,"%" PRIu64 " packets on input\n",f);
    f=trace_get_filtered_packets(trace);
    if (f!=UINT64_MAX)
      fprintf(stderr,"%" PRIu64 " packets filtered\n",f);
    f=trace_get_dropped_packets(trace);
    if (f!=UINT64_MAX)
      fprintf(stderr,"%" PRIu64 " packets dropped\n",f);
    f=trace_get_accepted_packets(trace);
    if (f!=UINT64_MAX)
      fprintf(stderr,"%" PRIu64 " packets accepted\n",f);

    trace_destroy(trace);
    session_manager_destroy(sm);
  }

  return 0;
}
int main(int argc, char *argv[])
{
	clock_t start;
	clock_t end;
	double function_time;

	struct time_adjust_record time_adjust_flow1 = {"0.0.0.0", 0, "0.0.0.0", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1};
	char * p = NULL;
	FILE * fp_time_adjust = NULL;
	FILE * fp_write = NULL;
	char time_adjust_file[128];
	char file_write[256];
	char input_file[256];
	char buffer[256];

	if (argc < 4)
	{
		printf("Please enter two parameters: read_file and write_file\n");
		return 1;
	}

	strcpy(input_file, argv[1]);
	strcpy(time_adjust_file, argv[2]);
	strcpy(file_write, argv[3]);

	if((fp_time_adjust = fopen(time_adjust_file, "r")) == NULL)  //open the file to read
	{
		fprintf(stderr, "Open time adjust file: %s failed\n", time_adjust_file);
		exit(1);
	}
	memset(buffer, '\0', sizeof(buffer));
	fgets(buffer, sizeof(buffer), fp_time_adjust);
	p = strtok(buffer, " ");
	if(p)
	{
		strcpy( time_adjust_flow1.src_ip, p);
	}
	p = strtok(NULL, " ");
	if(p)
	{
		time_adjust_flow1.src_port = atoi(p);
	}
	p = strtok(NULL, " ");
	if(p)
	{
		strcpy( time_adjust_flow1.dest_ip, p);
	}
	p = strtok(NULL, " ");
	if(p)
	{
		time_adjust_flow1.dest_port = atoi(p);
	}
	//printf("flow: %s:%d %s:%d\n", time_adjust_flow1.src_ip, time_adjust_flow1.src_port, time_adjust_flow1.dest_ip, time_adjust_flow1.dest_port);
	memset(buffer, '\0', sizeof(buffer));
	fgets(buffer, sizeof(buffer), fp_time_adjust);
	if( Set_Time_Adjust(buffer, &time_adjust_flow1.first_pkt_sec, &time_adjust_flow1.first_pkt_usec) != 0 )
	{
		printf("line: %d\n", __LINE__);
		exit(1);
	}
	//printf("First packet second: %d, usecond: %d\n", time_adjust_flow1.first_pkt_sec, time_adjust_flow1.first_pkt_usec);
	memset(buffer, '\0', sizeof(buffer));
	fgets(buffer, sizeof(buffer), fp_time_adjust);
	if( Set_Time_Adjust(buffer, &time_adjust_flow1.client_inter_sec, &time_adjust_flow1.client_inter_usec) != 0 )
	{
		printf("line: %d\n", __LINE__);
		exit(1);
	}
	//printf("Client packet inter arrival second: %d, usecond: %d\n", time_adjust_flow1.client_inter_sec, time_adjust_flow1.client_inter_usec);
	memset(buffer, '\0', sizeof(buffer));
	fgets(buffer, sizeof(buffer), fp_time_adjust);
	if( Set_Time_Adjust(buffer, &time_adjust_flow1.server_inter_sec, &time_adjust_flow1.server_inter_usec) != 0 )
	{
		printf("line: %d\n", __LINE__);
		exit(1);
	}
	//printf("Server packet inter arrival second: %d, usecond: %d\n", time_adjust_flow1.server_inter_sec, time_adjust_flow1.server_inter_usec);
	memset(buffer, '\0', sizeof(buffer));
	fgets(buffer, sizeof(buffer), fp_time_adjust);
	if( Set_Time_Adjust(buffer, &time_adjust_flow1.reaction_sec, &time_adjust_flow1.reaction_usec) != 0 )
	{
		printf("line: %d\n", __LINE__);
		exit(1);
	}
	//printf("Client reaction second: %d, usecond: %d\n", time_adjust_flow1.reaction_sec, time_adjust_flow1.reaction_usec);
	memset(buffer, '\0', sizeof(buffer));
	fgets(buffer, sizeof(buffer), fp_time_adjust);
	if( Set_Time_Adjust(buffer, &time_adjust_flow1.rtt_sec, &time_adjust_flow1.rtt_usec) != 0 )
	{
		printf("line: %d\n", __LINE__);
		exit(1);
	}
	//time_adjust_flow1.rtt_usec = time_adjust_flow1.rtt_sec*1000 + time_adjust_flow1.rtt_usec*1000;
	//time_adjust_flow1.rtt_sec = time_adjust_flow1.rtt_sec/1000;
	//printf("Client RTT second: %d, usecond: %d\n", time_adjust_flow1.rtt_sec, time_adjust_flow1.rtt_usec);
	fclose(fp_time_adjust);
	
	if((fp_write=fopen(file_write, "w")) == NULL)  //open the file to read
        {
                fprintf(stderr, "Open write file: %s failed\n", file_write);
                exit(1);
        }

	libtrace_t *trace = 0;
	libtrace_out_t *writer = 0;
	libtrace_packet_t *pkt = trace_create_packet();

	// Open traces for reading and writing.
	trace = trace_create(input_file);
	if (trace_is_err(trace)) {
		trace_perror(trace, "trace_create");
		trace_destroy(trace);
		return 1;
	}
	if (trace_start(trace) == -1) {
		trace_perror(trace,"Starting trace");
		//libtrace_cleanup(trace, output, packet);
		return 1;
	}

	//char output_file[] = "pcap:ttt.pcap";
	//writer = trace_create_output("pcap:testcp");
	/*writer = trace_create_output(output_file);
	if (trace_is_err_output(writer)) {
		trace_perror_output(writer, "trace_create_output");
		trace_destroy_output(writer);
		trace_destroy(trace);
		trace_destroy_packet(pkt);
		return 1;
	}

	if (trace_start_output(writer) == -1) {
		trace_perror_output(writer,"Starting output trace");
		//libtrace_cleanup(trace, output, packet);
		return 1;
	}*/

	int psize = 0;
	int pkt_count = 0;
	for (;;) {
		psize = trace_read_packet(trace, pkt);
		if (psize == 0) {
			break;
		}
		if (psize < 0) {
			trace_perror(trace, "read_packet");
			break;
		}
		//if(pkt_count < 50)
		{
			//printf("packet: %d\n", pkt_count);
			if ((per_packet(pkt, fp_write, &time_adjust_flow1)) == -1)
			{
				fprintf(stderr, "Something went wrong in per_packet.\n");
				break;
			}
		}
		pkt_count++;
	}

	trace_destroy_packet(pkt);
	trace_destroy(trace);
	//trace_destroy_output(writer);
	fclose(fp_write);

	return 0;
}