iface->state->lease.addr.s_addr = 0;
}
-void
-close_sockets(struct interface *iface)
-{
- if (iface->arp_fd != -1) {
- delete_event(iface->arp_fd);
- close(iface->arp_fd);
- iface->arp_fd = -1;
- }
- if (iface->raw_fd != -1) {
- delete_event(iface->raw_fd);
- close(iface->raw_fd);
- iface->raw_fd = -1;
- }
- if (iface->udp_fd != -1) {
- /* we don't listen to events on the udp */
- close(iface->udp_fd);
- iface->udp_fd = -1;
- }
-}
-
struct interface *
find_interface(const char *ifname)
{
iface->name, get_dhcp_op(type), state->xid,
timeval_to_double(&tv));
}
+
+ /* Ensure sockets are open. */
+ open_sockets(iface);
+
/* If we couldn't open a UDP port for our IP address
* then we cannot renew.
* This could happen if our IP was pulled out from underneath us.
to.s_addr = 0;
if (to.s_addr && to.s_addr != INADDR_BROADCAST) {
r = send_packet(iface, to, (uint8_t *)dhcp, len);
- if (r == -1)
+ if (r == -1) {
syslog(LOG_ERR, "%s: send_packet: %m", iface->name);
+ close_sockets(iface);
+ }
} else {
len = make_udp_packet(&udp, (uint8_t *)dhcp, len, from, to);
r = send_raw_packet(iface, ETHERTYPE_IP, udp, len);
}
}
free(dhcp);
+
/* Even if we fail to send a packet we should continue as we are
* as our failure timeouts will change out codepath when needed. */
if (callback)
drop_config(iface, "NAK");
unlink(iface->leasefile);
}
- delete_event(iface->raw_fd);
- close(iface->raw_fd);
- iface->raw_fd = -1;
- close(iface->udp_fd);
- iface->udp_fd = -1;
+ close_sockets(iface);
/* If we constantly get NAKS then we should slowly back off */
add_timeout_sec(state->nakoff, start_interface, iface);
state->nakoff *= 2;
free(dhcp);
}
-static void
-open_sockets(struct interface *iface)
-{
- if (iface->raw_fd == -1) {
- if (open_socket(iface, ETHERTYPE_IP) == -1)
- syslog(LOG_ERR, "%s: open_socket: %m", iface->name);
- else
- add_event(iface->raw_fd, handle_dhcp_packet, iface);
- }
- if (iface->udp_fd == -1 &&
- iface->addr.s_addr != 0 &&
- iface->state->new != NULL &&
- iface->state->new->cookie == htonl(MAGIC_COOKIE))
- {
- if (open_udp_socket(iface) == -1 && errno != EADDRINUSE)
- syslog(LOG_ERR, "%s: open_udp_socket: %m", iface->name);
- }
-}
-
static void
send_release(struct interface *iface)
{
{
syslog(LOG_INFO, "%s: releasing lease of %s",
iface->name, inet_ntoa(iface->state->lease.addr));
- open_sockets(iface);
iface->state->xid = dhcp_xid(iface);
send_message(iface, DHCP_RELEASE, NULL);
/* Give the packet a chance to go before dropping the ip */
void
send_decline(struct interface *iface)
{
- open_sockets(iface);
send_message(iface, DHCP_DECLINE, NULL);
}
iface->state->state = DHS_DISCOVER;
iface->state->xid = dhcp_xid(iface);
- open_sockets(iface);
delete_timeout(NULL, iface);
if (ifo->fallback)
add_timeout_sec(ifo->timeout, start_fallback, iface);
iface->name, inet_ntoa(iface->state->lease.addr));
iface->state->state = DHS_RENEW;
iface->state->xid = dhcp_xid(iface);
- open_sockets(iface);
send_renew(iface);
}
iface->state->state = DHS_REBIND;
delete_timeout(send_renew, iface);
iface->state->lease.server.s_addr = 0;
- if (iface->raw_fd == -1)
- open_sockets(iface);
send_rebind(iface);
}
iface->state->state = DHS_INFORM;
iface->state->xid = dhcp_xid(iface);
- open_sockets(iface);
send_inform(iface);
}
else if (!(ifo->options & DHCPCD_INFORM &&
options & (DHCPCD_MASTER | DHCPCD_DAEMONISED)))
add_timeout_sec(ifo->reboot, start_expire, iface);
- open_sockets(iface);
/* Don't bother ARP checking as the server could NAK us first. */
if (ifo->options & DHCPCD_INFORM)
send_inform(iface);
dst ? dst->s_addr : INADDR_ANY;
ifp->addr = *addr;
ifp->net = *net;
- open_sockets(ifp);
send_inform(ifp);
}
break;
return 0;
}
+void
+open_sockets(struct interface *iface)
+{
+ if (iface->raw_fd == -1) {
+ if (open_socket(iface, ETHERTYPE_IP) == -1)
+ syslog(LOG_ERR, "%s: open_socket: %m", iface->name);
+ else
+ add_event(iface->raw_fd, handle_dhcp_packet, iface);
+ }
+ if (iface->udp_fd == -1 &&
+ iface->addr.s_addr != 0 &&
+ iface->state->new != NULL &&
+ iface->state->new->cookie == htonl(MAGIC_COOKIE))
+ {
+ if (open_udp_socket(iface) == -1 && errno != EADDRINUSE)
+ syslog(LOG_ERR, "%s: open_udp_socket: %m", iface->name);
+ }
+}
+
+void
+close_sockets(struct interface *iface)
+{
+ if (iface->arp_fd != -1) {
+ delete_event(iface->arp_fd);
+ close(iface->arp_fd);
+ iface->arp_fd = -1;
+ }
+ if (iface->raw_fd != -1) {
+ delete_event(iface->raw_fd);
+ close(iface->raw_fd);
+ iface->raw_fd = -1;
+ }
+ if (iface->udp_fd != -1) {
+ /* we don't listen to events on the udp */
+ close(iface->udp_fd);
+ iface->udp_fd = -1;
+ }
+}
+
int
main(int argc, char **argv)
{