wpcap.c

00001 /*
00002  * Copyright (c) 2007, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Oliver Schmidt <ol.sc@web.de>
00032  *
00033  * $Id: wpcap.c,v 1.20 2010/10/19 20:30:47 oliverschmidt Exp $
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 /* __CYGWIN__ */
00047 #include <malloc.h>
00048 #endif /* __CYGWIN__ */
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 /* __CYGWIN__ */
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 /* uip_ethaddr is defined in uip.c. It is not used in uip6.c. 
00093  * If needed for some purpose it can be defined here
00094  */
00095 #if UIP_CONF_IPV6
00096 //struct uip_eth_addr uip_ethaddr;
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 //          pcap_setdirection(PCAP_D_IN);  //Not implemented in windows yet?
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 //      int i;for (i=0;i<6;i++) uip_ethaddr.addr[i] = adapters->PhysicalAddress[i];
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 /* __CYGWIN__ */
00226   addr.s_addr = inet_addr(__argv[1]);
00227 #endif /* __CYGWIN__ */
00228   if(addr.s_addr == INADDR_NONE) {
00229 //  error_exit("usage: <program> <ip addr of ethernet card to share>\n");
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 /* Since pcap_setdirection(PCAP_D_IN) is not implemented in winpcap all outgoing packets
00272  * will be echoed back. The stack will ignore any packets not addressed to it, but initial
00273  * ipv6 neighbor solicitations are addressed to everyone and the echoed NS sent on startup
00274  * would be processed as a conflicting NS race which would cause a stack shutdown.
00275  * So discard all packets with our source address (packet starts destaddr, srcaddr, ...)
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 /* UIP_CONF_IPV6 */
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 /* the dest must be multicast*/
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 /* UIP_CONF_IPV6 */
00324 void
00325 wpcap_send(void)
00326 {
00327   /*  if(rand() % 1000 > 900) {
00328     printf("Drop\n");
00329     return;
00330   } else {
00331     printf("Send\n");
00332     }*/
00333 
00334   if(pcap_sendpacket(pcap, uip_buf, uip_len) == -1) {
00335     error_exit("error on send\n");
00336   }
00337 }
00338 #endif /* UIP_CONF_IPV6 */
00339 /*---------------------------------------------------------------------------*/
00340 void
00341 wpcap_exit(void)
00342 {
00343   FreeLibrary(wpcap);
00344 }
00345 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:39 2011 for Contiki 2.5 by  doxygen 1.6.1