Version increased to 0.9.8cvs20050303-2.1maemo2.
[udhcp] / clientsocket.c
1 /*
2  * clientsocket.c -- DHCP client socket creation
3  *
4  * udhcp client
5  *
6  * Russ Dill <Russ.Dill@asu.edu> July 2001
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <unistd.h>
26 #include <netinet/in.h>
27 #include <features.h>
28 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
29 #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 "clientsocket.h"
38 #include "common.h"
39
40
41 int raw_socket(int ifindex)
42 {
43         int fd;
44         struct sockaddr_ll sock;
45
46         DEBUG(LOG_INFO, "Opening raw socket on ifindex %d", ifindex);
47         if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
48                 DEBUG(LOG_ERR, "socket call failed: %m");
49                 return -1;
50         }
51
52         sock.sll_family = AF_PACKET;
53         sock.sll_protocol = htons(ETH_P_IP);
54         sock.sll_ifindex = ifindex;
55         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
56                 DEBUG(LOG_ERR, "bind call failed: %m");
57                 close(fd);
58                 return -1;
59         }
60
61         return fd;
62 }