1 /* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */
4 * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
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.
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.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
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>
35 * Drop a tcp connection.
38 main(int argc, char *argv[])
40 struct addrinfo hints, *ail, *aif, *laddr, *faddr;
41 /* addrs[0] is a foreign socket, addrs[1] is a local one. */
42 struct sockaddr_storage addrs[2];
43 int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_DROP };
45 char fhbuf[NI_MAXHOST], fsbuf[NI_MAXSERV], lhbuf[NI_MAXHOST],
49 errx(1, "usage: tcpdrop laddr lport faddr fport\n");
50 memset(&hints, 0, sizeof(hints));
51 hints.ai_family = AF_UNSPEC;
52 hints.ai_socktype = SOCK_STREAM;
53 if ((gaierr = getaddrinfo(argv[1], argv[2], &hints, &laddr)) != 0)
54 errx(1, "%s port %s: %s", argv[1], argv[2],
55 gai_strerror(gaierr));
56 if ((gaierr = getaddrinfo(argv[3], argv[4], &hints, &faddr)) != 0) {
58 errx(1, "%s port %s: %s", argv[3], argv[4],
59 gai_strerror(gaierr));
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)
65 memcpy(&addrs[0], aif->ai_addr, aif->ai_addrlen);
66 memcpy(&addrs[1], ail->ai_addr, ail->ai_addrlen);
67 if (getnameinfo(aif->ai_addr, aif->ai_addrlen,
70 NI_NUMERICHOST | NI_NUMERICSERV) == -1)
71 err(1, "getnameinfo");
72 if (getnameinfo(ail->ai_addr, ail->ai_addrlen,
75 NI_NUMERICHOST | NI_NUMERICSERV) == -1)
76 err(1, "getnameinfo");
77 if (sysctl(mib, sizeof (mib) / sizeof (int), NULL,
78 NULL, &addrs, sizeof(addrs)) == -1) {
80 warn("%s %s %s %s", lhbuf, lsbuf, fhbuf, fsbuf);
82 printf("%s %s %s %s: dropped\n",
83 lhbuf, lsbuf, fhbuf, fsbuf);