projects
/
oweals
/
busybox.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
tls: can download kernels now :)
[oweals/busybox.git]
/
networking
/
udhcp
/
socket.c
diff --git
a/networking/udhcp/socket.c
b/networking/udhcp/socket.c
index 21a0585452d1d66dee735048560aa80d51a9b34c..4fd79f4238440a67e80c2fabe512d7514daffabb 100644
(file)
--- a/
networking/udhcp/socket.c
+++ b/
networking/udhcp/socket.c
@@
-1,6
+1,6
@@
/* vi: set sw=4 ts=4: */
/*
/* vi: set sw=4 ts=4: */
/*
- *
socket.c --
DHCP server client/server socket creation
+ * DHCP server client/server socket creation
*
* udhcp client/server
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
*
* udhcp client/server
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
@@
-22,104
+22,89
@@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-
-#include <net/if.h>
-#include <features.h>
-#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
-#include <netpacket/packet.h>
-#include <net/ethernet.h>
-#else
-#include <asm/types.h>
-#include <linux/if_packet.h>
-#include <linux/if_ether.h>
-#endif
-
#include "common.h"
#include "common.h"
+#include <net/if.h>
-
-int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
+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;
int fd;
- struct ifreq ifr;
struct sockaddr_in *our_ip;
struct sockaddr_in *our_ip;
- memset(&ifr, 0, sizeof(ifr));
- fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
- if (fd < 0) {
- bb_perror_msg("socket failed");
- return -1;
- }
+ memset(ifr, 0, sizeof(*ifr));
+ fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
- ifr
.
ifr_addr.sa_family = AF_INET;
- strncpy
(ifr.ifr_name, interface, sizeof(ifr.ifr_name)
);
- if (
addr
) {
- if (ioctl_or_perror(fd, SIOCGIFADDR,
&ifr,
+ 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;
}
"is interface %s up and configured?", interface)
) {
close(fd);
return -1;
}
- our_ip = (struct sockaddr_in *) &ifr
.
ifr_addr;
- *
addr
= our_ip->sin_addr.s_addr;
-
DEBUG("%s (our ip) = %s", ifr.ifr_name
, inet_ntoa(our_ip->sin_addr));
+ 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 (ifindex) {
- if (ioctl_or_warn(fd, SIOCGIFINDEX,
&
ifr) != 0) {
+ if (ioctl_or_warn(fd, SIOCGIFINDEX, ifr) != 0) {
close(fd);
return -1;
}
close(fd);
return -1;
}
-
DEBUG("adapter index %d", ifr.
ifr_ifindex);
- *ifindex = ifr
.
ifr_ifindex;
+
log1("adapter index %d", ifr->
ifr_ifindex);
+ *ifindex = ifr
->
ifr_ifindex;
}
}
- if (
arp
) {
- if (ioctl_or_warn(fd, SIOCGIFHWADDR,
&
ifr) != 0) {
+ if (
mac
) {
+ if (ioctl_or_warn(fd, SIOCGIFHWADDR, ifr) != 0) {
close(fd);
return -1;
}
close(fd);
return -1;
}
- memcpy(
arp, ifr.
ifr_hwaddr.sa_data, 6);
-
DEBUG("adapter hardware address
%02x:%02x:%02x:%02x:%02x:%02x",
-
arp[0], arp[1], arp[2], arp[3], arp[4], arp
[5]);
+ 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;
}
return 0;
}
-
-int listen_socket(uint32_t ip, int port, const char *inf)
+/* 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)
{
{
- struct ifreq interface;
int fd;
struct sockaddr_in addr;
int fd;
struct sockaddr_in addr;
+ char *colon;
-
DEBUG("Opening listen socket on 0x%08x:%d %s", ip
, port, inf);
+
log1("opening listen socket on *:%d %s"
, port, inf);
fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = ip;
+ setsockopt_reuseaddr(fd);
+ if (setsockopt_broadcast(fd) == -1)
+ bb_perror_msg_and_die("SO_BROADCAST");
- if (setsockopt_reuseaddr(fd) == -1) {
- close(fd);
- return -1;
- }
- if (setsockopt_broadcast(fd) == -1) {
- close(fd);
- return -1;
- }
+ /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */
+ colon = strrchr(inf, ':');
+ if (colon)
+ *colon = '\0';
- strncpy(interface.ifr_name, inf, IFNAMSIZ);
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(interface)) < 0) {
- close(fd);
- return -1;
- }
+ if (setsockopt_bindtodevice(fd, inf))
+ xfunc_die(); /* warning is already printed */
- if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- close(fd);
- return -1;
- }
+ 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;
}
return fd;
}