busybox/networking/udhcp/socket.c
James Byrne 6937487be7 libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d437 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().

This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.

Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.

This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.

The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):

Arm:     -92 bytes
MIPS:    -52 bytes
PPC:   -1836 bytes
x86_64: -938 bytes

Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.

Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 11:35:03 +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;
}
log2("ifindex %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);
log2("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_simple_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;
}