IP Address Lookup returning IPv6 instead of IPv4 Address (1 Viewer)

Minty

AWF VIP
Local time
Today, 15:17
Joined
Jul 26, 2013
Messages
10,371
This is happening to a number of users but not all, which makes fixing it rather troublesome.

I use the code below to return an IPv4 External IP address, so that we can automatically update an Azure SQL firewall, via some clever HTTP encryption.
It's loosely based on a number of similar codes on the interwebs.
Code:
Function GetMyPublicIP() As String

    Dim HttpRequest As Object

    On Error Resume Next
    'Create the XMLHttpRequest object.
    Set HttpRequest = CreateObject("MSXML2.XMLHTTP")

    'Check if the object was created.
    If Err.Number <> 0 Then
        'Return error message.
        GetMyPublicIP = "Could not create the XMLHttpRequest object!"
        'Release the object and exit.
        Set HttpRequest = Nothing
        Exit Function
    End If
    On Error GoTo 0

    'Create the request - no special parameters required.
    'HttpRequest.Open "GET", "http://myip.dnsomatic.com", False - ''  This site rejects me after a few uses.
    HttpRequest.Open "GET", "https://myexternalip.com/raw", False
    'Send the request to the site.
    HttpRequest.sEnd

    'Return the result of the request (the IP string).
    GetMyPublicIP = HttpRequest.responseText

End Function

The problem as stated above is that it is returning an IPv6 address, on some end users' machines. It works fine for my colleagues and other people in the same organisation. But not all of them.

I'm at a bit of a loss, we have checked and there is no common thread, majority are Windows 10 some are Win 11, some have been connected to a VPN some never connect to a VPN.

If they go to any of the websites directly via a browser, they always get the correct IPv4 address.
Confused.com
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 15:17
Joined
Jul 9, 2003
Messages
16,282
ChatGPT said:-

Fixing IPv6 Issue​

Advanced Data Analysis
Custom instructions details

MS Access code - Please analyse and suggest a fix...

Ah, the quirky issues that only show up in some environments—classic. Let's dig into this.
  1. On Error Resume Next: This statement hides all runtime errors. Although you've checked for object creation failure, any subsequent errors won't be caught. This could make troubleshooting difficult.
  2. HTTP Request URL: The URL "https://myexternalip.com/raw" might return IPv6 depending on the user's network configuration. The behavior can differ from a browser due to how the request is made.
  3. User-Agent: When making a request from a browser, a User-Agent string is sent, which might cause the server to behave differently compared to the plain XMLHTTP request.
  4. VPN & OS Differences: Different VPNs and operating systems may affect how requests are made.

Suggestions​

  1. Error Handling: Remove "On Error Resume Next" and add proper error handling to catch any issues.
  2. IPv4 Only: Consider using a service that guarantees an IPv4 response. Alternatively, you could parse the output and check if it's IPv4 or IPv6, re-requesting if needed.
  3. Set User-Agent: Mimic a browser request by setting the User-Agent string.
Here's a modified version of your code:

Code:
Function GetMyPublicIP() As String

Dim HttpRequest As Object
Dim strIP As String

On Error GoTo ErrorHandler
'Create the XMLHttpRequest object.
Set HttpRequest = CreateObject("MSXML2.XMLHTTP")

'Set User-Agent to mimic a browser
HttpRequest.setRequestHeader "User-Agent", "Mozilla/5.0"

'Send the request
HttpRequest.Open "GET", "https://myexternalip.com/raw", False
HttpRequest.sEnd

strIP = HttpRequest.responseText

'Check if the IP is IPv4. If not, handle accordingly.
If InStr(strIP, ":") = 0 Then 'No colons, should be IPv4
GetMyPublicIP = strIP
Else
GetMyPublicIP = "Received IPv6, handle accordingly."
End If

Exit Function

ErrorHandler:
GetMyPublicIP = "Error: " & Err.Description

End Function

Give this a whirl and see if it addresses your issues.
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:17
Joined
Sep 21, 2011
Messages
14,311
@Minty,
Are you saying happens to just some users that are all on the same WAN ip address?
I have IPV6 set on my wireless connection (normally I do not, so obviously forgot this one) and that code shows my WAN address, but ,my router has no option for IPV6.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 15:17
Joined
Jul 9, 2003
Messages
16,282
ChatGPT sources:-

Please site sources

Certainly, here are some sources to back up the points and suggestions:
  1. On Error Resume Next: The official Microsoft documentation discusses error handling in VBA, including the use of "On Error Resume Next" and its potential downsides.
  2. HTTP Request URL: The behavior of a website in returning IPv4 or IPv6 addresses can depend on various factors, including network configuration and how the request is made.
  3. User-Agent: User-Agent strings can affect how a server responds to your request. While I don't have a direct source indicating that "myexternalip.com" behaves differently based on User-Agent, it's a common practice.
  4. VPN & OS Differences: VPNs and different operating systems can affect how HTTP requests are made and how servers respond to them.
  5. SetRequestHeader Method: The method to set HTTP request headers like "User-Agent" in a VBA XMLHTTP request.
Hope these sources offer further insight!
 

Minty

AWF VIP
Local time
Today, 15:17
Joined
Jul 26, 2013
Messages
10,371
Well, I clicked on the first source that looked vaguely interesting:
404 - Error page not available.

When I add the line
HttpRequest.setRequestHeader "User-Agent", "Mozilla/5.0"
I get an unspecified error.
I checked they have either Edge or Chrome as their default browsers.

With all the best will in the world, after 20 + years of playing with Access, I'd like to think I understood Access Error handling enough to know what the effects of the line are.

