2 * Copyright (c) 2001, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
32 * Author: Adam Dunkels <adam@sics.se>
34 * $Id: tapdev.c,v 1.7.2.1 2003/10/07 13:23:19 adam Exp $
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <sys/ioctl.h>
53 #include <linux/if_tun.h>
54 #define DEVTAP "/dev/net/tun"
56 #define DEVTAP "/dev/tap0"
63 static unsigned long lasttime;
64 static struct timezone tz;
66 /*-----------------------------------------------------------------------------------*/
72 fd = open(DEVTAP, O_RDWR);
74 perror("tapdev: tapdev_init: open");
81 memset(&ifr, 0, sizeof(ifr));
82 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
83 if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
90 snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d",
91 UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3);
97 void dump_mem(int type, int len)
101 for(i = 0; i < len; i++)
102 printf("%c", uip_buf[i]);
105 printf("\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01");
106 printf("\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01");
108 printf("\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02");
109 printf("\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02");
115 /*-----------------------------------------------------------------------------------*/
120 struct timeval tv, now;
123 if(lasttime >= 500000) {
129 tv.tv_usec = 500000 - lasttime;
135 gettimeofday(&now, &tz);
136 ret = select(fd + 1, &fdset, NULL, NULL, &tv);
141 ret = read(fd, uip_buf, UIP_BUFSIZE);
143 perror("tap_dev: tapdev_read: read");
145 gettimeofday(&tv, &tz);
146 lasttime += (tv.tv_sec - now.tv_sec) * 1000000 + (tv.tv_usec - now.tv_usec);
150 /*-----------------------------------------------------------------------------------*/
159 char tmpbuf[UIP_BUFSIZE];
162 for(i = 0; i < 40 + UIP_LLH_LEN; i++) {
163 tmpbuf[i] = uip_buf[i];
166 for(; i < uip_len; i++) {
167 tmpbuf[i] = uip_appdata[i - 40 - UIP_LLH_LEN];
170 ret = write(fd, tmpbuf, uip_len);
174 if(uip_len < 40 + UIP_LLH_LEN) {
175 ret = write(fd, uip_buf, uip_len + UIP_LLH_LEN);
177 iov[0].iov_base = uip_buf;
178 iov[0].iov_len = 40 + UIP_LLH_LEN;
179 iov[1].iov_base = (char *)uip_appdata;
180 iov[1].iov_len = uip_len - (40 + UIP_LLH_LEN);
182 ret = writev(fd, iov, 2);
188 perror("tap_dev: tapdev_send: writev");
192 /*-----------------------------------------------------------------------------------*/