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 <[email protected]>
33 *
34 * $Id: tapdev.c,v 1.8 2006/06/07 08:39:58 adam Exp $
35 */
36
37 #define UIP_DRIPADDR0 192
38 #define UIP_DRIPADDR1 168
39 #define UIP_DRIPADDR2 0
40 #define UIP_DRIPADDR3 1
41
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/socket.h>
53
54 #ifdef linux
55 #include <sys/ioctl.h>
56 #include <linux/if.h>
57 #include <linux/if_tun.h>
58 #define DEVTAP "/dev/net/tun"
59 #else /* linux */
60 #define DEVTAP "/dev/tap0"
61 #endif /* linux */
62
63 #include "uip.h"
64
65 static int drop = 0;
66 static int fd;
67
68
69 /*---------------------------------------------------------------------------*/
70 void
tapdev_init(void)71 tapdev_init(void)
72 {
73 char buf[1024];
74
75 fd = open(DEVTAP, O_RDWR);
76 if(fd == -1) {
77 perror("tapdev: tapdev_init: open");
78 exit(1);
79 }
80
81 #ifdef linux
82 {
83 struct ifreq ifr;
84 memset(&ifr, 0, sizeof(ifr));
85 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
86 if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
87 perror(buf);
88 exit(1);
89 }
90 }
91 #endif /* Linux */
92
93 snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d",
94 UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3);
95 system(buf);
96
97 }
98 /*---------------------------------------------------------------------------*/
99 unsigned int
tapdev_read(void)100 tapdev_read(void)
101 {
102 fd_set fdset;
103 struct timeval tv, now;
104 int ret;
105
106 tv.tv_sec = 0;
107 tv.tv_usec = 1000;
108
109
110 FD_ZERO(&fdset);
111 FD_SET(fd, &fdset);
112
113 ret = select(fd + 1, &fdset, NULL, NULL, &tv);
114 if(ret == 0) {
115 return 0;
116 }
117 ret = read(fd, uip_buf, UIP_BUFSIZE);
118 if(ret == -1) {
119 perror("tap_dev: tapdev_read: read");
120 }
121
122 /* printf("--- tap_dev: tapdev_read: read %d bytes\n", ret);*/
123 /* {
124 int i;
125 for(i = 0; i < 20; i++) {
126 printf("%x ", uip_buf[i]);
127 }
128 printf("\n");
129 }*/
130 /* check_checksum(uip_buf, ret);*/
131 return ret;
132 }
133 /*---------------------------------------------------------------------------*/
134 void
tapdev_send(void)135 tapdev_send(void)
136 {
137 int ret;
138 /* printf("tapdev_send: sending %d bytes\n", size);*/
139 /* check_checksum(uip_buf, size);*/
140
141 /* drop++;
142 if(drop % 8 == 7) {
143 printf("Dropped a packet!\n");
144 return;
145 }*/
146 ret = write(fd, uip_buf, uip_len);
147 if(ret == -1) {
148 perror("tap_dev: tapdev_send: writev");
149 exit(1);
150 }
151 }
152 /*---------------------------------------------------------------------------*/
153