Contiki 3.x
wpcap.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  */
34 
35 #define WIN32_LEAN_AND_MEAN
36 #define _WIN32_WINNT 0x0501
37 #include <windows.h>
38 #include <winsock2.h>
39 #include <iphlpapi.h>
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 
44 #ifdef __CYGWIN__
45 #include <alloca.h>
46 #else /* __CYGWIN__ */
47 #include <malloc.h>
48 #endif /* __CYGWIN__ */
49 
50 #define DEBUG 0
51 #if DEBUG
52 #define PRINTF(...) printf(__VA_ARGS__)
53 #else
54 #define PRINTF(...)
55 #endif
56 
57 #include "contiki-net.h"
58 #include "sys/log.h"
59 
60 #include "net/wpcap.h"
61 
62 /* Handle native-border-router case where the fallback has ethernet headers.
63  * The command line args for native-border-router conflice with the passing
64  * of the interface addresses to connect to, so both must be hard coded.
65  * See comments in wpcap-drv.c
66  */
67 #ifdef SELECT_CALLBACK
68 #define FALLBACK_HAS_ETHERNET_HEADERS 1
69 #endif
70 
71 #if UIP_CONF_IPV6
72 #include <ws2tcpip.h>
73 struct in6_addr addr6;
74 char addr6str[64];
75 /*---------------------------------------------------------------------------*/
76 uint8_t
77 issame_ip6addr(struct in6_addr addr1, struct in6_addr addr2)
78 {
79  return ((addr1.s6_addr32[0]==addr2.s6_addr32[0]) &&
80  (addr1.s6_addr32[1]==addr2.s6_addr32[1]) &&
81  (addr1.s6_addr32[2]==addr2.s6_addr32[2]) &&
82  (addr1.s6_addr32[3]==addr2.s6_addr32[3]) );
83 }
84 /*---------------------------------------------------------------------------*/
85 uint8_t
86 iszero_ip6addr(struct in6_addr addr)
87 {
88  return ((addr.s6_addr32[0]==0) &&
89  (addr.s6_addr32[1]==0) &&
90  (addr.s6_addr32[2]==0) &&
91  (addr.s6_addr32[3]==0) );
92 }
93 /*---------------------------------------------------------------------------*/
94 uint8_t
95 sprint_ip6addr(struct in6_addr addr, char * result)
96 {
97  unsigned char i = 0;
98  unsigned char zerocnt = 0;
99  unsigned char numprinted = 0;
100  char * starting = result;
101 
102  *result++='[';
103  while (numprinted < 8) {
104  if ((addr.s6_addr16[i] == 0) && (zerocnt == 0)) {
105  while(addr.s6_addr16[zerocnt + i] == 0) zerocnt++;
106  if (zerocnt == 1) {
107  *result++ = '0';
108  numprinted++;
109  break;
110  }
111  i += zerocnt;
112  numprinted += zerocnt;
113  } else {
114  result += sprintf(result, "%x", (unsigned int) uip_ntohs(addr.s6_addr16[i]));
115  i++;
116  numprinted++;
117  }
118  if (numprinted != 8) *result++ = ':';
119  }
120  *result++=']';
121  *result=0;
122  return (result - starting);
123 }
124 
125 #endif /* UIP_CONF_IPV6 */
126 
127 
128 #ifdef __CYGWIN__
129 __attribute__((dllimport)) extern char **__argv[];
130 #endif /* __CYGWIN__ */
131 
132 struct pcap;
133 
134 struct pcap_if {
135  struct pcap_if *next;
136  char *name;
137  char *description;
138  struct pcap_addr {
139  struct pcap_addr *next;
140  struct sockaddr *addr;
141  struct sockaddr *netmask;
142  struct sockaddr *broadaddr;
143  struct sockaddr *dstaddr;
144  } *addresses;
145  DWORD flags;
146 };
147 
148 struct pcap_pkthdr {
149  struct timeval ts;
150  DWORD caplen;
151  DWORD len;
152 };
153 
154 HMODULE wpcap;
155 
156 static struct pcap *pcap;
157 
158 /* uip_lladdr is defined in uip.c. It is not used in uip6.c.
159  * If needed for some purpose it can be defined here
160  */
161 #if UIP_CONF_IPV6
162 //struct uip_eth_addr uip_lladdr;
163 #endif
164 
165 static int (* pcap_findalldevs)(struct pcap_if **, char *);
166 static struct pcap *(* pcap_open_live)(char *, int, int, int, char *);
167 static int (* pcap_next_ex)(struct pcap *, struct pcap_pkthdr **, unsigned char **);
168 static int (* pcap_sendpacket)(struct pcap *, unsigned char *, int);
169 
170 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
171 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
172 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
173 
174 #ifdef UIP_FALLBACK_INTERFACE
175 static struct pcap *pfall;
176 struct in_addr addrfall;
177 #if UIP_CONF_IPV6
178 struct in_addr6 addrfall6;
179 #endif
180 
181 /*---------------------------------------------------------------------------*/
182 static void
183 init(void)
184 {
185 /* Nothing to do here */
186 }
187 /*---------------------------------------------------------------------------*/
188 uint8_t wfall_send(const uip_lladdr_t *lladdr);
189 #if FALLBACK_HAS_ETHERNET_HEADERS
190 #undef IPBUF
191 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[14])
192 static uip_ipaddr_t last_sender;
193 #endif
194 
195 static void
196 output(void)
197 {
198 #if FALLBACK_HAS_ETHERNET_HEADERS&&0
199  if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
200  /* Do not bounce packets back to fallback if the packet was received from it */
201  PRINTF("FUT: trapping pingpong");
202  return;
203  }
204  uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr);
205 #endif
206  PRINTF("FUT: %u\n", uip_len);
207  wfall_send(0);
208 }
209 
210 const struct uip_fallback_interface rpl_interface = {
211  init, output
212 };
213 
214 #endif
215 
216 /*---------------------------------------------------------------------------*/
217 static void
218 error_exit(char *message)
219 {
220  printf("error_exit: %s", message);
221  exit(EXIT_FAILURE);
222 }
223 /*---------------------------------------------------------------------------*/
224 static void
225 set_ethaddr(struct in_addr addr)
226 {
227  PIP_ADAPTER_ADDRESSES adapters;
228  ULONG size = 0;
229 
230  if(GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
231  GAA_FLAG_SKIP_MULTICAST |
232  GAA_FLAG_SKIP_DNS_SERVER,
233  NULL, NULL, &size) != ERROR_BUFFER_OVERFLOW) {
234  error_exit("error on access to adapter list size\n");
235  }
236  adapters = alloca(size);
237  if(GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
238  GAA_FLAG_SKIP_MULTICAST |
239  GAA_FLAG_SKIP_DNS_SERVER,
240  NULL, adapters, &size) != ERROR_SUCCESS) {
241  error_exit("error on access to adapter list\n");
242  }
243 
244  while(adapters != NULL) {
245 
246  char buffer[256];
247  WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
248  buffer, sizeof(buffer), NULL, NULL);
249  log_message("set_ethaddr: found adapter: ", buffer);
250 
251  if(adapters->FirstUnicastAddress != NULL &&
252  adapters->FirstUnicastAddress->Address.lpSockaddr != NULL &&
253  adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET) {
254 
255  struct in_addr adapter_addr;
256  adapter_addr = ((struct sockaddr_in *)adapters->FirstUnicastAddress->Address.lpSockaddr)->sin_addr;
257  log_message("set_ethaddr: with address: ", inet_ntoa(adapter_addr));
258 
259  if(adapter_addr.s_addr == addr.s_addr) {
260  if(adapters->PhysicalAddressLength != 6) {
261  error_exit("ip addr specified on cmdline does not belong to an ethernet card\n");
262  }
263  wsprintf(buffer, "%02X-%02X-%02X-%02X-%02X-%02X",
264  adapters->PhysicalAddress[0], adapters->PhysicalAddress[1],
265  adapters->PhysicalAddress[2], adapters->PhysicalAddress[3],
266  adapters->PhysicalAddress[4], adapters->PhysicalAddress[5]);
267  log_message("set_ethaddr: ethernetaddr: ", buffer);
268 #if UIP_CONF_IPV6
269 // int i;for (i=0;i<6;i++) uip_lladdr.addr[i] = adapters->PhysicalAddress[i];
270 #else
271  uip_setethaddr((*(struct uip_eth_addr *)adapters->PhysicalAddress));
272 #endif
273  break;
274  }
275  }
276  adapters = adapters->Next;
277  }
278 
279  if(adapters == NULL) {
280  error_exit("no adapter found with ip addr specified on cmdline\n");
281  }
282 }
283 
284 #if UIP_CONF_IPV6
285 /*---------------------------------------------------------------------------*/
286 static void
287 set_ethaddr6(struct in_addr6 addr)
288 {
289  PIP_ADAPTER_ADDRESSES adapters;
290  ULONG size = 0;
291 
292  if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_SKIP_ANYCAST |
293  GAA_FLAG_SKIP_MULTICAST |
294  GAA_FLAG_SKIP_DNS_SERVER,
295  NULL, NULL, &size) != ERROR_BUFFER_OVERFLOW) {
296  error_exit("error on access to adapter list size\n");
297  }
298  adapters = alloca(size);
299  if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_SKIP_ANYCAST |
300  GAA_FLAG_SKIP_MULTICAST |
301  GAA_FLAG_SKIP_DNS_SERVER,
302  NULL, adapters, &size) != ERROR_SUCCESS) {
303  error_exit("error on access to adapter list\n");
304  }
305 
306  while(adapters != NULL) {
307 
308  char buffer[256];
309  WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
310  buffer, sizeof(buffer), NULL, NULL);
311  log_message("set_ethaddr: found adapter: ", buffer);
312 
313  if(adapters->FirstUnicastAddress != NULL &&
314  adapters->FirstUnicastAddress->Address.lpSockaddr != NULL &&
315  adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET6) {
316 
317  struct in_addr6 adapter_addr;
318  adapter_addr = ((struct sockaddr_in6 *)adapters->FirstUnicastAddress->Address.lpSockaddr)->sin6_addr;
319  sprint_ip6addr(adapter_addr, addr6str);
320  log_message("set_ethaddr: with ipv6 address: : ", addr6str);
321  if(issame_ip6addr(adapter_addr,addr6)) {
322  if(adapters->PhysicalAddressLength != 6) {
323  error_exit("ip addr specified on cmdline does not belong to an ethernet card\n");
324  }
325  wsprintf(buffer, "%02X-%02X-%02X-%02X-%02X-%02X",
326  adapters->PhysicalAddress[0], adapters->PhysicalAddress[1],
327  adapters->PhysicalAddress[2], adapters->PhysicalAddress[3],
328  adapters->PhysicalAddress[4], adapters->PhysicalAddress[5]);
329  log_message("set_ethaddr: ethernetaddr: ", buffer);
330 #if UIP_CONF_IPV6
331 // int i;for (i=0;i<6;i++) uip_lladdr.addr[i] = adapters->PhysicalAddress[i]; //does this need doing?
332 #else
333  uip_setethaddr((*(struct uip_eth_addr *)adapters->PhysicalAddress));
334 #endif
335  break;
336  }
337  }
338  adapters = adapters->Next;
339  }
340 
341  if(adapters == NULL) {
342  error_exit("no adapter found with ip addr specified on cmdline\n");
343  }
344 }
345 #endif
346 /*---------------------------------------------------------------------------*/
347 static void
348 init_pcap(struct in_addr addr)
349 {
350  struct pcap_if *interfaces;
351  struct pcap_addr *paddr;
352  char error[256];
353 
354  if(pcap_findalldevs(&interfaces, error) == -1) {
355  error_exit(error);
356  }
357 
358  while(interfaces != NULL) {
359  log_message("init_pcap: found interface: ", interfaces->description);
360 
361  if(interfaces->addresses != NULL) {
362  for(paddr = interfaces->addresses;
363  paddr != NULL;
364  paddr = paddr->next) {
365  if(paddr->addr != NULL && paddr->addr->sa_family == AF_INET) {
366  struct in_addr interface_addr;
367  interface_addr = ((struct sockaddr_in *)paddr->addr)->sin_addr;
368  log_message("init_pcap: with address: ", inet_ntoa(interface_addr));
369 
370  if(interface_addr.s_addr == addr.s_addr) {
371  pcap = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
372  if(pcap == NULL) {
373  error_exit(error);
374  }
375 #ifdef UIP_FALLBACK_INTERFACE
376  log_message("init_pcap: Opened as primary interface","");
377 #else
378  log_message("init_pcap: Opened as interface","");
379 #endif
380 // pcap_setdirection(PCAP_D_IN); //Not implemented in windows yet?
381  set_ethaddr(addr);
382 
383 #ifdef UIP_FALLBACK_INTERFACE
384  }
385  if (pfall && pcap) return;
386  if(interface_addr.s_addr == addrfall.s_addr) {
387  pfall = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
388  if(pfall == NULL) {
389  error_exit(error);
390  }
391  log_message("init_pcap: Opened as fallback interface","");
392  if (pcap) return;
393  }
394 #else
395  return;
396  }
397 #endif
398 
399 #if UIP_CONF_IPV6
400 
401  } else if(paddr->addr != NULL && paddr->addr->sa_family == AF_INET6) {
402  struct in6_addr interface_addr;
403  interface_addr = ((struct sockaddr_in6 *)paddr->addr)->sin6_addr;
404 
405  sprint_ip6addr(interface_addr, addr6str);
406  log_message("init_pcap: with ipv6 address: ", addr6str);
407 
408  if (issame_ip6addr(interface_addr, addr6)) {
409  pcap = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
410  if(pcap == NULL) {
411  error_exit(error);
412  }
413 #ifdef UIP_FALLBACK_INTERFACE
414  log_message("init_pcap: Opened as primary interface","");
415 #endif
416  set_ethaddr6(addr6);
417 // pcap_setdirection(PCAP_D_IN); //Not implemented in windows yet?
418 #ifdef UIP_FALLBACK_INTERFACE
419  }
420  if (pfall && pcap) return; //exit when we have both interfaces
421 
422  if (issame_ip6addr(interface_addr, addrfall6)) {
423  pfall = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
424  if(pfall == NULL) {
425  error_exit(error);
426  }
427  log_message("init_pcap: Opened as fallback interface","");
428  if (pcap) return;
429  }
430 #else
431  return;
432  }
433 #endif
434 #endif /* UIP_CONF_IPV6 */
435  }
436  }
437  }
438  interfaces = interfaces->next;
439 
440  }
441 
442  if(interfaces == NULL) {
443  error_exit("no interface found with specified ip address\n");
444  }
445 }
446 /*---------------------------------------------------------------------------*/
447 void
448 wpcap_init(void)
449 {
450  struct in_addr addr;
451  addr.s_addr = INADDR_NONE; //255.255.255.255
452 #ifdef UIP_FALLBACK_INTERFACE
453  addrfall.s_addr = INADDR_NONE;
454 #endif
455 
456  /* Pick up possible ip addresses from command line */
457 #ifdef __CYGWIN__
458  if ((*__argv)[1]) {
459  addr.s_addr = inet_addr((*__argv)[1]);
460 #if UIP_CONF_IPV6
461  uiplib_ipaddrconv((*__argv)[1],(uip_ipaddr_t*) &addr6.s6_addr);
462 #endif
463 #ifdef UIP_FALLBACK_INTERFACE
464  if ((*__argv)[2]) {
465  addrfall.s_addr = inet_addr((*__argv)[2]);
466 #if UIP_CONF_IPV6
467  uiplib_ipaddrconv((*__argv)[2],(uip_ipaddr_t*) &addrfall6.s6_addr);
468 #endif
469  }
470 #endif
471  }
472 
473 #else /* __CYGWIN__ */
474 /* VC++ build on win32 platform. Currently the platform has no ipv6 support */
475  addr.s_addr = inet_addr(__argv[1]);
476 #if UIP_CONF_IPV6
477  if((__argv)[1])
478  uiplib_ipaddrconv((__argv)[1],(uip_ipaddr_t*) &addr6.s6_addr);
479 #endif
480 #ifdef UIP_FALLBACK_INTERFACE
481  addrfall.s_addr = inet_addr(__argv[2]);
482 #if UIP_CONF_IPV6
483  if((__argv)[2])
484  uiplib_ipaddrconv((__argv)[2],(uip_ipaddr_t*) &addrfall6.s6_addr);
485 #endif
486 #endif
487 #endif /* __CYGWIN__ */
488 
489 #if DEBUG
490  log_message("wpcap_init:Passed ipv4 ", inet_ntoa(addr));
491  sprint_ip6addr(addr6, addr6str);
492  log_message("wpcap_init:Passed ipv6 ", addr6str);
493 #ifdef UIP_FALLBACK_INTERFACE
494  log_message("wpcap_init:Passed fallback ipv4 ", inet_ntoa(addrfall));
495  sprint_ip6addr(addrfall6, addr6str);
496  log_message("wpcap_init:Passed fallback ipv6 ", addr6str);
497 #endif
498 #endif
499 
500  /* Use build defaults if not enough addresses passed */
501 #if UIP_CONF_IPV6
502 
503 #ifdef UIP_FALLBACK_INTERFACE
504  if(addrfall.s_addr == INADDR_NONE) {
505  if(iszero_ip6addr(addrfall6)) {
506 #ifdef WPCAP_FALLBACK_ADDRESS
507  addrfall.s_addr = inet_addr(WPCAP_FALLBACK_ADDRESS);
508 // if(addrfall.s_addr == INADDR_NONE) { //use ipv6 if contiki-conf.h override
509  uiplib_ipaddrconv(WPCAP_FALLBACK_ADDRESS,(uip_ipaddr_t*) &addrfall6.s6_addr);
510 // }
511 #else
512 // addrfall.s_addr = inet_addr("10.2.10.10");
513  uiplib_ipaddrconv("bbbb::1",(uip_ipaddr_t*) &addrfall6.s6_addr);
514 #endif
515  }
516  }
517 #endif
518 
519  if(addr.s_addr == INADDR_NONE) {
520  if(iszero_ip6addr(addr6)) {
521 #ifdef WPCAP_INTERFACE_ADDRESS
522  addr.s_addr = inet_addr(WPCAP_INTERFACE_ADDRESS);
523 // if(addr.s_addr == INADDR_NONE) {
524  uiplib_ipaddrconv(WPCAP_INTERFACE_ADDRESS,(uip_ipaddr_t*) &addr6.s6_addr);
525 // }
526 #else
527  addr.s_addr = inet_addr("10.10.10.10"); //prefer ipv4 default for legacy compatibility
528 // uiplib_ipaddrconv("aaaa::1",(uip_ipaddr_t*) &addr6.s6_addr);
529 #endif
530 
531 #ifdef UIP_FALLBACK_INTERFACE
532  log_message("usage: <program> <ip addr of interface> <ip addr of fallback>\n","");
533  if(addr.s_addr != INADDR_NONE) {
534  log_message("-->I'll try interface address ", inet_ntoa(addr));
535  } else {
536  sprint_ip6addr(addr6, addr6str);
537  log_message("-->I'll try interface address ", addr6str);
538  }
539  if(addrfall.s_addr != INADDR_NONE) {
540  log_message("--> and fallback address ", inet_ntoa(addrfall));
541  } else {
542  sprint_ip6addr(addrfall6, addr6str);
543  log_message("--> and fallback address ", addr6str);
544  }
545 #else
546  if(addr.s_addr != INADDR_NONE) {
547  log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", inet_ntoa(addr));
548  } else {
549  sprint_ip6addr(addr6, addr6str);
550  log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", addr6str);
551  }
552 #endif
553  }
554  }
555 #else /* ip4 build */
556  if(addr.s_addr == INADDR_NONE) {
557 #ifdef WPCAP_INTERFACE_ADDRESS
558  addr.s_addr = inet_addr(WPCAP_INTERFACE_ADDRESS);
559 #else
560  addr.s_addr = inet_addr("10.10.10.10");
561 #endif
562  log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", inet_ntoa(addr));
563  }
564 #endif /* UIP_CONF_IPV6 */
565 
566 #if DEBUG
567  log_message("wpcap_init:Using ipv4 ", inet_ntoa(addr));
568  sprint_ip6addr(addr6, addr6str);
569  log_message("wpcap_init:Using ipv6 ", addr6str);
570 #ifdef UIP_FALLBACK_INTERFACE
571  log_message("wpcap_init:Using fallback ipv4 ", inet_ntoa(addrfall));
572  sprint_ip6addr(addrfall6, addr6str);
573  log_message("wpcap_init:Using fallback ipv6 ", addr6str);
574 #endif
575 #endif
576 
577  // log_message("wpcap_init:cmdline address: ", inet_ntoa(addr));
578 
579 
580  wpcap = LoadLibrary("wpcap.dll");
581  pcap_findalldevs = (int (*)(struct pcap_if **, char *))
582  GetProcAddress(wpcap, "pcap_findalldevs");
583  pcap_open_live = (struct pcap *(*)(char *, int, int, int, char *))
584  GetProcAddress(wpcap, "pcap_open_live");
585  pcap_next_ex = (int (*)(struct pcap *, struct pcap_pkthdr **, unsigned char **))
586  GetProcAddress(wpcap, "pcap_next_ex");
587  pcap_sendpacket = (int (*)(struct pcap *, unsigned char *, int))
588  GetProcAddress(wpcap, "pcap_sendpacket");
589 
590  if(pcap_findalldevs == NULL || pcap_open_live == NULL ||
591  pcap_next_ex == NULL || pcap_sendpacket == NULL) {
592  error_exit("error on access to winpcap library\n");
593  }
594 
595  init_pcap(addr);
596 
597 }
598 
599 /*---------------------------------------------------------------------------*/
600 uint16_t
601 wpcap_poll(void)
602 {
603  struct pcap_pkthdr *packet_header;
604  unsigned char *packet;
605 
606  switch(pcap_next_ex(pcap, &packet_header, &packet)) {
607  case -1:
608  error_exit("error on poll\n");
609  case 0:
610  return 0;
611  }
612 
613 #if UIP_CONF_IPV6
614 /* Since pcap_setdirection(PCAP_D_IN) is not implemented in winpcap all outgoing packets
615  * will be echoed back. The stack will ignore any packets not addressed to it, but initial
616  * ipv6 neighbor solicitations are addressed to everyone and the echoed NS sent on startup
617  * would be processed as a conflicting NS race which would cause a stack shutdown.
618  * So discard all packets with our source address (packet starts destaddr, srcaddr, ...)
619  *
620  */
621  int i;
622  for (i=0;i<UIP_LLADDR_LEN;i++) if (*(packet+UIP_LLADDR_LEN+i)!=uip_lladdr.addr[i]) break;
623  if (i==UIP_LLADDR_LEN) {
624  PRINTF("SIN: Discarding echoed packet\n");
625  return 0;
626  }
627 
628 /* To implement multihop, ignore packets to us from specified source macs
629  */
630 // printf("ethtype=%x %x",*(packet+2*UIP_LLADDR_LEN),*(packet+2*UIP_LLADDR_LEN+1));
631 // printf("hopcount=%u",*(packet+21));
632 #if 0
633  if (0
634 // || (*(packet+11) ==0x1) //20 ignores router
635 // || (*(packet+11) ==0x10)
636  || (*(packet+11) ==0x20) //router ignores 20
637  ) {
638  printf("i%x",*(packet+11));
639  return 0;
640  }
641 /* If we are not the recipient, ignore packets from other RPL nodes */
642  if (0
643  || (*(packet+5) !=0x1) //router
644 // || (*(packet+11) ==0x10)
645  // || (*(packet+11) ==0x20) //router ignores 20
646  ) {
647  printf("r%x",*(packet+11));
648  return 0;
649  }
650 #endif
651 
652 #endif /* UIP_CONF_IPV6 */
653 
654  if(packet_header->caplen > UIP_BUFSIZE) {
655  return 0;
656  }
657 // PRINTF("SIN: %lu\n", packet_header->caplen);
658  CopyMemory(uip_buf, packet, packet_header->caplen);
659  return (uint16_t)packet_header->caplen;
660 
661 }
662 
663 #ifdef UIP_FALLBACK_INTERFACE
664 uint16_t
665 wfall_poll(void)
666 {
667  struct pcap_pkthdr *packet_header;
668  unsigned char *packet;
669 
670  switch(pcap_next_ex(pfall, &packet_header, &packet)) {
671  case -1:
672  error_exit("error on fallback poll\n");
673  case 0:
674  return 0;
675  }
676 #if UIP_CONF_IPV6
677 #if FALLBACK_HAS_ETHERNET_HEADERS
678 #define ETHERNET_LLADDR_LEN 6
679 #else
680 #define ETHERNET_LLADDR_LEN UIP_LLADDR_LEN
681 #endif
682 /* Since pcap_setdirection(PCAP_D_IN) is not implemented in winpcap all outgoing packets
683  * will be echoed back. The stack will ignore any packets not addressed to it, but initial
684  * ipv6 neighbor solicitations are addressed to everyone and the echoed NS sent on startup
685  * would be processed as a conflicting NS race which would cause a stack shutdown.
686  * So discard all packets with our source address (packet starts destaddr, srcaddr, ...)
687  *
688  */
689  int i;
690  for (i=0;i<ETHERNET_LLADDR_LEN;i++) if (*(packet+ETHERNET_LLADDR_LEN+i)!=uip_lladdr.addr[i]) break;
691  if (i==ETHERNET_LLADDR_LEN) {
692  PRINTF("Discarding echoed packet\n");
693  return 0;
694  }
695 #endif /* UIP_CONF_IPV6 */
696 
697  if(packet_header->caplen > UIP_BUFSIZE) {
698  return 0;
699  }
700  PRINTF("FIN: %lu\n", packet_header->caplen);
701  CopyMemory(uip_buf, packet, packet_header->caplen);
702  return (uint16_t)packet_header->caplen;
703 
704 }
705 
706 #endif
707 
708 /*---------------------------------------------------------------------------*/
709 #if UIP_CONF_IPV6
710 uint8_t
711 wpcap_send(const uip_lladdr_t *lladdr)
712 {
713  if(lladdr == NULL) {
714 /* the dest must be multicast*/
715  (&BUF->dest)->addr[0] = 0x33;
716  (&BUF->dest)->addr[1] = 0x33;
717  (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
718  (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
719  (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
720  (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
721  } else {
722  //when fallback used this gets ptr to lladdr of all zeroes on forwarded packets, turn them into multicast(?)
723  if ((lladdr->addr[0]==0)&&(lladdr->addr[1]==0)&&(lladdr->addr[2]==0)&&(lladdr->addr[3]==0)&&(lladdr->addr[4]==0)&&(lladdr->addr[5]==0)) {
724  (&BUF->dest)->addr[0] = 0x33;
725  (&BUF->dest)->addr[1] = 0x33;
726  (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
727  (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
728  (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
729  (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
730  } else {
731  memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
732  }
733  }
734  memcpy(&BUF->src, &uip_lladdr, UIP_LLADDR_LEN);
735  PRINTF("SUT: %u\n", uip_len);
736  PRINTF("Src= %02x %02x %02x %02x %02x %02x",(&BUF->src)->addr[0],(&BUF->src)->addr[1],(&BUF->src)->addr[2],(&BUF->src)->addr[3],(&BUF->src)->addr[4],(&BUF->src)->addr[5]);
737  PRINTF(" Dst= %02x %02x %02x %02x %02x %02x",(&BUF->dest)->addr[0],(&BUF->dest)->addr[1],(&BUF->dest)->addr[2],(&BUF->dest)->addr[3],(&BUF->dest)->addr[4],(&BUF->dest)->addr[5]);
738  PRINTF(" Type=%04x\n",BUF->type);
739  BUF->type = UIP_HTONS(UIP_ETHTYPE_IPV6);
740  uip_len += sizeof(struct uip_eth_hdr);
741  if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
742  error_exit("error on send\n");
743  }
744 return 0;
745 }
746 #ifdef UIP_FALLBACK_INTERFACE
747 uint8_t
748 wfall_send(const uip_lladdr_t *lladdr)
749 {
750 #if FALLBACK_HAS_ETHERNET_HEADERS
751  //make room for ethernet header
752 //{int i;printf("\n");for (i=0;i<uip_len;i++) printf("%02x ",*(char*)(uip_buf+i));printf("\n");}
753 {int i;for(i=uip_len;i>=0;--i) *(char *)(uip_buf+i+14) = *(char *)(uip_buf+i);}
754 //{int i;printf("\n");for (i=0;i<uip_len;i++) printf("%02x ",*(char*)(uip_buf+i));printf("\n");}
755 #endif
756  if(lladdr == NULL) {
757 /* the dest must be multicast*/
758  (&BUF->dest)->addr[0] = 0x33;
759  (&BUF->dest)->addr[1] = 0x33;
760  (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
761  (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
762  (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
763  (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
764  } else {
765  memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
766  }
767  memcpy(&BUF->src, &uip_lladdr, UIP_LLADDR_LEN);
768  PRINTF("FUT: %u\n", uip_len);
769  PRINTF("Srcf= %02x %02x %02x %02x %02x %02x",(&BUF->src)->addr[0],(&BUF->src)->addr[1],(&BUF->src)->addr[2],(&BUF->src)->addr[3],(&BUF->src)->addr[4],(&BUF->src)->addr[5]);
770  PRINTF(" Dstf= %02x %02x %02x %02x %02x %02x",(&BUF->dest)->addr[0],(&BUF->dest)->addr[1],(&BUF->dest)->addr[2],(&BUF->dest)->addr[3],(&BUF->dest)->addr[4],(&BUF->dest)->addr[5]);
771  PRINTF(" Typef=%04x\n",BUF->type);
772  BUF->type = UIP_HTONS(UIP_ETHTYPE_IPV6);
773  uip_len += sizeof(struct uip_eth_hdr);
774  if(pcap_sendpacket(pfall, uip_buf, uip_len) == -1) {
775  error_exit("error on fallback send\n");
776  }
777 return 0;
778 }
779 #endif
780 #else /* UIP_CONF_IPV6 */
781 void
782 wpcap_send(void)
783 {
784  /* if(rand() % 1000 > 900) {
785  printf("Drop\n");
786  return;
787  } else {
788  printf("Send\n");
789  }*/
790 
791  if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
792  error_exit("error on send\n");
793  }
794 }
795 #endif /* UIP_CONF_IPV6 */
796 /*---------------------------------------------------------------------------*/
797 void
798 wpcap_exit(void)
799 {
800  FreeLibrary(wpcap);
801 }
802 /*---------------------------------------------------------------------------*/
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
#define uip_setethaddr(eaddr)
Specifiy the Ethernet MAC address.
Definition: uip_arp.h:131
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
void __attribute__((interrupt))
This ISR handles most of the business interacting with the 1-wire bus.
Definition: onewire.c:174
#define NULL
The null pointer.
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:173
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
The Ethernet header.
Definition: uip_arp.h:60
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
802.3 address
Definition: uip.h:135