blob: 64beba82baa2f7c14d10f05a97e19f7fee30512e [file] [log] [blame]
Eric Andersen7467c8d2001-07-12 20:26:32 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Denis Vlasenko4e6d5112008-03-12 22:14:34 +00005 * create raw socket for icmp protocol
Eric Andersenaff114c2004-04-14 17:51:38 +00006 * and drop root privileges if running setuid
Eric Andersen7467c8d2001-07-12 20:26:32 +00007 */
8
Eric Andersen7467c8d2001-07-12 20:26:32 +00009#include "libbb.h"
10
11int create_icmp_socket(void)
12{
Eric Andersen7467c8d2001-07-12 20:26:32 +000013 int sock;
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000014#if 0
15 struct protoent *proto;
Eric Andersen7467c8d2001-07-12 20:26:32 +000016 proto = getprotobyname("icmp");
17 /* if getprotobyname failed, just silently force
18 * proto->p_proto to have the correct value for "icmp" */
Denis Vlasenko19c238b2007-03-03 00:36:35 +000019 sock = socket(AF_INET, SOCK_RAW,
20 (proto ? proto->p_proto : 1)); /* 1 == ICMP */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000021#else
22 sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
23#endif
Denis Vlasenko8e858e22007-03-07 09:35:43 +000024 if (sock < 0) {
Eric Andersen7467c8d2001-07-12 20:26:32 +000025 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko19c238b2007-03-03 00:36:35 +000027 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
Eric Andersen7467c8d2001-07-12 20:26:32 +000028 }
29
30 /* drop root privs if running setuid */
Rob Landley53437472006-07-16 08:14:35 +000031 xsetuid(getuid());
Eric Andersen7467c8d2001-07-12 20:26:32 +000032
33 return sock;
34}