2 * Copyright (c) 2001, Swedish Institute of Computer Science.
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * 1. Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
12 * 2. Redistributions in binary form must reproduce the above copyright
\r
13 * notice, this list of conditions and the following disclaimer in the
\r
14 * documentation and/or other materials provided with the distribution.
\r
16 * 3. Neither the name of the Institute nor the names of its contributors
\r
17 * may be used to endorse or promote products derived from this software
\r
18 * without specific prior written permission.
\r
20 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
\r
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
\r
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
\r
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\r
32 * Author: Adam Dunkels <adam@sics.se>
\r
34 * $Id: tapdev.c,v 1.7.2.1 2003/10/07 13:23:19 adam Exp $
\r
43 #include <sys/ioctl.h>
\r
44 #include <sys/socket.h>
\r
45 #include <sys/types.h>
\r
46 #include <sys/time.h>
\r
47 #include <sys/uio.h>
\r
48 #include <sys/socket.h>
\r
51 #include <sys/ioctl.h>
\r
52 #include <linux/if.h>
\r
53 #include <linux/if_tun.h>
\r
54 #define DEVTAP "/dev/net/tun"
\r
56 #define DEVTAP "/dev/tap0"
\r
63 static unsigned long lasttime;
\r
64 static struct timezone tz;
\r
66 /*-----------------------------------------------------------------------------------*/
\r
72 fd = open(DEVTAP, O_RDWR);
\r
74 perror("tapdev: tapdev_init: open");
\r
81 memset(&ifr, 0, sizeof(ifr));
\r
82 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
\r
83 if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
\r
90 snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d",
\r
91 UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3);
\r
96 /*-----------------------------------------------------------------------------------*/
\r
101 struct timeval tv, now;
\r
104 if(lasttime >= 500000) {
\r
110 tv.tv_usec = 500000 - lasttime;
\r
114 FD_SET(fd, &fdset);
\r
116 gettimeofday(&now, &tz);
\r
117 ret = select(fd + 1, &fdset, NULL, NULL, &tv);
\r
122 ret = read(fd, uip_buf, UIP_BUFSIZE);
\r
124 perror("tap_dev: tapdev_read: read");
\r
126 gettimeofday(&tv, &tz);
\r
127 lasttime += (tv.tv_sec - now.tv_sec) * 1000000 + (tv.tv_usec - now.tv_usec);
\r
131 /*-----------------------------------------------------------------------------------*/
\r
136 struct iovec iov[2];
\r
140 char tmpbuf[UIP_BUFSIZE];
\r
143 for(i = 0; i < 40 + UIP_LLH_LEN; i++) {
\r
144 tmpbuf[i] = uip_buf[i];
\r
147 for(; i < uip_len; i++) {
\r
148 tmpbuf[i] = uip_appdata[i - 40 - UIP_LLH_LEN];
\r
151 ret = write(fd, tmpbuf, uip_len);
\r
155 if(uip_len < 40 + UIP_LLH_LEN) {
\r
156 ret = write(fd, uip_buf, uip_len + UIP_LLH_LEN);
\r
158 iov[0].iov_base = uip_buf;
\r
159 iov[0].iov_len = 40 + UIP_LLH_LEN;
\r
160 iov[1].iov_base = (char *)uip_appdata;
\r
161 iov[1].iov_len = uip_len - (40 + UIP_LLH_LEN);
\r
163 ret = writev(fd, iov, 2);
\r
167 perror("tap_dev: tapdev_send: writev");
\r
171 /*-----------------------------------------------------------------------------------*/
\r