1 /* $NetBSD: tcpdrop.c,v 1.2 2007/06/25 23:58:42 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Herb Hasler and Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 /* $OpenBSD: tcpdrop.c,v 1.5 2006/01/03 01:46:27 stevesk Exp $ */
38 * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
40 * Permission to use, copy, modify, and distribute this software for any
41 * purpose with or without fee is hereby granted, provided that the above
42 * copyright notice and this permission notice appear in all copies.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
45 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
47 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
50 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/sysctl.h>
57 #include <netinet/in.h>
58 #include <netinet/tcp.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/tcp_timer.h>
61 #include <netinet/tcp_var.h>
71 char host
[NI_MAXHOST
];
72 char serv
[NI_MAXSERV
];
75 static struct addrinfo
*
76 egetaddrinfo(const char *host
, const char *serv
)
78 static const struct addrinfo hints
= {
79 .ai_family
= AF_UNSPEC
,
80 .ai_socktype
= SOCK_STREAM
,
85 if ((gaierr
= getaddrinfo(host
, serv
, &hints
, &ai
)) != 0)
86 errx(1, "%s port %s: %s", host
, serv
, gai_strerror(gaierr
));
91 egetnameinfo(const struct addrinfo
*ai
, struct hpinfo
*hp
)
95 if ((gaierr
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
96 hp
->host
, sizeof(hp
->host
), hp
->serv
, sizeof(hp
->serv
),
97 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0)
98 errx(1, "getnameinfo: %s", gai_strerror(gaierr
));
102 * Drop a tcp connection.
105 main(int argc
, char **argv
)
107 int mib
[] = { CTL_NET
, 0, IPPROTO_TCP
, TCPCTL_DROP
};
108 struct addrinfo
*ail
, *aif
, *laddr
, *faddr
;
109 struct sockaddr_storage sa
[2];
110 struct hpinfo fhp
, lhp
;
113 setprogname(argv
[0]);
116 (void)fprintf(stderr
, "Usage: %s laddr lport faddr fport\n",
121 laddr
= egetaddrinfo(argv
[1], argv
[2]);
122 faddr
= egetaddrinfo(argv
[3], argv
[4]);
124 for (ail
= laddr
; ail
; ail
= ail
->ai_next
) {
125 for (aif
= faddr
; aif
; aif
= aif
->ai_next
) {
127 if (ail
->ai_family
!= aif
->ai_family
)
130 egetnameinfo(ail
, &lhp
);
131 egetnameinfo(aif
, &fhp
);
133 (void)memset(sa
, 0, sizeof(sa
));
135 assert(aif
->ai_addrlen
<= sizeof(*sa
));
136 assert(ail
->ai_addrlen
<= sizeof(*sa
));
138 (void)memcpy(&sa
[0], aif
->ai_addr
, aif
->ai_addrlen
);
139 (void)memcpy(&sa
[1], ail
->ai_addr
, ail
->ai_addrlen
);
141 switch (ail
->ai_family
) {
149 warnx("Unsupported socket address family %d",
154 if (sysctl(mib
, sizeof(mib
) / sizeof(int), NULL
,
155 NULL
, sa
, sizeof(sa
)) == -1) {
158 lhp
.host
, lhp
.serv
, fhp
.host
, fhp
.serv
);
160 (void)printf("%s:%s %s:%s dropped\n",
161 lhp
.host
, lhp
.serv
, fhp
.host
, fhp
.serv
);