Contiki 3.x
wpcap-drv.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  */
32 
33 #include "contiki-net.h"
34 #include "net/ipv4/uip-neighbor.h"
35 #include "net/wpcap.h"
36 
37 #include "net/wpcap-drv.h"
38 #include <string.h>
39 
40 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
41 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
42 
43 /* It is not particularly easy to install tun interfaces in Windows/cygwin, so wpcap
44  * is used instead. The ip4 or ip6 address of the interface to connect to is passed
45  * on the command line that invokes the minimal-net and native executables.
46  *
47  * The minimal-net border router uses wpcap to connect to both primary
48  * and fallback interfaces. It is passed two addresses, and the uip stack is compiled
49  * with space for the ethernet headers on both interfaces.
50  *
51  * However the native border router uses wpcap to connect to a fallback interface only.
52  * The primary interface is the serial connection to the slip radio, and the
53  * uip stack is compiled without space for ethernet headers.
54  * The following define adds or strips ethernet headers from the fallback interface.
55  * Since it is at present used only with the native border router, it is also used
56  * as a hack to bypass polling of the primary interface.
57  *
58  * SELECT_CALLBACK is defined in /examples/ipv6/native-border-router/project-conf.h
59  */
60 #ifdef SELECT_CALLBACK
61 #define FALLBACK_HAS_ETHERNET_HEADERS 1
62 #endif
63 
64 PROCESS(wpcap_process, "WinPcap driver");
65 
66 /*---------------------------------------------------------------------------*/
67 #if !UIP_CONF_IPV6
68 uint8_t
69 wpcap_output(void)
70 {
71  uip_arp_out();
72  wpcap_send();
73 
74  return 0;
75 }
76 #endif /* !UIP_CONF_IPV6 */
77 /*---------------------------------------------------------------------------*/
78 static void
79 pollhandler(void)
80 {
81 #if !FALLBACK_HAS_ETHERNET_HEADERS //native br is fallback only
82  process_poll(&wpcap_process);
83  uip_len = wpcap_poll();
84 
85  if(uip_len > 0) {
86 #if UIP_CONF_IPV6
87  if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
88 // printf("wpcap poll calls tcpip");
89  tcpip_input();
90  } else
91 #endif /* UIP_CONF_IPV6 */
92  if(BUF->type == uip_htons(UIP_ETHTYPE_IP)) {
93  uip_len -= sizeof(struct uip_eth_hdr);
94  tcpip_input();
95 #if !UIP_CONF_IPV6
96  } else if(BUF->type == uip_htons(UIP_ETHTYPE_ARP)) {
97  uip_arp_arpin(); //math
98  /* If the above function invocation resulted in data that
99  should be sent out on the network, the global variable
100  uip_len is set to a value > 0. */
101  if(uip_len > 0) {
102  wpcap_send();
103  }
104 #endif /* !UIP_CONF_IPV6 */
105  } else {
106  uip_len = 0;
107  }
108  }
109 #endif
110 #ifdef UIP_FALLBACK_INTERFACE
111 
112  process_poll(&wpcap_process);
113  uip_len = wfall_poll();
114 
115  if(uip_len > 0) {
116 #if FALLBACK_HAS_ETHERNET_HEADERS
117  if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
118  //remove ethernet header and pass ipv6 packet to stack
119  uip_len-=14;
120 //{int i;printf("\n0000 ");for (i=0;i<uip_len;i++) printf("%02x ",*(unsigned char*)(uip_buf+i));printf("\n");}
121  // memcpy(uip_buf, uip_buf+14, uip_len);
122  memcpy(&uip_buf[UIP_LLH_LEN], uip_buf+14, uip_len); //LLH_LEN is zero for native border router to slip radio
123 // CopyMemory(uip_buf, uip_buf+14, uip_len);
124 //{int i;printf("\n0000 ");for (i=0;i<uip_len;i++) printf("%02x ",*(char*)(uip_buf+i));printf("\n");}
125  tcpip_input();
126  } else
127  goto bail;
128 #elif UIP_CONF_IPV6
129  if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
130  tcpip_input();
131  } else
132  goto bail;
133 #endif /* UIP_CONF_IPV6 */
134  if(BUF->type == uip_htons(UIP_ETHTYPE_IP)) {
135  uip_len -= sizeof(struct uip_eth_hdr);
136  tcpip_input();
137 #if !UIP_CONF_IPV6
138  } else if(BUF->type == uip_htons(UIP_ETHTYPE_ARP)) {
139  uip_arp_arpin(); //math
140  /* If the above function invocation resulted in data that
141  should be sent out on the network, the global variable
142  uip_len is set to a value > 0. */
143  if(uip_len > 0) {
144  wfall_send();
145  }
146 #endif /* !UIP_CONF_IPV6 */
147  } else {
148 bail:
149  uip_len = 0;
150  }
151  }
152 #endif
153 
154 }
155 /*---------------------------------------------------------------------------*/
156 PROCESS_THREAD(wpcap_process, ev, data)
157 {
158  PROCESS_POLLHANDLER(pollhandler());
159 
160  PROCESS_BEGIN();
161 
162  wpcap_init();
163 
164 #if !UIP_CONF_IPV6
165  tcpip_set_outputfunc(wpcap_output);
166 #else
167 #if !FALLBACK_HAS_ETHERNET_HEADERS
168  tcpip_set_outputfunc(wpcap_send);
169 #endif
170 #endif /* !UIP_CONF_IPV6 */
171 
172  process_poll(&wpcap_process);
173 
174  PROCESS_WAIT_UNTIL(ev == PROCESS_EVENT_EXIT);
175 
176  wpcap_exit();
177 
178  PROCESS_END();
179 }
180 /*---------------------------------------------------------------------------*/
void uip_arp_out(void)
Prepend Ethernet header to an outbound IP packet and see if we need to send out an ARP request...
Definition: uip_arp.c:363
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
#define PROCESS_WAIT_UNTIL(c)
Wait for a condition to occur.
Definition: process.h:192
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:529
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Header file for database of link-local neighbors, used by IPv6 code and to be used by future ...
The Ethernet header.
Definition: uip_arp.h:60
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
#define PROCESS_POLLHANDLER(handler)
Specify an action when a process is polled.
Definition: process.h:242
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void uip_arp_arpin(void)
ARP processing for incoming IP packets.
Definition: uip_arp.c:283