Two users have never used a VPN and are suffering from the issue. I tried it through a VPN from home (I'm on a fixed IP, so have a VPN available to check things like this work) and still got the correct results.

I have also wondered if it is the MSXML2 provider request, however, that works on my Win 11 machine and a Win 10 machine I have here.

@Gasman - no they are separate home networks, no VPN, I suspect different providers, although which provider they are with might be worth checking.

So far that makes the score Mr ChatGPT 0 - Rest of the World 0

But RotW has made an interesting suggestion about checking the providers...
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 15:17
Joined
Jul 9, 2003
Messages
16,282
I asked ChatGPT about the broken link:-

I apologize for the inconvenience. The IPv4/IPv6 preference topic is often discussed in the context of network protocols and standards, but it's difficult to cite a specific source for how a particular website like "myexternalip.com" might behave.

For general understanding on why a system might prefer IPv6 over IPv4, you can consult the Internet Engineering Task Force's document:
- [IETF - IPv6 Address Selection](https://datatracker.ietf.org/doc/html/rfc6724)

This document outlines how systems generally determine which type of IP address to use when both are available. It may not directly answer your question but provides some background.

Again, I apologize for the inconvenience earlier.
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:17
Joined
Sep 21, 2011
Messages
14,311
Well, I clicked on the first source that looked vaguely interesting:
404 - Error page not available.

When I add the line
HttpRequest.setRequestHeader "User-Agent", "Mozilla/5.0"
I get an unspecified error.
I checked they have either Edge or Chrome as their default browsers.

With all the best will in the world, after 20 + years of playing with Access, I'd like to think I understood Access Error handling enough to know what the effects of the line are.

Two users have never used a VPN and are suffering from the issue. I tried it through a VPN from home (I'm on a fixed IP, so have a VPN available to check things like this work) and still got the correct results.

I have also wondered if it is the MSXML2 provider request, however, that works on my Win 11 machine and a Win 10 machine I have here.

@Gasman - no they are separate home networks, no VPN, I suspect different providers, although which provider they are with might be worth checking.

So far that makes the score Mr ChatGPT 0 - Rest of the World 0

But RotW has made an interesting suggestion about checking the providers...
Well I am on Virgin, but suspect if the router cannot hold an IPV6 address, then IPV4 would be all you get?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 15:17
Joined
Jul 9, 2003
Messages
16,282
if the router cannot hold an IPV6 address, then IPV4 would be all you get?

I asked my mate ChatGPT who said:-

Exactly, the type of IP address you'll end up with can depend heavily on your network hardware, particularly your router. If a router isn't configured for IPv6 or doesn't support it, then you'd naturally only get an IPv4 address.

1. **IPv4-only Router**: If the router only supports IPv4, then that's all you're going to get, regardless of what your ISP, operating system, or any other factor might support.

2. **IPv6 Configured but Disabled**: Even if a router supports IPv6, it might be disabled in the router settings, resulting in IPv4-only operation.

3. **ISP Support**: Also, your Internet Service Provider must provide you with an IPv6 address. If they don't, you won't have one, even if your router supports it.

4. **Fallback Mechanisms**: Some systems use "dual-stack" where both IPv4 and IPv6 are enabled. In such cases, if IPv6 fails for some reason, the system will fall back to IPv4.

So, yes, the router can be a determining factor in whether you get an IPv4 or IPv6 address.
 

Minty

AWF VIP
Local time
Today, 15:17
Joined
Jul 26, 2013
Messages
10,371
But by definition, you would be far more likely to get an IPv4 ( which is what I need), than the IPv6 address.
Has anyone actually tested the original code to see what they get, rather than relying on the useless ramblings of the AI?

It's very difficult to investigate when I can't make it go wrong.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:17
Joined
Feb 28, 2001
Messages
27,191
The problem as stated above is that it is returning an IPv6 address, on some end users' machines. It works fine for my colleagues and other people in the same organisation. But not all of them.

IF you have a router, you are internally using Network Address Translation, and NAT substitutes local DHCP-assigned addresses for socket targets. You might not have a public IPv4 address if the connection in question is based on an IPv6 connection. Not all IPv6 addresses translate down.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:17
Joined
Oct 29, 2018
Messages
21,474
Has anyone actually tested the original code to see what they get,
Hi. I just tried the code you posted and got an IPv4 address, which I compared against what I got when I browsed to https://whatismyipaddress.com and they matched. However, the website also said my IPv6 is "Not detected." So, I'm not sure if that helps or not. Cheers!
 

sonic8

AWF VIP
Local time
Today, 16:17
Joined
Oct 27, 2015
Messages
998
What happens if you enter nslookup host using powershell or cmd in those machines?
Depending on the network configuration, you'll either get the computers' IP private address on the local network, or no IP at all.
Only if the computers are connected directly to the internet, which is rather unlikely for a client workstation, you will get an IP address usable to configure a remote firewall.
 

Edgar_

Active member
Local time
Today, 09:17
Joined
Jul 8, 2023
Messages
431
Depending on the network configuration, you'll either get the computers' IP private address on the local network, or no IP at all.
Only if the computers are connected directly to the internet, which is rather unlikely for a client workstation, you will get an IP address usable to configure a remote firewall.
I just checked, it's not the public IP of my computers, just something similar. Maybe the IP of the device providing connectivity to my home router, given that it returns the same in different computers here.

I guess OP just has to find a more reliable provider for his IPs, the http request seems alright, although I don't know why he wants to check if the http request object was created, but the more checks in place, the better, sometimes.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:17
Joined
Feb 28, 2001
Messages
27,191
Maybe the IP of the device providing connectivity to my home router, given that it returns the same in different computers here.

From that description, it is probably the ISP's IP address for the server currently servicing your account. Which might be related to the ISP's physical wiring scheme.

For instance, I know that a LOT of people in my neighborhood will have similar addresses because the AT&T connection box is across the street from my house. I watched them lay the fiber cable for that box and watched them when they ran fiber from there to my house (replacing the thin-copper connections.)
 

Minty

AWF VIP
Local time
Today, 15:17
Joined
Jul 26, 2013
Messages
10,371
Sorry been away!

It's the WAN IP address that's required, and what is returned 99.9% of the time by sites like What's My IP.
We use it for home workers, and I suspect it's stopped working due to a configuration change rolled out by a corporate IT.

It has been working for about 2 years, and has suddenly started giving strange results.
 

Users who are viewing this thread

Top Bottom