/* wm5 dll's related to tcpip networking ws2.dll iphlpapi.dll ipnat.dll ipsec.dll ipsecsvc.dll ndis.dll tcpip6.dll tcpstk.dll ipv6hlp.dll ws2instl.dll netui.dll */ #include #include #include "stringutils.h" #include "vectorutils.h" #include "debug.h" class win32_error { public: win32_error(const char*msg) : _msg(msg), _err(GetLastError()) { } win32_error(DWORD err, const char*msg) : _msg(msg), _err(err) { } ~win32_error() { error(_err, "%s\n", _msg); } private: const char *_msg; DWORD _err; }; ByteVector read_adapterinfo() { ByteVector v(sizeof(IP_ADAPTER_INFO)); DWORD size= v.size(); DWORD res= GetAdaptersInfo((IP_ADAPTER_INFO*)&v[0], &size); if (res==ERROR_BUFFER_OVERFLOW) { v.resize(size); res= GetAdaptersInfo((IP_ADAPTER_INFO*)&v[0], &size); } if (res!=ERROR_SUCCESS) throw win32_error(res, "GetAdaptersInfo"); return v; } ByteVector read_interfaceinfo() { ByteVector v(sizeof(IP_INTERFACE_INFO)); DWORD size= v.size(); DWORD res= GetInterfaceInfo((IP_INTERFACE_INFO*)&v[0], &size); if (res==ERROR_BUFFER_OVERFLOW) { v.resize(size); res= GetInterfaceInfo((IP_INTERFACE_INFO*)&v[0], &size); } if (res!=ERROR_SUCCESS) throw win32_error(res, "GetInterfaceInfo"); return v; } std::string IpMaskString(IP_MASK_STRING *p) { return p->String; } std::string IpAddressString(IP_ADDRESS_STRING *p) { return p->String; } std::string IpAddrString(IP_ADDR_STRING *p) { return stringformat("ct=%08lx: m=%s, a=%s\n", p->Context, IpMaskString(&p->IpMask).c_str(), IpAddressString(&p->IpAddress).c_str()); } std::string IpAddrList(IP_ADDR_STRING *p) { StringList l; while (p) { l.push_back(IpAddrString(p)); p= p->Next; } return JoinStringList(l, ", "); } void dump_adapter_info(IP_ADAPTER_INFO *p) { debug("c-%d i-%d t-%d dhcp-%d wins-%d %s %s\n", p->ComboIndex, p->Index, p->Type, p->DhcpEnabled, p->HaveWins, p->AdapterName, p->Description); debug("addr(l=%d): %s\n", p->AddressLength, hexdump(p->Address, p->AddressLength).c_str()); debug("curaddr=%08lx\n", p->CurrentIpAddress); debug("ipaddress: %s\n", IpAddrList(&p->IpAddressList).c_str()); debug("gateway: %s\n", IpAddrList(&p->GatewayList).c_str()); debug("dhcpsvr: %s\n", IpAddrList(&p->DhcpServer).c_str()); debug("wins1: %s\n", IpAddrList(&p->PrimaryWinsServer).c_str()); debug("wins2: %s\n", IpAddrList(&p->SecondaryWinsServer).c_str()); debug("lease: obt=%08lx, exp=%08lx\n", p->LeaseObtained, p->LeaseExpires); } void dump_adapters(IP_ADAPTER_INFO *p) { while (p) { dump_adapter_info(p); p= p->Next; } } void dump_adapter(IP_ADAPTER_INDEX_MAP *p) { debug("%2d : %s\n", p->Index, ToString(p->Name).c_str()); } void dump_interfaces(IP_INTERFACE_INFO *p) { for (int i=0 ; iNumAdapters ; i++) { dump_adapter(&p->Adapter[i]); } } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DebugSetLogfile("tcpipdump.log"); ByteVector ai= read_adapterinfo(); dump_adapters((IP_ADAPTER_INFO*)&ai[0]); ByteVector ii= read_interfaceinfo(); dump_interfaces((IP_INTERFACE_INFO*)&ii[0]); }