blob: edf4355b500644b1eb32e90a3c49cb5b2b9de67d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
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
Mike Frysinger7031f622006-05-08 03:20:50 +000026#include <net/if.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000027#include <features.h>
Denis Vlasenko83e5d6f2006-12-18 21:49:06 +000028#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
Mike Frysinger7031f622006-05-08 03:20:50 +000029#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
36
37#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000038
Mike Frysinger7031f622006-05-08 03:20:50 +000039
Denis Vlasenkof1980f62008-09-26 09:34:59 +000040int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
Mike Frysinger7031f622006-05-08 03:20:50 +000041{
42 int fd;
43 struct ifreq ifr;
44 struct sockaddr_in *our_ip;
45
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000046 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko80edead2007-08-02 22:31:05 +000047 fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000048
49 ifr.ifr_addr.sa_family = AF_INET;
Denis Vlasenko360d9662008-12-02 18:18:50 +000050 strncpy_IFNAMSIZ(ifr.ifr_name, interface);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000051 if (addr) {
Denis Vlasenko49a128a2007-07-17 21:42:59 +000052 if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000053 "is interface %s up and configured?", interface)
54 ) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000055 close(fd);
56 return -1;
57 }
58 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
59 *addr = our_ip->sin_addr.s_addr;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000060 DEBUG("ip of %s = %s", interface, inet_ntoa(our_ip->sin_addr));
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000061 }
62
Denis Vlasenko736230e2006-11-20 19:40:36 +000063 if (ifindex) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000064 if (ioctl_or_warn(fd, SIOCGIFINDEX, &ifr) != 0) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000065 close(fd);
66 return -1;
67 }
68 DEBUG("adapter index %d", ifr.ifr_ifindex);
69 *ifindex = ifr.ifr_ifindex;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000070 }
71
Denis Vlasenko736230e2006-11-20 19:40:36 +000072 if (arp) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000073 if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000074 close(fd);
75 return -1;
76 }
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]);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000080 }
81
Denis Vlasenko80edead2007-08-02 22:31:05 +000082 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +000083 return 0;
84}
85
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000086/* 1. None of the callers expects it to ever fail */
87/* 2. ip was always INADDR_ANY */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000088int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
Mike Frysinger7031f622006-05-08 03:20:50 +000089{
Mike Frysinger7031f622006-05-08 03:20:50 +000090 int fd;
91 struct sockaddr_in addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000092
Denis Vlasenko6ee023c2007-08-23 10:52:52 +000093 DEBUG("Opening listen socket on *:%d %s", port, inf);
Bernhard Reutner-Fischer28fbd692006-12-19 16:31:09 +000094 fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Mike Frysinger7031f622006-05-08 03:20:50 +000095
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000096 setsockopt_reuseaddr(fd);
97 if (setsockopt_broadcast(fd) == -1)
98 bb_perror_msg_and_die("SO_BROADCAST");
99
Denis Vlasenko7ff85c52008-10-27 12:56:58 +0000100 /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000101 if (setsockopt_bindtodevice(fd, inf))
102 xfunc_die(); /* warning is already printed */
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000103
Mike Frysinger7031f622006-05-08 03:20:50 +0000104 memset(&addr, 0, sizeof(addr));
105 addr.sin_family = AF_INET;
106 addr.sin_port = htons(port);
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000107 /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
108 xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000109
110 return fd;
111}