Contiki 2.5
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 #if UIP_CONF_IPV6
63 #include <ws2tcpip.h>
64 struct in6_addr addr6;
65 char addr6str[64];
66 /*---------------------------------------------------------------------------*/
67 uint8_t
68 issame_ip6addr(struct in6_addr addr1, struct in6_addr addr2)
69 {
70  return ((addr1.s6_addr32[0]==addr2.s6_addr32[0]) &&
71  (addr1.s6_addr32[1]==addr2.s6_addr32[1]) &&
72  (addr1.s6_addr32[2]==addr2.s6_addr32[2]) &&
73  (addr1.s6_addr32[3]==addr2.s6_addr32[3]) );
74 }
75 /*---------------------------------------------------------------------------*/
76 uint8_t
77 iszero_ip6addr(struct in6_addr addr)
78 {
79  return ((addr.s6_addr32[0]==0) &&
80  (addr.s6_addr32[1]==0) &&
81  (addr.s6_addr32[2]==0) &&
82  (addr.s6_addr32[3]==0) );
83 }
84 /*---------------------------------------------------------------------------*/
85 uint8_t
86 sprint_ip6addr(struct in6_addr addr, char * result)
87 {
88  unsigned char i = 0;
89  unsigned char zerocnt = 0;
90  unsigned char numprinted = 0;
91  char * starting = result;
92 
93  *result++='[';
94  while (numprinted < 8) {
95  if ((addr.s6_addr16[i] == 0) && (zerocnt == 0)) {
96  while(addr.s6_addr16[zerocnt + i] == 0) zerocnt++;
97  if (zerocnt == 1) {
98  *result++ = '0';
99  numprinted++;
100  break;
101  }
102  i += zerocnt;
103  numprinted += zerocnt;
104  } else {
105  result += sprintf(result, "%x", (unsigned int) uip_ntohs(addr.s6_addr16[i]));
106  i++;
107  numprinted++;
108  }
109  if (numprinted != 8) *result++ = ':';
110  }
111  *result++=']';
112  *result=0;
113  return (result - starting);
114 }
115 
116 #endif /* UIP_CONF_IPV6 */
117 
118 
119 #ifdef __CYGWIN__
120 __attribute__((dllimport)) extern char **__argv[];
121 #endif /* __CYGWIN__ */
122 
123 struct pcap;
124 
125 struct pcap_if {
126  struct pcap_if *next;
127  char *name;
128  char *description;
129  struct pcap_addr {
130  struct pcap_addr *next;
131  struct sockaddr *addr;
132  struct sockaddr *netmask;
133  struct sockaddr *broadaddr;
134  struct sockaddr *dstaddr;
135  } *addresses;
136  DWORD flags;
137 };
138 
139 struct pcap_pkthdr {
140  struct timeval ts;
141  DWORD caplen;
142  DWORD len;
143 };
144 
145 HMODULE wpcap;
146 
147 static struct pcap *pcap;
148 
149 /* uip_ethaddr is defined in uip.c. It is not used in uip6.c.
150  * If needed for some purpose it can be defined here
151  */
152 #if UIP_CONF_IPV6
153 //struct uip_eth_addr uip_ethaddr;
154 #endif
155 
156 static int (* pcap_findalldevs)(struct pcap_if **, char *);
157 static struct pcap *(* pcap_open_live)(char *, int, int, int, char *);
158 static int (* pcap_next_ex)(struct pcap *, struct pcap_pkthdr **, unsigned char **);
159 static int (* pcap_sendpacket)(struct pcap *, unsigned char *, int);
160 
161 #ifdef UIP_FALLBACK_INTERFACE
162 static struct pcap *pfall;
163 struct in_addr addrfall;
164 #if UIP_CONF_IPV6
165 struct in_addr6 addrfall6;
166 #endif
167 
168 /*---------------------------------------------------------------------------*/
169 static void
170 init(void)
171 {
172 /* Nothing to do here */
173 }
174 /*---------------------------------------------------------------------------*/
175 u8_t wfall_send(uip_lladdr_t *lladdr);
176 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
177 static void
178 output(void)
179 {
180 #if 0
181  if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
182  /* Do not bounce packets back to fallback if the packet was received from it */
183  PRINTF("fallback: Destination off-link but no route src=");
184  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
185  PRINTF(" dst=");
186  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
187  PRINTF("\n");
188  } else {
189 #endif
190  PRINTF("FUT: %u\n", uip_len);
191  wfall_send(0);
192 }
193 
194 const struct uip_fallback_interface rpl_interface = {
195  init, output
196 };
197 
198 #endif
199 
200 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
201 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
202 
203 /*---------------------------------------------------------------------------*/
204 static void
205 error_exit(char *message)
206 {
207  printf("error_exit: %s", message);
208  exit(EXIT_FAILURE);
209 }
210 /*---------------------------------------------------------------------------*/
211 static void
212 set_ethaddr(struct in_addr addr)
213 {
214  PIP_ADAPTER_ADDRESSES adapters;
215  ULONG size = 0;
216 
217  if(GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
218  GAA_FLAG_SKIP_MULTICAST |
219  GAA_FLAG_SKIP_DNS_SERVER,
220  NULL, NULL, &size) != ERROR_BUFFER_OVERFLOW) {
221  error_exit("error on access to adapter list size\n");
222  }
223  adapters = alloca(size);
224  if(GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
225  GAA_FLAG_SKIP_MULTICAST |
226  GAA_FLAG_SKIP_DNS_SERVER,
227  NULL, adapters, &size) != ERROR_SUCCESS) {
228  error_exit("error on access to adapter list\n");
229  }
230 
231  while(adapters != NULL) {
232 
233  char buffer[256];
234  WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
235  buffer, sizeof(buffer), NULL, NULL);
236  log_message("set_ethaddr: found adapter: ", buffer);
237 
238  if(adapters->FirstUnicastAddress != NULL &&
239  adapters->FirstUnicastAddress->Address.lpSockaddr != NULL &&
240  adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET) {
241 
242  struct in_addr adapter_addr;
243  adapter_addr = ((struct sockaddr_in *)adapters->FirstUnicastAddress->Address.lpSockaddr)->sin_addr;
244  log_message("set_ethaddr: with address: ", inet_ntoa(adapter_addr));
245 
246  if(adapter_addr.s_addr == addr.s_addr) {
247  if(adapters->PhysicalAddressLength != 6) {
248  error_exit("ip addr specified on cmdline does not belong to an ethernet card\n");
249  }
250  wsprintf(buffer, "%02X-%02X-%02X-%02X-%02X-%02X",
251  adapters->PhysicalAddress[0], adapters->PhysicalAddress[1],
252  adapters->PhysicalAddress[2], adapters->PhysicalAddress[3],
253  adapters->PhysicalAddress[4], adapters->PhysicalAddress[5]);
254  log_message("set_ethaddr: ethernetaddr: ", buffer);
255 #if UIP_CONF_IPV6
256 // int i;for (i=0;i<6;i++) uip_ethaddr.addr[i] = adapters->PhysicalAddress[i];
257 #else
258  uip_setethaddr((*(struct uip_eth_addr *)adapters->PhysicalAddress));
259 #endif
260  break;
261  }
262  }
263  adapters = adapters->Next;
264  }
265 
266  if(adapters == NULL) {
267  error_exit("no adapter found with ip addr specified on cmdline\n");
268  }
269 }
270 
271 #if UIP_CONF_IPV6
272 /*---------------------------------------------------------------------------*/
273 static void
274 set_ethaddr6(struct in_addr6 addr)
275 {
276  PIP_ADAPTER_ADDRESSES adapters;
277  ULONG size = 0;
278 
279  if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_SKIP_ANYCAST |
280  GAA_FLAG_SKIP_MULTICAST |
281  GAA_FLAG_SKIP_DNS_SERVER,
282  NULL, NULL, &size) != ERROR_BUFFER_OVERFLOW) {
283  error_exit("error on access to adapter list size\n");
284  }
285  adapters = alloca(size);
286  if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_SKIP_ANYCAST |
287  GAA_FLAG_SKIP_MULTICAST |
288  GAA_FLAG_SKIP_DNS_SERVER,
289  NULL, adapters, &size) != ERROR_SUCCESS) {
290  error_exit("error on access to adapter list\n");
291  }
292 
293  while(adapters != NULL) {
294 
295  char buffer[256];
296  WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
297  buffer, sizeof(buffer), NULL, NULL);
298  log_message("set_ethaddr: found adapter: ", buffer);
299 
300  if(adapters->FirstUnicastAddress != NULL &&
301  adapters->FirstUnicastAddress->Address.lpSockaddr != NULL &&
302  adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET6) {
303 
304  struct in_addr6 adapter_addr;
305  adapter_addr = ((struct sockaddr_in6 *)adapters->FirstUnicastAddress->Address.lpSockaddr)->sin6_addr;
306  sprint_ip6addr(adapter_addr, addr6str);
307  log_message("set_ethaddr: with ipv6 address: : ", addr6str);
308  if(issame_ip6addr(adapter_addr,addr6)) {
309  if(adapters->PhysicalAddressLength != 6) {
310  error_exit("ip addr specified on cmdline does not belong to an ethernet card\n");
311  }
312  wsprintf(buffer, "%02X-%02X-%02X-%02X-%02X-%02X",
313  adapters->PhysicalAddress[0], adapters->PhysicalAddress[1],
314  adapters->PhysicalAddress[2], adapters->PhysicalAddress[3],
315  adapters->PhysicalAddress[4], adapters->PhysicalAddress[5]);
316  log_message("set_ethaddr: ethernetaddr: ", buffer);
317 #if UIP_CONF_IPV6
318 // int i;for (i=0;i<6;i++) uip_ethaddr.addr[i] = adapters->PhysicalAddress[i]; //does this need doing?
319 #else
320  uip_setethaddr((*(struct uip_eth_addr *)adapters->PhysicalAddress));
321 #endif
322  break;
323  }
324  }
325  adapters = adapters->Next;
326  }
327 
328  if(adapters == NULL) {
329  error_exit("no adapter found with ip addr specified on cmdline\n");
330  }
331 }
332 #endif
333 /*---------------------------------------------------------------------------*/
334 static void
335 init_pcap(struct in_addr addr)
336 {
337  struct pcap_if *interfaces;
338  struct pcap_addr *paddr;
339  char error[256];
340 
341  if(pcap_findalldevs(&interfaces, error) == -1) {
342  error_exit(error);
343  }
344 
345  while(interfaces != NULL) {
346  log_message("init_pcap: found interface: ", interfaces->description);
347 
348  if(interfaces->addresses != NULL) {
349  for(paddr = interfaces->addresses;
350  paddr != NULL;
351  paddr = paddr->next) {
352  if(paddr->addr != NULL && paddr->addr->sa_family == AF_INET) {
353  struct in_addr interface_addr;
354  interface_addr = ((struct sockaddr_in *)paddr->addr)->sin_addr;
355  log_message("init_pcap: with address: ", inet_ntoa(interface_addr));
356 
357  if(interface_addr.s_addr == addr.s_addr) {
358  pcap = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
359  if(pcap == NULL) {
360  error_exit(error);
361  }
362 #ifdef UIP_FALLBACK_INTERFACE
363  log_message("init_pcap: Opened as primary interface","");
364 #endif
365 // pcap_setdirection(PCAP_D_IN); //Not implemented in windows yet?
366  set_ethaddr(addr);
367 
368 #ifdef UIP_FALLBACK_INTERFACE
369  }
370  if (pfall && pcap) return;
371  if(interface_addr.s_addr == addrfall.s_addr) {
372  pfall = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
373  if(pfall == NULL) {
374  error_exit(error);
375  }
376  log_message("init_pcap: Opened as fallback interface","");
377  if (pcap) return;
378  }
379 #else
380  return;
381  }
382 #endif
383 
384 #if UIP_CONF_IPV6
385 
386  } else if(paddr->addr != NULL && paddr->addr->sa_family == AF_INET6) {
387  struct in6_addr interface_addr;
388  interface_addr = ((struct sockaddr_in6 *)paddr->addr)->sin6_addr;
389 
390  sprint_ip6addr(interface_addr, addr6str);
391  log_message("init_pcap: with ipv6 address: ", addr6str);
392 
393  if (issame_ip6addr(interface_addr, addr6)) {
394  pcap = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
395  if(pcap == NULL) {
396  error_exit(error);
397  }
398 #ifdef UIP_FALLBACK_INTERFACE
399  log_message("init_pcap: Opened as primary interface","");
400 #endif
401  set_ethaddr6(addr6);
402 // pcap_setdirection(PCAP_D_IN); //Not implemented in windows yet?
403 #ifdef UIP_FALLBACK_INTERFACE
404  }
405  if (pfall && pcap) return; //exit when we have both interfaces
406 
407  if (issame_ip6addr(interface_addr, addrfall6)) {
408  pfall = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
409  if(pfall == NULL) {
410  error_exit(error);
411  }
412  log_message("init_pcap: Opened as fallback interface","");
413  if (pcap) return;
414  }
415 #else
416  return;
417  }
418 #endif
419 #endif /* UIP_CONF_IPV6 */
420  }
421  }
422  }
423  interfaces = interfaces->next;
424 
425  }
426 
427  if(interfaces == NULL) {
428  error_exit("no interface found with specified ip address\n");
429  }
430 }
431 /*---------------------------------------------------------------------------*/
432 void
433 wpcap_init(void)
434 {
435  struct in_addr addr;
436  addr.s_addr = INADDR_NONE; //255.255.255.255
437 #ifdef UIP_FALLBACK_INTERFACE
438  addrfall.s_addr = INADDR_NONE;
439 #endif
440 
441  /* Pick up possible ip addresses from command line */
442 #ifdef __CYGWIN__
443  if ((*__argv)[1]) {
444  addr.s_addr = inet_addr((*__argv)[1]);
445 #if UIP_CONF_IPV6
446  uiplib_ipaddrconv((*__argv)[1],(uip_ipaddr_t*) &addr6.s6_addr);
447 #endif
448 #ifdef UIP_FALLBACK_INTERFACE
449  if ((*__argv)[2]) {
450  addrfall.s_addr = inet_addr((*__argv)[2]);
451 #if UIP_CONF_IPV6
452  uiplib_ipaddrconv((*__argv)[2],(uip_ipaddr_t*) &addrfall6.s6_addr);
453 #endif
454  }
455 #endif
456  }
457 
458 #else /* __CYGWIN__ */
459 /* VC++ build on win32 platform. Currently the platform has no ipv6 support */
460  addr.s_addr = inet_addr(__argv[1]);
461 #if UIP_CONF_IPV6
462  if((__argv)[1])
463  uiplib_ipaddrconv((__argv)[1],(uip_ipaddr_t*) &addr6.s6_addr);
464 #endif
465 #ifdef UIP_FALLBACK_INTERFACE
466  addrfall.s_addr = inet_addr(__argv[2]);
467 #if UIP_CONF_IPV6
468  if((__argv)[2])
469  uiplib_ipaddrconv((__argv)[2],(uip_ipaddr_t*) &addrfall6.s6_addr);
470 #endif
471 #endif
472 #endif /* __CYGWIN__ */
473 
474 #if DEBUG
475  log_message("wpcap_init:Passed ipv4 ", inet_ntoa(addr));
476  sprint_ip6addr(addr6, addr6str);
477  log_message("wpcap_init:Passed ipv6 ", addr6str);
478 #ifdef UIP_FALLBACK_INTERFACE
479  log_message("wpcap_init:Passed fallback ipv4 ", inet_ntoa(addrfall));
480  sprint_ip6addr(addrfall6, addr6str);
481  log_message("wpcap_init:Passed fallback ipv6 ", addr6str);
482 #endif
483 #endif
484 
485  /* Use build defaults if not enough addresses passed */
486 #if UIP_CONF_IPV6
487 
488 #ifdef UIP_FALLBACK_INTERFACE
489  if(addrfall.s_addr == INADDR_NONE) {
490  if(iszero_ip6addr(addrfall6)) {
491 #ifdef WPCAP_WPCAP_FALLBACK_ADDRESS
492  addrfall.s_addr = inet_addr(WPCAP_FALLBACK_ADDRESS);
493 // if(addrfall.s_addr == INADDR_NONE) { //use ipv6 if contiki-conf.h override
494  uiplib_ipaddrconv(WPCAP_FALLBACK_ADDRESS,(uip_ipaddr_t*) &addrfall6.s6_addr);
495 // }
496 #else
497 // addrfall.s_addr = inet_addr("10.2.10.10");
498  uiplib_ipaddrconv("bbbb::1",(uip_ipaddr_t*) &addrfall6.s6_addr);
499 #endif
500  }
501  }
502 #endif
503 
504  if(addr.s_addr == INADDR_NONE) {
505  if(iszero_ip6addr(addr6)) {
506 #ifdef WPCAP_INTERFACE_ADDRESS
507  addr.s_addr = inet_addr(WPCAP_INTERFACE_ADDRESS);
508 // if(addr.s_addr == INADDR_NONE) {
509  uiplib_ipaddrconv(WPCAP_INTERFACE_ADDRESS,(uip_ipaddr_t*) &addr6.s6_addr);
510 // }
511 #else
512  addr.s_addr = inet_addr("10.10.10.10"); //prefer ipv4 default for legacy compatibility
513 // uiplib_ipaddrconv("aaaa::1",(uip_ipaddr_t*) &addr6.s6_addr);
514 #endif
515 
516 #ifdef UIP_FALLBACK_INTERFACE
517  log_message("usage: <program> <ip addr of interface> <ip addr of fallback>\n","");
518  if(addr.s_addr != INADDR_NONE) {
519  log_message("-->I'll try interface address ", inet_ntoa(addr));
520  } else {
521  sprint_ip6addr(addr6, addr6str);
522  log_message("-->I'll try interface address ", addr6str);
523  }
524  if(addrfall.s_addr != INADDR_NONE) {
525  log_message("--> and fallback address ", inet_ntoa(addrfall));
526  } else {
527  sprint_ip6addr(addrfall6, addr6str);
528  log_message("--> and fallback address ", addr6str);
529  }
530 #else
531  if(addr.s_addr != INADDR_NONE) {
532  log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", inet_ntoa(addr));
533  } else {
534  sprint_ip6addr(addr6, addr6str);
535  log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", addr6str);
536  }
537 #endif
538  }
539  }
540 #else /* ip4 build */
541  if(addr.s_addr == INADDR_NONE) {
542 #ifdef WPCAP_INTERFACE_ADDRESS
543  addr.s_addr = inet_addr(WPCAP_INTERFACE_ADDRESS);
544 #else
545  addr.s_addr = inet_addr("10.10.10.10");
546 #endif
547  log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", inet_ntoa(addr));
548  }
549 #endif /* UIP_CONF_IPV6 */
550 
551 #if DEBUG
552  log_message("wpcap_init:Using ipv4 ", inet_ntoa(addr));
553  sprint_ip6addr(addr6, addr6str);
554  log_message("wpcap_init:Using ipv6 ", addr6str);
555 #ifdef UIP_FALLBACK_INTERFACE
556  log_message("wpcap_init:Using fallback ipv4 ", inet_ntoa(addrfall));
557  sprint_ip6addr(addrfall6, addr6str);
558  log_message("wpcap_init:Using fallback ipv6 ", addr6str);
559 #endif
560 #endif
561 
562  // log_message("wpcap_init:cmdline address: ", inet_ntoa(addr));
563 
564 
565  wpcap = LoadLibrary("wpcap.dll");
566  pcap_findalldevs = (int (*)(struct pcap_if **, char *))
567  GetProcAddress(wpcap, "pcap_findalldevs");
568  pcap_open_live = (struct pcap *(*)(char *, int, int, int, char *))
569  GetProcAddress(wpcap, "pcap_open_live");
570  pcap_next_ex = (int (*)(struct pcap *, struct pcap_pkthdr **, unsigned char **))
571  GetProcAddress(wpcap, "pcap_next_ex");
572  pcap_sendpacket = (int (*)(struct pcap *, unsigned char *, int))
573  GetProcAddress(wpcap, "pcap_sendpacket");
574 
575  if(pcap_findalldevs == NULL || pcap_open_live == NULL ||
576  pcap_next_ex == NULL || pcap_sendpacket == NULL) {
577  error_exit("error on access to winpcap library\n");
578  }
579 
580  init_pcap(addr);
581 
582 }
583 
584 /*---------------------------------------------------------------------------*/
585 u16_t
586 wpcap_poll(void)
587 {
588  struct pcap_pkthdr *packet_header;
589  unsigned char *packet;
590 
591  switch(pcap_next_ex(pcap, &packet_header, &packet)) {
592  case -1:
593  error_exit("error on poll\n");
594  case 0:
595  return 0;
596  }
597 
598 #if UIP_CONF_IPV6
599 /* Since pcap_setdirection(PCAP_D_IN) is not implemented in winpcap all outgoing packets
600  * will be echoed back. The stack will ignore any packets not addressed to it, but initial
601  * ipv6 neighbor solicitations are addressed to everyone and the echoed NS sent on startup
602  * would be processed as a conflicting NS race which would cause a stack shutdown.
603  * So discard all packets with our source address (packet starts destaddr, srcaddr, ...)
604  *
605  */
606  int i;
607  for (i=0;i<UIP_LLADDR_LEN;i++) if (*(packet+UIP_LLADDR_LEN+i)!=uip_lladdr.addr[i]) break;
608  if (i==UIP_LLADDR_LEN) {
609  PRINTF("SIN: Discarding echoed packet\n");
610  return 0;
611  }
612 
613 /* To implement multihop, ignore packets to us from specified source macs
614  */
615 // printf("ethtype=%x %x",*(packet+2*UIP_LLADDR_LEN),*(packet+2*UIP_LLADDR_LEN+1));
616 // printf("hopcount=%u",*(packet+21));
617 #if 0
618  if (0
619 // || (*(packet+11) ==0x1) //20 ignores router
620 // || (*(packet+11) ==0x10)
621  || (*(packet+11) ==0x20) //router ignores 20
622  ) {
623  printf("i%x",*(packet+11));
624  return 0;
625  }
626 /* If we are not the recipient, ignore packets from other RPL nodes */
627  if (0
628  || (*(packet+5) !=0x1) //router
629 // || (*(packet+11) ==0x10)
630  // || (*(packet+11) ==0x20) //router ignores 20
631  ) {
632  printf("r%x",*(packet+11));
633  return 0;
634  }
635 #endif
636 
637 #endif /* UIP_CONF_IPV6 */
638 
639  if(packet_header->caplen > UIP_BUFSIZE) {
640  return 0;
641  }
642 // PRINTF("SIN: %lu\n", packet_header->caplen);
643  CopyMemory(uip_buf, packet, packet_header->caplen);
644  return (u16_t)packet_header->caplen;
645 
646 }
647 
648 #ifdef UIP_FALLBACK_INTERFACE
649 u16_t
650 wfall_poll(void)
651 {
652  struct pcap_pkthdr *packet_header;
653  unsigned char *packet;
654 
655  switch(pcap_next_ex(pfall, &packet_header, &packet)) {
656  case -1:
657  error_exit("error on fallback poll\n");
658  case 0:
659  return 0;
660  }
661 #if UIP_CONF_IPV6
662 /* Since pcap_setdirection(PCAP_D_IN) is not implemented in winpcap all outgoing packets
663  * will be echoed back. The stack will ignore any packets not addressed to it, but initial
664  * ipv6 neighbor solicitations are addressed to everyone and the echoed NS sent on startup
665  * would be processed as a conflicting NS race which would cause a stack shutdown.
666  * So discard all packets with our source address (packet starts destaddr, srcaddr, ...)
667  *
668  */
669  int i;
670  for (i=0;i<UIP_LLADDR_LEN;i++) if (*(packet+UIP_LLADDR_LEN+i)!=uip_lladdr.addr[i]) break;
671  if (i==UIP_LLADDR_LEN) {
672  PRINTF("Discarding echoed packet\n");
673  return 0;
674  }
675 #endif /* UIP_CONF_IPV6 */
676 
677  if(packet_header->caplen > UIP_BUFSIZE) {
678  return 0;
679  }
680  PRINTF("FIN: %lu\n", packet_header->caplen);
681  CopyMemory(uip_buf, packet, packet_header->caplen);
682  return (u16_t)packet_header->caplen;
683 
684 }
685 
686 #endif
687 
688 /*---------------------------------------------------------------------------*/
689 #if UIP_CONF_IPV6
690 u8_t
691 wpcap_send(uip_lladdr_t *lladdr)
692 {
693  if(lladdr == NULL) {
694 /* the dest must be multicast*/
695  (&BUF->dest)->addr[0] = 0x33;
696  (&BUF->dest)->addr[1] = 0x33;
697  (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
698  (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
699  (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
700  (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
701  } else {
702  //when fallback used this gets ptr to lladdr of all zeroes on forwarded packets, turn them into multicast(?)
703  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)) {
704  (&BUF->dest)->addr[0] = 0x33;
705  (&BUF->dest)->addr[1] = 0x33;
706  (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
707  (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
708  (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
709  (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
710  } else {
711  memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
712  }
713  }
714  memcpy(&BUF->src, &uip_lladdr, UIP_LLADDR_LEN);
715  PRINTF("SUT: %u\n", uip_len);
716  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]);
717  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]);
718  PRINTF(" Type=%04x\n",BUF->type);
719  BUF->type = UIP_HTONS(UIP_ETHTYPE_IPV6);
720  uip_len += sizeof(struct uip_eth_hdr);
721  if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
722  error_exit("error on send\n");
723  }
724 return 0;
725 }
726 #ifdef UIP_FALLBACK_INTERFACE
727 u8_t
728 wfall_send(uip_lladdr_t *lladdr)
729 {
730  if(lladdr == NULL) {
731 /* the dest must be multicast*/
732  (&BUF->dest)->addr[0] = 0x33;
733  (&BUF->dest)->addr[1] = 0x33;
734  (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
735  (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
736  (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
737  (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
738  } else {
739  memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
740  }
741  memcpy(&BUF->src, &uip_lladdr, UIP_LLADDR_LEN);
742  PRINTF("FUT: %u\n", uip_len);
743  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]);
744  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]);
745  PRINTF(" Typef=%04x\n",BUF->type);
746  BUF->type = UIP_HTONS(UIP_ETHTYPE_IPV6);
747  uip_len += sizeof(struct uip_eth_hdr);
748  if(pcap_sendpacket(pfall, uip_buf, uip_len) == -1) {
749  error_exit("error on fallback send\n");
750  }
751 return 0;
752 }
753 #endif
754 #else /* UIP_CONF_IPV6 */
755 void
756 wpcap_send(void)
757 {
758  /* if(rand() % 1000 > 900) {
759  printf("Drop\n");
760  return;
761  } else {
762  printf("Send\n");
763  }*/
764 
765  if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
766  error_exit("error on send\n");
767  }
768 }
769 #endif /* UIP_CONF_IPV6 */
770 /*---------------------------------------------------------------------------*/
771 void
772 wpcap_exit(void)
773 {
774  FreeLibrary(wpcap);
775 }
776 /*---------------------------------------------------------------------------*/