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 #include <sys/uio.h> 27 #include <sys/types.h> 28 #include <sys/socket.h> 29 #include <netinet/in_systm.h> 30 #include <netinet/in.h> 31 #include <arpa/inet.h> 32 #include <netinet/ip.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <fcntl.h> 38 #include <errno.h> 39 #include <signal.h> 40 #include <err.h> 41 42 void hexdump(void *b, int len) 43 { 44 unsigned char *p = (unsigned char*) b; 45 46 while (len--) 47 printf("%.2X ", *p++); 48 printf("\n"); 49 } 50 51 int handle_data(int dude, char *buf, int len) 52 { 53 struct ip *ih; 54 unsigned short id; 55 char tmp[4]; 56 struct iovec iov[2]; 57 struct msghdr mh; 58 59 ih = (struct ip*) buf; 60 61 /* XXX IP FRAGS */ 62 63 /* filter */ 64 if (ih->ip_p != 0) 65 return 0; 66 67 if (ih->ip_hl != 5) 68 return 0; 69 70 /* get info */ 71 id = ih->ip_id; 72 len -= 20; 73 buf += 20; 74 printf("Got %d bytes [%d]\n", len, ntohs(id)); 75 #if 0 76 hexdump(buf, len); 77 #endif 78 79 /* prepare packet */ 80 memcpy(tmp, &id, 2); 81 id = htons(len); 82 memcpy(&tmp[2], &id, 2); 83 84 iov[0].iov_base = tmp; 85 iov[0].iov_len = 4; 86 iov[1].iov_base = buf; 87 iov[1].iov_len = len; 88 89 memset(&mh, 0, sizeof(mh)); 90 mh.msg_iov = iov; 91 mh.msg_iovlen = sizeof(iov)/sizeof(struct iovec); 92 93 /* write */ 94 if (sendmsg(dude, &mh, 0) != (4 + len)) 95 return -1; 96 return 0; 97 } 98 99 void handle_dude(int dude, int raw) 100 { 101 char buf[4096]; 102 int rd; 103 104 while (1) { 105 rd = recv(raw, buf, sizeof(buf), 0); 106 if (rd == -1) 107 err(1, "recv()"); 108 109 if (handle_data(dude, buf, rd) == -1) 110 return; 111 } 112 } 113 114 void hand(int s) 115 { 116 printf("sigpipe\n"); 117 } 118 119 int main(int argc, char *argv[]) 120 { 121 int s, dude; 122 struct sockaddr_in s_in; 123 int len; 124 int raw; 125 126 memset(&s_in, 0, sizeof(s_in)); 127 s_in.sin_family = PF_INET; 128 s_in.sin_port = htons(666); 129 s_in.sin_addr.s_addr = INADDR_ANY; 130 131 if ((raw = socket(PF_INET, SOCK_RAW, 0)) == -1) 132 err(1, "socket()"); 133 134 if ((s = socket(s_in.sin_family, SOCK_STREAM, IPPROTO_TCP)) == -1) 135 err(1, "socket()"); 136 137 if (bind(s, (struct sockaddr*)&s_in, sizeof(s_in)) == -1) 138 err(1, "bind()"); 139 140 if (listen(s, 5) == -1) 141 err(1, "listen()"); 142 143 if (signal(SIGPIPE, hand) == SIG_ERR) 144 err(1, "signal()"); 145 146 while (1) { 147 len = sizeof(s_in); 148 dude = accept(s, (struct sockaddr*)&s_in, &len); 149 if (dude == -1) 150 err(1, "accept()"); 151 152 printf("Got dude %s\n", inet_ntoa(s_in.sin_addr)); 153 handle_dude(dude, raw); 154 printf("Done\n"); 155 } 156 } 157