blob: 1e62f8e40f0cba063390f361a87d0c4b707b0e4e [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 Vlasenkob6aae0f2007-01-29 22:51:25 +000040int 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);
48// if (fd < 0) {
49// bb_perror_msg("socket failed");
50// return -1;
51// }
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000052
53 ifr.ifr_addr.sa_family = AF_INET;
Denis Vlasenko229b3d22006-11-27 23:44:57 +000054 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000055 if (addr) {
Denis Vlasenko49a128a2007-07-17 21:42:59 +000056 if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000057 "is interface %s up and configured?", interface)
58 ) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000059 close(fd);
60 return -1;
61 }
62 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
63 *addr = our_ip->sin_addr.s_addr;
64 DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
65 }
66
Denis Vlasenko736230e2006-11-20 19:40:36 +000067 if (ifindex) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000068 if (ioctl_or_warn(fd, SIOCGIFINDEX, &ifr) != 0) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000069 close(fd);
70 return -1;
71 }
72 DEBUG("adapter index %d", ifr.ifr_ifindex);
73 *ifindex = ifr.ifr_ifindex;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000074 }
75
Denis Vlasenko736230e2006-11-20 19:40:36 +000076 if (arp) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000077 if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000078 close(fd);
79 return -1;
80 }
81 memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
82 DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
83 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000084 }
85
Denis Vlasenko80edead2007-08-02 22:31:05 +000086 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +000087 return 0;
88}
89
90
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000091int listen_socket(uint32_t ip, int port, const char *inf)
Mike Frysinger7031f622006-05-08 03:20:50 +000092{
93 struct ifreq interface;
94 int fd;
95 struct sockaddr_in addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000096
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000097 DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
Bernhard Reutner-Fischer28fbd692006-12-19 16:31:09 +000098 fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Mike Frysinger7031f622006-05-08 03:20:50 +000099
100 memset(&addr, 0, sizeof(addr));
101 addr.sin_family = AF_INET;
102 addr.sin_port = htons(port);
103 addr.sin_addr.s_addr = ip;
104
Denis Vlasenko48237b02006-11-22 23:22:06 +0000105 if (setsockopt_reuseaddr(fd) == -1) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000106 close(fd);
107 return -1;
108 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000109 if (setsockopt_broadcast(fd) == -1) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000110 close(fd);
111 return -1;
112 }
113
114 strncpy(interface.ifr_name, inf, IFNAMSIZ);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000115 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(interface)) < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000116 close(fd);
117 return -1;
118 }
119
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000120 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000121 close(fd);
122 return -1;
123 }
124
125 return fd;
126}