Contiki 3.x
tapdev.c
1 /*
2  * Copyright (c) 2001, 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  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the Institute nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Author: Adam Dunkels <adam@sics.se>
33  *
34  */
35 
36 #include "net/ip/uip.h"
37 #include "net/ip/uipopt.h"
38 
39 #if !NETSTACK_CONF_WITH_IPV6
40 
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <sys/uio.h>
51 #include <sys/socket.h>
52 
53 #ifdef linux
54 #include <sys/ioctl.h>
55 #include <linux/if.h>
56 #include <linux/if_tun.h>
57 #define DEVTAP "/dev/net/tun"
58 #else /* linux */
59 #define DEVTAP "/dev/tap0"
60 #endif /* linux */
61 
62 #include "contiki-net.h"
63 #include "tapdev.h"
64 
65 #define DROP 0
66 
67 #if DROP
68 static int drop = 0;
69 #endif
70 
71 static int fd;
72 
73 static unsigned long lasttime;
74 
75 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
76 
77 #define DEBUG 0
78 #if DEBUG
79 #define PRINTF(...) fprintf(stderr, __VA_ARGS__)
80 #else
81 #define PRINTF(...)
82 #endif
83 
84 /*---------------------------------------------------------------------------*/
85 int
86 tapdev_fd(void)
87 {
88  return fd;
89 }
90 
91 /*---------------------------------------------------------------------------*/
92 static void
93 remove_route(void)
94 {
95  char buf[1024];
96  int ret;
97 
98  snprintf(buf, sizeof(buf), "route delete -net 172.18.0.0");
99  ret = system(buf);
100  fprintf(stderr, "ret %d\n", ret);
101  fprintf(stderr, "%s\n", buf);
102 
103 }
104 /*---------------------------------------------------------------------------*/
105 void
106 tapdev_init(void)
107 {
108  char buf[1024];
109  int ret;
110 
111  fd = open(DEVTAP, O_RDWR);
112  if(fd == -1) {
113  perror("tapdev: tapdev_init: open");
114  return;
115  }
116 
117 #ifdef linux
118  {
119  struct ifreq ifr;
120  memset(&ifr, 0, sizeof(ifr));
121  ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
122  if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
123  perror("ioctl(TUNSETIFF)");
124  exit(1);
125  }
126  }
127 #endif /* Linux */
128 
129  snprintf(buf, sizeof(buf), "ifconfig tap0 inet 172.18.0.1/16");
130  ret = system(buf);
131  fprintf(stderr, "ret %d\n", ret);
132  fprintf(stderr, "%s\n", buf);
133 #ifdef linux
134  /* route add for linux */
135  snprintf(buf, sizeof(buf), "route add -net 172.18.0.0/16 dev tap0");
136 #else /* linux */
137  /* route add for freebsd */
138  snprintf(buf, sizeof(buf), "route add -net 172.18.0.0/16 -iface tap0");
139 #endif /* linux */
140 
141  ret = system(buf);
142  fprintf(stderr, "ret %d\n", ret);
143  fprintf(stderr, "%s\n", buf);
144  atexit(remove_route);
145 
146  lasttime = 0;
147 }
148 /*---------------------------------------------------------------------------*/
149 uint16_t
150 tapdev_poll(void)
151 {
152  fd_set fdset;
153  struct timeval tv;
154  int ret;
155 
156  tv.tv_sec = 0;
157  tv.tv_usec = 0;
158 
159  FD_ZERO(&fdset);
160  if(fd > 0) {
161  FD_SET(fd, &fdset);
162  }
163 
164  ret = select(fd + 1, &fdset, NULL, NULL, &tv);
165 
166  if(ret == 0) {
167  return 0;
168  }
169  ret = read(fd, uip_buf, UIP_BUFSIZE);
170  PRINTF("tapdev_poll: read %d bytes\n", ret);
171 
172  if(ret == -1) {
173  perror("tapdev_poll: read");
174  }
175  return ret;
176 }
177 /*---------------------------------------------------------------------------*/
178 void
179 tapdev_send(void)
180 {
181  int ret;
182 
183  if(fd <= 0) {
184  return;
185  }
186 
187  /* printf("tapdev_send: sending %d bytes\n", size);*/
188  /* check_checksum(uip_buf, size);*/
189 
190 #if DROP
191  drop++;
192  if(drop % 8 == 7) {
193  fprintf(stderr, "Dropped an output packet!\n");
194  return;
195  }
196 #endif /* DROP */
197 
198  PRINTF("tapdev_send: sending %d bytes\n", uip_len);
199  ret = write(fd, uip_buf, uip_len);
200 
201  if(ret == -1) {
202  perror("tap_dev: tapdev_send: writev");
203  exit(1);
204  }
205 }
206 /*---------------------------------------------------------------------------*/
207 void
208 tapdev_exit(void)
209 {
210 }
211 /*---------------------------------------------------------------------------*/
212 
213 #endif /* !NETSTACK_CONF_WITH_IPV6 */
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
Configuration options for uIP.
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:523
#define NULL
The null pointer.
Header file for the uIP TCP/IP stack.
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:173