+ md5_ctx_t md5;
+ uint8_t secretbytes[64];
+ memset(secretbytes, 0, sizeof(secretbytes));
+ memcpy(secretbytes, a->key, sizeof(a->key));
+
+ for (size_t i = 0; i < sizeof(secretbytes); ++i)
+ secretbytes[i] ^= 0x36;
+
+ md5_begin(&md5);
+ md5_hash(secretbytes, sizeof(secretbytes), &md5);
+ md5_hash(&fr_msg, sizeof(fr_msg), &md5);
+ md5_end(auth_o->key, &md5);
+
+ for (size_t i = 0; i < sizeof(secretbytes); ++i) {
+ secretbytes[i] ^= 0x36;
+ secretbytes[i] ^= 0x5c;
+ }
+
+ md5_begin(&md5);
+ md5_hash(secretbytes, sizeof(secretbytes), &md5);
+ md5_hash(auth_o->key, sizeof(auth_o->key), &md5);
+ md5_end(auth_o->key, &md5);
+ } else {
+ dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_SERVERID, 4,
+ &a->fr_ip->addr.addr.in.s_addr);
+ dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
+ }
+
+ struct sockaddr_in dest;
+ memset(&dest, 0, sizeof(dest));
+ dest.sin_family = AF_INET;
+ dest.sin_port = htons(DHCPV4_CLIENT_PORT);
+ dest.sin_addr.s_addr = a->addr;
+
+ if (sendto(iface->dhcpv4_event.uloop.fd, &fr_msg, sizeof(fr_msg),
+ MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
+ syslog(LOG_ERR, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg),
+ odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
+ else
+ syslog(LOG_WARNING, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg),
+ odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
+}
+
+static void dhcpv4_fr_timer(struct uloop_timeout *event)
+{
+ struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
+
+ if (a->fr_cnt > 0 && a->fr_cnt < 8) {
+ dhcpv4_fr_send(a);
+ uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
+ a->fr_cnt++;
+ } else
+ dhcpv4_fr_stop(a);
+}
+
+static void dhcpv4_fr_start(struct dhcpv4_assignment *a)
+{
+ uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
+ a->fr_timer.cb = dhcpv4_fr_timer;
+ a->fr_cnt++;
+
+ dhcpv4_fr_send(a);
+}
+
+static void dhcpv4_fr_delay_timer(struct uloop_timeout *event)
+{
+ struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
+ struct interface *iface = a->iface;
+
+ (iface->dhcpv4_event.uloop.fd == -1 ? dhcpv4_fr_rand_delay(a) : dhcpv4_fr_start(a));
+}
+
+static void dhcpv4_fr_rand_delay(struct dhcpv4_assignment *a)
+{
+#define MIN_DELAY 500
+#define MAX_FUZZ 500
+ int msecs;
+
+ odhcpd_urandom(&msecs, sizeof(msecs));
+
+ msecs = labs(msecs)%MAX_FUZZ + MIN_DELAY;
+
+ uloop_timeout_set(&a->fr_timer, msecs);
+ a->fr_timer.cb = dhcpv4_fr_delay_timer;
+}
+
+static void dhcpv4_fr_stop(struct dhcpv4_assignment *a)
+{
+ uloop_timeout_cancel(&a->fr_timer);
+ decr_ref_cnt_ip(&a->fr_ip, a->iface);
+ a->fr_cnt = 0;
+ a->fr_timer.cb = NULL;
+}
+
+/* Handler for DHCPv4 messages */