示例#1
0
int main(int argc, char **argv)
{
	//Check whether user has entered enough arguments
	if(argc < 2)
	{
		printf("Usage: ./firewall mode [input pcap file] [output pcap file]\n",
				"           Mode: 1/2\n");
	}
    char errbuf[100];
    int mode = atoi(argv[1]);

    //If mode = 1 then use the interfaces to capture packets
    if(mode == 1)
    {
		pid_t childPID;
		//Open the interface handlers
		in_handle = pcap_open_live(INT_IN,65536,1,0,errbuf);
		out_handle = pcap_open_live(INT_OUT,65536,1,0,errbuf);
		//Create two processes for two interfaces
		childPID = fork();

		if(childPID >= 0)
		{
			//Associate the handlers with the interfaces
			if(childPID == 0)
			{
				capture_loop(in_handle, -1, (pcap_handler)parse_packet, NULL);
			}
			else
			{
				capture_loop(out_handle, -1, (pcap_handler)parse_packet_p, NULL);
			}
		}
		else
		{
			printf("\n Fork failed, quitting!!!!!!\n");
			return 1;
		}
	}
    //Else open the input pcap file for the processing and write to output pcap file
	else
	{
		//Check for the valid arguments
		if(argc < 4)
		{
			printf("Usage: ./firewall mode [input pcap file] [output pcap file]\n",
				"           Mode: 1/2\n");
		}
		in_handle = pcap_open_offline(argv[2],errbuf);
		//802.3 = 1 - link type
		//open new pcap handler
		out_handle = pcap_open_dead(1,65536);
		//open the file with the handler
		dumper = pcap_dump_open(out_handle, argv[3]);
		//Process all the packets from the file
		capture_loop(in_handle, -1, (pcap_handler)parse_packet_file, (u_char*)dumper);
	}
    return 0;
}
示例#2
0
文件: dag.c 项目: DPMI/mp
void* dag_legacy_capture(void* ptr){
	struct CI* CI = (struct CI*)ptr;
	struct dag_context cap;
	capture_init(CI, &cap.base);

	logmsg(verbose, CAPTURE, "CI[%d] initializing capture on %s using DAGv1 (memory at %p).\n", CI->id, cap.base.iface, &datamem[CI->id]);

	if ( !setup_device(CI) ){
		/* error already show */
		return NULL;
	}

	cap.fd = CI->sd;
	cap.buffer = dag_mmap(CI->sd);
	cap.top = 0;
	cap.bottom = 0;

	/* setup callbacks */
	cap.base.init = (init_callback)dagcapture_init;
	cap.base.destroy = (destroy_callback)dagcapture_destroy;
	cap.base.read_packet = (read_packet_callback)legacy_read_packet;

	/* start capture */
	capture_loop(CI, (struct capture_context*)&cap);
	return NULL;
}
示例#3
0
文件: dag.c 项目: DPMI/mp
void* dag_capture(void* ptr){
	struct CI* CI = (struct CI*)ptr;
	struct dag_context cap;
	capture_init(CI, &cap.base);

	logmsg(verbose, CAPTURE, "CI[%d] initializing capture on %s using DAGv2 (memory at %p).\n", CI->id, cap.base.iface, CI->buffer);

	if ( !setup_device(CI) ){
		/* error already show */
		return NULL;
	}

	cap.fd = CI->sd;
	cap.buffer = NULL; /* not used by this driver */
	cap.top = NULL;
	cap.bottom = NULL;

	/* setup callbacks */
	if ( dag_mode == 0 ){
		cap.base.init = (init_callback)dagcapture_init_rxtx;
		cap.base.destroy = (destroy_callback)dagcapture_destroy_rxtx;
		cap.base.read_packet = (read_packet_callback)read_packet_rxtx;
	} else if ( dag_mode == 1 ){
		cap.base.init = (init_callback)dagcapture_init_wiretap;
		cap.base.destroy = (destroy_callback)dagcapture_destroy_wiretap;
		cap.base.read_packet = (read_packet_callback)read_packet_wiretap;
	} else {
		logmsg(stderr, CAPTURE, "Unsupported mode: %d\n", dag_mode);
		abort();
	}

	/* start capture */
	capture_loop(CI, (struct capture_context*)&cap);
	return NULL;
}
示例#4
0
static void twin_capture(const char *basename, float framerate, int cam, bool eight_bit_mode, bool testonly)
{
	struct chameleon_camera *c1=NULL, *c2=NULL;
	do {
		if (c1) chameleon_camera_free(c1);
		if (c2) chameleon_camera_free(c2);
		if (cam == -1 || cam == 0) {
			c1 = open_camera(true, eight_bit_mode);
		}
		if (cam == -1 || cam == 1) {
			c2 = open_camera(false, eight_bit_mode);
		}
		printf("Got camera c1=%p c2=%p\n", c1, c2);
		if (!c1 || !c2) sleep(1);
	} while (c1 == NULL && c2 == NULL);

	signal(SIGCHLD, SIG_IGN);

	capture_loop(c1, c2, framerate, basename, testonly, eight_bit_mode);
	if (c1) {
		chameleon_camera_free(c1);
	}
	if (c2) {
		chameleon_camera_free(c2);
	}
}
示例#5
0
static DWORD WINAPI main_capture_thread(HANDLE thread_handle)
{
	if (!init_hook(thread_handle)) {
		DbgOut("Failed to init hook\n");
		free_hook();
		return 0;
	}

	capture_loop();
	return 0;
}
示例#6
0
int main(int argc, char const *argv[])
{
	 char interface[256] = "", bpfstr[256] = "";
    int packets = 0, c, i;
 
    // Get the command line options, if any
    while ((c = getopt (argc, argv, "hi:n:")) != -1)
    {
        switch (c)
        {
        case 'h':
            printf("usage: %s [-h] [-i ] [-n ] []\n", argv[0]);
            exit(0);
            break;
        case 'i':
            strcpy(interface, optarg);
            break;
        case 'n':
            packets = atoi(optarg);
            break;
        }
    }
 
    // Get the packet capture filter expression, if any.
    for (i = optind; i < argc; i++)
    {
        strcat(bpfstr, argv[i]);
        strcat(bpfstr, " ");
    }
 
    // Open libpcap, set the program termination signals then start
    // processing packets.
    if ((pd = open_pcap_socket(interface, bpfstr)))
    {
        signal(SIGINT, bailout);
        signal(SIGTERM, bailout);
        signal(SIGQUIT, bailout);
        capture_loop(pd, packets, (pcap_handler)parse_packet);
        bailout(0);
    }
//     pcap_loop(descr,-1,another_callback,NULL);


	return 0;
}
示例#7
0
static int run_capture(const char *basename, float delay, bool testonly)
{
	dc1394camera_t *camera;

	printf("Waiting for camera\n");
	while ((camera = open_camera()) == NULL) {
		printf(".");
		fflush(stdout);
		sleep(1);
	}

	dc1394_camera_reset(camera);

	capture_loop(camera, basename, delay, testonly);
	close_camera(camera);

	return 0;
}
示例#8
0
int internal_main( output_callback_func_ptr callback_ptr )
{
  output = OUTPUT_CALLBACK;

  output_callback_ptr = callback_ptr;

  device_open();
  init_device();
  start_capturing();
 
  capture_loop();
 
  stop_capturing();
  uninit_device();
  device_close();

  return 0;
}
示例#9
0
文件: main.c 项目: ChenyuanHu/nptool
static void start_capture(char *ifname)
{
	char errbuf[PCAP_ERRBUF_SIZE];
//	char filter_exp[FLITER_STR_MAX_SIZE];
//	struct bpf_program filter;
//	bpf_u_int32 mask;
//	bpf_u_int32 net;

	if ((pd = pcap_open_live(ifname, SNAPLEN, IS_PROMISC, 
			WAITMS, errbuf)) == NULL) {
		fprintf(stderr, "pcap_open err %s, %s\n", ifname, errbuf);
		exit(1);
	}

	capture_loop();

	if (pd != NULL) {
		pcap_close(pd);
		pd = NULL;
	}
}
示例#10
0
int main(int argc,char **argv)
{
    mac_array = malloc(sizeof(struct MACarray));
    ip_array = malloc(sizeof(struct IParray));
    initIParray(ip_array,10);
    initMACarray(mac_array,10);
    char interface[256] = "", bpfstr[256] = "";
    int packets = 0, c, i;

    while ((c = getopt (argc, argv, "hi:n:")) != -1)
    {
        switch (c)
        {
        case 'h':
            printf("Usage: %s [-h] [-i ] []\n", argv[0]);
            exit(0);
            break;
        case 'i':
            strcpy(interface, optarg);
            break;
        }
    }

    for (i = optind; i < argc; i++)
    {
        strcat(bpfstr, argv[i]);
        strcat(bpfstr, " ");
    }

    if ((handle = open_pcap_socket(interface, bpfstr)))
    {
        signal(SIGINT, program_terminate);
        signal(SIGTERM, program_terminate);
        signal(SIGQUIT, program_terminate);
        capture_loop(handle, packets, (pcap_handler)handle_packet);
    }
    return 0;
}
示例#11
0
int main(int argc, char **argv)
{
	//Check whether user has entered enough arguments
	if(argc < 2)
	{
		printf("Usage: ./firewall mode rules_file [input pcap file] [output pcap file] 1/0\n",
				"           Mode: 1/2\n"
				"           Debugging: On/Off\n");
	}
    char errbuf[100];
    int mode = atoi(argv[1]);

    //Convert rules from File to linkedlist
    createList(argv[2]);
    //Print the rules for Debugging
    //iterList();

    //Initialize the hashtables
	state_tbl = NULL;
	arp_tbl = NULL;

	//Intitialze the locks
	if (pthread_rwlock_init(&arp_lock,NULL) != 0)
	{
		printf("ARP lock init failed.\n");
	}
	if (pthread_rwlock_init(&state_lock,NULL) != 0)
	{
		printf("State lock init failed.\n");
	}

	//If mode = 1 then use the interfaces to capture packets
    if(mode == 1)
    {
    	debugging = atoi(argv[3]);

    	pthread_t threads[4];
		//Open the interface handlers
		in_handle = pcap_open_live(INT_IN,65536,1,0,errbuf);
		out_handle = pcap_open_live(INT_OUT,65536,1,0,errbuf);

		//Create four threads
		pthread_create(threads + 0, NULL, func1, (void *) 0);
		pthread_create(threads + 1, NULL, func2, (void *) 1);
		pthread_create(threads + 2, NULL, func3, (void *) 2);
		pthread_create(threads + 3, NULL, func4, (void *) 3);

		//Wait for the four threads
		pthread_join(threads[0], NULL);
		pthread_join(threads[1], NULL);
		pthread_join(threads[2], NULL);
		pthread_join(threads[3], NULL);

		pthread_exit(NULL);
	}
    //Else open the input pcap file for the processing and write to output pcap file
	else
	{
		//Check for the valid arguments
		if(argc < 5)
		{
			printf("Usage: ./firewall mode rule_file [input pcap file] [output pcap file] 0/1\n",
				"           Mode: 1/2\n"
				"           Debug:On/Off\n");
		}
		debugging = atoi(argv[5]);
		in_handle = pcap_open_offline(argv[3],errbuf);
		//802.3 = 1 - link type
		//open new pcap handler
		out_handle = pcap_open_dead(1,65536);
		//open the file with the handler
		dumper = pcap_dump_open(out_handle, argv[4]);
		capture_loop(in_handle, -1, (pcap_handler)parse_packet_file, (u_char*)dumper);
	}
    //Memory cleanup
    cleanList();
    return 0;
}
示例#12
0
//Thread 2 - which processes packets from another interface
void func2()
{
	printf("Thread-2 Started.\n");
	capture_loop(out_handle, -1, (pcap_handler)parse_packet_p, NULL);
	pthread_exit(NULL);
}
示例#13
0
//Thread 1 - which processes packets from one interface
void func1()
{
	printf("Thread-1 Started.\n");
	capture_loop(in_handle, -1, (pcap_handler)parse_packet, NULL);
	pthread_exit(NULL);
}
示例#14
0
static int capture(char *dev_name, int x_res, int y_res, int n_frames,
		   char *out_dir, int block, int threads, int sleep_ms)
{
	struct v4l2_format		fmt;
	struct v4l2_buffer		buf;
	struct v4l2_requestbuffers	req;
	enum v4l2_buf_type		type;
	int				fd = -1;
	unsigned int			i, n_buffers;
	struct buffer			*buffers;

	if (block)
		fd = v4l2_open(dev_name, O_RDWR, 0);
	else
		fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
	if (fd < 0) {
		perror("Cannot open device");
		exit(EXIT_FAILURE);
	}

	CLEAR(fmt);
	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	fmt.fmt.pix.width       = x_res;
	fmt.fmt.pix.height      = y_res;
	fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
	fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
	xioctl(fd, VIDIOC_S_FMT, &fmt);
	if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
		printf("Libv4l didn't accept RGB24 format. Can't proceed.\n");
		exit(EXIT_FAILURE);
	}
	if ((fmt.fmt.pix.width != x_res) || (fmt.fmt.pix.height != y_res))
		printf("Warning: driver is sending image at %dx%d\n",
			fmt.fmt.pix.width, fmt.fmt.pix.height);

	CLEAR(req);
	req.count = 2;
	req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	req.memory = V4L2_MEMORY_MMAP;
	xioctl(fd, VIDIOC_REQBUFS, &req);

	buffers = calloc(req.count, sizeof(*buffers));
	for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
		CLEAR(buf);

		buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory      = V4L2_MEMORY_MMAP;
		buf.index       = n_buffers;

		xioctl(fd, VIDIOC_QUERYBUF, &buf);

		buffers[n_buffers].length = buf.length;
		buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
			      PROT_READ | PROT_WRITE, MAP_SHARED,
			      fd, buf.m.offset);

		if (MAP_FAILED == buffers[n_buffers].start) {
			perror("mmap");
			exit(EXIT_FAILURE);
		}
	}

	for (i = 0; i < n_buffers; ++i) {
		CLEAR(buf);
		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory = V4L2_MEMORY_MMAP;
		buf.index = i;
		xioctl(fd, VIDIOC_QBUF, &buf);
	}
	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

	xioctl(fd, VIDIOC_STREAMON, &type);
	if (threads)
		capture_threads(fd, buffers, 2, fmt, n_frames, out_dir,
				sleep_ms);
	else
		capture_loop(fd, buffers, fmt, n_frames, out_dir);

	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	xioctl(fd, VIDIOC_STREAMOFF, &type);
	for (i = 0; i < n_buffers; ++i)
		v4l2_munmap(buffers[i].start, buffers[i].length);
	v4l2_close(fd);

	return 0;
}
示例#15
0
int main(int argc, char **argv)
{
  init_defaults();

  while (1) {
    int index, c = 0;

    c = getopt_long(argc, argv, short_options, long_options, &index);

    if (c == -1)
      break;

    switch (c) {
    case 0:
      break;

    case 'd':
      free(device_name);
      device_name = optarg;
      break;

    case 'W':
      v_width = atoi(optarg);
      break;

    case 'H':
      v_height = atoi(optarg);
      break;

    case 'c':
      num_frames = atoi(optarg);
      break;

    case 'f':
      framerate = atoi(optarg);
      break;

    case 'j':
      use_jpeg_header_boundry = true;
    break;

    case 'o':
      output = OUTPUT_STDOUT;
    break;

    default:
      usage(stderr, argc, argv);
      exit(EXIT_FAILURE);
    }

  }

  if ( io == IO_METHOD_USERPTR ) {
    fprintf(stderr, "IO_METHOD_USERPTR not supported yet, using MMAP instead");
    io = IO_METHOD_MMAP;
  }


  device_open();
  init_device();
  start_capturing();
 
  capture_loop();
 
  stop_capturing();
  uninit_device();
  device_close();

  return 0;
}