Release tizen_2.0_beta
[framework/connectivity/connman.git] / tools / tap-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <sys/poll.h>
33 #include <sys/ioctl.h>
34
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_arp.h>
40 #include <linux/if_tun.h>
41
42 static int inet_ifup(const char *ifname)
43 {
44         struct ifreq ifr;
45         int sk, err;
46
47         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
48         if (sk < 0)
49                 return -errno;
50
51         memset(&ifr, 0, sizeof(ifr));
52         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
53
54         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
55                 err = -errno;
56                 goto done;
57         }
58
59         if (ifr.ifr_flags & IFF_UP) {
60                 err = -EALREADY;
61                 goto done;
62         }
63
64         ifr.ifr_flags |= IFF_UP;
65
66         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
67                 err = -errno;
68                 goto done;
69         }
70
71         err = 0;
72
73 done:
74         close(sk);
75
76         return err;
77 }
78
79 static int create_tap(const char *ifname)
80 {
81         struct ifreq ifr;
82         int fd, val;
83
84         fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
85         if (fd < 0) {
86                 perror("Failed to open TUN/TAP device");
87                 return -1;
88         }
89
90         memset(&ifr, 0, sizeof(ifr));
91         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
92         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
93
94         if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
95                 perror("Failed to set TUN/TAP interface");
96                 close(fd);
97                 return -1;
98         }
99
100         val = ARPHRD_ETHER;
101
102         if (ioctl(fd, TUNSETLINK, (unsigned long) val) < 0)
103                 perror("Failed to set TUN/TAP link type");
104
105         return fd;
106 }
107
108 static void dump_packet(unsigned char *buf, int len)
109 {
110         int i;
111
112         printf("[");
113         for (i = 0; i < len; i++) {
114                 printf(" %02x", buf[i]);
115                 if ((i + 1) % 16 == 0)
116                         printf("\n ");
117                 else if ((i + 1) % 8 == 0)
118                         printf(" ");
119         }
120         printf(" ]\n");
121 }
122
123 static void decode_ip(unsigned char *buf, int len)
124 {
125         struct iphdr *ip = (void *) buf;
126
127         printf("    IP proto %d saddr 0x%08x daddr 0x%08x\n",
128                                         ip->protocol, ip->saddr, ip->daddr);
129 }
130
131 static void decode_packet(unsigned char *buf, int len)
132 {
133         struct ether_header *eh = (void *) buf;
134         char src[18], dst[18];
135         uint16_t type;
136
137         snprintf(dst, sizeof(dst), "%02x:%02x:%02x:%02x:%02x:%02x",
138                                 eh->ether_dhost[0], eh->ether_dhost[1],
139                                 eh->ether_dhost[2], eh->ether_dhost[3],
140                                 eh->ether_dhost[4], eh->ether_dhost[5]);
141
142         snprintf(src, sizeof(src), "%02x:%02x:%02x:%02x:%02x:%02x",
143                                 eh->ether_shost[0], eh->ether_shost[1],
144                                 eh->ether_shost[2], eh->ether_shost[3],
145                                 eh->ether_shost[4], eh->ether_shost[5]);
146
147         type = ntohs(eh->ether_type);
148
149         printf("> type 0x%04x src %s dst %s <\n", type, src, dst);
150
151         switch (type) {
152         case ETHERTYPE_IP:
153                 decode_ip(buf + 14, len - 14);
154                 break;
155         case ETHERTYPE_LOOPBACK:
156                 dump_packet(buf, len);
157                 break;
158         }
159 }
160
161 int main(int argc, char *argv[])
162 {
163         const char *ifname = "xxx";
164         struct pollfd p;
165         int fd;
166
167         fd = create_tap(ifname);
168         if (fd < 0)
169                 return 1;
170
171         if (inet_ifup(ifname) < 0) {
172                 close(fd);
173                 return 1;
174         }
175
176         memset(&p, 0, sizeof(p));
177         p.fd = fd;
178         p.events = POLLHUP | POLLIN;
179
180         while (1) {
181                 unsigned char buf[2048];
182                 int len;
183
184                 len = poll(&p, 1, -1);
185                 if (len < 0)
186                         break;
187                 if (len == 0)
188                         continue;
189
190                 len = read(fd, buf, sizeof(buf));
191                 if (len < 0)
192                         break;
193
194                 decode_packet(buf, len);
195         }
196
197         return 0;
198 }