Skip to content
Newer
Older
100644 168 lines (152 sloc) 5.96 KB
57ba30f @msantos Add a license.
authored Feb 5, 2010
1 /* Copyright (c) 2005-2010, Michael Santos <michael.santos@gmail.com>
2 * All rights reserved.
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
3 *
57ba30f @msantos Add a license.
authored Feb 5, 2010
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
7 *
57ba30f @msantos Add a license.
authored Feb 5, 2010
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
10 *
57ba30f @msantos Add a license.
authored Feb 5, 2010
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
14 *
57ba30f @msantos Add a license.
authored Feb 5, 2010
15 * Neither the name of the author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
18 *
57ba30f @msantos Add a license.
authored Feb 5, 2010
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
33 /*
ee8b9c1 @msantos Imported from http://code.google.com/p/tweag/ cause I <3 github
authored Dec 29, 2009
34 * rst: TCP connect reset utility
35 *
36 * Aggressively resets TCP connections using TCP RST's or
37 * ICMP.
38 *
39 */
40 #include "rst.h"
41
42 int
43 main(int argc, char *argv[])
44 {
45 pkt_t *rst = NULL;
46
47 int ch = 0;
48 int exclude = 0;
49 char *host = NULL;
50 in_port_t port = 0;
51
52 char *user = strdup(RST_USER);
53 char *group = strdup(RST_GROUP);
54 char *path = strdup(RST_DIR);
55
56 ISNULL(rst = (pkt_t *)calloc(1, sizeof(pkt_t)));
57
58 rst->flags = RST_TCP_RST;
59 rst->icmp.type = ICMP_UNREACH; /* Default is ICMP unreachable (type 3) */
60 rst->icmp.code = ICMP_UNREACH_PORT; /* Default is ICMP port unreachable (code 3) */
61
62 while ( (ch = getopt(argc, argv, "c:d:g:hH:i:Ip:rR:t:u:xX:")) != EOF) {
63 switch (ch) {
64 case 'c': /* ICMP code */
65 rst->icmp.code = (u_int8_t)atoi(optarg);
66 rst->flags = RST_ICMP;
67 break;
68 case 'd': /* chroot path */
69 path = strdup(optarg);
70 break;
71 case 'g': /* unprivileged group */
72 group = strdup(optarg);
73 break;
74 case 'h': /* Help */
75 usage();
76 break;
77 case 'H':
78 if (strlen(optarg) > MAXHOSTNAMELEN)
79 errx(EXIT_FAILURE, "Hostname too long");
80 host = strdup(optarg);
81 break;
82 case 'i': /* Use interface */
83 rst->dev = strdup(optarg);
84 break;
85 case 'I':
86 rst->flags = RST_ICMP;
87 break;
88 case 'p': /* list of ports */
89 port = (in_port_t)atoi(optarg);
90 break;
91 case 'r':
92 rst->flags = RST_TCP_RST; /* TCP RST */
93 break;
94 case 'R':
95 rst->sleep_for = MICROSECONDS / (useconds_t)atoi(optarg); /* Maximum number of packets sent per second */
96 (void)fprintf(stderr, "[%s] Sleeping for %u usec between sending packets\n", __progname, rst->sleep_for);
97 break;
98 case 't': /* ICMP type */
99 rst->icmp.type = (u_int8_t)atoi(optarg);
100 break;
101 case 'u': /* unprivileged user */
102 user = strdup(optarg);
103 break;
104 case 'x': /* Exclude our IP address */
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
105 exclude = 1;
ee8b9c1 @msantos Imported from http://code.google.com/p/tweag/ cause I <3 github
authored Dec 29, 2009
106 break;
107 case 'X': /* Exclude by specifying our IP address */
108 exclude = 1;
f599839 @msantos Antidote for the trailing whitespace plague.
authored Feb 20, 2010
109 rst->myip = strdup(optarg);
ee8b9c1 @msantos Imported from http://code.google.com/p/tweag/ cause I <3 github
authored Dec 29, 2009
110 break;
111 default:
112 usage();
113 break;
114 }
115 }
116
117 rst_net_check(rst);
118
119 ISNULL(rst->l = rst_net_open(rst->dev));
120 ISNULL(rst->p = rst_pcap_open(rst->dev));
121
122 /* Drop privileges */
123 if (rst_priv_drop(user, group, path) != 0)
124 exit (EXIT_FAILURE);
125
126 ISLTZERO(rst_pcap_init(rst, host, port, exclude));
127 ISLTZERO(rst_pcap_run(rst));
128
129 ISLTZERO(rst_net_close(rst->l));
130 ISLTZERO(rst_pcap_close(rst->p));
131 if (rst->dev)
132 free(rst->dev);
133 if (rst->myip)
134 free(rst->myip);
135 if (host)
136 free(host);
137
138 free(user);
139 free(group);
140 free(path);
141
142 exit (EXIT_FAILURE);
143 }
144
145 void
146 usage(void)
147 {
0dad4d3 @msantos Clean up help.
authored Jan 3, 2010
148 (void)fprintf(stderr, "[%s v%s: Reset TCP connections]\n",
ee8b9c1 @msantos Imported from http://code.google.com/p/tweag/ cause I <3 github
authored Dec 29, 2009
149 __progname, RST_BUILD);
0dad4d3 @msantos Clean up help.
authored Jan 4, 2010
150 (void)fprintf(stderr, "Usage: %s [-h|-H <host>|-i <interface>|-I <icmp type>|-p <port>|-R <number>|-x|-X <ip>]\n"
151 "-h usage\n\n"
152 "-x exclude our IP address\n"
153 "-X <IP Address> specify an IP address to exclude\n\n"
154 "-H <host> hostname\n"
155 "-p <port> port to reset\n"
156 "-i <interface> interface\n"
157 "-R <number> rate limit, number of packets per second\n\n"
158 "-d <path> chroot path (default = %s)\n"
159 "-u <user> unprivliged user (default = %s)\n"
160 "-g <group> unprivilged group (default = %s)\n\n"
161 "-c <ICMP code> code to use for ICMP resets\n"
162 "-I reset using ICMP\n"
163 "-t <ICMP type> type to use for ICMP resets\n\n"
164 "[Bug reports to michael.santos@gmail.com]\n",
165 __progname, RST_DIR, RST_USER, RST_GROUP);
ee8b9c1 @msantos Imported from http://code.google.com/p/tweag/ cause I <3 github
authored Dec 29, 2009
166 exit (EXIT_FAILURE);
167 }
Something went wrong with that request. Please try again.