2 * socket.c -- DHCP server client/server socket creation
4 * Moreton Bay DHCP Server
5 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
6 * Chris Trew <ctrew@moreton.com.au>
8 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
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.
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.
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
33 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
34 #include <netpacket/packet.h>
35 #include <net/ethernet.h>
37 #include <asm/types.h>
38 #include <linux/if_packet.h>
39 #include <linux/if_ether.h>
45 int listen_socket(unsigned int ip, int port, char *inf)
47 struct ifreq interface;
49 struct sockaddr_in addr;
52 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
53 DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
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;
62 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
66 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
71 strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
72 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
77 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
86 int raw_socket(int ifindex)
89 struct sockaddr_ll sock;
91 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
92 DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
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]);