2 * Copyright (c) 2006, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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>
44 void hexdump(void *b, int len)
46 unsigned char *p = (unsigned char*) b;
49 printf("%.2X ", *p++);
53 int handle_data(int dude, char *buf, int len)
61 ih = (struct ip*) buf;
76 printf("Got %d bytes [%d]\n", len, ntohs(id));
84 memcpy(&tmp[2], &id, 2);
86 iov[0].iov_base = tmp;
88 iov[1].iov_base = buf;
91 memset(&mh, 0, sizeof(mh));
93 mh.msg_iovlen = sizeof(iov)/sizeof(struct iovec);
96 if (sendmsg(dude, &mh, 0) != (4 + len))
101 void handle_dude(int dude, int raw)
107 rd = recv(raw, buf, sizeof(buf), 0);
111 if (handle_data(dude, buf, rd) == -1)
121 int main(int argc, char *argv[])
124 struct sockaddr_in s_in;
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;
133 if ((raw = socket(PF_INET, SOCK_RAW, 0)) == -1)
136 if ((s = socket(s_in.sin_family, SOCK_STREAM, IPPROTO_TCP)) == -1)
139 if (bind(s, (struct sockaddr*)&s_in, sizeof(s_in)) == -1)
142 if (listen(s, 5) == -1)
145 if (signal(SIGPIPE, hand) == SIG_ERR)
150 dude = accept(s, (struct sockaddr*)&s_in, &len);
154 printf("Got dude %s\n", inet_ntoa(s_in.sin_addr));
155 handle_dude(dude, raw);