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 !UIP_CONF_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  snprintf(buf, sizeof(buf), "route delete -net 172.18.0.0");
97  system(buf);
98  fprintf(stderr, "%s\n", buf);
99 
100 }
101 /*---------------------------------------------------------------------------*/
102 void
103 tapdev_init(void)
104 {
105  char buf[1024];
106 
107  fd = open(DEVTAP, O_RDWR);
108  if(fd == -1) {
109  perror("tapdev: tapdev_init: open");
110  return;
111  }
112 
113 #ifdef linux
114  {
115  struct ifreq ifr;
116  memset(&ifr, 0, sizeof(ifr));
117  ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
118  if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
119  perror("ioctl(TUNSETIFF)");
120  exit(1);
121  }
122  }
123 #endif /* Linux */
124 
125  snprintf(buf, sizeof(buf), "ifconfig tap0 inet 172.18.0.1/16");
126  system(buf);
127  fprintf(stderr, "%s\n", buf);
128 #ifdef linux
129  /* route add for linux */
130  snprintf(buf, sizeof(buf), "route add -net 172.18.0.0/16 dev tap0");
131 #else /* linux */
132  /* route add for freebsd */
133  snprintf(buf, sizeof(buf), "route add -net 172.18.0.0/16 -iface tap0");
134 #endif /* linux */
135 
136  system(buf);
137  fprintf(stderr, "%s\n", buf);
138  atexit(remove_route);
139 
140  lasttime = 0;
141 }
142 /*---------------------------------------------------------------------------*/
143 uint16_t
144 tapdev_poll(void)
145 {
146  fd_set fdset;
147  struct timeval tv;
148  int ret;
149 
150  tv.tv_sec = 0;
151  tv.tv_usec = 0;
152 
153  FD_ZERO(&fdset);
154  if(fd > 0) {
155  FD_SET(fd, &fdset);
156  }
157 
158  ret = select(fd + 1, &fdset, NULL, NULL, &tv);
159 
160  if(ret == 0) {
161  return 0;
162  }
163  ret = read(fd, uip_buf, UIP_BUFSIZE);
164  PRINTF("tapdev_poll: read %d bytes\n", ret);
165 
166  if(ret == -1) {
167  perror("tapdev_poll: read");
168  }
169  return ret;
170 }
171 /*---------------------------------------------------------------------------*/
172 void
173 tapdev_send(void)
174 {
175  int ret;
176 
177  if(fd <= 0) {
178  return;
179  }
180 
181  /* printf("tapdev_send: sending %d bytes\n", size);*/
182  /* check_checksum(uip_buf, size);*/
183 
184 #if DROP
185  drop++;
186  if(drop % 8 == 7) {
187  fprintf(stderr, "Dropped an output packet!\n");
188  return;
189  }
190 #endif /* DROP */
191 
192  PRINTF("tapdev_send: sending %d bytes\n", uip_len);
193  ret = write(fd, uip_buf, uip_len);
194 
195  if(ret == -1) {
196  perror("tap_dev: tapdev_send: writev");
197  exit(1);
198  }
199 }
200 /*---------------------------------------------------------------------------*/
201 void
202 tapdev_exit(void)
203 {
204 }
205 /*---------------------------------------------------------------------------*/
206 
207 #endif /* !UIP_CONF_IPV6 */
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
Header file for the uIP TCP/IP stack.
#define NULL
The null pointer.
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:173
Configuration options for uIP.