{
struct bpf_hdr packet;
struct ether_header hw;
- ssize_t cur_len;
+ ssize_t bytes;
const unsigned char *payload, *d;
+ if ((size_t)len > iface->buffer_size) {
+ errno = ENOBUFS;
+ return -1;
+ }
for (;;) {
if (iface->buffer_len == 0) {
- cur_len = read(iface->fd, iface->buffer,
- iface->buffer_size);
- if (cur_len == -1)
+ bytes = read(iface->fd, iface->buffer,
+ iface->buffer_size);
+ if (bytes == -1)
return errno == EAGAIN ? 0 : -1;
- else if ((size_t)cur_len < sizeof(packet))
+ else if ((size_t)bytes < sizeof(packet))
return -1;
- iface->buffer_len = cur_len;
+ iface->buffer_len = bytes;
}
-
- cur_len = -1;
-
+ bytes = -1;
memcpy(&packet, iface->buffer + iface->buffer_pos,
sizeof(packet));
if (packet.bh_caplen != packet.bh_datalen)
memcpy(&hw, iface->buffer + packet.bh_hdrlen, sizeof(hw));
payload = iface->buffer + packet.bh_hdrlen + sizeof(hw);
if (hw.ether_type == htons(ETHERTYPE_ARP)) {
- cur_len = packet.bh_caplen - sizeof(hw);
- memcpy(data, payload, len);
+ bytes = packet.bh_caplen - sizeof(hw);
+ if (bytes > len)
+ bytes = len;
+ memcpy(data, payload, bytes);
} else if (valid_udp_packet(payload) >= 0) {
- cur_len = get_udp_data(&d, payload);
- memcpy(data, d, cur_len);
+ bytes = get_udp_data(&d, payload);
+ if (bytes > len)
+ bytes = len;
+ memcpy(data, d, bytes);
} else
- cur_len = -1;
+ bytes = -1;
next:
iface->buffer_pos += BPF_WORDALIGN(packet.bh_hdrlen +
packet.bh_caplen);
if (iface->buffer_pos >= iface->buffer_len)
iface->buffer_len = iface->buffer_pos = 0;
- if (cur_len != -1)
- return cur_len;
+ if (bytes != -1)
+ return bytes;
}
}
* the first one fails for any reason, we can use the next. */
dhcp = xmalloc(sizeof(*dhcp));
- do {
+ do {
+ memset(dhcp, 0, sizeof(*dhcp));
bytes = get_packet(iface, dhcp, sizeof(*dhcp));
if (bytes == -1)
break;
do_socket(state, SOCKET_CLOSED);
free_routes(iface->routes);
free(iface->clientid);
+ free(iface->buffer);
free(iface);
}
unsigned char *hwaddr = NULL;
size_t hwlen = 0;
sa_family_t family = 0;
- unsigned short mtu;
#ifdef __linux__
char *p;
#endif
if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
goto eexit;
}
- mtu = ifr.ifr_mtu;
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
#ifdef __linux__
int fd;
int udp_fd;
- size_t buffer_size, buffer_pos, buffer_len;
+ size_t buffer_size, buffer_len, buffer_pos;
unsigned char *buffer;
#ifdef __linux__
#include "net.h"
#include "bpf-filter.h"
-/* A suitably large buffer for all transactions.
- * BPF buffer size is set by the kernel, so no define. */
-#ifdef __linux__
-# define BUFFER_LENGTH 4096
-#endif
+/* A suitably large buffer for all transactions. */
+#define BUFFER_LENGTH 4096
/* Broadcast address for IPoIB */
static const uint8_t ipv4_bcast_addr[] = {
int
open_socket(struct interface *iface, int protocol)
{
- int flags, s;
+ int s;
union sockunion {
struct sockaddr sa;
struct sockaddr_in sin;
close(iface->fd);
iface->fd = s;
iface->socket_protocol = protocol;
- iface->buffer_length = 0;
+ iface->buffer_size = BUFFER_LENGTH;
+ iface->buffer = xmalloc(iface->buffer_size);
+ iface->buffer_len = iface->buffer_pos = 0;
return s;
eexit:
return sendto(iface->fd, data, len, 0, &su.sa, sizeof(su));
}
-/* Linux has no need for the buffer as we can read as much as we want.
- * We only have the buffer listed to keep the same API. */
ssize_t
get_packet(struct interface *iface, void *data, ssize_t len)
{
ssize_t bytes;
- struct timespec ts;
const uint8_t *p;
- memset(data, 0, len);
- bytes = read(iface->fd, data, len);
+ bytes = read(iface->fd, iface->buffer, iface->buffer_size);
if (bytes == -1)
- return errno == EGAIN ? 0 : -1;
+ return errno == EAGAIN ? 0 : -1;
/* If it's an ARP reply, then just send it back */
if (iface->socket_protocol == ETHERTYPE_ARP)
return bytes;
- if (valid_udp_packet(data) != 0)
+ if (valid_udp_packet(iface->buffer) != 0)
return -1;
- bytes = get_udp_data(&p, buffer);
- memmove(data, p, bytes);
+ bytes = get_udp_data(&p, iface->buffer);
+ if (bytes > len)
+ bytes = len;
+ memcpy(data, p, bytes);
return bytes;
}