xref: /dragonfly/tools/tools/net80211/w00t/redir/buddy.c (revision 063b119abbc0884b488a4191f800cf331c3be529)
1 /*-
2  * Copyright (c) 2006, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/tools/tools/net80211/w00t/redir/buddy.c,v 1.1 2006/08/05 05:18:03 sam Exp $
27  */
28 #include <sys/uio.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <netinet/ip.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <err.h>
43 
hexdump(void * b,int len)44 void hexdump(void *b, int len)
45 {
46         unsigned char *p = (unsigned char*) b;
47 
48         while (len--)
49                 printf("%.2X ", *p++);
50         printf("\n");
51 }
52 
handle_data(int dude,char * buf,int len)53 int handle_data(int dude, char *buf, int len)
54 {
55           struct ip *ih;
56           unsigned short id;
57           char tmp[4];
58           struct iovec iov[2];
59           struct msghdr mh;
60 
61           ih = (struct ip*) buf;
62 
63           /* XXX IP FRAGS */
64 
65           /* filter */
66           if (ih->ip_p != 0)
67                     return 0;
68 
69           if (ih->ip_hl != 5)
70                     return 0;
71 
72           /* get info */
73           id = ih->ip_id;
74           len -= 20;
75           buf += 20;
76           printf("Got %d bytes [%d]\n", len, ntohs(id));
77 #if 0
78           hexdump(buf, len);
79 #endif
80 
81           /* prepare packet */
82           memcpy(tmp, &id, 2);
83           id = htons(len);
84           memcpy(&tmp[2], &id, 2);
85 
86           iov[0].iov_base = tmp;
87           iov[0].iov_len = 4;
88           iov[1].iov_base = buf;
89           iov[1].iov_len = len;
90 
91           memset(&mh, 0, sizeof(mh));
92           mh.msg_iov = iov;
93           mh.msg_iovlen = sizeof(iov)/sizeof(struct iovec);
94 
95           /* write */
96           if (sendmsg(dude, &mh, 0) != (4 + len))
97                     return -1;
98           return 0;
99 }
100 
handle_dude(int dude,int raw)101 void handle_dude(int dude, int raw)
102 {
103           char buf[4096];
104           int rd;
105 
106           while (1) {
107                     rd = recv(raw, buf, sizeof(buf), 0);
108                     if (rd == -1)
109                               err(1, "recv()");
110 
111                     if (handle_data(dude, buf, rd) == -1)
112                               return;
113           }
114 }
115 
hand(int s)116 void hand(int s)
117 {
118           printf("sigpipe\n");
119 }
120 
main(int argc,char * argv[])121 int main(int argc, char *argv[])
122 {
123           int s, dude;
124           struct sockaddr_in s_in;
125           int len;
126           int raw;
127 
128           memset(&s_in, 0, sizeof(s_in));
129           s_in.sin_family = PF_INET;
130           s_in.sin_port = htons(666);
131           s_in.sin_addr.s_addr = INADDR_ANY;
132 
133           if ((raw = socket(PF_INET, SOCK_RAW, 0)) == -1)
134                     err(1, "socket()");
135 
136           if ((s = socket(s_in.sin_family, SOCK_STREAM, IPPROTO_TCP)) == -1)
137                     err(1, "socket()");
138 
139           if (bind(s, (struct sockaddr*)&s_in, sizeof(s_in)) == -1)
140                     err(1, "bind()");
141 
142           if (listen(s, 5) == -1)
143                     err(1, "listen()");
144 
145           if (signal(SIGPIPE, hand) == SIG_ERR)
146                     err(1, "signal()");
147 
148           while (1) {
149                     len = sizeof(s_in);
150                     dude = accept(s, (struct sockaddr*)&s_in, &len);
151                     if (dude == -1)
152                               err(1, "accept()");
153 
154                     printf("Got dude %s\n", inet_ntoa(s_in.sin_addr));
155                     handle_dude(dude, raw);
156                     printf("Done\n");
157           }
158 }
159