bobD 6 Posted yesterday at 03:57 PM Basically, program needs to act differently if at home or away--so how do I know? I've looked at using Winsock and HostEnt := gethostbyaddr(@SocketAddr.sin_addr.S_addr, 4, AF_INET); and although that tells me if my home interbase server is available, in a dynamic DHCP environment that IP address isn't necessarily constant. And a call to addr 192.168.1.1 (home router and default DNS gateway) returns nil: the peer computers respond, but the router doesn't. Is there another way to get the name of the local network LAN that works both wired (desktop machines) and wifi? I'd like to try using winsock's gethostbyname but I haven't found a good use example. Or is there a better way? bobD Share this post Link to post
aehimself 402 Posted 20 hours ago When you are trying to detect your home network, don't rely on a successful (reverse) DNS lookup. I usually ping my home router (using fqdn) but even better, attempt a connection to a well-known service. However I only use this method to decide if VPN should be fired up or not; it's better to set up your DNS properly so tools will work from inside and outside. If you are keen to go this way I found these two snipplets in my codebase: Function LookupHostName(inIP: String): String; Var host: PHostEnt; addr: Integer; dat: TWSAData; Begin Result := ''; WSAStartup($0101, dat); Try addr := inet_addr(PAnsiChar(AnsiString(inIP))); host := GetHostByAddr(@addr, 4, PF_INET); If host <> nil Then Result := String(host.h_name); Finally WSACleanup; End; End; This one was abandoned halfway as no variables seem to be declared: Write('Attempting to resolve ' + HostName + '...'); If WSAStartup($0101, wsdata) = 0 Then Try tmp := GetHostByName(PAnsiChar(HostName)); If tmp <> nil Then Begin tmpin.sin_addr.S_addr := LongInt(PLongInt(tmp^.h_addr_list^)^); IPAddress := inet_ntoa(tmpin.sin_addr); WriteLn(IPAddress); End; Finally WSACleanup; End; May I ask why your program needs to behave different from a specific network? There might be a better way than relying on simple checks, which might trigger in places you don't want them to. Share this post Link to post
Remy Lebeau 1572 Posted 20 hours ago (edited) 3 hours ago, bobD said: Basically, program needs to act differently if at home or away--so how do I know? Ask the user. The OS only knows whether there is a network connection or not. It doesn't care where the network is located or what it's connected to. That's up to the hardware to deal with. Quote Is there another way to get the name of the local network LAN that works both wired (desktop machines) and wifi? No. And as far as sockets are concerned, there is no difference whatsoever whether you are connected to a wired LAN, or to a Wifi, or a cellular provider, etc. The socket API works the same way. The difference is in how the hardware routes the traffic. Quote I'd like to try using winsock's gethostbyname but I haven't found a good use example. Likely because this is not what that API is intended for. Quote Or is there a better way? What EXACTLY are you trying to accomplish in the first place? Edited 20 hours ago by Remy Lebeau Share this post Link to post
Brian Evans 117 Posted 14 hours ago (edited) There are network profiles in Windows which can be Private, Public or Domain. Normally your home network would be Private and anytime you go elsewhere it would be Public (discovery disabled) or Domain. You could check what the active connection is. Or just key off the active profile name for what network settings your application should use. Snippet below for PowerShell. Get-WmiObject MSFT_NetConnectionProfile -Namespace root/StandardCimv2 | select Name,@{n='ActiveNetworkProfile';e={ switch ($_.NetworkCategory){ 0 {'Public'} 1 {'Private'} 2 {'Domain'} Default {$_.NetworkCategory} } } } Edited 14 hours ago by Brian Evans Share this post Link to post
aehimself 402 Posted 8 hours ago 5 hours ago, Brian Evans said: There are network profiles in Windows which can be Private, Public or Domain. Normally your home network would be Private and anytime you go elsewhere it would be Public (discovery disabled) or Domain. You could check what the active connection is. Or just key off the active profile name for what network settings your application should use. This check will trigger on all networks, which are set to private which is probably not a desired behavior. While these will work, OP's solution will be a different design pattern. What if you put the code in a separate DLL, which you are not distributing; and lives only on your home dev PC? Share this post Link to post