aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2013-06-28 00:30:46 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2013-06-28 00:30:46 +0200
commit59f46676a9c03303809f40d1a27c4d2e9e640697 (patch)
treecb95d1c4f3da2e3ebc9358917a2aac3dafa6cc50
parent4928e9f7d0a17898300f20567a331417fafc16c1 (diff)
downloadbusybox-w32-59f46676a9c03303809f40d1a27c4d2e9e640697.tar.gz
busybox-w32-59f46676a9c03303809f40d1a27c4d2e9e640697.tar.bz2
busybox-w32-59f46676a9c03303809f40d1a27c4d2e9e640697.zip
Move create_icmp[6]_socket to its only user, and simplify it
function old new delta run_applet_and_exit 711 714 +3 sendping_tail 239 236 -3 common_ping_main 1798 1770 -28 create_icmp_socket 65 - -65 create_icmp6_socket 65 - -65 ------------------------------------------------------------------------------ (add/remove: 0/4 grow/shrink: 1/2 up/down: 3/-161) Total: -158 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h3
-rw-r--r--libbb/Kbuild.src2
-rw-r--r--libbb/create_icmp6_socket.c38
-rw-r--r--libbb/create_icmp_socket.c36
-rw-r--r--networking/ping.c64
5 files changed, 44 insertions, 99 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 65c6b9f39..085210431 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1109,9 +1109,6 @@ void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
1109 1109
1110 1110
1111/* Networking */ 1111/* Networking */
1112int create_icmp_socket(void) FAST_FUNC;
1113int create_icmp6_socket(void) FAST_FUNC;
1114/* interface.c */
1115/* This structure defines protocol families and their handlers. */ 1112/* This structure defines protocol families and their handlers. */
1116struct aftype { 1113struct aftype {
1117 const char *name; 1114 const char *name;
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index 61eec26f7..c5d86c092 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -27,8 +27,6 @@ lib-y += concat_subpath_file.o
27lib-y += copy_file.o 27lib-y += copy_file.o
28lib-y += copyfd.o 28lib-y += copyfd.o
29lib-y += crc32.o 29lib-y += crc32.o
30lib-y += create_icmp6_socket.o
31lib-y += create_icmp_socket.o
32lib-y += default_error_retval.o 30lib-y += default_error_retval.o
33lib-y += device_open.o 31lib-y += device_open.o
34lib-y += dump.o 32lib-y += dump.o
diff --git a/libbb/create_icmp6_socket.c b/libbb/create_icmp6_socket.c
deleted file mode 100644
index 368c69028..000000000
--- a/libbb/create_icmp6_socket.c
+++ /dev/null
@@ -1,38 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * create raw socket for icmp (IPv6 version) protocol
6 * and drop root privileges if running setuid
7 *
8 * Licensed under GPLv2, see file LICENSE in this source tree.
9 */
10
11#include "libbb.h"
12
13#if ENABLE_FEATURE_IPV6
14int FAST_FUNC create_icmp6_socket(void)
15{
16 int sock;
17#if 0
18 struct protoent *proto;
19 proto = getprotobyname("ipv6-icmp");
20 /* if getprotobyname failed, just silently force
21 * proto->p_proto to have the correct value for "ipv6-icmp" */
22 sock = socket(AF_INET6, SOCK_RAW,
23 (proto ? proto->p_proto : IPPROTO_ICMPV6));
24#else
25 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
26#endif
27 if (sock < 0) {
28 if (errno == EPERM)
29 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
30 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
31 }
32
33 /* drop root privs if running setuid */
34 xsetuid(getuid());
35
36 return sock;
37}
38#endif
diff --git a/libbb/create_icmp_socket.c b/libbb/create_icmp_socket.c
deleted file mode 100644
index 585626983..000000000
--- a/libbb/create_icmp_socket.c
+++ /dev/null
@@ -1,36 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * create raw socket for icmp protocol
6 * and drop root privileges if running setuid
7 *
8 * Licensed under GPLv2, see file LICENSE in this source tree.
9 */
10
11#include "libbb.h"
12
13int FAST_FUNC create_icmp_socket(void)
14{
15 int sock;
16#if 0
17 struct protoent *proto;
18 proto = getprotobyname("icmp");
19 /* if getprotobyname failed, just silently force
20 * proto->p_proto to have the correct value for "icmp" */
21 sock = socket(AF_INET, SOCK_RAW,
22 (proto ? proto->p_proto : 1)); /* 1 == ICMP */
23#else
24 sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
25#endif
26 if (sock < 0) {
27 if (errno == EPERM)
28 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
29 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
30 }
31
32 /* drop root privs if running setuid */
33 xsetuid(getuid());
34
35 return sock;
36}
diff --git a/networking/ping.c b/networking/ping.c
index e919b3a09..8c08c8074 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -149,8 +149,33 @@ enum {
149 MAX_DUP_CHK = (8 * 128), 149 MAX_DUP_CHK = (8 * 128),
150 MAXWAIT = 10, 150 MAXWAIT = 10,
151 PINGINTERVAL = 1, /* 1 second */ 151 PINGINTERVAL = 1, /* 1 second */
152 pingsock = 0,
152}; 153};
153 154
155static void
156#if ENABLE_PING6
157create_icmp_socket(len_and_sockaddr *lsa)
158#else
159create_icmp_socket(void)
160#define create_icmp_socket(lsa) create_icmp_socket()
161#endif
162{
163 int sock;
164#if ENABLE_PING6
165 if (lsa->u.sa.sa_family == AF_INET6)
166 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
167 else
168#endif
169 sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
170 if (sock < 0) {
171 if (errno == EPERM)
172 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
173 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
174 }
175
176 xmove_fd(sock, pingsock);
177}
178
154#if !ENABLE_FEATURE_FANCY_PING 179#if !ENABLE_FEATURE_FANCY_PING
155 180
156/* Simple version */ 181/* Simple version */
@@ -171,12 +196,10 @@ static void noresp(int ign UNUSED_PARAM)
171static void ping4(len_and_sockaddr *lsa) 196static void ping4(len_and_sockaddr *lsa)
172{ 197{
173 struct icmp *pkt; 198 struct icmp *pkt;
174 int pingsock, c; 199 int c;
175
176 pingsock = create_icmp_socket();
177 200
178 pkt = (struct icmp *) G.packet; 201 pkt = (struct icmp *) G.packet;
179 memset(pkt, 0, sizeof(G.packet)); 202 /*memset(pkt, 0, sizeof(G.packet)); already is */
180 pkt->icmp_type = ICMP_ECHO; 203 pkt->icmp_type = ICMP_ECHO;
181 pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet)); 204 pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet));
182 205
@@ -184,11 +207,15 @@ static void ping4(len_and_sockaddr *lsa)
184 207
185 /* listen for replies */ 208 /* listen for replies */
186 while (1) { 209 while (1) {
210#if 0
187 struct sockaddr_in from; 211 struct sockaddr_in from;
188 socklen_t fromlen = sizeof(from); 212 socklen_t fromlen = sizeof(from);
189 213
190 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0, 214 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
191 (struct sockaddr *) &from, &fromlen); 215 (struct sockaddr *) &from, &fromlen);
216#else
217 c = recv(pingsock, G.packet, sizeof(G.packet), 0);
218#endif
192 if (c < 0) { 219 if (c < 0) {
193 if (errno != EINTR) 220 if (errno != EINTR)
194 bb_perror_msg("recvfrom"); 221 bb_perror_msg("recvfrom");
@@ -210,13 +237,11 @@ static void ping4(len_and_sockaddr *lsa)
210static void ping6(len_and_sockaddr *lsa) 237static void ping6(len_and_sockaddr *lsa)
211{ 238{
212 struct icmp6_hdr *pkt; 239 struct icmp6_hdr *pkt;
213 int pingsock, c; 240 int c;
214 int sockopt; 241 int sockopt;
215 242
216 pingsock = create_icmp6_socket();
217
218 pkt = (struct icmp6_hdr *) G.packet; 243 pkt = (struct icmp6_hdr *) G.packet;
219 memset(pkt, 0, sizeof(G.packet)); 244 /*memset(pkt, 0, sizeof(G.packet)); already is */
220 pkt->icmp6_type = ICMP6_ECHO_REQUEST; 245 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
221 246
222 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); 247 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
@@ -226,18 +251,21 @@ static void ping6(len_and_sockaddr *lsa)
226 251
227 /* listen for replies */ 252 /* listen for replies */
228 while (1) { 253 while (1) {
254#if 0
229 struct sockaddr_in6 from; 255 struct sockaddr_in6 from;
230 socklen_t fromlen = sizeof(from); 256 socklen_t fromlen = sizeof(from);
231 257
232 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0, 258 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
233 (struct sockaddr *) &from, &fromlen); 259 (struct sockaddr *) &from, &fromlen);
260#else
261 c = recv(pingsock, G.packet, sizeof(G.packet), 0);
262#endif
234 if (c < 0) { 263 if (c < 0) {
235 if (errno != EINTR) 264 if (errno != EINTR)
236 bb_perror_msg("recvfrom"); 265 bb_perror_msg("recvfrom");
237 continue; 266 continue;
238 } 267 }
239 if (c >= ICMP_MINLEN) { /* icmp6_hdr */ 268 if (c >= ICMP_MINLEN) { /* icmp6_hdr */
240 pkt = (struct icmp6_hdr *) G.packet;
241 if (pkt->icmp6_type == ICMP6_ECHO_REPLY) 269 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
242 break; 270 break;
243 } 271 }
@@ -285,6 +313,7 @@ static int common_ping_main(sa_family_t af, char **argv)
285 signal(SIGALRM, noresp); 313 signal(SIGALRM, noresp);
286 alarm(5); /* give the host 5000ms to respond */ 314 alarm(5); /* give the host 5000ms to respond */
287 315
316 create_icmp_socket(lsa);
288#if ENABLE_PING6 317#if ENABLE_PING6
289 if (lsa->u.sa.sa_family == AF_INET6) 318 if (lsa->u.sa.sa_family == AF_INET6)
290 ping6(lsa); 319 ping6(lsa);
@@ -318,7 +347,6 @@ enum {
318 347
319 348
320struct globals { 349struct globals {
321 int pingsock;
322 int if_index; 350 int if_index;
323 char *str_I; 351 char *str_I;
324 len_and_sockaddr *source_lsa; 352 len_and_sockaddr *source_lsa;
@@ -347,7 +375,6 @@ struct globals {
347 char rcvd_tbl[MAX_DUP_CHK / 8]; 375 char rcvd_tbl[MAX_DUP_CHK / 8];
348} FIX_ALIASING; 376} FIX_ALIASING;
349#define G (*(struct globals*)&bb_common_bufsiz1) 377#define G (*(struct globals*)&bb_common_bufsiz1)
350#define pingsock (G.pingsock )
351#define if_index (G.if_index ) 378#define if_index (G.if_index )
352#define source_lsa (G.source_lsa ) 379#define source_lsa (G.source_lsa )
353#define str_I (G.str_I ) 380#define str_I (G.str_I )
@@ -369,7 +396,6 @@ void BUG_ping_globals_too_big(void);
369#define INIT_G() do { \ 396#define INIT_G() do { \
370 if (sizeof(G) > COMMON_BUFSIZE) \ 397 if (sizeof(G) > COMMON_BUFSIZE) \
371 BUG_ping_globals_too_big(); \ 398 BUG_ping_globals_too_big(); \
372 pingsock = -1; \
373 datalen = DEFDATALEN; \ 399 datalen = DEFDATALEN; \
374 timeout = MAXWAIT; \ 400 timeout = MAXWAIT; \
375 tmin = UINT_MAX; \ 401 tmin = UINT_MAX; \
@@ -655,7 +681,6 @@ static void ping4(len_and_sockaddr *lsa)
655{ 681{
656 int sockopt; 682 int sockopt;
657 683
658 pingsock = create_icmp_socket();
659 pingaddr.sin = lsa->u.sin; 684 pingaddr.sin = lsa->u.sin;
660 if (source_lsa) { 685 if (source_lsa) {
661 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF, 686 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
@@ -663,8 +688,6 @@ static void ping4(len_and_sockaddr *lsa)
663 bb_error_msg_and_die("can't set multicast source interface"); 688 bb_error_msg_and_die("can't set multicast source interface");
664 xbind(pingsock, &source_lsa->u.sa, source_lsa->len); 689 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
665 } 690 }
666 if (str_I)
667 setsockopt_bindtodevice(pingsock, str_I);
668 691
669 /* enable broadcast pings */ 692 /* enable broadcast pings */
670 setsockopt_broadcast(pingsock); 693 setsockopt_broadcast(pingsock);
@@ -713,13 +736,9 @@ static void ping6(len_and_sockaddr *lsa)
713 struct iovec iov; 736 struct iovec iov;
714 char control_buf[CMSG_SPACE(36)]; 737 char control_buf[CMSG_SPACE(36)];
715 738
716 pingsock = create_icmp6_socket();
717 pingaddr.sin6 = lsa->u.sin6; 739 pingaddr.sin6 = lsa->u.sin6;
718 /* untested whether "-I addr" really works for IPv6: */
719 if (source_lsa) 740 if (source_lsa)
720 xbind(pingsock, &source_lsa->u.sa, source_lsa->len); 741 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
721 if (str_I)
722 setsockopt_bindtodevice(pingsock, str_I);
723 742
724#ifdef ICMP6_FILTER 743#ifdef ICMP6_FILTER
725 { 744 {
@@ -806,6 +825,11 @@ static void ping(len_and_sockaddr *lsa)
806 } 825 }
807 printf(": %d data bytes\n", datalen); 826 printf(": %d data bytes\n", datalen);
808 827
828 create_icmp_socket(lsa);
829 /* untested whether "-I addr" really works for IPv6: */
830 if (str_I)
831 setsockopt_bindtodevice(pingsock, str_I);
832
809 G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN; 833 G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
810 G.rcv_packet = xzalloc(G.sizeof_rcv_packet); 834 G.rcv_packet = xzalloc(G.sizeof_rcv_packet);
811#if ENABLE_PING6 835#if ENABLE_PING6