tcpdrop.c (141381) | tcpdrop.c (141886) |
---|---|
1/* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */ 2 3/*- 4 * Copyright (c) 2004 Markus Friedl <markus@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <sys/cdefs.h> | 1/* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */ 2 3/*- 4 * Copyright (c) 2004 Markus Friedl <markus@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <sys/cdefs.h> |
20__FBSDID("$FreeBSD: head/usr.sbin/tcpdrop/tcpdrop.c 141381 2005-02-06 10:47:12Z maxim $"); | 20__FBSDID("$FreeBSD: head/usr.sbin/tcpdrop/tcpdrop.c 141886 2005-02-14 07:37:51Z maxim $"); |
21 22#include <sys/types.h> 23#include <sys/socket.h> 24#include <sys/sysctl.h> 25#include <netinet/in.h> 26#include <netinet/tcp_var.h> 27 28#include <err.h> --- 4 unchanged lines hidden (view full) --- 33 34/* 35 * Drop a tcp connection. 36 */ 37int 38main(int argc, char *argv[]) 39{ 40 struct addrinfo hints, *ail, *aif, *laddr, *faddr; | 21 22#include <sys/types.h> 23#include <sys/socket.h> 24#include <sys/sysctl.h> 25#include <netinet/in.h> 26#include <netinet/tcp_var.h> 27 28#include <err.h> --- 4 unchanged lines hidden (view full) --- 33 34/* 35 * Drop a tcp connection. 36 */ 37int 38main(int argc, char *argv[]) 39{ 40 struct addrinfo hints, *ail, *aif, *laddr, *faddr; |
41 struct tcp_ident_mapping tir; | 41 /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 42 struct sockaddr_storage addrs[2]; |
42 int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_DROP }; 43 int gaierr, rval = 0; 44 char fhbuf[NI_MAXHOST], fsbuf[NI_MAXSERV], lhbuf[NI_MAXHOST], 45 lsbuf[NI_MAXSERV]; 46 47 if (argc != 5) 48 errx(1, "usage: tcpdrop laddr lport faddr fport\n"); 49 memset(&hints, 0, sizeof(hints)); --- 6 unchanged lines hidden (view full) --- 56 freeaddrinfo(laddr); 57 errx(1, "%s port %s: %s", argv[3], argv[4], 58 gai_strerror(gaierr)); 59 } 60 for (ail = laddr; ail; ail = ail->ai_next) { 61 for (aif = faddr; aif; aif = aif->ai_next) { 62 if (ail->ai_family != aif->ai_family) 63 continue; | 43 int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_DROP }; 44 int gaierr, rval = 0; 45 char fhbuf[NI_MAXHOST], fsbuf[NI_MAXSERV], lhbuf[NI_MAXHOST], 46 lsbuf[NI_MAXSERV]; 47 48 if (argc != 5) 49 errx(1, "usage: tcpdrop laddr lport faddr fport\n"); 50 memset(&hints, 0, sizeof(hints)); --- 6 unchanged lines hidden (view full) --- 57 freeaddrinfo(laddr); 58 errx(1, "%s port %s: %s", argv[3], argv[4], 59 gai_strerror(gaierr)); 60 } 61 for (ail = laddr; ail; ail = ail->ai_next) { 62 for (aif = faddr; aif; aif = aif->ai_next) { 63 if (ail->ai_family != aif->ai_family) 64 continue; |
64 memcpy(&tir.faddr, aif->ai_addr, aif->ai_addrlen); 65 memcpy(&tir.laddr, ail->ai_addr, ail->ai_addrlen); | 65 memcpy(&addrs[0], aif->ai_addr, aif->ai_addrlen); 66 memcpy(&addrs[1], ail->ai_addr, ail->ai_addrlen); |
66 if (getnameinfo(aif->ai_addr, aif->ai_addrlen, 67 fhbuf, sizeof(fhbuf), 68 fsbuf, sizeof(fsbuf), 69 NI_NUMERICHOST | NI_NUMERICSERV) == -1) 70 err(1, "getnameinfo"); 71 if (getnameinfo(ail->ai_addr, ail->ai_addrlen, 72 lhbuf, sizeof(lhbuf), 73 lsbuf, sizeof(lsbuf), 74 NI_NUMERICHOST | NI_NUMERICSERV) == -1) 75 err(1, "getnameinfo"); 76 if (sysctl(mib, sizeof (mib) / sizeof (int), NULL, | 67 if (getnameinfo(aif->ai_addr, aif->ai_addrlen, 68 fhbuf, sizeof(fhbuf), 69 fsbuf, sizeof(fsbuf), 70 NI_NUMERICHOST | NI_NUMERICSERV) == -1) 71 err(1, "getnameinfo"); 72 if (getnameinfo(ail->ai_addr, ail->ai_addrlen, 73 lhbuf, sizeof(lhbuf), 74 lsbuf, sizeof(lsbuf), 75 NI_NUMERICHOST | NI_NUMERICSERV) == -1) 76 err(1, "getnameinfo"); 77 if (sysctl(mib, sizeof (mib) / sizeof (int), NULL, |
77 NULL, &tir, sizeof(tir)) == -1) { | 78 NULL, &addrs, sizeof(addrs)) == -1) { |
78 rval = 1; 79 warn("%s %s %s %s", lhbuf, lsbuf, fhbuf, fsbuf); 80 } else 81 printf("%s %s %s %s: dropped\n", 82 lhbuf, lsbuf, fhbuf, fsbuf); 83 } 84 } 85 freeaddrinfo(laddr); 86 freeaddrinfo(faddr); 87 exit(rval); 88} | 79 rval = 1; 80 warn("%s %s %s %s", lhbuf, lsbuf, fhbuf, fsbuf); 81 } else 82 printf("%s %s %s %s: dropped\n", 83 lhbuf, lsbuf, fhbuf, fsbuf); 84 } 85 } 86 freeaddrinfo(laddr); 87 freeaddrinfo(faddr); 88 exit(rval); 89} |