blob: 20655170e423c5fb882e18d79670290dd933227a [file] [log] [blame]
Eric Andersen51b8bd62002-07-03 11:46:38 +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 (IPv6 version) protocol
Eric Andersenaff114c2004-04-14 17:51:38 +00006 * and drop root privileges if running setuid
Eric Andersen51b8bd62002-07-03 11:46:38 +00007 */
8
Eric Andersen51b8bd62002-07-03 11:46:38 +00009#include "libbb.h"
10
Denis Vlasenkoe3241842007-08-13 10:36:25 +000011#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000012int FAST_FUNC create_icmp6_socket(void)
Eric Andersen51b8bd62002-07-03 11:46:38 +000013{
Eric Andersen51b8bd62002-07-03 11:46:38 +000014 int sock;
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000015#if 0
16 struct protoent *proto;
Eric Andersen51b8bd62002-07-03 11:46:38 +000017 proto = getprotobyname("ipv6-icmp");
18 /* if getprotobyname failed, just silently force
19 * proto->p_proto to have the correct value for "ipv6-icmp" */
Denis Vlasenko19c238b2007-03-03 00:36:35 +000020 sock = socket(AF_INET6, SOCK_RAW,
21 (proto ? proto->p_proto : IPPROTO_ICMPV6));
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000022#else
23 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
24#endif
Denis Vlasenko19c238b2007-03-03 00:36:35 +000025 if (sock < 0) {
Eric Andersen51b8bd62002-07-03 11:46:38 +000026 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko19c238b2007-03-03 00:36:35 +000028 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
Eric Andersen51b8bd62002-07-03 11:46:38 +000029 }
30
31 /* drop root privs if running setuid */
Rob Landley53437472006-07-16 08:14:35 +000032 xsetuid(getuid());
Eric Andersen51b8bd62002-07-03 11:46:38 +000033
34 return sock;
35}
36#endif