USR910x_3.00L.03_consumer.tar.gz
[bcm963xx.git] / userapps / opensource / udhcp.paul / socket.c
1 /*
2  * socket.c -- DHCP server client/server socket creation
3  *
4  * Moreton Bay DHCP 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 <netinet/in.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <net/if.h>
31 #include <errno.h>
32 #include <features.h>
33 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
34 #include <netpacket/packet.h>
35 #include <net/ethernet.h>
36 #else
37 #include <asm/types.h>
38 #include <linux/if_packet.h>
39 #include <linux/if_ether.h>
40 #endif
41
42
43 #include "debug.h"
44
45 int listen_socket(unsigned int ip, int port, char *inf)
46 {
47         struct ifreq interface;
48         int fd;
49         struct sockaddr_in addr;
50         int n = 1;
51
52         if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
53                 DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
54                 return -1;
55         }
56         
57         memset(&addr, 0, sizeof(addr));
58         addr.sin_family = AF_INET;
59         addr.sin_port = htons(port);
60         addr.sin_addr.s_addr = ip;
61
62         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
63                 close(fd);
64                 return -1;
65         }
66         if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
67                 close(fd);
68                 return -1;
69         }
70
71         strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
72         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
73                 close(fd);
74                 return -1;
75         }
76
77         if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
78                 close(fd);
79                 return -1;
80         }
81         
82         return fd;
83 }
84
85
86 int raw_socket(int ifindex)
87 {
88         int fd;
89         struct sockaddr_ll sock;
90
91         if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
92                 DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
93                 return -1;
94         }
95         
96         sock.sll_family = AF_PACKET;
97         sock.sll_protocol = htons(ETH_P_IP);
98         sock.sll_ifindex = ifindex;
99         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
100                 DEBUG(LOG_ERR, "bind call failed: %s", sys_errlist[errno]);
101                 close(fd);
102                 return -1;
103         }
104
105         return fd;
106
107 }
108