0
votes

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...).

1
if you are on linux, default settings from the firewall prevent you from spoofing ip addressesGizmo
Why do you need to set the IP to 0.0.0.0? That's an invalid IP, and will likely cause issues with Wireshark.AStopher
@ʎǝʞuoɯɹǝqʎɔ 0.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.HamZa
Because I want to send DHCP Discover message. What is the correct address in this situation? When I'm using dhclient, source address of this packet is 0.0.0.0.trivelt
dhclient uses raw sockets and manually adds the IP header in front of the data packet. Sending from a socket bound to 0.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

1 Answers

5
votes

The kernel is doing you a service by populating "for free" the source address with the IP on the exit interface.

If you don't want that you'll probably have to use raw sockets and provide your own IP header with IP_HDRINCL. Look for SOCK_RAW.


A simple way to cheat through this is to strace or truss your DHCP client and see what it does.