35 #define WIN32_LEAN_AND_MEAN
36 #define _WIN32_WINNT 0x0501
52 #define PRINTF(...) printf(__VA_ARGS__)
57 #include "contiki-net.h"
60 #include "net/wpcap.h"
64 struct in6_addr addr6;
68 issame_ip6addr(
struct in6_addr addr1,
struct in6_addr addr2)
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]) );
77 iszero_ip6addr(
struct in6_addr addr)
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) );
86 sprint_ip6addr(
struct in6_addr addr,
char * result)
89 unsigned char zerocnt = 0;
90 unsigned char numprinted = 0;
91 char * starting = result;
94 while (numprinted < 8) {
95 if ((addr.s6_addr16[i] == 0) && (zerocnt == 0)) {
96 while(addr.s6_addr16[zerocnt + i] == 0) zerocnt++;
103 numprinted += zerocnt;
105 result += sprintf(result,
"%x", (
unsigned int) uip_ntohs(addr.s6_addr16[i]));
109 if (numprinted != 8) *result++ =
':';
113 return (result - starting);
126 struct pcap_if *
next;
130 struct pcap_addr *
next;
131 struct sockaddr *addr;
132 struct sockaddr *netmask;
133 struct sockaddr *broadaddr;
134 struct sockaddr *dstaddr;
147 static struct pcap *pcap;
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);
161 #ifdef UIP_FALLBACK_INTERFACE
162 static struct pcap *pfall;
163 struct in_addr addrfall;
165 struct in_addr6 addrfall6;
176 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
183 PRINTF(
"fallback: Destination off-link but no route src=");
194 const struct uip_fallback_interface rpl_interface = {
200 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
201 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
205 error_exit(
char *message)
207 printf(
"error_exit: %s", message);
212 set_ethaddr(
struct in_addr addr)
214 PIP_ADAPTER_ADDRESSES adapters;
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");
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");
231 while(adapters !=
NULL) {
234 WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
235 buffer,
sizeof(buffer),
NULL,
NULL);
236 log_message(
"set_ethaddr: found adapter: ", buffer);
238 if(adapters->FirstUnicastAddress !=
NULL &&
239 adapters->FirstUnicastAddress->Address.lpSockaddr !=
NULL &&
240 adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET) {
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));
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");
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);
263 adapters = adapters->Next;
266 if(adapters ==
NULL) {
267 error_exit(
"no adapter found with ip addr specified on cmdline\n");
274 set_ethaddr6(
struct in_addr6 addr)
276 PIP_ADAPTER_ADDRESSES adapters;
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");
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");
293 while(adapters !=
NULL) {
296 WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
297 buffer,
sizeof(buffer),
NULL,
NULL);
298 log_message(
"set_ethaddr: found adapter: ", buffer);
300 if(adapters->FirstUnicastAddress !=
NULL &&
301 adapters->FirstUnicastAddress->Address.lpSockaddr !=
NULL &&
302 adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET6) {
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");
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);
325 adapters = adapters->Next;
328 if(adapters ==
NULL) {
329 error_exit(
"no adapter found with ip addr specified on cmdline\n");
335 init_pcap(
struct in_addr addr)
337 struct pcap_if *interfaces;
338 struct pcap_addr *paddr;
341 if(pcap_findalldevs(&interfaces, error) == -1) {
345 while(interfaces !=
NULL) {
346 log_message(
"init_pcap: found interface: ", interfaces->description);
348 if(interfaces->addresses !=
NULL) {
349 for(paddr = interfaces->addresses;
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));
357 if(interface_addr.s_addr == addr.s_addr) {
358 pcap = pcap_open_live(interfaces->name,
UIP_BUFSIZE, 0, -1, error);
362 #ifdef UIP_FALLBACK_INTERFACE
363 log_message(
"init_pcap: Opened as primary interface",
"");
368 #ifdef UIP_FALLBACK_INTERFACE
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);
376 log_message(
"init_pcap: Opened as fallback interface",
"");
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;
390 sprint_ip6addr(interface_addr, addr6str);
391 log_message(
"init_pcap: with ipv6 address: ", addr6str);
393 if (issame_ip6addr(interface_addr, addr6)) {
394 pcap = pcap_open_live(interfaces->name,
UIP_BUFSIZE, 0, -1, error);
398 #ifdef UIP_FALLBACK_INTERFACE
399 log_message(
"init_pcap: Opened as primary interface",
"");
403 #ifdef UIP_FALLBACK_INTERFACE
405 if (pfall && pcap)
return;
407 if (issame_ip6addr(interface_addr, addrfall6)) {
408 pfall = pcap_open_live(interfaces->name,
UIP_BUFSIZE, 0, -1, error);
412 log_message(
"init_pcap: Opened as fallback interface",
"");
423 interfaces = interfaces->next;
427 if(interfaces ==
NULL) {
428 error_exit(
"no interface found with specified ip address\n");
436 addr.s_addr = INADDR_NONE;
437 #ifdef UIP_FALLBACK_INTERFACE
438 addrfall.s_addr = INADDR_NONE;
444 addr.s_addr = inet_addr((*__argv)[1]);
448 #ifdef UIP_FALLBACK_INTERFACE
450 addrfall.s_addr = inet_addr((*__argv)[2]);
460 addr.s_addr = inet_addr(__argv[1]);
465 #ifdef UIP_FALLBACK_INTERFACE
466 addrfall.s_addr = inet_addr(__argv[2]);
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);
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);
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);
512 addr.s_addr = inet_addr(
"10.10.10.10");
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));
521 sprint_ip6addr(addr6, addr6str);
522 log_message(
"-->I'll try interface address ", addr6str);
524 if(addrfall.s_addr != INADDR_NONE) {
525 log_message(
"--> and fallback address ", inet_ntoa(addrfall));
527 sprint_ip6addr(addrfall6, addr6str);
528 log_message(
"--> and fallback address ", addr6str);
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));
534 sprint_ip6addr(addr6, addr6str);
535 log_message(
"usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", addr6str);
541 if(addr.s_addr == INADDR_NONE) {
542 #ifdef WPCAP_INTERFACE_ADDRESS
543 addr.s_addr = inet_addr(WPCAP_INTERFACE_ADDRESS);
545 addr.s_addr = inet_addr(
"10.10.10.10");
547 log_message(
"usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", inet_ntoa(addr));
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);
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");
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");
588 struct pcap_pkthdr *packet_header;
589 unsigned char *packet;
591 switch(pcap_next_ex(pcap, &packet_header, &packet)) {
593 error_exit(
"error on poll\n");
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");
621 || (*(packet+11) ==0x20)
623 printf(
"i%x",*(packet+11));
628 || (*(packet+5) !=0x1)
632 printf(
"r%x",*(packet+11));
643 CopyMemory(uip_buf, packet, packet_header->caplen);
644 return (u16_t)packet_header->caplen;
648 #ifdef UIP_FALLBACK_INTERFACE
652 struct pcap_pkthdr *packet_header;
653 unsigned char *packet;
655 switch(pcap_next_ex(pfall, &packet_header, &packet)) {
657 error_exit(
"error on fallback poll\n");
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");
680 PRINTF(
"FIN: %lu\n", packet_header->caplen);
681 CopyMemory(uip_buf, packet, packet_header->caplen);
682 return (u16_t)packet_header->caplen;
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];
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];
711 memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
714 memcpy(&BUF->src, &
uip_lladdr, UIP_LLADDR_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);
721 if(pcap_sendpacket(pcap, uip_buf,
uip_len) == -1) {
722 error_exit(
"error on send\n");
726 #ifdef UIP_FALLBACK_INTERFACE
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];
739 memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
741 memcpy(&BUF->src, &
uip_lladdr, UIP_LLADDR_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);
748 if(pcap_sendpacket(pfall, uip_buf,
uip_len) == -1) {
749 error_exit(
"error on fallback send\n");
765 if(pcap_sendpacket(pcap, uip_buf,
uip_len) == -1) {
766 error_exit(
"error on send\n");