blob: 58562698359cf27d107d172d3f94579a00e527e3 [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
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen7467c8d2001-07-12 20:26:32 +00009 */
10
Eric Andersen7467c8d2001-07-12 20:26:32 +000011#include "libbb.h"
12
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000013int FAST_FUNC create_icmp_socket(void)
Eric Andersen7467c8d2001-07-12 20:26:32 +000014{
Eric Andersen7467c8d2001-07-12 20:26:32 +000015 int sock;
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000016#if 0
17 struct protoent *proto;
Eric Andersen7467c8d2001-07-12 20:26:32 +000018 proto = getprotobyname("icmp");
19 /* if getprotobyname failed, just silently force
20 * proto->p_proto to have the correct value for "icmp" */
Denis Vlasenko19c238b2007-03-03 00:36:35 +000021 sock = socket(AF_INET, SOCK_RAW,
22 (proto ? proto->p_proto : 1)); /* 1 == ICMP */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000023#else
24 sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
25#endif
Denis Vlasenko8e858e22007-03-07 09:35:43 +000026 if (sock < 0) {
Eric Andersen7467c8d2001-07-12 20:26:32 +000027 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko19c238b2007-03-03 00:36:35 +000029 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
Eric Andersen7467c8d2001-07-12 20:26:32 +000030 }
31
32 /* drop root privs if running setuid */
Rob Landley53437472006-07-16 08:14:35 +000033 xsetuid(getuid());
Eric Andersen7467c8d2001-07-12 20:26:32 +000034
35 return sock;
36}