blob: 91e478ec83c62191510364c0212dda69d1f4b28d [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
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00007 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Eric Andersen51b8bd62002-07-03 11:46:38 +00009 */
10
Eric Andersen51b8bd62002-07-03 11:46:38 +000011#include "libbb.h"
12
Denis Vlasenkoe3241842007-08-13 10:36:25 +000013#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000014int FAST_FUNC create_icmp6_socket(void)
Eric Andersen51b8bd62002-07-03 11:46:38 +000015{
Eric Andersen51b8bd62002-07-03 11:46:38 +000016 int sock;
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000017#if 0
18 struct protoent *proto;
Eric Andersen51b8bd62002-07-03 11:46:38 +000019 proto = getprotobyname("ipv6-icmp");
20 /* if getprotobyname failed, just silently force
21 * proto->p_proto to have the correct value for "ipv6-icmp" */
Denis Vlasenko19c238b2007-03-03 00:36:35 +000022 sock = socket(AF_INET6, SOCK_RAW,
23 (proto ? proto->p_proto : IPPROTO_ICMPV6));
Denis Vlasenko4e6d5112008-03-12 22:14:34 +000024#else
25 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
26#endif
Denis Vlasenko19c238b2007-03-03 00:36:35 +000027 if (sock < 0) {
Eric Andersen51b8bd62002-07-03 11:46:38 +000028 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko19c238b2007-03-03 00:36:35 +000030 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
Eric Andersen51b8bd62002-07-03 11:46:38 +000031 }
32
33 /* drop root privs if running setuid */
Rob Landley53437472006-07-16 08:14:35 +000034 xsetuid(getuid());
Eric Andersen51b8bd62002-07-03 11:46:38 +000035
36 return sock;
37}
38#endif