blob: ea2913172e8ff342c5ee332a7889b0d3bc5c9e7f [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 Vlasenkoa7189f02006-11-17 20:29:00 +000028#if (__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
40int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
41{
42 int fd;
43 struct ifreq ifr;
44 struct sockaddr_in *our_ip;
45
46 memset(&ifr, 0, sizeof(struct ifreq));
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000047 fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
48 if (fd < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000049 bb_perror_msg("socket failed");
Mike Frysinger7031f622006-05-08 03:20:50 +000050 return -1;
51 }
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000052
53 ifr.ifr_addr.sa_family = AF_INET;
54 strcpy(ifr.ifr_name, interface);
55 if (addr) {
56 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
57 bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?");
58 close(fd);
59 return -1;
60 }
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));
64 }
65
66 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
67 bb_perror_msg("SIOCGIFINDEX failed");
68 close(fd);
69 return -1;
70 }
71
72 DEBUG("adapter index %d", ifr.ifr_ifindex);
73 *ifindex = ifr.ifr_ifindex;
74 if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) {
75 bb_perror_msg("SIOCGIFHWADDR failed");
76 close(fd);
77 return -1;
78 }
79
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]);
Mike Frysinger7031f622006-05-08 03:20:50 +000083 return 0;
84}
85
86
87int listen_socket(uint32_t ip, int port, char *inf)
88{
89 struct ifreq interface;
90 int fd;
91 struct sockaddr_in addr;
92 int n = 1;
93
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000094 DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000095 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
96 if (fd < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000097 bb_perror_msg("socket");
Mike Frysinger7031f622006-05-08 03:20:50 +000098 return -1;
99 }
100
101 memset(&addr, 0, sizeof(addr));
102 addr.sin_family = AF_INET;
103 addr.sin_port = htons(port);
104 addr.sin_addr.s_addr = ip;
105
106 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
107 close(fd);
108 return -1;
109 }
110 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
111 close(fd);
112 return -1;
113 }
114
115 strncpy(interface.ifr_name, inf, IFNAMSIZ);
116 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
117 close(fd);
118 return -1;
119 }
120
121 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
122 close(fd);
123 return -1;
124 }
125
126 return fd;
127}