1 /* vi: set sw=4 ts=4: */
3 * socket.c -- DHCP server client/server socket creation
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 <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <netinet/in.h>
32 #include <arpa/inet.h>
36 #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
37 #include <netpacket/packet.h>
38 #include <net/ethernet.h>
40 #include <asm/types.h>
41 #include <linux/if_packet.h>
42 #include <linux/if_ether.h>
48 int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
52 struct sockaddr_in *our_ip;
54 memset(&ifr, 0, sizeof(struct ifreq));
55 if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
56 ifr.ifr_addr.sa_family = AF_INET;
57 strcpy(ifr.ifr_name, interface);
60 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
61 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
62 *addr = our_ip->sin_addr.s_addr;
63 DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
65 bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?");
71 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
72 DEBUG("adapter index %d", ifr.ifr_ifindex);
73 *ifindex = ifr.ifr_ifindex;
75 bb_perror_msg("SIOCGIFINDEX failed");
79 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
80 memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
81 DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
82 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
84 bb_perror_msg("SIOCGIFHWADDR failed");
89 bb_perror_msg("socket failed");
97 int listen_socket(uint32_t ip, int port, char *inf)
99 struct ifreq interface;
101 struct sockaddr_in addr;
104 DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
105 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
106 bb_perror_msg("socket");
110 memset(&addr, 0, sizeof(addr));
111 addr.sin_family = AF_INET;
112 addr.sin_port = htons(port);
113 addr.sin_addr.s_addr = ip;
115 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
119 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
124 strncpy(interface.ifr_name, inf, IFNAMSIZ);
125 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
130 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {