source: MondoRescue/branches/2.2.9/mindi-busybox/networking/udhcp/socket.c@ 2888

Last change on this file since 2888 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 3.0 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]2/*
[2725]3 * DHCP server client/server socket creation
[821]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#include <net/if.h>
[1765]26#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
[2725]27# include <netpacket/packet.h>
28# include <net/ethernet.h>
[821]29#else
[2725]30# include <asm/types.h>
31# include <linux/if_packet.h>
32# include <linux/if_ether.h>
[821]33#endif
34
35#include "common.h"
36
[2725]37int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
[821]38{
39 int fd;
40 struct ifreq ifr;
41 struct sockaddr_in *our_ip;
42
[1765]43 memset(&ifr, 0, sizeof(ifr));
44 fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
[821]45
[1765]46 ifr.ifr_addr.sa_family = AF_INET;
[2725]47 strncpy_IFNAMSIZ(ifr.ifr_name, interface);
48 if (nip) {
[1765]49 if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
50 "is interface %s up and configured?", interface)
51 ) {
52 close(fd);
53 return -1;
[821]54 }
[1765]55 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
[2725]56 *nip = our_ip->sin_addr.s_addr;
57 log1("IP %s", inet_ntoa(our_ip->sin_addr));
[1765]58 }
[821]59
[1765]60 if (ifindex) {
61 if (ioctl_or_warn(fd, SIOCGIFINDEX, &ifr) != 0) {
[821]62 close(fd);
63 return -1;
64 }
[2725]65 log1("Adapter index %d", ifr.ifr_ifindex);
[1765]66 *ifindex = ifr.ifr_ifindex;
67 }
68
[2725]69 if (mac) {
[1765]70 if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) {
[821]71 close(fd);
72 return -1;
73 }
[2725]74 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
75 log1("MAC %02x:%02x:%02x:%02x:%02x:%02x",
76 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
[821]77 }
[1765]78
[821]79 close(fd);
80 return 0;
81}
82
[1765]83/* 1. None of the callers expects it to ever fail */
84/* 2. ip was always INADDR_ANY */
[2725]85int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
[821]86{
[1765]87 int fd;
[821]88 struct sockaddr_in addr;
89
[2725]90 log1("Opening listen socket on *:%d %s", port, inf);
[1765]91 fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
[821]92
[1765]93 setsockopt_reuseaddr(fd);
94 if (setsockopt_broadcast(fd) == -1)
95 bb_perror_msg_and_die("SO_BROADCAST");
96
[2725]97 /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
98 if (setsockopt_bindtodevice(fd, inf))
99 xfunc_die(); /* warning is already printed */
[1765]100
[821]101 memset(&addr, 0, sizeof(addr));
102 addr.sin_family = AF_INET;
103 addr.sin_port = htons(port);
[1765]104 /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
105 xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
[821]106
107 return fd;
108}
Note: See TracBrowser for help on using the repository browser.