From: Tom Gundersen Date: Fri, 11 Apr 2014 17:54:04 +0000 (+0200) Subject: sd-dhcp-client: assert that we can only create DISCOVER or REQUEST messages X-Git-Tag: v213~437 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=8a9e761600d0d44bcc2e665e4fe1fda30e3e4749 sd-dhcp-client: assert that we can only create DISCOVER or REQUEST messages --- diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 4be37a238..e690f6785 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -242,6 +242,7 @@ static sd_dhcp_client *client_stop(sd_dhcp_client *client, int error) { static int client_message_init(sd_dhcp_client *client, DHCPMessage *message, uint8_t type, uint8_t **opt, size_t *optlen) { + be16_t max_size; int r; assert(client); @@ -249,6 +250,7 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message, assert(message); assert(opt); assert(optlen); + assert(type == DHCP_DISCOVER || type == DHCP_REQUEST); r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt, optlen); @@ -272,27 +274,23 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message, if (r < 0) return r; - if (type == DHCP_DISCOVER || type == DHCP_REQUEST) { - be16_t max_size; - - r = dhcp_option_append(opt, optlen, - DHCP_OPTION_PARAMETER_REQUEST_LIST, - client->req_opts_size, - client->req_opts); - if (r < 0) - return r; + r = dhcp_option_append(opt, optlen, + DHCP_OPTION_PARAMETER_REQUEST_LIST, + client->req_opts_size, + client->req_opts); + if (r < 0) + return r; - /* Some DHCP servers will send bigger DHCP packets than the - defined default size unless the Maximum Messge Size option - is explicitely set */ - max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE + - DHCP_MIN_OPTIONS_SIZE); - r = dhcp_option_append(opt, optlen, - DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, - 2, &max_size); - if (r < 0) - return r; - } + /* Some DHCP servers will send bigger DHCP packets than the + defined default size unless the Maximum Messge Size option + is explicitely set */ + max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE + + DHCP_MIN_OPTIONS_SIZE); + r = dhcp_option_append(opt, optlen, + DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, + 2, &max_size); + if (r < 0) + return r; return 0; }