00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #define WIN32_LEAN_AND_MEAN
00037 #define _WIN32_WINNT 0x0501
00038 #include <windows.h>
00039 #include <winsock2.h>
00040 #include <iphlpapi.h>
00041
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #ifdef __CYGWIN__
00045 #include <alloca.h>
00046 #else
00047 #include <malloc.h>
00048 #endif
00049
00050 #define DEBUG 0
00051 #if DEBUG
00052 #define PRINTF(...) printf(__VA_ARGS__)
00053 #else
00054 #define PRINTF(...)
00055 #endif
00056
00057 #include "contiki-net.h"
00058 #include "sys/log.h"
00059
00060 #include "net/wpcap.h"
00061
00062 #ifdef __CYGWIN__
00063 __attribute__((dllimport)) extern char **__argv[];
00064 #endif
00065
00066 struct pcap;
00067
00068 struct pcap_if {
00069 struct pcap_if *next;
00070 char *name;
00071 char *description;
00072 struct pcap_addr {
00073 struct pcap_addr *next;
00074 struct sockaddr *addr;
00075 struct sockaddr *netmask;
00076 struct sockaddr *broadaddr;
00077 struct sockaddr *dstaddr;
00078 } *addresses;
00079 DWORD flags;
00080 };
00081
00082 struct pcap_pkthdr {
00083 struct timeval ts;
00084 DWORD caplen;
00085 DWORD len;
00086 };
00087
00088 HMODULE wpcap;
00089
00090 static struct pcap *pcap;
00091
00092
00093
00094
00095 #if UIP_CONF_IPV6
00096
00097 #endif
00098
00099 static int (* pcap_findalldevs)(struct pcap_if **, char *);
00100 static struct pcap *(* pcap_open_live)(char *, int, int, int, char *);
00101 static int (* pcap_next_ex)(struct pcap *, struct pcap_pkthdr **, unsigned char **);
00102 static int (* pcap_sendpacket)(struct pcap *, unsigned char *, int);
00103
00104 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
00105 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
00106
00107
00108 static void
00109 error_exit(char *message)
00110 {
00111 printf("error_exit: %s", message);
00112 exit(EXIT_FAILURE);
00113 }
00114
00115 static void
00116 init_pcap(struct in_addr addr)
00117 {
00118 struct pcap_if *interfaces;
00119 struct pcap_addr *paddr;
00120 char error[256];
00121
00122 if(pcap_findalldevs(&interfaces, error) == -1) {
00123 error_exit(error);
00124 }
00125
00126 while(interfaces != NULL) {
00127 log_message("init_pcap: found interface: ", interfaces->description);
00128
00129 if(interfaces->addresses != NULL) {
00130 for(paddr = interfaces->addresses;
00131 paddr != NULL;
00132 paddr = paddr->next) {
00133 if(paddr->addr != NULL && paddr->addr->sa_family == AF_INET) {
00134
00135 struct in_addr interface_addr;
00136 interface_addr = ((struct sockaddr_in *)paddr->addr)->sin_addr;
00137 log_message("init_pcap: with address: ", inet_ntoa(interface_addr));
00138
00139 if(interface_addr.s_addr == addr.s_addr) {
00140 pcap = pcap_open_live(interfaces->name, UIP_BUFSIZE, 0, -1, error);
00141 if(pcap == NULL) {
00142 error_exit(error);
00143 }
00144
00145 return;
00146 }
00147 }
00148 }
00149 }
00150 interfaces = interfaces->next;
00151 }
00152
00153 if(interfaces == NULL) {
00154 error_exit("no interface found with specified ip address\n");
00155 }
00156 }
00157
00158 static void
00159 set_ethaddr(struct in_addr addr)
00160 {
00161 PIP_ADAPTER_ADDRESSES adapters;
00162 ULONG size = 0;
00163
00164 if(GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
00165 GAA_FLAG_SKIP_MULTICAST |
00166 GAA_FLAG_SKIP_DNS_SERVER,
00167 NULL, NULL, &size) != ERROR_BUFFER_OVERFLOW) {
00168 error_exit("error on access to adapter list size\n");
00169 }
00170 adapters = alloca(size);
00171 if(GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
00172 GAA_FLAG_SKIP_MULTICAST |
00173 GAA_FLAG_SKIP_DNS_SERVER,
00174 NULL, adapters, &size) != ERROR_SUCCESS) {
00175 error_exit("error on access to adapter list\n");
00176 }
00177
00178 while(adapters != NULL) {
00179
00180 char buffer[256];
00181 WideCharToMultiByte(CP_ACP, 0, adapters->Description, -1,
00182 buffer, sizeof(buffer), NULL, NULL);
00183 log_message("set_ethaddr: found adapter: ", buffer);
00184
00185 if(adapters->FirstUnicastAddress != NULL &&
00186 adapters->FirstUnicastAddress->Address.lpSockaddr != NULL &&
00187 adapters->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET) {
00188
00189 struct in_addr adapter_addr;
00190 adapter_addr = ((struct sockaddr_in *)adapters->FirstUnicastAddress->Address.lpSockaddr)->sin_addr;
00191 log_message("set_ethaddr: with address: ", inet_ntoa(adapter_addr));
00192
00193 if(adapter_addr.s_addr == addr.s_addr) {
00194 if(adapters->PhysicalAddressLength != 6) {
00195 error_exit("ip addr specified on cmdline does not belong to an ethernet card\n");
00196 }
00197 wsprintf(buffer, "%02X-%02X-%02X-%02X-%02X-%02X",
00198 adapters->PhysicalAddress[0], adapters->PhysicalAddress[1],
00199 adapters->PhysicalAddress[2], adapters->PhysicalAddress[3],
00200 adapters->PhysicalAddress[4], adapters->PhysicalAddress[5]);
00201 log_message("set_ethaddr: ethernetaddr: ", buffer);
00202 #if UIP_CONF_IPV6
00203
00204 #else
00205 uip_setethaddr((*(struct uip_eth_addr *)adapters->PhysicalAddress));
00206 #endif
00207 break;
00208 }
00209 }
00210 adapters = adapters->Next;
00211 }
00212
00213 if(adapters == NULL) {
00214 error_exit("no adapter found with ip addr specified on cmdline\n");
00215 }
00216 }
00217
00218 void
00219 wpcap_init(void)
00220 {
00221 struct in_addr addr;
00222
00223 #ifdef __CYGWIN__
00224 addr.s_addr = inet_addr((*__argv)[1]);
00225 #else
00226 addr.s_addr = inet_addr(__argv[1]);
00227 #endif
00228 if(addr.s_addr == INADDR_NONE) {
00229
00230 addr.s_addr = inet_addr("10.10.10.10");
00231 log_message("usage: <program> <ip addr of ethernet card to share>\n-->I'll try guessing ", inet_ntoa(addr));
00232 } else {
00233 log_message("wpcap_init:cmdline address: ", inet_ntoa(addr));
00234 }
00235
00236 wpcap = LoadLibrary("wpcap.dll");
00237 pcap_findalldevs = (int (*)(struct pcap_if **, char *))
00238 GetProcAddress(wpcap, "pcap_findalldevs");
00239 pcap_open_live = (struct pcap *(*)(char *, int, int, int, char *))
00240 GetProcAddress(wpcap, "pcap_open_live");
00241 pcap_next_ex = (int (*)(struct pcap *, struct pcap_pkthdr **, unsigned char **))
00242 GetProcAddress(wpcap, "pcap_next_ex");
00243 pcap_sendpacket = (int (*)(struct pcap *, unsigned char *, int))
00244 GetProcAddress(wpcap, "pcap_sendpacket");
00245
00246 if(pcap_findalldevs == NULL || pcap_open_live == NULL ||
00247 pcap_next_ex == NULL || pcap_sendpacket == NULL) {
00248 error_exit("error on access to winpcap library\n");
00249 }
00250
00251 init_pcap(addr);
00252 set_ethaddr(addr);
00253
00254 }
00255
00256
00257 u16_t
00258 wpcap_poll(void)
00259 {
00260 struct pcap_pkthdr *packet_header;
00261 unsigned char *packet;
00262
00263 switch(pcap_next_ex(pcap, &packet_header, &packet)) {
00264 case -1:
00265 error_exit("error on poll\n");
00266 case 0:
00267 return 0;
00268 }
00269
00270 #if UIP_CONF_IPV6
00271
00272
00273
00274
00275
00276
00277
00278 int i;
00279 for (i=0;i<UIP_LLADDR_LEN;i++) if (*(packet+UIP_LLADDR_LEN+i)!=uip_lladdr.addr[i]) break;
00280 if (i==UIP_LLADDR_LEN) {
00281 PRINTF("Discarding echoed packet\n");
00282 return 0;
00283 }
00284 #endif
00285
00286 if(packet_header->caplen > UIP_BUFSIZE) {
00287 return 0;
00288 }
00289
00290 CopyMemory(uip_buf, packet, packet_header->caplen);
00291 return (u16_t)packet_header->caplen;
00292
00293 }
00294
00295
00296 #if UIP_CONF_IPV6
00297 u8_t
00298 wpcap_send(uip_lladdr_t *lladdr)
00299 {
00300 if(lladdr == NULL) {
00301
00302 (&BUF->dest)->addr[0] = 0x33;
00303 (&BUF->dest)->addr[1] = 0x33;
00304 (&BUF->dest)->addr[2] = IPBUF->destipaddr.u8[12];
00305 (&BUF->dest)->addr[3] = IPBUF->destipaddr.u8[13];
00306 (&BUF->dest)->addr[4] = IPBUF->destipaddr.u8[14];
00307 (&BUF->dest)->addr[5] = IPBUF->destipaddr.u8[15];
00308 } else {
00309 memcpy(&BUF->dest, lladdr, UIP_LLADDR_LEN);
00310 }
00311 memcpy(&BUF->src, &uip_lladdr, UIP_LLADDR_LEN);
00312
00313 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]);
00314 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]);
00315 PRINTF(" Type=%04x\n",BUF->type);
00316 BUF->type = UIP_HTONS(UIP_ETHTYPE_IPV6);
00317 uip_len += sizeof(struct uip_eth_hdr);
00318 if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
00319 error_exit("error on send\n");
00320 }
00321 return 0;
00322 }
00323 #else
00324 void
00325 wpcap_send(void)
00326 {
00327
00328
00329
00330
00331
00332
00333
00334 if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
00335 error_exit("error on send\n");
00336 }
00337 }
00338 #endif
00339
00340 void
00341 wpcap_exit(void)
00342 {
00343 FreeLibrary(wpcap);
00344 }
00345