CyberShadow / dhcptest

Cross-platform DHCP test client
https://blog.cy.md/2013/01/10/dhcp-test-client
366 stars 57 forks source link

Support using the device MAC address #3

Open yan12125 opened 9 years ago

yan12125 commented 9 years ago

I would like to use device MAC address in the --mac option. For example, on Linux, a possible usage may be:

$ sudo ./dhcptest --quiet
d @eth0
  op=BOOTREPLY chaddr=[MAC address of device eth0] hops=1 xid=D772D15D secs=0 flags=8000
  ciaddr=0.0.0.0 yiaddr=[my IP] siaddr=0.0.0.0 giaddr=[my IP] sname= file=
  10 options:
     53 (DHCP Message Type): offer
     54 (Server Identifier): [DHCP server IP]
     51 (IP Address Lease Time): 86400 (1 day)
      1 (Subnet Mask): 255.255.255.0
      3 (Router Option): [Router IP]
      6 (Domain Name Server Option): [DNS servers' IPs]
     58 (Renewal (T1) Time Value): 43200 (12 hours)
     59 (Rebinding (T2) Time Value): 75600 (21 hours)
     28 (Broadcast Address Option): [Broadcast address]
     15 (Domain Name): [Domain name]
q

I have an implementation at https://github.com/yan12125/dhcptest, however, I'm not familiar with D and the implementation is naive. Could you add the support?

CyberShadow commented 9 years ago

Why not use dhclient? And, why not run ./dhcptest --mac $(ip link show eth0 | tail -1 | cut -d ' ' -f 6) OSLT?

CyberShadow commented 9 years ago

I am hesitant to add platform-specific functionality to this tool. I think there are many Linux-specific tools, e.g.: https://github.com/saravana815/dhtest

scotepi commented 9 years ago

I would love to something added such as --clientmac that would allow using the client mac without having to look it up first on windows or remember that string on linux.

Having a random mac is great for some networks but others that are slightly locked down using the clients is a lot more reliable.

yan12125 commented 9 years ago

dhtest and other linux-specific tools are also great tools on testing DHCP functionality. However, dhcptest is more handy becuase it dumps all DHCP options. I may try to implement the Windows corresponding part if I have time. After all, still many thanks to the information!

CyberShadow commented 9 years ago

Here is some Windows code:

struct NetworkInterface
{
    string name;
    ubyte[] mac;
    string[] ips;
}

NetworkInterface[] getInterfaces()
{
    version (Windows)
    {
        // For this functionality, the Windows bindings are required:
        // $ git clone https://github.com/CS-svnmirror/dsource-bindings-win32 win32
        version(HAVE_WIN32)
        {
            import win32.windef;
            import win32.iphlpapi;
            import win32.iptypes;
            import std.windows.syserror;

            DWORD dwBufLen = 0;
            GetAdaptersInfo(null,  &dwBufLen);
            PIP_ADAPTER_INFO adapterInfo = cast(PIP_ADAPTER_INFO)new ubyte[dwBufLen];
            GetAdaptersInfo(adapterInfo, &dwBufLen).wenforce("GetAdaptersInfo");

            NetworkInterface[] result;
            for (; adapterInfo; adapterInfo = adapterInfo.Next)
            {
                NetworkInterface iface;
                iface.name = adapterInfo.AdapterName.to!string();
                iface.mac = adapterInfo.Address[0..adapterInfo.AddressLength];
                for (auto addr = &adapterInfo.IpAddressList; addr; addr = addr.Next)
                    iface.ips ~= addr.IpAddress.String.to!string();
            }
            return result;
        }
    }
    version (linux)
    {
        // ...
    }

    assert(false, "This functionality is not available in this version or on this platform");
}

Windows interface names are long and don't make much sense, so it would be good to also allow using the interface IP address, using the same syntax.