Recent

Author Topic: Get public IP adress  (Read 1502 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • 'The knowledge is the key.'
    • FPCProjectShare
Get public IP adress
« on: April 13, 2020, 08:51:42 pm »
Hello!  (Sorry if I didn't found it, but answerd in other topic %))

How can I get and use public IP?
(I can find it with browser but i want to use in a program)
And
I heard that the TCP/IP connection working with public IP adress without any help.
Really?

Thanks for answers!
Check my ProjectShare site and share your content: FPCShare

GetMem

  • Hero Member
  • *****
  • Posts: 3782
Re: Get public IP adress
« Reply #1 on: April 13, 2020, 08:59:04 pm »
@Jake012345
Quote
How can I get and use public IP?
Try this:
Code: Pascal  [Select][+][-]
  1. uses fphttpclient;
  2.  
  3. function GetURL(URL: String; var URLRes: String): Boolean;
  4. var
  5.   SS: TStringStream;
  6.   FPHTTPClient: TFPHTTPClient;
  7. begin
  8.   Result := False;
  9.   FPHTTPClient := TFPHTTPClient.Create(nil);
  10.   try
  11.     FPHTTPClient.AllowRedirect := True;
  12.     SS := TStringStream.Create('');
  13.     try
  14.       Screen.Cursor := crHourglass;
  15.       try
  16.         FPHTTPClient.Get(URL, SS);
  17.       finally
  18.         Screen.Cursor := crDefault;
  19.       end;
  20.       if FPHTTPClient.ResponseStatusCode = 200 then
  21.       begin
  22.         URLRes := SS.DataString;
  23.         Result := True;
  24.       end;
  25.     finally
  26.       SS.Free;
  27.     end;
  28.   finally
  29.     FPHTTPClient.Free;
  30.   end;
  31. end;
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. var
  35.   Str: String;
  36. begin
  37.   if GetURL('http://bot.whatismyipaddress.com/', Str) then
  38.     ShowMessage('My public IP is: ' + Str);
  39. end;

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Get public IP adress
« Reply #2 on: April 14, 2020, 11:41:11 am »
Thank You!
Check my ProjectShare site and share your content: FPCShare

Thaddy

  • Hero Member
  • *****
  • Posts: 11924
Re: Get public IP adress
« Reply #3 on: April 14, 2020, 12:03:05 pm »
or ip.thaddy.com which is much older....
And more reliable...
Thing is you need a real connection to show what your real external IP is.
I also host a different one that is slightly nore advanced. (resolves proxies)
« Last Edit: April 14, 2020, 12:09:34 pm by Thaddy »
Black themes should be banned.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1144
    • Lebeau Software
Re: Get public IP adress
« Reply #4 on: April 15, 2020, 12:05:53 am »
Typically, querying a public website for the IP that connects to it works fine, and does report YOUR IP that is connected to the external world.  This is the approach I usually recommend, same as other users.

However, it has recently come to my attention that some ISPs hide their users behind a NAT on the ISP's side, in which case such sites will see the ISP's IP, not the user's IP.  So, to get your own external IP (ie, the IP that is connected to the ISP) in that situation, you would have to query your modem/router directly instead, such as with uPNP.
« Last Edit: April 15, 2020, 12:09:38 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1988
  • Former Delphi 1-7, 10.2 user
Re: Get public IP adress
« Reply #5 on: April 15, 2020, 07:54:29 am »
However, it has recently come to my attention that some ISPs hide their users behind a NAT on the ISP's side, in which case such sites will see the ISP's IP, not the user's IP.  So, to get your own external IP (ie, the IP that is connected to the ISP) in that situation, you would have to query your modem/router directly instead, such as with uPNP.

With the current scarcity of IPv4, many ISPs (in Australia) are now using CG-NAT. However, querying the user's modem/router in this situation will not provide an external/public IP, but rather the private IP used within the ISP's network and which is useless outside of the ISP's network.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1144
    • Lebeau Software
Re: Get public IP adress
« Reply #6 on: April 15, 2020, 07:31:09 pm »
However, querying the user's modem/router in this situation will not provide an external/public IP, but rather the private IP used within the ISP's network

True.  But it will still be the external IP from the perspective of the user's PC.

and which is useless outside of the ISP's network.

That depends on why you are querying the IP in the first place.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 11924
Re: Get public IP adress
« Reply #7 on: April 15, 2020, 07:55:43 pm »
That depends on why you are querying the IP in the first place.
This is usually very common as a - small - part of user identification, e.g. in banking, where users need to register their devices.
It can be an indicator of proper use or mis-use. If multiple indicators fail, any transaction will be invalid.

Note it is not as relevant as it used to be, since multiple factor authentication is now common practice. But it is still such a factor....
« Last Edit: April 15, 2020, 08:03:15 pm by Thaddy »
Black themes should be banned.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1144
    • Lebeau Software
Re: Get public IP adress
« Reply #8 on: April 16, 2020, 04:32:28 am »
This is usually very common as a - small - part of user identification, e.g. in banking, where users need to register their devices.
It can be an indicator of proper use or mis-use. If multiple indicators fail, any transaction will be invalid.

The only time I've ever needed to query my local LAN's external IP is when using protocols, like FTP or custom, that accept incoming connections from the outside world and need to give the IP to an outside party so they know where to connect to.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

blastiko

  • Newbie
  • Posts: 1
Re: Get public IP adress
« Reply #9 on: August 15, 2022, 11:42:29 pm »
Under Linux, or in any case with curl installed:
Code: Pascal  [Select][+][-]
  1. function getPublicIP() : string;
  2.         var
  3.             proc: TProcess;
  4.             outp : TStringList;
  5.             ip : string;
  6.         begin
  7.                 proc := TProcess.create(nil);
  8.                 proc.Executable := '/usr/bin/curl';
  9.                 proc.Parameters.Add('ifconfig.me');
  10.                 proc.Options := proc.Options + [poWaitOnExit, poUsePipes];
  11.                 proc.Execute;
  12.                 outp := TStringList.Create;
  13.                 outp.LoadFromStream(proc.Output);
  14.                 if (outp.Count > 0) then begin
  15.                         ip := outp[0];
  16.                         if (not IsIp(ip)) then ip := '---';
  17.                 end     else
  18.                 ip := '---';
  19.  
  20.             outp.Free;
  21.                 proc.Free;
  22.  
  23.                 Result := ip;
  24.         end;
  25.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 5174
Re: Get public IP adress
« Reply #10 on: August 16, 2022, 09:11:19 am »
Under Linux, or in any case with curl installed:

Noting that that's your first post, welcome to the forum... but commenting to a mature thread tends to be frowned upon :-)

There's actually much better ways than using curl, based on looking in /proc... if using Linux, which OP didn't specifiy. But unfortunately this is a bit of a trick question, since even a computer with a single interface can have multiple IP addresses (and both v4 and v6 addresses) associated with it.

The right question is probably "if I connect to such-and-such a remote address, what local address will I be using".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

rvk

  • Hero Member
  • *****
  • Posts: 4948
Re: Get public IP adress
« Reply #11 on: August 16, 2022, 10:31:16 am »
But unfortunately this is a bit of a trick question, since even a computer with a single interface can have multiple IP addresses (and both v4 and v6 addresses) associated with it.
Not sure you understood the answer. It uses curl to query the site http://ifconfig.me.
That will give you just one external public IP address on Linux just like the other answers.

(Although I would rather suggest some http component already installed in FPC like TFPHTTPClient which also works under Linux.)



MarkMLl

  • Hero Member
  • *****
  • Posts: 5174
Re: Get public IP adress
« Reply #12 on: August 16, 2022, 10:51:47 am »
Not sure you understood the answer. It uses curl to query the site http://ifconfig.me.
That will give you just one external public IP address on Linux just like the other answers.

Yes, but if the NIC is multi-homed using different subnets for different jobs?

OP might actually have been thinking about the address when he talked to a local database: how's querying an external website going to help with that? How's it going to help if his network routing uses different paths depending on the protocol, e.g. one path to an SMTP smarthost (which is the one he wants to know about) and another for everything else? How's it going to help if he goes via a NATing router for external connections but wants to know the host's /actual/ address?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

rvk

  • Hero Member
  • *****
  • Posts: 4948
Re: Get public IP adress
« Reply #13 on: August 16, 2022, 10:57:16 am »
OP might actually have been thinking about the address when he talked to a local database: how's querying an external website going to help with that?
OP already stated he can find the public IP with a browser
(I can find it with browser but i want to use in a program)
and thanked for the first answer. So I guess the issue was already resolved and it was indeed the actual external IP address of his internet connection what he was after.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5174
Re: Get public IP adress
« Reply #14 on: August 16, 2022, 11:20:57 am »
and thanked for the first answer. So I guess the issue was already resolved and it was indeed the actual external IP address of his internet connection what he was after.

...so in this case the NATed one rather than the machine's apparent IP address. I've got a vague recollection of looking at that in the past for security reasons: I think there's an RFC (possibly just a draft) that discussed Carrier Grade NAT but I can't recall whether there's any standalone way of probing it other than using something akin to traceroute.

Looking back through some of my notes: I've certainly seen NATing mess up /apparent/ IP address allocation on e.g. IP going over a 4G connection, with the reported IP address being nothing like anything that appeared in a traceroute sequence. They've also reminded me that historically port 113 (ident) was significant, with either some sort of useful identifier reported or at the very least with an attempted external connection promptly refused rather than being tarpitted or allowed to time out.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018