Tomato 1.28
[tomato.git] / release / src / router / busybox / networking / udhcp / socket.c
blobedf4355b500644b1eb32e90a3c49cb5b2b9de67d
1 /* vi: set sw=4 ts=4: */
2 /*
3 * socket.c -- DHCP server client/server socket creation
5 * udhcp client/server
6 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
7 * Chris Trew <ctrew@moreton.com.au>
9 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
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.
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.
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.
26 #include <net/if.h>
27 #include <features.h>
28 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
29 #include <netpacket/packet.h>
30 #include <net/ethernet.h>
31 #else
32 #include <asm/types.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_ether.h>
35 #endif
37 #include "common.h"
40 int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
42 int fd;
43 struct ifreq ifr;
44 struct sockaddr_in *our_ip;
46 memset(&ifr, 0, sizeof(ifr));
47 fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
49 ifr.ifr_addr.sa_family = AF_INET;
50 strncpy_IFNAMSIZ(ifr.ifr_name, interface);
51 if (addr) {
52 if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
53 "is interface %s up and configured?", interface)
54 ) {
55 close(fd);
56 return -1;
58 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
59 *addr = our_ip->sin_addr.s_addr;
60 DEBUG("ip of %s = %s", interface, inet_ntoa(our_ip->sin_addr));
63 if (ifindex) {
64 if (ioctl_or_warn(fd, SIOCGIFINDEX, &ifr) != 0) {
65 close(fd);
66 return -1;
68 DEBUG("adapter index %d", ifr.ifr_ifindex);
69 *ifindex = ifr.ifr_ifindex;
72 if (arp) {
73 if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) {
74 close(fd);
75 return -1;
77 memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
78 DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
79 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
82 close(fd);
83 return 0;
86 /* 1. None of the callers expects it to ever fail */
87 /* 2. ip was always INADDR_ANY */
88 int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
90 int fd;
91 struct sockaddr_in addr;
93 DEBUG("Opening listen socket on *:%d %s", port, inf);
94 fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
96 setsockopt_reuseaddr(fd);
97 if (setsockopt_broadcast(fd) == -1)
98 bb_perror_msg_and_die("SO_BROADCAST");
100 /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
101 if (setsockopt_bindtodevice(fd, inf))
102 xfunc_die(); /* warning is already printed */
104 memset(&addr, 0, sizeof(addr));
105 addr.sin_family = AF_INET;
106 addr.sin_port = htons(port);
107 /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
108 xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
110 return fd;