blob: 3a465588578ca01d4f0c29ab3386c4701fee12ae [file] [log] [blame]
Eric Andersen7467c8d2001-07-12 20:26:32 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * create raw socket for icmp protocol test permission
6 * and drop root privileges if running setuid
Eric Andersen7467c8d2001-07-12 20:26:32 +00007 *
8 */
9
Denis Vlasenkoe3241842007-08-13 10:36:25 +000010//#include <sys/types.h>
11//#include <netdb.h>
12//#include <sys/socket.h>
Eric Andersen7467c8d2001-07-12 20:26:32 +000013#include "libbb.h"
14
15int create_icmp_socket(void)
16{
17 struct protoent *proto;
18 int sock;
19
20 proto = getprotobyname("icmp");
21 /* if getprotobyname failed, just silently force
22 * proto->p_proto to have the correct value for "icmp" */
Denis Vlasenko19c238b2007-03-03 00:36:35 +000023 sock = socket(AF_INET, SOCK_RAW,
24 (proto ? proto->p_proto : 1)); /* 1 == ICMP */
Denis Vlasenko8e858e22007-03-07 09:35:43 +000025 if (sock < 0) {
Eric Andersen7467c8d2001-07-12 20:26:32 +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 Andersen7467c8d2001-07-12 20:26:32 +000029 }
30
31 /* drop root privs if running setuid */
Rob Landley53437472006-07-16 08:14:35 +000032 xsetuid(getuid());
Eric Andersen7467c8d2001-07-12 20:26:32 +000033
34 return sock;
35}