tap.cc (105:5440d7a4f376) | tap.cc (202:aa3db11c4e84) |
---|---|
1/* 2 * Copyright (c) 2003 The Regents of The University of Michigan 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 are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 57 unchanged lines hidden (view full) --- 66 "\t%s [-b bufsize] [-d] [-f filter] [-p port] [-v] <device> <host>\n" 67 "\t%s [-b bufsize] [-d] [-f filter] [-l] [-p port] [-v] <device>\n", 68 program, program); 69 exit(2); 70} 71 72int verbose = 0; 73#define DPRINTF(args...) do { \ | 1/* 2 * Copyright (c) 2003 The Regents of The University of Michigan 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 are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 57 unchanged lines hidden (view full) --- 66 "\t%s [-b bufsize] [-d] [-f filter] [-p port] [-v] <device> <host>\n" 67 "\t%s [-b bufsize] [-d] [-f filter] [-l] [-p port] [-v] <device>\n", 68 program, program); 69 exit(2); 70} 71 72int verbose = 0; 73#define DPRINTF(args...) do { \ |
74 if (verbose > 1) \ | 74 if (verbose >= 1) \ |
75 cprintf(args); \ 76} while (0) 77 78#define DDUMP(args...) do { \ | 75 cprintf(args); \ 76} while (0) 77 78#define DDUMP(args...) do { \ |
79 if (verbose > 2) \ | 79 if (verbose >= 2) \ |
80 dump((const u_char *)args); \ 81} while (0) 82 83void 84dump(const u_char *data, int len) 85{ 86 int c, i, j; 87 --- 33 unchanged lines hidden (view full) --- 121} 122 123 124int 125Socket(int reuse) 126{ 127 int fd = ::socket(PF_INET, SOCK_STREAM, 0); 128 if (fd < 0) | 80 dump((const u_char *)args); \ 81} while (0) 82 83void 84dump(const u_char *data, int len) 85{ 86 int c, i, j; 87 --- 33 unchanged lines hidden (view full) --- 121} 122 123 124int 125Socket(int reuse) 126{ 127 int fd = ::socket(PF_INET, SOCK_STREAM, 0); 128 if (fd < 0) |
129 panic("Can't create socket!"); | 129 panic("Can't create socket!\n"); |
130 131 if (reuse) { 132 int i = 1; 133 if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i, 134 sizeof(i)) < 0) | 130 131 if (reuse) { 132 int i = 1; 133 if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i, 134 sizeof(i)) < 0) |
135 panic("setsockopt() SO_REUSEADDR failed!"); | 135 panic("setsockopt() SO_REUSEADDR failed!\n"); |
136 } 137 138 return fd; 139} 140 141void 142Listen(int fd, int port) 143{ 144 struct sockaddr_in sockaddr; 145 sockaddr.sin_family = PF_INET; 146 sockaddr.sin_addr.s_addr = INADDR_ANY; 147 148 sockaddr.sin_port = htons(port); 149 int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr)); 150 if (ret == -1) | 136 } 137 138 return fd; 139} 140 141void 142Listen(int fd, int port) 143{ 144 struct sockaddr_in sockaddr; 145 sockaddr.sin_family = PF_INET; 146 sockaddr.sin_addr.s_addr = INADDR_ANY; 147 148 sockaddr.sin_port = htons(port); 149 int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr)); 150 if (ret == -1) |
151 panic("bind() failed!"); | 151 panic("bind() failed!\n"); |
152 153 if (::listen(fd, 1) == -1) | 152 153 if (::listen(fd, 1) == -1) |
154 panic("listen() failed!"); | 154 panic("listen() failed!\n"); |
155} 156 157// Open a connection. Accept will block, so if you don't want it to, 158// make sure a connection is ready before you call accept. 159int 160Accept(int fd, bool nodelay) 161{ 162 struct sockaddr_in sockaddr; 163 socklen_t slen = sizeof (sockaddr); 164 int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen); 165 if (sfd == -1) | 155} 156 157// Open a connection. Accept will block, so if you don't want it to, 158// make sure a connection is ready before you call accept. 159int 160Accept(int fd, bool nodelay) 161{ 162 struct sockaddr_in sockaddr; 163 socklen_t slen = sizeof (sockaddr); 164 int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen); 165 if (sfd == -1) |
166 panic("accept() failed!"); | 166 panic("accept() failed!\n"); |
167 168 if (nodelay) { 169 int i = 1; 170 ::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, sizeof(i)); 171 } 172 return sfd; 173} 174 --- 8 unchanged lines hidden (view full) --- 183 panic("Host %s not found\n", host); 184 185 sockaddr.sin_family = hp->h_addrtype; 186 memcpy(&sockaddr.sin_addr, hp->h_addr, hp->h_length); 187 } 188 189 sockaddr.sin_port = htons(port); 190 if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) | 167 168 if (nodelay) { 169 int i = 1; 170 ::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, sizeof(i)); 171 } 172 return sfd; 173} 174 --- 8 unchanged lines hidden (view full) --- 183 panic("Host %s not found\n", host); 184 185 sockaddr.sin_family = hp->h_addrtype; 186 memcpy(&sockaddr.sin_addr, hp->h_addr, hp->h_length); 187 } 188 189 sockaddr.sin_port = htons(port); 190 if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) |
191 panic("could not connect to %s on port %d", host, port); | 191 panic("could not connect to %s on port %d\n", host, port); |
192 193 DPRINTF("connected to %s on port %d\n", host, port); 194} 195 196int 197main(int argc, char *argv[]) 198{ 199 int port = 3500; --- 64 unchanged lines hidden (view full) --- 264 } else { 265 if (argc != 1) 266 usage(); 267 268 host = *argv; 269 } 270 271 char errbuf[PCAP_ERRBUF_SIZE]; | 192 193 DPRINTF("connected to %s on port %d\n", host, port); 194} 195 196int 197main(int argc, char *argv[]) 198{ 199 int port = 3500; --- 64 unchanged lines hidden (view full) --- 264 } else { 265 if (argc != 1) 266 usage(); 267 268 host = *argv; 269 } 270 271 char errbuf[PCAP_ERRBUF_SIZE]; |
272 memset(errbuf, 0, sizeof errbuf); |
|
272 pcap_t *pcap = pcap_open_live(device, 1500, 1, -1, errbuf); 273 if (pcap == NULL) 274 panic("pcap_open_live failed: %s\n", errbuf); 275 276 bpf_program program; 277 bpf_u_int32 localnet, netmask; | 273 pcap_t *pcap = pcap_open_live(device, 1500, 1, -1, errbuf); 274 if (pcap == NULL) 275 panic("pcap_open_live failed: %s\n", errbuf); 276 277 bpf_program program; 278 bpf_u_int32 localnet, netmask; |
278 if (!pcap_lookupnet(device, &localnet, &netmask, errbuf)) | 279 if (pcap_lookupnet(device, &localnet, &netmask, errbuf) == -1) |
279 panic("pcap_lookupnet failed: %s\n", errbuf); 280 281 if (pcap_compile(pcap, &program, filter, 1, netmask) == -1) 282 panic("pcap_compile failed, invalid filter:\n%s\n", filter); 283 284 if (pcap_setfilter(pcap, &program) == -1) 285 panic("pcap_setfilter failed\n"); 286 287 eth_t *ethernet = eth_open(device); | 280 panic("pcap_lookupnet failed: %s\n", errbuf); 281 282 if (pcap_compile(pcap, &program, filter, 1, netmask) == -1) 283 panic("pcap_compile failed, invalid filter:\n%s\n", filter); 284 285 if (pcap_setfilter(pcap, &program) == -1) 286 panic("pcap_setfilter failed\n"); 287 288 eth_t *ethernet = eth_open(device); |
289 if (!ethernet) 290 panic("cannot open the ethernet device for writing\n"); |
|
288 289 pollfd pfds[3]; 290 pfds[0].fd = Socket(true); 291 pfds[0].events = POLLIN; 292 pfds[0].revents = 0; 293 294 if (listening) 295 Listen(pfds[0].fd, port); --- 11 unchanged lines hidden (view full) --- 307 pollfd *listen_pfd = listening ? &pfds[0] : NULL; 308 pollfd *tap_pfd = &pfds[1]; 309 pollfd *client_pfd = listening ? NULL : &pfds[0]; 310 int npfds = 2; 311 312 int32_t buffer_offset = 0; 313 int32_t data_len = 0; 314 | 291 292 pollfd pfds[3]; 293 pfds[0].fd = Socket(true); 294 pfds[0].events = POLLIN; 295 pfds[0].revents = 0; 296 297 if (listening) 298 Listen(pfds[0].fd, port); --- 11 unchanged lines hidden (view full) --- 310 pollfd *listen_pfd = listening ? &pfds[0] : NULL; 311 pollfd *tap_pfd = &pfds[1]; 312 pollfd *client_pfd = listening ? NULL : &pfds[0]; 313 int npfds = 2; 314 315 int32_t buffer_offset = 0; 316 int32_t data_len = 0; 317 |
318 DPRINTF("Begin poll loop\n"); |
|
315 while (!quit) { 316 int ret = ::poll(pfds, npfds, INFTIM); 317 if (ret < 0) 318 continue; 319 320 if (listen_pfd && listen_pfd->revents) { 321 if (listen_pfd->revents & POLLIN) { 322 int fd = Accept(listen_pfd->fd, false); --- 64 unchanged lines hidden (view full) --- 387 if (client_pfd->revents & POLLERR) { 388 error: 389 DPRINTF("Error on client socket\n"); 390 close(client_pfd->fd); 391 client_pfd = NULL; 392 393 if (listening) 394 npfds--; | 319 while (!quit) { 320 int ret = ::poll(pfds, npfds, INFTIM); 321 if (ret < 0) 322 continue; 323 324 if (listen_pfd && listen_pfd->revents) { 325 if (listen_pfd->revents & POLLIN) { 326 int fd = Accept(listen_pfd->fd, false); --- 64 unchanged lines hidden (view full) --- 391 if (client_pfd->revents & POLLERR) { 392 error: 393 DPRINTF("Error on client socket\n"); 394 close(client_pfd->fd); 395 client_pfd = NULL; 396 397 if (listening) 398 npfds--; |
395 else | 399 else { 400 DPRINTF("Calling it quits because of poll error\n"); |
396 quit = true; | 401 quit = true; |
402 } |
|
397 } 398 399 if (client_pfd) 400 client_pfd->revents = 0; 401 } | 403 } 404 405 if (client_pfd) 406 client_pfd->revents = 0; 407 } |
402 | |
403 } 404 405 delete [] buffer; 406 pcap_close(pcap); 407 eth_close(ethernet); 408 if (listen_pfd) 409 close(listen_pfd->fd); 410 411 if (client_pfd) 412 close(client_pfd->fd); 413 414 return 0; 415} | 408 } 409 410 delete [] buffer; 411 pcap_close(pcap); 412 eth_close(ethernet); 413 if (listen_pfd) 414 close(listen_pfd->fd); 415 416 if (client_pfd) 417 close(client_pfd->fd); 418 419 return 0; 420} |