Contiki 2.5
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  * $Id: tapdev.c,v 1.2 2007/05/20 21:32:24 oliverschmidt Exp $
35  */
36 
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/uio.h>
47 #include <sys/socket.h>
48 
49 #ifdef linux
50 #include <sys/ioctl.h>
51 #include <linux/if.h>
52 #include <linux/if_tun.h>
53 #define DEVTAP "/dev/net/tun"
54 #else /* linux */
55 #define DEVTAP "/dev/tap0"
56 #endif /* linux */
57 
58 #include "contiki-net.h"
59 #include "tapdev.h"
60 
61 #define DROP 0
62 
63 #if DROP
64 static int drop = 0;
65 #endif
66 
67 static int fd;
68 
69 static unsigned long lasttime;
70 
71 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
72 
73 /*---------------------------------------------------------------------------*/
74 static void
75 remove_route(void)
76 {
77  char buf[1024];
78  snprintf(buf, sizeof(buf), "route delete -net 172.16.0.0");
79  system(buf);
80  printf("%s\n", buf);
81 
82 }
83 /*---------------------------------------------------------------------------*/
84 void
85 tapdev_init(void)
86 {
87  char buf[1024];
88 
89  fd = open(DEVTAP, O_RDWR);
90  if(fd == -1) {
91  perror("tapdev: tapdev_init: open");
92  return;
93  }
94 
95 #ifdef linux
96  {
97  struct ifreq ifr;
98  memset(&ifr, 0, sizeof(ifr));
99  ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
100  if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
101  perror(buf);
102  exit(1);
103  }
104  }
105 #endif /* Linux */
106 
107  snprintf(buf, sizeof(buf), "ifconfig tap0 inet 192.168.1.1");
108  system(buf);
109  printf("%s\n", buf);
110 #ifdef linux
111  /* route add for linux */
112  snprintf(buf, sizeof(buf), "route add -net 172.16.0.0/16 gw 192.168.1.2");
113 #else /* linux */
114  /* route add for freebsd */
115  snprintf(buf, sizeof(buf), "route add -net 172.16.0.0/16 192.168.1.2");
116 #endif /* linux */
117 
118  system(buf);
119  printf("%s\n", buf);
120  atexit(remove_route);
121 
122  lasttime = 0;
123 }
124 /*---------------------------------------------------------------------------*/
125 u16_t
126 tapdev_poll(void)
127 {
128  fd_set fdset;
129  struct timeval tv;
130  int ret;
131 
132  tv.tv_sec = 0;
133  tv.tv_usec = 0;
134 
135  FD_ZERO(&fdset);
136  if(fd > 0) {
137  FD_SET(fd, &fdset);
138  }
139 
140  ret = select(fd + 1, &fdset, NULL, NULL, &tv);
141 
142  if(ret == 0) {
143  return 0;
144  }
145  ret = read(fd, uip_buf, UIP_BUFSIZE);
146 
147  if(ret == -1) {
148  perror("tapdev_poll: read");
149  }
150  return ret;
151 }
152 /*---------------------------------------------------------------------------*/
153 void
154 tapdev_send(void)
155 {
156  int ret;
157 
158  if(fd <= 0) {
159  return;
160  }
161 
162  /* printf("tapdev_send: sending %d bytes\n", size);*/
163  /* check_checksum(uip_buf, size);*/
164 
165 #if DROP
166  drop++;
167  if(drop % 8 == 7) {
168  printf("Dropped an output packet!\n");
169  return;
170  }
171 #endif /* DROP */
172 
173  ret = write(fd, uip_buf, uip_len);
174 
175  if(ret == -1) {
176  perror("tap_dev: tapdev_send: writev");
177  exit(1);
178  }
179 }
180 /*---------------------------------------------------------------------------*/
181 void
182 tapdev_exit(void)
183 {
184 }
185 /*---------------------------------------------------------------------------*/