diff --git a/prefab/packet_parsers/dhcp.c b/prefab/packet_parsers/dhcp.c index 843b559..2a946f9 100644 --- a/prefab/packet_parsers/dhcp.c +++ b/prefab/packet_parsers/dhcp.c @@ -215,7 +215,7 @@ static void dhcp_discover() { memcpy(props.chaddr, s.intf->mac, ETH_HW_ADDR_LEN); DhcpOption optEnd = {DHCP_OPT_End, 0, NULL}; - uint16_t maxSize = 1500; + uint16_t maxSize = 1500; // TODO... DhcpOption maxMsgSize = {DHCP_OPT_MaxMsgSize, 2, &optEnd, {UINT16_TO_BE_BYTES(maxSize)}}; DhcpOption msgType = {DHCP_OPT_MsgType, 1, &maxMsgSize, {DHCPDISCOVER}}; @@ -239,11 +239,15 @@ void dhcp_request(ip4_addr reqAddr, ip4_addr dhcpServerAddr) { memcpy(props.chaddr, s.intf->mac, ETH_HW_ADDR_LEN); + uint16_t maxSize = 1500; // TODO... + DhcpOption optEnd = {DHCP_OPT_End, 0, NULL}; DhcpOption paramReq = {DHCP_OPT_ParamReqList, 4, &optEnd, {1, 3, 6, 51}}; // TODO... DhcpOption reqIp = {DHCP_OPT_RequestedIpAddress, 4, ¶mReq, {IPv4_ADDR_TO_BE_BYTES(reqAddr)}}; - DhcpOption clId = {DHCP_OPT_ClientIdentifier, 7, &reqIp, {DHCP_HW_TYPE_ETHERNET, HWADDR_TO_BE_BYTES(s.intf->mac)}}; - DhcpOption msgType = {DHCP_OPT_MsgType, 1, &clId, {DHCPREQUEST}}; + DhcpOption serverIp = {DHCP_OPT_ServerId, 4, &reqIp, {IPv4_ADDR_TO_BE_BYTES(dhcpServerAddr)}}; + DhcpOption clId = {DHCP_OPT_ClientIdentifier, 7, &serverIp, {DHCP_HW_TYPE_ETHERNET, HWADDR_TO_BE_BYTES(s.intf->mac)}}; + DhcpOption maxMsgSize = {DHCP_OPT_MaxMsgSize, 2, &clId, {UINT16_TO_BE_BYTES(maxSize)}}; + DhcpOption msgType = {DHCP_OPT_MsgType, 1, &maxMsgSize, {DHCPREQUEST}}; dhcp_send(&props, &msgType); } @@ -261,10 +265,13 @@ static void dhcp_process(DhcpProps *props, DhcpOption *opts) { case DHCP_SELECTING: { const DhcpOption *msgType = dhcp_get_option(opts, DHCP_OPT_MsgType); if (msgType->value[0] == DHCPOFFER) { - ip4_addr addrOffer = props->yiaddr; - ip4_addr serverAddr = props->siaddr; - - dhcp_request(addrOffer, serverAddr); + const DhcpOption * serverIdentifier = dhcp_get_option(opts, DHCP_OPT_ServerId); + if (serverIdentifier != NULL) { + ip4_addr serverAddr; + FETCH_DWORD(&serverAddr, serverIdentifier->value); + ip4_addr addrOffer = props->yiaddr; + dhcp_request(addrOffer, serverAddr); + } s.state = DHCP_REQUESTING; } @@ -292,8 +299,8 @@ static void dhcp_process(DhcpProps *props, DhcpOption *opts) { uint32_t dhcpLeaseTime_s; FETCH_DWORD_H2N(&dhcpLeaseTime_s, opt->value); - AlarmUserData params = { 0 }; - timer_sched_rel(E.tmr, ((int64_t)dhcpLeaseTime_s) * 1000000, NULL, params); + AlarmUserData params = {0}; + timer_sched_rel(E.tmr, ((int64_t) dhcpLeaseTime_s) * 1000000, NULL, params); MSG("DHCP done!\n"); MSG("IP: ");