socket.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * DHCP server client/server socket creation
  4. *
  5. * udhcp client/server
  6. * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
  7. * Chris Trew <ctrew@moreton.com.au>
  8. *
  9. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include "common.h"
  26. #include <net/if.h>
  27. int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
  28. {
  29. /* char buffer instead of bona-fide struct avoids aliasing warning */
  30. char ifr_buf[sizeof(struct ifreq)];
  31. struct ifreq *const ifr = (void *)ifr_buf;
  32. int fd;
  33. struct sockaddr_in *our_ip;
  34. memset(ifr, 0, sizeof(*ifr));
  35. fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  36. ifr->ifr_addr.sa_family = AF_INET;
  37. strncpy_IFNAMSIZ(ifr->ifr_name, interface);
  38. if (nip) {
  39. if (ioctl_or_perror(fd, SIOCGIFADDR, ifr,
  40. "is interface %s up and configured?", interface)
  41. ) {
  42. close(fd);
  43. return -1;
  44. }
  45. our_ip = (struct sockaddr_in *) &ifr->ifr_addr;
  46. *nip = our_ip->sin_addr.s_addr;
  47. log1("IP %s", inet_ntoa(our_ip->sin_addr));
  48. }
  49. if (ifindex) {
  50. if (ioctl_or_warn(fd, SIOCGIFINDEX, ifr) != 0) {
  51. close(fd);
  52. return -1;
  53. }
  54. log2("ifindex %d", ifr->ifr_ifindex);
  55. *ifindex = ifr->ifr_ifindex;
  56. }
  57. if (mac) {
  58. if (ioctl_or_warn(fd, SIOCGIFHWADDR, ifr) != 0) {
  59. close(fd);
  60. return -1;
  61. }
  62. memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
  63. log2("MAC %02x:%02x:%02x:%02x:%02x:%02x",
  64. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  65. }
  66. close(fd);
  67. return 0;
  68. }
  69. /* 1. None of the callers expects it to ever fail */
  70. /* 2. ip was always INADDR_ANY */
  71. int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
  72. {
  73. int fd;
  74. struct sockaddr_in addr;
  75. char *colon;
  76. log2("opening listen socket on *:%d %s", port, inf);
  77. fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  78. setsockopt_reuseaddr(fd);
  79. if (setsockopt_broadcast(fd) == -1)
  80. bb_simple_perror_msg_and_die("SO_BROADCAST");
  81. /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */
  82. colon = strrchr(inf, ':');
  83. if (colon)
  84. *colon = '\0';
  85. if (setsockopt_bindtodevice(fd, inf))
  86. xfunc_die(); /* warning is already printed */
  87. if (colon)
  88. *colon = ':';
  89. memset(&addr, 0, sizeof(addr));
  90. addr.sin_family = AF_INET;
  91. addr.sin_port = htons(port);
  92. /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
  93. xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
  94. return fd;
  95. }