updated on Thu Jan 19 20:01:47 UTC 2012
[aur-mirror.git] / libpcap-any / 20-fix-any-intf.diff
blob84afa6822526517b9378e99f0d9f0af24759f586
1 commit 8fa17a5a554aaeb85d3ec4118b45a31f1efd6808
2 Author: guy <guy>
3 Date: Wed Nov 19 08:20:39 2008 +0000
5 Fix the handling of the "any" device, including making it reject
6 attempts to open it in monitor mode.
7 ---
8 pcap-linux.c | 68 ++++++++++++++++++++++++++++++++++++-----------------------
9 1 file changed, 42 insertions(+), 26 deletions(-)
11 --- a/pcap-linux.c
12 +++ b/pcap-linux.c
13 @@ -297,6 +297,12 @@
15 pcap_t *handle;
17 + /*
18 + * A null device name is equivalent to the "any" device.
19 + */
20 + if (device == NULL)
21 + device = "any";
23 #ifdef HAVE_DAG_API
24 if (strstr(device, "dag")) {
25 return dag_create(device, ebuf);
26 @@ -338,10 +344,9 @@
27 struct iwreq ireq;
28 #endif
30 - if (p->opt.source == NULL) {
31 + if (strcmp(p->opt.source, "any") == 0) {
33 - * This is equivalent to the "any" device, and we don't
34 - * support monitor mode on it.
35 + * Monitor mode makes no sense on the "any" device.
37 return 0;
39 @@ -518,12 +523,11 @@
40 handle->stats_op = pcap_stats_linux;
43 - * NULL and "any" are special devices which give us the hint to
44 - * monitor all devices.
45 + * The "any" device is a special device which causes us not
46 + * to bind to a particular device and thus to look at all
47 + * devices.
49 - if (!device || strcmp(device, "any") == 0) {
50 - device = NULL;
51 - handle->md.device = strdup("any");
52 + if (strcmp(device, "any") == 0) {
53 if (handle->opt.promisc) {
54 handle->opt.promisc = 0;
55 /* Just a warning. */
56 @@ -531,10 +535,9 @@
57 "Promiscuous mode not supported on the \"any\" device");
58 status = PCAP_WARNING_PROMISC_NOTSUP;
60 + }
62 - } else
63 - handle->md.device = strdup(device);
65 + handle->md.device = strdup(device);
66 if (handle->md.device == NULL) {
67 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s",
68 pcap_strerror(errno) );
69 @@ -1657,19 +1660,21 @@
70 activate_new(pcap_t *handle)
72 #ifdef HAVE_PF_PACKET_SOCKETS
73 + const char *device = handle->opt.source;
74 + int is_any_device = (strcmp(device, "any") == 0);
75 int sock_fd = -1, arptype, val;
76 int err = 0;
77 struct packet_mreq mr;
78 - const char* device = handle->opt.source;
81 - * Open a socket with protocol family packet. If a device is
82 - * given we try to open it in raw mode otherwise we use
83 - * the cooked interface.
84 - */
85 - sock_fd = device ?
86 - socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
87 - : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
88 + * Open a socket with protocol family packet. If the
89 + * "any" device was specified, we open a SOCK_DGRAM
90 + * socket for the cooked interface, otherwise we first
91 + * try a SOCK_RAW socket for the raw interface.
92 + */
93 + sock_fd = is_any_device ?
94 + socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
95 + socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
97 if (sock_fd == -1) {
98 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
99 @@ -1704,7 +1709,7 @@
100 * to cooked mode if we have an unknown interface type
101 * or a type we know doesn't work well in raw mode.
103 - if (device) {
104 + if (!is_any_device) {
105 /* Assume for now we don't need cooked mode. */
106 handle->md.cooked = 0;
108 @@ -1819,15 +1824,23 @@
110 } else {
112 - * This is cooked mode.
113 + * The "any" device.
114 + */
115 + if (handle->opt.rfmon) {
116 + /*
117 + * It doesn't support monitor mode.
118 + */
119 + return PCAP_ERROR_RFMON_NOTSUP;
122 + /*
123 + * It uses cooked mode.
125 handle->md.cooked = 1;
126 handle->linktype = DLT_LINUX_SLL;
129 * We're not bound to a device.
130 - * XXX - true? Or true only if we're using
131 - * the "any" device?
132 * For now, we're using this as an indication
133 * that we can't transmit; stop doing that only
134 * if we figure out how to transmit in cooked
135 @@ -1852,10 +1865,13 @@
138 * Hmm, how can we set promiscuous mode on all interfaces?
139 - * I am not sure if that is possible at all.
140 + * I am not sure if that is possible at all. For now, we
141 + * silently ignore attempts to turn promiscuous mode on
142 + * for the "any" device (so you don't have to explicitly
143 + * disable it in programs such as tcpdump).
146 - if (device && handle->opt.promisc) {
147 + if (!is_any_device && handle->opt.promisc) {
148 memset(&mr, 0, sizeof(mr));
149 mr.mr_ifindex = handle->md.ifindex;
150 mr.mr_type = PACKET_MR_PROMISC;
151 @@ -3118,7 +3134,7 @@
153 /* Bind to the given device */
155 - if (!device) {
156 + if (strcmp(device, "any") == 0) {
157 strncpy(handle->errbuf, "pcap_activate: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
158 PCAP_ERRBUF_SIZE);
159 return PCAP_ERROR;