Makefile: change version to 1.10.0.svn

udhcpc: make UDP packet sending the same as raw sending in regards
  to error messages. Minor code size shrink. Total size grows due
  to added messages:

   text    data     bss     dec     hex filename
 770312     683    7244  778239   bdfff busybox_old
 770327     683    7244  778254   be00e busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-12-24 17:32:22 +00:00
parent 299c5c379e
commit 8e5b6f58a2
4 changed files with 57 additions and 47 deletions

View File

@ -1,7 +1,7 @@
VERSION = 1 VERSION = 1
PATCHLEVEL = 9 PATCHLEVEL = 10
SUBLEVEL = 0 SUBLEVEL = 0
EXTRAVERSION = EXTRAVERSION = .svn
NAME = Unnamed NAME = Unnamed
# *DOCUMENTATION* # *DOCUMENTATION*

View File

@ -167,39 +167,40 @@ int send_release(uint32_t server, uint32_t ciaddr)
} }
/* return -1 on errors that are fatal for the socket, -2 for those that aren't */ /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
int get_raw_packet(struct dhcpMessage *payload, int fd) int get_raw_packet(struct dhcpMessage *payload, int fd)
{ {
int bytes; int bytes;
struct udp_dhcp_packet packet; struct udp_dhcp_packet packet;
uint16_t check; uint16_t check;
memset(&packet, 0, sizeof(struct udp_dhcp_packet)); memset(&packet, 0, sizeof(packet));
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); bytes = safe_read(fd, &packet, sizeof(packet));
if (bytes < 0) { if (bytes < 0) {
DEBUG("Cannot read on raw listening socket - ignoring"); DEBUG("Cannot read on raw listening socket - ignoring");
sleep(1); /* possible down interface, looping condition */ sleep(1); /* possible down interface, looping condition */
return -1; return bytes; /* returns -1 */
} }
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
DEBUG("Message too short, ignoring"); DEBUG("Packet is too short, ignoring");
return -2; return -2;
} }
if (bytes < ntohs(packet.ip.tot_len)) { if (bytes < ntohs(packet.ip.tot_len)) {
DEBUG("Truncated packet"); /* packet is bigger than sizeof(packet), we did partial read */
DEBUG("Oversized packet, ignoring");
return -2; return -2;
} }
/* ignore any extra garbage bytes */ /* ignore any extra garbage bytes */
bytes = ntohs(packet.ip.tot_len); bytes = ntohs(packet.ip.tot_len);
/* Make sure its the right packet for us, and that it passes sanity checks */ /* make sure its the right packet for us, and that it passes sanity checks */
if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
|| packet.ip.ihl != (sizeof(packet.ip) >> 2) || packet.ip.ihl != (sizeof(packet.ip) >> 2)
|| packet.udp.dest != htons(CLIENT_PORT) || packet.udp.dest != htons(CLIENT_PORT)
|| bytes > (int) sizeof(struct udp_dhcp_packet) /* || bytes > (int) sizeof(packet) - can't happen */
|| ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
) { ) {
DEBUG("Unrelated/bogus packet"); DEBUG("Unrelated/bogus packet");
@ -211,12 +212,12 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
packet.ip.check = 0; packet.ip.check = 0;
if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) { if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
DEBUG("Bad IP header checksum, ignoring"); DEBUG("Bad IP header checksum, ignoring");
return -1; return -2;
} }
/* verify UDP checksum. IP header has to be modified for this */ /* verify UDP checksum. IP header has to be modified for this */
memset(&packet.ip, 0, offsetof(struct iphdr, protocol)); memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
/* fields which are not memset: protocol, check, saddr, daddr */ /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
packet.ip.tot_len = packet.udp.len; /* yes, this is needed */ packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
check = packet.udp.check; check = packet.udp.check;
packet.udp.check = 0; packet.udp.check = 0;
@ -228,7 +229,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp))); memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
if (payload->cookie != htonl(DHCP_MAGIC)) { if (payload->cookie != htonl(DHCP_MAGIC)) {
bb_error_msg("received bogus message (bad magic) - ignoring"); bb_error_msg("received bogus message (bad magic), ignoring");
return -2; return -2;
} }
DEBUG("Got valid DHCP packet"); DEBUG("Got valid DHCP packet");

View File

@ -454,7 +454,7 @@ int udhcpc_main(int argc, char **argv)
len = udhcp_recv_packet(&packet, sockfd); len = udhcp_recv_packet(&packet, sockfd);
else len = get_raw_packet(&packet, sockfd); else len = get_raw_packet(&packet, sockfd);
if (len == -1 && errno != EINTR) { if (len == -1) { /* error is severe, reopen socket */
DEBUG("error on read, %s, reopening socket", strerror(errno)); DEBUG("error on read, %s, reopening socket", strerror(errno));
change_listen_mode(listen_mode); /* just close and reopen */ change_listen_mode(listen_mode); /* just close and reopen */
} }

View File

