]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/tcpdrop/tcpdrop.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / usr.sbin / tcpdrop / tcpdrop.c
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$");
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>
29 #include <netdb.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 /*
35  * Drop a tcp connection.
36  */
37 int
38 main(int argc, char *argv[])
39 {
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 };
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                 fprintf(stderr, "usage: tcpdrop laddr lport faddr fport\n");
50                 exit(1);
51         }
52         memset(&hints, 0, sizeof(hints));
53         hints.ai_family = AF_UNSPEC;
54         hints.ai_socktype = SOCK_STREAM;
55         if ((gaierr = getaddrinfo(argv[1], argv[2], &hints, &laddr)) != 0)
56                 errx(1, "%s port %s: %s", argv[1], argv[2],
57                     gai_strerror(gaierr));
58         if ((gaierr = getaddrinfo(argv[3], argv[4], &hints, &faddr)) != 0) {
59                 freeaddrinfo(laddr);
60                 errx(1, "%s port %s: %s", argv[3], argv[4],
61                     gai_strerror(gaierr));
62         }
63         for (ail = laddr; ail; ail = ail->ai_next) {
64                 for (aif = faddr; aif; aif = aif->ai_next) {
65                         if (ail->ai_family != aif->ai_family)
66                                 continue;
67                         memcpy(&addrs[0], aif->ai_addr, aif->ai_addrlen);
68                         memcpy(&addrs[1], ail->ai_addr, ail->ai_addrlen);
69                         if (getnameinfo(aif->ai_addr, aif->ai_addrlen,
70                             fhbuf, sizeof(fhbuf),
71                             fsbuf, sizeof(fsbuf),
72                             NI_NUMERICHOST | NI_NUMERICSERV) == -1)
73                                 err(1, "getnameinfo");
74                         if (getnameinfo(ail->ai_addr, ail->ai_addrlen,
75                             lhbuf, sizeof(lhbuf),
76                             lsbuf, sizeof(lsbuf),
77                             NI_NUMERICHOST | NI_NUMERICSERV) == -1)
78                                 err(1, "getnameinfo");
79                         if (sysctl(mib, sizeof (mib) / sizeof (int), NULL,
80                             NULL, &addrs, sizeof(addrs)) == -1) {
81                                 rval = 1;
82                                 warn("%s %s %s %s", lhbuf, lsbuf, fhbuf, fsbuf);
83                         } else
84                                 printf("%s %s %s %s: dropped\n",
85                                     lhbuf, lsbuf, fhbuf, fsbuf);
86                 }
87         }
88         freeaddrinfo(laddr);
89         freeaddrinfo(faddr);
90         exit(rval);
91 }