chiark / gitweb /
sd-dhcp-client: check return from dhcp_option_append
[elogind.git] / src / libsystemd-network / sd-dhcp-client.c
index 67593c42cdd6ba27d58279e133958057ebb0b78b..c1af6df13191352488b6715c95553846c2d17493 100644 (file)
@@ -286,6 +286,15 @@ static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret,
            refuse to issue an DHCP lease if 'secs' is set to zero */
         packet->dhcp.secs = htobe16(client->secs);
 
+        /* RFC2132 section 4.1
+           A client that cannot receive unicast IP datagrams until its protocol
+           software has been configured with an IP address SHOULD set the
+           BROADCAST bit in the 'flags' field to 1 in any DHCPDISCOVER or
+           DHCPREQUEST messages that client sends.  The BROADCAST bit will
+           provide a hint to the DHCP server and BOOTP relay agent to broadcast
+           any messages to the client on the client's subnet. */
+        packet->dhcp.flags = htobe16(0x8000);
+
         /* RFC2132 section 4.1.1:
            The client MUST include its hardware address in the â€™chaddr’ field, if
            necessary for delivery of DHCP reply messages.
@@ -389,6 +398,8 @@ static int client_send_discover(sd_dhcp_client *client) {
 
         r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
                                DHCP_OPTION_END, 0, NULL);
+        if (r < 0)
+                return r;
 
         /* We currently ignore:
            The client SHOULD wait a random time between one and ten seconds to
@@ -404,7 +415,7 @@ static int client_send_discover(sd_dhcp_client *client) {
 }
 
 static int client_send_request(sd_dhcp_client *client) {
-        _cleanup_free_ DHCPPacket *request;
+        _cleanup_free_ DHCPPacket *request = NULL;
         size_t optoffset, optlen;
         int r;
 
@@ -1200,7 +1211,7 @@ static int client_receive_message_udp(sd_event_source *s, int fd,
         len = read(fd, message, buflen);
         if (len < 0) {
                 log_dhcp_client(client, "could not receive message from UDP "
-                                "socket: %s", strerror(errno));
+                                "socket: %m");
                 return 0;
         } else if ((size_t)len < sizeof(DHCPMessage))
                 return 0;
@@ -1245,7 +1256,7 @@ static int client_receive_message_raw(sd_event_source *s, int fd,
         len = recvmsg(fd, &msg, 0);
         if (len < 0) {
                 log_dhcp_client(client, "could not receive message from raw "
-                                "socket: %s", strerror(errno));
+                                "socket: %m");
                 return 0;
         } else if ((size_t)len < sizeof(DHCPPacket))
                 return 0;