Add missing break statement to WISPr client
[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 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/poll.h>
32 #include <sys/ioctl.h>
33
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #include <net/ethernet.h>
37 #include <net/if.h>
38 #include <net/if_arp.h>
39 #include <linux/if_tun.h>
40
41 static int inet_ifup(const char *ifname)
42 {
43         struct ifreq ifr;
44         int sk, err;
45
46         sk = socket(PF_INET, SOCK_DGRAM, 0);
47         if (sk < 0)
48                 return -errno;
49
50         memset(&ifr, 0, sizeof(ifr));
51         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
52
53         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
54                 err = -errno;
55                 goto done;
56         }
57
58         if (ifr.ifr_flags & IFF_UP) {
59                 err = -EALREADY;
60                 goto done;
61         }
62
63         ifr.ifr_flags |= IFF_UP;
64
65         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
66                 err = -errno;
67                 goto done;
68         }
69
70         err = 0;
71
72 done:
73         close(sk);
74
75         return err;
76 }
77
78 static int create_tap(const char *ifname)
79 {
80         struct ifreq ifr;
81         int fd, val;
82
83         fd = open("/dev/net/tun", O_RDWR);
84         if (fd < 0) {
85                 perror("Failed to open TUN/TAP device");
86                 return -1;
87         }
88
89         memset(&ifr, 0, sizeof(ifr));
90         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
91         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
92
93         if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
94                 perror("Failed to set TUN/TAP interface");
95                 close(fd);
96                 return -1;
97         }
98
99         val = ARPHRD_ETHER;
100
101         if (ioctl(fd, TUNSETLINK, (unsigned long) val) < 0)
102                 perror("Failed to set TUN/TAP link type");
103
104         return fd;
105 }
106
107 static void dump_packet(unsigned char *buf, int len)
108 {
109         int i;
110
111         printf("[");
112         for (i = 0; i < len; i++) {
113                 printf(" %02x", buf[i]);
114                 if ((i + 1) % 16 == 0)
115                         printf("\n ");
116                 else if ((i + 1) % 8 == 0)
117                         printf(" ");
118         }
119         printf(" ]\n");
120 }
121
122 static void decode_ip(unsigned char *buf, int len)
123 {
124         struct iphdr *ip = (void *) buf;
125
126         printf("    IP proto %d saddr 0x%08x daddr 0x%08x\n",
127                                         ip->protocol, ip->saddr, ip->daddr);
128 }
129
130 static void decode_packet(unsigned char *buf, int len)
131 {
132         struct ether_header *eh = (void *) buf;
133         char src[18], dst[18];
134         uint16_t type;
135
136         snprintf(dst, sizeof(dst), "%02x:%02x:%02x:%02x:%02x:%02x",
137                                 eh->ether_dhost[0], eh->ether_dhost[1],
138                                 eh->ether_dhost[2], eh->ether_dhost[3],
139                                 eh->ether_dhost[4], eh->ether_dhost[5]);
140
141         snprintf(src, sizeof(src), "%02x:%02x:%02x:%02x:%02x:%02x",
142                                 eh->ether_shost[0], eh->ether_shost[1],
143                                 eh->ether_shost[2], eh->ether_shost[3],
144                                 eh->ether_shost[4], eh->ether_shost[5]);
145
146         type = ntohs(eh->ether_type);
147
148         printf("> type 0x%04x src %s dst %s <\n", type, src, dst);
149
150         switch (type) {
151         case ETHERTYPE_IP:
152                 decode_ip(buf + 14, len - 14);
153                 break;
154         case ETHERTYPE_LOOPBACK:
155                 dump_packet(buf, len);
156                 break;
157         }
158 }
159
160 int main(int argc, char *argv[])
161 {
162         const char *ifname = "xxx";
163         struct pollfd p;
164         int fd;
165
166         fd = create_tap(ifname);
167         if (fd < 0)
168                 return 1;
169
170         if (inet_ifup(ifname) < 0) {
171                 close(fd);
172                 return 1;
173         }
174
175         memset(&p, 0, sizeof(p));
176         p.fd = fd;
177         p.events = POLLHUP | POLLIN;
178
179         while (1) {
180                 unsigned char buf[2048];
181                 int len;
182
183                 len = poll(&p, 1, -1);
184                 if (len < 0)
185                         break;
186                 if (len == 0)
187                         continue;
188
189                 len = read(fd, buf, sizeof(buf));
190                 if (len < 0)
191                         break;
192
193                 decode_packet(buf, len);
194         }
195
196         return 0;
197 }