Contiki 2.5
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  * @(#)$Id: wpcap-drv.c,v 1.7 2010/10/19 18:29:04 adamdunkels Exp $
32  */
33 
34 #include "contiki-net.h"
35 #include "net/uip-neighbor.h"
36 #include "net/wpcap.h"
37 
38 #include "net/wpcap-drv.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 PROCESS(wpcap_process, "WinPcap driver");
44 
45 /*---------------------------------------------------------------------------*/
46 #if !UIP_CONF_IPV6
47 u8_t
48 wpcap_output(void)
49 {
50  uip_arp_out();
51  wpcap_send();
52 
53  return 0;
54 }
55 #endif /* !UIP_CONF_IPV6 */
56 /*---------------------------------------------------------------------------*/
57 static void
58 pollhandler(void)
59 {
60  process_poll(&wpcap_process);
61  uip_len = wpcap_poll();
62 
63  if(uip_len > 0) {
64 #if UIP_CONF_IPV6
65  if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
66  tcpip_input();
67  } else
68 #endif /* UIP_CONF_IPV6 */
69  if(BUF->type == uip_htons(UIP_ETHTYPE_IP)) {
70  uip_len -= sizeof(struct uip_eth_hdr);
71  tcpip_input();
72 #if !UIP_CONF_IPV6
73  } else if(BUF->type == uip_htons(UIP_ETHTYPE_ARP)) {
74  uip_arp_arpin(); //math
75  /* If the above function invocation resulted in data that
76  should be sent out on the network, the global variable
77  uip_len is set to a value > 0. */
78  if(uip_len > 0) {
79  wpcap_send();
80  }
81 #endif /* !UIP_CONF_IPV6 */
82  } else {
83  uip_len = 0;
84  }
85  }
86 
87 #ifdef UIP_FALLBACK_INTERFACE
88 
89  process_poll(&wpcap_process);
90  uip_len = wfall_poll();
91 
92  if(uip_len > 0) {
93 #if UIP_CONF_IPV6
94  if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
95  tcpip_input();
96  } else
97  goto bail;
98 #endif /* UIP_CONF_IPV6 */
99  if(BUF->type == uip_htons(UIP_ETHTYPE_IP)) {
100  uip_len -= sizeof(struct uip_eth_hdr);
101  tcpip_input();
102 #if !UIP_CONF_IPV6
103  } else if(BUF->type == uip_htons(UIP_ETHTYPE_ARP)) {
104  uip_arp_arpin(); //math
105  /* If the above function invocation resulted in data that
106  should be sent out on the network, the global variable
107  uip_len is set to a value > 0. */
108  if(uip_len > 0) {
109  wfall_send();
110  }
111 #endif /* !UIP_CONF_IPV6 */
112  } else {
113 bail:
114  uip_len = 0;
115  }
116  }
117 #endif
118 
119 }
120 /*---------------------------------------------------------------------------*/
121 PROCESS_THREAD(wpcap_process, ev, data)
122 {
123  PROCESS_POLLHANDLER(pollhandler());
124 
125  PROCESS_BEGIN();
126 
127  wpcap_init();
128 
129 #if !UIP_CONF_IPV6
130  tcpip_set_outputfunc(wpcap_output);
131 #else
132  tcpip_set_outputfunc(wpcap_send);
133 #endif /* !UIP_CONF_IPV6 */
134 
135  process_poll(&wpcap_process);
136 
137  PROCESS_WAIT_UNTIL(ev == PROCESS_EVENT_EXIT);
138 
139  wpcap_exit();
140 
141  PROCESS_END();
142 }
143 /*---------------------------------------------------------------------------*/