socket.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * socket.c -- 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 <net/if.h>
  26. #include <features.h>
  27. #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
  28. #include <netpacket/packet.h>
  29. #include <net/ethernet.h>
  30. #else
  31. #include <asm/types.h>
  32. #include <linux/if_packet.h>
  33. #include <linux/if_ether.h>
  34. #endif
  35. #include "common.h"
  36. int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
  37. {
  38. int fd;
  39. struct ifreq ifr;
  40. struct sockaddr_in *our_ip;
  41. memset(&ifr, 0, sizeof(struct ifreq));
  42. fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  43. if (fd < 0) {
  44. bb_perror_msg("socket failed");
  45. return -1;
  46. }
  47. ifr.ifr_addr.sa_family = AF_INET;
  48. strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
  49. if (addr) {
  50. if (ioctl(fd, SIOCGIFADDR, &ifr) != 0) {
  51. bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?");
  52. close(fd);
  53. return -1;
  54. }
  55. our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
  56. *addr = our_ip->sin_addr.s_addr;
  57. DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
  58. }
  59. if (ifindex) {
  60. if (ioctl(fd, SIOCGIFINDEX, &ifr) != 0) {
  61. bb_perror_msg("SIOCGIFINDEX failed");
  62. close(fd);
  63. return -1;
  64. }
  65. DEBUG("adapter index %d", ifr.ifr_ifindex);
  66. *ifindex = ifr.ifr_ifindex;
  67. }
  68. if (arp) {
  69. if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) {
  70. bb_perror_msg("SIOCGIFHWADDR failed");
  71. close(fd);
  72. return -1;
  73. }
  74. memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
  75. DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
  76. arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
  77. }
  78. return 0;
  79. }
  80. int listen_socket(uint32_t ip, int port, char *inf)
  81. {
  82. struct ifreq interface;
  83. int fd;
  84. struct sockaddr_in addr;
  85. DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
  86. fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  87. memset(&addr, 0, sizeof(addr));
  88. addr.sin_family = AF_INET;
  89. addr.sin_port = htons(port);
  90. addr.sin_addr.s_addr = ip;
  91. if (setsockopt_reuseaddr(fd) == -1) {
  92. close(fd);
  93. return -1;
  94. }
  95. if (setsockopt_broadcast(fd) == -1) {
  96. close(fd);
  97. return -1;
  98. }
  99. strncpy(interface.ifr_name, inf, IFNAMSIZ);
  100. if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface, sizeof(interface)) < 0) {
  101. close(fd);
  102. return -1;
  103. }
  104. if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
  105. close(fd);
  106. return -1;
  107. }
  108. return fd;
  109. }