I want to write simple DHCP client (which will be working over WLAN) and I have a problem with correctly sending the initial message DHCP DISCOVER
- it is sent (I see it in Wireshark when capturing the WLAN interface), but source address is address of my adapter. How can I set the IP to '0.0.0.0'?
Here is a part of my code:
sockaddr_in src_addr;
memset(&src_addr, 0, sizeof(struct sockaddr_in));
src_addr.sin_family = AF_INET;
src_addr.sin_port = htons(m_sport);
src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
status = bind(m_sockfd, reinterpret_cast<sockaddr *>(&src_addr), sizeof(sockaddr_in));
When I try src_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
, source address is still set from eth0 (10.132...).
0.0.0.0
? That's an invalid IP, and will likely cause issues with Wireshark. – AStopher0.0.0.0
is a valid IP address. In the context of dhcp it is used to indicate that the device has not yet been assigned an IP address yet. – HamZadhclient
, source address of this packet is0.0.0.0
. – triveltdhclient
uses raw sockets and manually adds the IP header in front of the data packet. Sending from a socket bound to0.0.0.0
is esentially the same as sending from a unbound socket. Source IP is selected according to the routing table. I short: You need to use raw sockets. – dhke