create_icmp_socket.c 776 B

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