diff options
Diffstat (limited to 'sbin/dhclient/dhclient.c')
| -rw-r--r-- | sbin/dhclient/dhclient.c | 105 |
1 files changed, 47 insertions, 58 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index e89901d7424..d97133feecf 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.169 2012/11/07 15:40:13 krw Exp $ */ +/* $OpenBSD: dhclient.c,v 1.170 2012/11/08 21:32:55 krw Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -76,9 +76,8 @@ int no_daemon; int unknown_ok = 1; int routefd = -1; -struct iaddr iaddr_broadcast = { 4, { 255, 255, 255, 255 } }; -struct iaddr iaddr_deleting; -struct iaddr iaddr_adding; +struct in_addr deleting; +struct in_addr adding; struct in_addr inaddr_any; struct sockaddr_in sockaddr_broadcast; @@ -154,20 +153,19 @@ get_ifa(char *cp, int n) return (NULL); } -struct iaddr defaddr = { 4 }; void routehandler(void) { - int linkstat; char msg[2048]; + struct in_addr a; + ssize_t n; + int linkstat; struct rt_msghdr *rtm; struct if_msghdr *ifm; struct ifa_msghdr *ifam; struct if_announcemsghdr *ifan; struct sockaddr *sa; - struct iaddr a; - ssize_t n; char *errmsg; do { @@ -195,10 +193,8 @@ routehandler(void) goto die; } - if ((a.len = sizeof(struct in_addr)) > sizeof(a.iabuf)) - error("king bula sez: len mismatch"); - memcpy(a.iabuf, &((struct sockaddr_in *)sa)->sin_addr, a.len); - if (addr_eq(a, defaddr)) + memcpy(&a, &((struct sockaddr_in *)sa)->sin_addr, sizeof(a)); + if (a.s_addr == INADDR_ANY) break; /* @@ -206,14 +202,14 @@ routehandler(void) * messages generated by that process. */ if (rtm->rtm_type == RTM_NEWADDR) { - if (addr_eq(a, iaddr_adding)) { - iaddr_adding = defaddr; + if (a.s_addr == adding.s_addr) { + adding.s_addr = INADDR_ANY; break; } errmsg = "interface address added"; } else { - if (addr_eq(a, iaddr_deleting)) { - iaddr_deleting = defaddr; + if (a.s_addr == deleting.s_addr) { + deleting.s_addr = INADDR_ANY; break; } errmsg = "interface address deleted"; @@ -482,7 +478,7 @@ state_reboot(void) /* Make a DHCPREQUEST packet, and set appropriate per-interface flags. */ make_request(client->active); - client->destination = iaddr_broadcast; + client->destination.s_addr = INADDR_BROADCAST; client->first_sending = time(NULL); client->interval = 0; @@ -500,7 +496,7 @@ state_init(void) flags. */ make_discover(client->active); client->xid = client->packet.xid; - client->destination = iaddr_broadcast; + client->destination.s_addr = INADDR_BROADCAST; client->state = S_SELECTING; client->first_sending = time(NULL); client->interval = 0; @@ -568,7 +564,7 @@ state_selecting(void) } /* Go to the REQUESTING state. */ - client->destination = iaddr_broadcast; + client->destination.s_addr = INADDR_BROADCAST; client->state = S_REQUESTING; client->first_sending = cur_time; client->interval = 0; @@ -584,7 +580,7 @@ state_selecting(void) } void -dhcpack(struct iaddr client_addr, struct option_data *options) +dhcpack(struct in_addr client_addr, struct option_data *options) { struct client_lease *lease; time_t cur_time; @@ -655,10 +651,9 @@ dhcpack(struct iaddr client_addr, struct option_data *options) void bind_lease(void) { - struct iaddr gateway; + struct in_addr gateway, mask; struct option_data *options; struct client_lease *lease; - in_addr_t mask; char *domainname, *nameservers; delete_old_addresses(ifi->name, ifi->rdomain); @@ -667,13 +662,12 @@ bind_lease(void) lease = apply_defaults(client->new); options = lease->options; - memcpy(&mask, options[DHO_SUBNET_MASK].data, sizeof(mask)); + memcpy(&mask.s_addr, options[DHO_SUBNET_MASK].data, sizeof(in_addr_t)); add_new_address(ifi->name, ifi->rdomain, client->new->address, mask); if (options[DHO_ROUTERS].len) { memset(&gateway, 0, sizeof(gateway)); /* XXX Only use FIRST router address for now. */ - gateway.len = sizeof(in_addr_t); - memcpy(gateway.iabuf, options[DHO_ROUTERS].data, + memcpy(&gateway.s_addr, options[DHO_ROUTERS].data, sizeof(in_addr_t)); add_default_route(ifi->rdomain, client->new->address, gateway); } @@ -708,7 +702,7 @@ bind_lease(void) set_timeout(client->active->renewal, state_bound); note("bound to %s -- renewal in %lld seconds.", - piaddr(client->active->address), + inet_ntoa(client->active->address), (long long)(client->active->renewal - time(NULL))); client->state = S_BOUND; go_daemon(); @@ -728,12 +722,11 @@ state_bound(void) client->xid = client->packet.xid; if (client->active->options[DHO_DHCP_SERVER_IDENTIFIER].len == 4) { - memcpy(client->destination.iabuf, + memcpy(&client->destination.s_addr, client->active->options[DHO_DHCP_SERVER_IDENTIFIER].data, - 4); - client->destination.len = 4; + sizeof(in_addr_t)); } else - client->destination = iaddr_broadcast; + client->destination.s_addr = INADDR_BROADCAST; client->first_sending = time(NULL); client->interval = 0; @@ -743,7 +736,7 @@ state_bound(void) } void -dhcpoffer(struct iaddr client_addr, struct option_data *options) +dhcpoffer(struct in_addr client_addr, struct option_data *options) { struct client_lease *lease, *lp; int i; @@ -766,9 +759,8 @@ dhcpoffer(struct iaddr client_addr, struct option_data *options) /* If we've already seen this lease, don't record it again. */ for (lease = client->offered_leases; lease; lease = lease->next) { - if (lease->address.len == sizeof(client->packet.yiaddr) && - !memcmp(lease->address.iabuf, - &client->packet.yiaddr, lease->address.len)) { + if (!memcmp(&lease->address.s_addr, &client->packet.yiaddr, + sizeof(in_addr_t))) { #ifdef DEBUG debug("%s already seen.", name); #endif @@ -799,7 +791,7 @@ dhcpoffer(struct iaddr client_addr, struct option_data *options) /* If this is the lease we asked for, put it at the head of the list, and don't mess with the arp request timeout. */ - if (addr_eq(lease->address, client->requested_address)) { + if (lease->address.s_addr == client->requested_address.s_addr) { lease->next = client->offered_leases; client->offered_leases = lease; } else { @@ -830,7 +822,7 @@ dhcpoffer(struct iaddr client_addr, struct option_data *options) * parameters in the specified packet. */ struct client_lease * -packet_to_lease(struct iaddr client_addr, struct option_data *options) +packet_to_lease(struct in_addr client_addr, struct option_data *options) { struct client_lease *lease; int i; @@ -858,9 +850,8 @@ packet_to_lease(struct iaddr client_addr, struct option_data *options) } } - lease->address.len = sizeof(client->packet.yiaddr); - memcpy(lease->address.iabuf, &client->packet.yiaddr, - lease->address.len); + memcpy(&lease->address.s_addr, &client->packet.yiaddr, + sizeof(in_addr_t)); /* If the server name was filled out, copy it. */ if ((!lease->options[DHO_DHCP_OPTION_OVERLOAD].len || @@ -900,7 +891,7 @@ packet_to_lease(struct iaddr client_addr, struct option_data *options) } void -dhcpnak(struct iaddr client_addr, struct option_data *options) +dhcpnak(struct in_addr client_addr, struct option_data *options) { if (client->state != S_REBOOTING && client->state != S_REQUESTING && @@ -1014,7 +1005,7 @@ state_panic(void) while (client->active) { if (client->active->expiry > cur_time) { note("Trying recorded lease %s", - piaddr(client->active->address)); + inet_ntoa(client->active->address)); /* * If the old lease is still good and doesn't @@ -1144,14 +1135,13 @@ send_request(void) cur_time > client->active->rebind) destination.sin_addr.s_addr = INADDR_BROADCAST; else - memcpy(&destination.sin_addr.s_addr, client->destination.iabuf, - sizeof(destination.sin_addr.s_addr)); + destination.sin_addr.s_addr = client->destination.s_addr; destination.sin_port = htons(REMOTE_PORT); destination.sin_family = AF_INET; destination.sin_len = sizeof(destination); if (client->state != S_REQUESTING) - memcpy(&from, client->active->address.iabuf, sizeof(from)); + from.s_addr = client->active->address.s_addr; else from.s_addr = INADDR_ANY; @@ -1207,10 +1197,10 @@ make_discover(struct client_lease *lease) if (lease) { client->requested_address = lease->address; i = DHO_DHCP_REQUESTED_ADDRESS; - options[i].data = lease->address.iabuf; - options[i].len = lease->address.len; + options[i].data = (char *)&lease->address; + options[i].len = sizeof(lease->address); } else - client->requested_address.len = 0; + client->requested_address.s_addr = INADDR_ANY; /* Send any options requested in the config file. */ for (i = 0; i < 256; i++) @@ -1276,10 +1266,9 @@ make_request(struct client_lease * lease) client->state == S_REBOOTING) { client->requested_address = lease->address; i = DHO_DHCP_REQUESTED_ADDRESS; - options[i].data = lease->address.iabuf; - options[i].len = lease->address.len; - } else - client->requested_address.len = 0; + options[i].data = (char *)&lease->address.s_addr; + options[i].len = sizeof(in_addr_t); + } /* Send any options requested in the config file. */ for (i = 0; i < 256; i++) @@ -1309,8 +1298,8 @@ make_request(struct client_lease * lease) if (client->state == S_BOUND || client->state == S_RENEWING || client->state == S_REBINDING) { - memcpy(&client->packet.ciaddr, - lease->address.iabuf, lease->address.len); + memcpy(&client->packet.ciaddr, &lease->address.s_addr, + sizeof(in_addr_t)); } else { memset(&client->packet.ciaddr, 0, sizeof(client->packet.ciaddr)); @@ -1345,8 +1334,8 @@ make_decline(struct client_lease *lease) /* Send back the address we're declining. */ i = DHO_DHCP_REQUESTED_ADDRESS; - options[i].data = lease->address.iabuf; - options[i].len = lease->address.len; + options[i].data = (char *)&lease->address.s_addr; + options[i].len = sizeof(in_addr_t); /* Send the uid if the user supplied one. */ i = DHO_DHCP_CLIENT_IDENTIFIER; @@ -1408,8 +1397,8 @@ rewrite_client_leases(void) rewind(leaseFile); for (lp = client->leases; lp; lp = lp->next) { - if (client->active && addr_eq(lp->address, - client->active->address)) + if (client->active && lp->address.s_addr == + client->active->address.s_addr) continue; write_client_lease(lp); } @@ -1440,7 +1429,7 @@ write_client_lease(struct client_lease *lease) if (lease->is_bootp) fprintf(leaseFile, " bootp;\n"); fprintf(leaseFile, " interface \"%s\";\n", ifi->name); - fprintf(leaseFile, " fixed-address %s;\n", piaddr(lease->address)); + fprintf(leaseFile, " fixed-address %s;\n", inet_ntoa(lease->address)); if (lease->filename) fprintf(leaseFile, " filename \"%s\";\n", lease->filename); if (lease->server_name) |
