iCub-main
pcap_wrapper_linux.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Valentina Gaggero
4  * email: valentina.gaggero@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17 */
18 
19 /* @file main.cpp
20  @brief
21  @author valentina.gaggero@iit.it
22  @date 03/19/2013
23 **/
24 
25 // --------------------------------------------------------------------------------------------------------------------
26 // - external dependencies
27 // --------------------------------------------------------------------------------------------------------------------
28 
29 #include <unistd.h>
30 #include <stdio.h>
31 
32 #include <pcap.h>
33 
34 
35 
36 
37 
38 //embody stuff
39 #include "EoCommon.h"
40 
41 
42 
43 
44 
45 
46 // --------------------------------------------------------------------------------------------------------------------
47 // - declaration of extern public interface
48 // --------------------------------------------------------------------------------------------------------------------
49 
50 
51 
52 
53 // --------------------------------------------------------------------------------------------------------------------
54 // - declaration of extern hidden interface
55 // --------------------------------------------------------------------------------------------------------------------
56 
57 
58 
59 
60 // --------------------------------------------------------------------------------------------------------------------
61 // - #define with internal scope
62 // --------------------------------------------------------------------------------------------------------------------
63 #define TIMEOUT_MS 1000
64 
65 // --------------------------------------------------------------------------------------------------------------------
66 // - definition (and initialisation) of extern variables, but better using _get(), _set()
67 // --------------------------------------------------------------------------------------------------------------------
68 // empty-section
69 
70 
71 
72 // --------------------------------------------------------------------------------------------------------------------
73 // - typedef with internal scope
74 // --------------------------------------------------------------------------------------------------------------------
75 // empty-section
76 
77 
78 // --------------------------------------------------------------------------------------------------------------------
79 // - declaration of static functions
80 // --------------------------------------------------------------------------------------------------------------------
81 
82 
83 
84 
85 // --------------------------------------------------------------------------------------------------------------------
86 // - definition (and initialisation) of static variables
87 // --------------------------------------------------------------------------------------------------------------------
88 static pcap_t *handle = NULL;
89 static struct bpf_program fp; /* The compiled filter expression */
90 
91 // --------------------------------------------------------------------------------------------------------------------
92 // - definition of extern public functions
93 // --------------------------------------------------------------------------------------------------------------------
94 
95 extern uint8_t wrapperPcap_init(char* dev, char*filter_expr)
96 {
97 
98  char errbuf[PCAP_ERRBUF_SIZE];
99  bpf_u_int32 mask; /* The netmask of our sniffing device */
100  bpf_u_int32 net; /* The IP of our sniffing device */
101 
102  //3) init pcap
103 
104  if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1)
105  {
106  printf("Sorry, Can't get netmask for device %s\n", dev);
107  net = 0;
108  mask = 0;
109  }
110 
111  handle = pcap_open_live(dev, BUFSIZ, 0, TIMEOUT_MS, errbuf); // 0 = non promiscuo
112  if (handle == NULL)
113  {
114  printf("Sorry, occured problem while opening pcap\n");
115  //fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
116  return(0);
117  }
118 
119  /* make sure we're capturing on an Ethernet device [2] */
120  if (pcap_datalink(handle) != DLT_EN10MB)
121  {
122  printf("Sorry, pcap has been open on a NOT ethernet device\n");
123  //fprintf(stderr, "%s is not an Ethernet\n", dev);
124  return(0);
125  }
126 
127 
128  if (pcap_compile(handle, &fp, filter_expr, 0, net) == -1)
129  {
130  printf("Sorry, pcap can't paser filter%s: %s\n", filter_expr, pcap_geterr(handle));
131  //fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
132  return(0);
133  }
134 
135  if (pcap_setfilter(handle, &fp) == -1)
136  {
137  printf("Sorry, pcap couldn't install filter %s: %s\n", filter_expr, pcap_geterr(handle));
138  // fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
139  return(0);
140  }
141 
142  return(1); //ok
143 
144 }
145 
146 #warning cnt 32 o 64 bit???
147 extern uint8_t wrapperPcap_loop(int32_t cnt, pcap_handler callback, uint8_t *user)
148 {
149  uint8_t ret;
150 
151  ret = pcap_loop(handle, cnt, callback, user); // Get into the loop
152 
153  return(ret);
154 }
155 
156 extern void wrapperPcap_close(void)
157 {
158  pcap_freecode(&fp);
159  pcap_close(handle);
160 }
161 
162 // --------------------------------------------------------------------------------------------------------------------
163 // - definition of extern hidden functions
164 // --------------------------------------------------------------------------------------------------------------------
165 
166 
167 
168 // --------------------------------------------------------------------------------------------------------------------
169 // - definition of static functions
170 // --------------------------------------------------------------------------------------------------------------------
171 
172 
173 
174 
175 // --------------------------------------------------------------------------------------------------------------------
176 // - end-of-file (leave a blank line after)
177 // --------------------------------------------------------------------------------------------------------------------
178 
179 
180 
181 
uint8_t wrapperPcap_loop(int32_t cnt, pcap_handler callback, uint8_t *user)
uint8_t wrapperPcap_init(char *dev, char *filter_expr)
static struct bpf_program fp
static pcap_t * handle
void wrapperPcap_close(void)
#define TIMEOUT_MS