Patch from Christian Meyer:
[oweals/busybox.git] / networking / udhcp / socket.c
1 /*
2  * socket.c -- DHCP server client/server socket creation
3  *
4  * udhcp client/server
5  * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
6  *                      Chris Trew <ctrew@moreton.com.au>
7  *
8  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <arpa/inet.h>
32 #include <net/if.h>
33 #include <errno.h>
34 #include <features.h>
35 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
36 #include <netpacket/packet.h>
37 #include <net/ethernet.h>
38 #else
39 #include <asm/types.h>
40 #include <linux/if_packet.h>
41 #include <linux/if_ether.h>
42 #endif
43
44 #include "common.h"
45
46 int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp)
47 {
48         int fd;
49         struct ifreq ifr;
50         struct sockaddr_in *our_ip;
51
52         memset(&ifr, 0, sizeof(struct ifreq));
53         if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
54                 ifr.ifr_addr.sa_family = AF_INET;
55                 strcpy(ifr.ifr_name, interface);
56
57                 if (addr) { 
58                         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
59                                 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
60                                 *addr = our_ip->sin_addr.s_addr;
61                                 DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
62                         } else {
63                                 LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %m");
64                                 return -1;
65                         }
66                 }
67                 
68                 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
69                         DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
70                         *ifindex = ifr.ifr_ifindex;
71                 } else {
72                         LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m");
73                         return -1;
74                 }
75                 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
76                         memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
77                         DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
78                                 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
79                 } else {
80                         LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m");
81                         return -1;
82                 }
83         } else {
84                 LOG(LOG_ERR, "socket failed!: %m");
85                 return -1;
86         }
87         close(fd);
88         return 0;
89 }
90
91
92 int listen_socket(unsigned int ip, int port, char *inf)
93 {
94         struct ifreq interface;
95         int fd;
96         struct sockaddr_in addr;
97         int n = 1;
98
99         DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
100         if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
101                 DEBUG(LOG_ERR, "socket call failed: %m");
102                 return -1;
103         }
104         
105         memset(&addr, 0, sizeof(addr));
106         addr.sin_family = AF_INET;
107         addr.sin_port = htons(port);
108         addr.sin_addr.s_addr = ip;
109
110         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
111                 close(fd);
112                 return -1;
113         }
114         if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
115                 close(fd);
116                 return -1;
117         }
118
119         strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
120         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
121                 close(fd);
122                 return -1;
123         }
124
125         if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
126                 close(fd);
127                 return -1;
128         }
129         
130         return fd;
131 }
132
133
134 int raw_socket(int ifindex)
135 {
136         int fd;
137         struct sockaddr_ll sock;
138
139         DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
140         if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
141                 DEBUG(LOG_ERR, "socket call failed: %m");
142                 return -1;
143         }
144         
145         sock.sll_family = AF_PACKET;
146         sock.sll_protocol = htons(ETH_P_IP);
147         sock.sll_ifindex = ifindex;
148         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
149                 DEBUG(LOG_ERR, "bind call failed: %m");
150                 close(fd);
151                 return -1;
152         }
153
154         return fd;
155 }