I have called getpeername
on my connected socket and now have an IPv6 address of the connected peer. How do I tell whether my peer's IP address is localhost?
Edit: To clarify, I mean specifically localhost, as in the loopback IP address.
In IPv6, the loopback address is ::1
.
At the binary level, that is 127 0's followed by a single 1.
After ruling out the loopback address, retreive the local machine's list of current active IPs (the machine may have multiple IPs assigned to it for different networks) and then loop through that list until you find a match. How you actually get that list depends on the OS you are using. The OS may have its own APIs for that (Windows has GetAdaptersInfo() and related functions, for instance), or you can try using gethostname() with gethostbyname() or getaddrinfo().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With