create_icmp6_socket.c 840 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * create raw socket for icmp (IPv6 version) protocol test permission
  6. * and drop root privileges if running setuid
  7. *
  8. */
  9. //#include <sys/types.h>
  10. //#include <netdb.h>
  11. //#include <sys/socket.h>
  12. #include "libbb.h"
  13. #if ENABLE_FEATURE_IPV6
  14. int create_icmp6_socket(void)
  15. {
  16. struct protoent *proto;
  17. int sock;
  18. proto = getprotobyname("ipv6-icmp");
  19. /* if getprotobyname failed, just silently force
  20. * proto->p_proto to have the correct value for "ipv6-icmp" */
  21. sock = socket(AF_INET6, SOCK_RAW,
  22. (proto ? proto->p_proto : IPPROTO_ICMPV6));
  23. if (sock < 0) {
  24. if (errno == EPERM)
  25. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  26. bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
  27. }
  28. /* drop root privs if running setuid */
  29. xsetuid(getuid());
  30. return sock;
  31. }
  32. #endif