hush/networking/udhcp/socket.c
Denys Vlasenko 8f2e99c813 udhcp: get rid of bb_info_msg()
function                                             old     new   delta
udhcpd_main                                         1501    1531     +30
d6_recv_raw_packet                                   251     264     +13
perform_d6_release                                   188     198     +10
udhcpc6_main                                        2443    2449      +6
udhcp_recv_raw_packet                                582     588      +6
udhcp_recv_kernel_packet                             132     138      +6
send_d6_renew                                        140     146      +6
d6_recv_kernel_packet                                118     124      +6
send_renew                                            77      82      +5
send_discover                                         85      90      +5
send_decline                                          84      89      +5
send_d6_select                                        97     102      +5
send_d6_discover                                     174     179      +5
perform_release                                      167     172      +5
count_lines                                           72      74      +2
udhcpc_main                                         2836    2837      +1
bb_info_msg                                          125       -    -125
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 17/4 up/down: 117/-180)         Total: -63 bytes
   text	   data	    bss	    dec	    hex	filename
 924935	    906	  17160	 943001	  e6399	busybox_old
 924736	    906	  17160	 942802	  e62d2	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-03-30 18:41:23 +02:00

111 lines
3.0 KiB
C

/* vi: set sw=4 ts=4: */
/*
* DHCP server client/server socket creation
*
* udhcp client/server
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
* Chris Trew <ctrew@moreton.com.au>
*
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "common.h"
#include <net/if.h>
int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
{
/* char buffer instead of bona-fide struct avoids aliasing warning */
char ifr_buf[sizeof(struct ifreq)];
struct ifreq *const ifr = (void *)ifr_buf;
int fd;
struct sockaddr_in *our_ip;
memset(ifr, 0, sizeof(*ifr));
fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
ifr->ifr_addr.sa_family = AF_INET;
strncpy_IFNAMSIZ(ifr->ifr_name, interface);
if (nip) {
if (ioctl_or_perror(fd, SIOCGIFADDR, ifr,
"is interface %s up and configured?", interface)
) {
close(fd);
return -1;
}
our_ip = (struct sockaddr_in *) &ifr->ifr_addr;
*nip = our_ip->sin_addr.s_addr;
log1("IP %s", inet_ntoa(our_ip->sin_addr));
}
if (ifindex) {
if (ioctl_or_warn(fd, SIOCGIFINDEX, ifr) != 0) {
close(fd);
return -1;
}
log1("adapter index %d", ifr->ifr_ifindex);
*ifindex = ifr->ifr_ifindex;
}
if (mac) {
if (ioctl_or_warn(fd, SIOCGIFHWADDR, ifr) != 0) {
close(fd);
return -1;
}
memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
log1("MAC %02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
close(fd);
return 0;
}
/* 1. None of the callers expects it to ever fail */
/* 2. ip was always INADDR_ANY */
int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
{
int fd;
struct sockaddr_in addr;
char *colon;
log1("opening listen socket on *:%d %s", port, inf);
fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
setsockopt_reuseaddr(fd);
if (setsockopt_broadcast(fd) == -1)
bb_perror_msg_and_die("SO_BROADCAST");
/* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */
colon = strrchr(inf, ':');
if (colon)
*colon = '\0';
if (setsockopt_bindtodevice(fd, inf))
xfunc_die(); /* warning is already printed */
if (colon)
*colon = ':';
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
/* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
return fd;
}