@ -41,20 +41,14 @@ void udhcp_init_header(struct dhcpMessage *packet, char type)
/* read a packet from socket fd, return -1 on read error, -2 on packet error */ /* read a packet from socket fd, return -1 on read error, -2 on packet error */
int udhcp_recv_packet(struct dhcpMessage *packet, int fd) int udhcp_recv_packet(struct dhcpMessage *packet, int fd)
{ {
#if 0
static const char broken_vendors[][8] = {
"MSFT 98",
""
};
#endif
int bytes; int bytes;
unsigned char *vendor; unsigned char *vendor;
memset(packet, 0, sizeof(*packet)); memset(packet, 0, sizeof(*packet));
bytes = read(fd, packet, sizeof(*packet)); bytes = safe_read(fd, packet, sizeof(*packet));
if (bytes < 0) { if (bytes < 0) {
DEBUG("cannot read on listening socket, ignoring"); DEBUG("cannot read on listening socket, ignoring");
return -1; return bytes; /* returns -1 */
} }
if (packet->cookie != htonl(DHCP_MAGIC)) { if (packet->cookie != htonl(DHCP_MAGIC)) {
@ -67,6 +61,10 @@ int udhcp_recv_packet(struct dhcpMessage *packet, int fd)
vendor = get_option(packet, DHCP_VENDOR); vendor = get_option(packet, DHCP_VENDOR);
if (vendor) { if (vendor) {
#if 0 #if 0
static const char broken_vendors[][8] = {
"MSFT 98",
""
};
int i; int i;
for (i = 0; broken_vendors[i][0]; i++) { for (i = 0; broken_vendors[i][0]; i++) {
if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i]) if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
@ -127,10 +125,11 @@ int udhcp_send_raw_packet(struct dhcpMessage *payload,
uint32_t source_ip, int source_port, uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex) uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex)
{ {
int fd;
int result;
struct sockaddr_ll dest; struct sockaddr_ll dest;
struct udp_dhcp_packet packet; struct udp_dhcp_packet packet;
int fd;
int result = -1;
const char *msg;
enum { enum {
IP_UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, IP_UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
@ -139,8 +138,8 @@ int udhcp_send_raw_packet(struct dhcpMessage *payload,
fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
if (fd < 0) { if (fd < 0) {
bb_perror_msg("socket"); msg = "socket(%s)";
return -1; goto ret_msg;
} }
memset(&dest, 0, sizeof(dest)); memset(&dest, 0, sizeof(dest));
@ -152,10 +151,9 @@ int udhcp_send_raw_packet(struct dhcpMessage *payload,
dest.sll_ifindex = ifindex; dest.sll_ifindex = ifindex;
dest.sll_halen = 6; dest.sll_halen = 6;
memcpy(dest.sll_addr, dest_arp, 6); memcpy(dest.sll_addr, dest_arp, 6);
if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) { if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
bb_perror_msg("bind"); msg = "bind(%s)";
close(fd); goto ret_close;
return -1;
} }
packet.ip.protocol = IPPROTO_UDP; packet.ip.protocol = IPPROTO_UDP;
@ -179,11 +177,15 @@ int udhcp_send_raw_packet(struct dhcpMessage *payload,
* If you need to change this: last byte of the packet is * If you need to change this: last byte of the packet is
* packet.data.options[end_option(packet.data.options)] * packet.data.options[end_option(packet.data.options)]
*/ */
result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, (struct sockaddr *) &dest, sizeof(dest)); result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
if (result <= 0) { (struct sockaddr *) &dest, sizeof(dest));
bb_perror_msg("sendto"); msg = "sendto";
} ret_close:
close(fd); close(fd);
if (result < 0) {
ret_msg:
bb_perror_msg(msg, "PACKET");
}
return result; return result;
} }
@ -193,41 +195,48 @@ int udhcp_send_kernel_packet(struct dhcpMessage *payload,
uint32_t source_ip, int source_port, uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port) uint32_t dest_ip, int dest_port)
{ {
int fd, result;
struct sockaddr_in client; struct sockaddr_in client;
int fd;
int result = -1;
const char *msg;
enum { enum {
DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
}; };
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0) if (fd < 0) {
return -1; msg = "socket(%s)";
goto ret_msg;
}
setsockopt_reuseaddr(fd); setsockopt_reuseaddr(fd);
memset(&client, 0, sizeof(client)); memset(&client, 0, sizeof(client));
client.sin_family = AF_INET; client.sin_family = AF_INET;
client.sin_port = htons(source_port); client.sin_port = htons(source_port);
client.sin_addr.s_addr = source_ip; client.sin_addr.s_addr = source_ip;
if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
close(fd); msg = "bind(%s)";
return -1; goto ret_close;
} }
memset(&client, 0, sizeof(client)); memset(&client, 0, sizeof(client));
client.sin_family = AF_INET; client.sin_family = AF_INET;
client.sin_port = htons(dest_port); client.sin_port = htons(dest_port);
client.sin_addr.s_addr = dest_ip; client.sin_addr.s_addr = dest_ip;
if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) { msg = "connect";
close(fd); goto ret_close;
return -1;
} }
/* Currently we send full-sized DHCP packets (see above) */ /* Currently we send full-sized DHCP packets (see above) */
result = write(fd, payload, DHCP_SIZE); result = safe_write(fd, payload, DHCP_SIZE);
msg = "write";
ret_close:
close(fd); close(fd);
if (result < 0) {
ret_msg:
bb_perror_msg(msg, "UDP");
}
return result; return result;
} }