Merge pull request #580 from wigyori/cc-libpcap
[15.05/openwrt.git] / package / libs / libpcap / patches / 202-protocol_api.patch
1 This API extension is used by ead (Emergency Access Daemon)
2
3 --- a/pcap-linux.c
4 +++ b/pcap-linux.c
5 @@ -425,7 +425,7 @@ static int  iface_get_id(int fd, const ch
6  static int     iface_get_mtu(int fd, const char *device, char *ebuf);
7  static int     iface_get_arptype(int fd, const char *device, char *ebuf);
8  #ifdef HAVE_PF_PACKET_SOCKETS
9 -static int     iface_bind(int fd, int ifindex, char *ebuf);
10 +static int     iface_bind(int fd, int ifindex, char *ebuf, unsigned short proto);
11  #ifdef IW_MODE_MONITOR
12  static int     has_wext(int sock_fd, const char *device, char *ebuf);
13  #endif /* IW_MODE_MONITOR */
14 @@ -1059,7 +1059,7 @@ pcap_can_set_rfmon_linux(pcap_t *handle)
15          * (We assume that if we have Wireless Extensions support
16          * we also have PF_PACKET support.)
17          */
18 -       sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
19 +       sock_fd = socket(PF_PACKET, SOCK_RAW, p->opt.proto);
20         if (sock_fd == -1) {
21                 (void)pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
22                     "socket: %s", pcap_strerror(errno));
23 @@ -1456,6 +1456,9 @@ pcap_activate_linux(pcap_t *handle)
24         handle->read_op = pcap_read_linux;
25         handle->stats_op = pcap_stats_linux;
26  
27 +       if (handle->opt.proto < 0)
28 +               handle->opt.proto = (int) htons(ETH_P_ALL);
29 +
30         /*
31          * The "any" device is a special device which causes us not
32          * to bind to a particular device and thus to look at all
33 @@ -3335,8 +3338,8 @@ activate_new(pcap_t *handle)
34          * try a SOCK_RAW socket for the raw interface.
35          */
36         sock_fd = is_any_device ?
37 -               socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
38 -               socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
39 +               socket(PF_PACKET, SOCK_DGRAM, handle->opt.proto) :
40 +               socket(PF_PACKET, SOCK_RAW, handle->opt.proto);
41  
42         if (sock_fd == -1) {
43                 if (errno == EINVAL || errno == EAFNOSUPPORT) {
44 @@ -3454,7 +3457,7 @@ activate_new(pcap_t *handle)
45                                 return PCAP_ERROR;
46                         }
47                         sock_fd = socket(PF_PACKET, SOCK_DGRAM,
48 -                           htons(ETH_P_ALL));
49 +                           handle->opt.proto);
50                         if (sock_fd == -1) {
51                                 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
52                                     "socket: %s", pcap_strerror(errno));
53 @@ -3518,7 +3521,7 @@ activate_new(pcap_t *handle)
54                 }
55  
56                 if ((err = iface_bind(sock_fd, handlep->ifindex,
57 -                   handle->errbuf)) != 1) {
58 +                   handle->errbuf, handle->opt.proto)) != 1) {
59                         close(sock_fd);
60                         if (err < 0)
61                                 return err;
62 @@ -5271,7 +5274,7 @@ iface_get_id(int fd, const char *device,
63   *  or a PCAP_ERROR_ value on a hard error.
64   */
65  static int
66 -iface_bind(int fd, int ifindex, char *ebuf)
67 +iface_bind(int fd, int ifindex, char *ebuf, unsigned short proto)
68  {
69         struct sockaddr_ll      sll;
70         int                     err;
71 @@ -5280,7 +5283,7 @@ iface_bind(int fd, int ifindex, char *eb
72         memset(&sll, 0, sizeof(sll));
73         sll.sll_family          = AF_PACKET;
74         sll.sll_ifindex         = ifindex;
75 -       sll.sll_protocol        = htons(ETH_P_ALL);
76 +       sll.sll_protocol        = proto;
77  
78         if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
79                 if (errno == ENETDOWN) {
80 @@ -6325,7 +6328,7 @@ activate_old(pcap_t *handle)
81  
82         /* Open the socket */
83  
84 -       handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
85 +       handle->fd = socket(PF_INET, SOCK_PACKET, handle->opt.proto);
86         if (handle->fd == -1) {
87                 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
88                          "socket: %s", pcap_strerror(errno));
89 --- a/pcap.c
90 +++ b/pcap.c
91 @@ -578,6 +578,7 @@ pcap_create_common(char *ebuf, size_t si
92         p->opt.promisc = 0;
93         p->opt.rfmon = 0;
94         p->opt.immediate = 0;
95 +       p->opt.proto = -1;
96         p->opt.tstamp_type = -1;        /* default to not setting time stamp type */
97         p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
98  
99 @@ -771,6 +772,15 @@ pcap_get_tstamp_precision(pcap_t *p)
100  }
101  
102  int
103 +pcap_set_protocol(pcap_t *p, unsigned short proto)
104 +{
105 +       if (pcap_check_activated(p))
106 +               return PCAP_ERROR_ACTIVATED;
107 +       p->opt.proto = proto;
108 +       return 0;
109 +}
110 +
111 +int
112  pcap_activate(pcap_t *p)
113  {
114         int status;
115 --- a/pcap/pcap.h
116 +++ b/pcap/pcap.h
117 @@ -68,6 +68,7 @@ extern "C" {
118  #define PCAP_VERSION_MINOR 4
119  
120  #define PCAP_ERRBUF_SIZE 256
121 +#define HAS_PROTO_EXTENSION
122  
123  /*
124   * Compatibility for systems that have a bpf.h that
125 @@ -287,6 +288,7 @@ PCAP_API int        pcap_set_timeout(pcap_t *,
126  PCAP_API int   pcap_set_tstamp_type(pcap_t *, int);
127  PCAP_API int   pcap_set_immediate_mode(pcap_t *, int);
128  PCAP_API int   pcap_set_buffer_size(pcap_t *, int);
129 +PCAP_API int   pcap_set_protocol(pcap_t *, unsigned short);
130  PCAP_API int   pcap_set_tstamp_precision(pcap_t *, int);
131  PCAP_API int   pcap_get_tstamp_precision(pcap_t *);
132  PCAP_API int   pcap_activate(pcap_t *);
133 --- a/pcap-int.h
134 +++ b/pcap-int.h
135 @@ -111,6 +111,7 @@ struct pcap_opt {
136         char    *device;
137         int     timeout;        /* timeout for buffering */
138         u_int   buffer_size;
139 +       int     proto;          /* protocol for packet socket (linux) */
140         int     promisc;
141         int     rfmon;          /* monitor mode */
142         int     immediate;      /* immediate mode - deliver packets as soon as they arrive */