I am developing an application in which I need to know my ip address
irespective of my internet connection(PPPoE, LAN, etc).
Somebody told me that I can query a web server over internet to know
my address. How can I do thins in C ????.
|
|
0
|
|
|
|
Reply
|
lipun4u (226)
|
10/12/2008 6:02:32 PM |
|
asit <lipun4u@gmail.com> wrote:
> I am developing an application in which I need to know my ip address
> irespective of my internet connection(PPPoE, LAN, etc).
>
> Somebody told me that I can query a web server over internet to know
> my address. How can I do thins in C ????.
First of all, you may have multiple IP addresses.
The sanest way to do find your local IP address(es), IMO, is to use
getifaddrs(). getifaddrs() is widely found on unix-like systems, though not
part of an official standard. `grep -r /usr/include getifaddrs' for the
interface, or google for the AIX man page.
As for the method recommended to you, in short you would create a socket,
connect to an address, then use getsockname(2) to acquire the local address
used for the connection. That, ostensibly, would be your IP address at the
time you called connect(2).
|
|
0
|
|
|
|
Reply
|
William
|
10/12/2008 6:31:56 PM
|
|
In article <80c76669-fb65-4694-ac22-be55160484fa@f37g2000pri.googlegroups.com>,
asit <lipun4u@gmail.com> writes:
> I am developing an application in which I need to know my ip address
> irespective of my internet connection(PPPoE, LAN, etc).
Why do you want to know? Not really enough info to answer question.
Generally, a system has multiple IP addressesi (usually at least 2)
and IP addresses can be added/removed/change at any time.
> Somebody told me that I can query a web server over internet to know
> my address. How can I do thins in C ????.
You probably don't want to do that, but as I said, more info needed.
--
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]
|
|
0
|
|
|
|
Reply
|
andrew
|
10/12/2008 6:33:22 PM
|
|
On Oct 12, 11:02=A0am, asit <lipu...@gmail.com> wrote:
> I am developing an application in which I need to know my ip address
> irespective of my internet connection(PPPoE, LAN, etc).
>
> Somebody told me that I can query a web server over internet to know
> my address. How can I do thins in C ????.
Your IP address is 127.0.0.1.
If you want a useful answer, you need to explain your problem. All
you've explained is how you plan to solve it. We can't tell whether
your solution is right or wrong or how to fix it without knowing the
problem.
Why do you think you need to know your IP address?
DS
|
|
0
|
|
|
|
Reply
|
David
|
10/13/2008 4:09:30 AM
|
|
>I am developing an application in which I need to know my ip address
>irespective of my internet connection(PPPoE, LAN, etc).
You get one IP address per interface (minimum), and net-capable
systems that aren't connected to any network still likely have
"127.0.0.1" as an IP address. However, I doubt that's the answer
you want.
Running "ifconfig -a" might give you the answer you want mixed in
with some answers you don't. Or it might fail completely when that
command doesn't exist on your system or you don't have permission
to run it.
Talking about "my ip address" is a lot like talking about "Pressing
THE key with THE finger on THE keyboard", as if there is only one
finger.
>Somebody told me that I can query a web server over internet to know
>my address.
Not if you don't have Internet connectivity. Which web server?
>How can I do thins in C ????.
If you've got a network connection set up, getsockname() on the
socket on your end of the connection gets you the address of your
end of the connection.
|
|
0
|
|
|
|
Reply
|
gordonb.0gpy3 (1)
|
10/13/2008 5:25:03 AM
|
|
asit <lipu...@gmail.com> wrote:
> I am developing an application in which I need to know my ip address
> irespective of my internet connection(PPPoE, LAN, etc).
>
> Somebody told me that I can query a web server over internet to know
> my address. How can I do thins in C ????.
If I understand right, what you want is not you own actual IP but the
IP everybody sees from the internet. For example my IP is 10.0.0.31
and my gateway is 10.0.0.2 . But if I enter http://whatismyip.com/ the
page says: "Your IP Address Is 190.138.107.47" which is my public IP.
Is this what you want to find out??
|
|
0
|
|
|
|
Reply
|
danmath06 (17)
|
10/15/2008 4:17:50 AM
|
|
On Oct 15, 9:17 am, danmat...@gmail.com wrote:
> asit <lipu...@gmail.com> wrote:
> > I am developing an application in which I need to know my ip address
> > irespective of my internet connection(PPPoE, LAN, etc).
>
> > Somebody told me that I can query a web server over internet to know
> > my address. How can I do thins in C ????.
>
> If I understand right, what you want is not you own actual IP but the
> IP everybody sees from the internet. For example my IP is 10.0.0.31
> and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
> page says: "Your IP Address Is 190.138.107.47" which is my public IP.
> Is this what you want to find out??
Yes...
|
|
0
|
|
|
|
Reply
|
lipun4u (226)
|
10/16/2008 2:11:01 PM
|
|
On Oct 16, 11:11 am, asit <lipu...@gmail.com> wrote:
> On Oct 15, 9:17 am, danmat...@gmail.com wrote:
>
> > asit <lipu...@gmail.com> wrote:
> > > I am developing an application in which I need to know my ip address
> > > irespective of my internet connection(PPPoE, LAN, etc).
>
> > > Somebody told me that I can query a web server over internet to know
> > > my address. How can I do thins in C ????.
>
> > If I understand right, what you want is not you own actual IP but the
> > IP everybody sees from the internet. For example my IP is 10.0.0.31
> > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
> > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
> > Is this what you want to find out??
>
> Yes...
You can parse the output of:
lynx -dump www.whatismyip.com | grep 'Your IP'
or
lynx -dump checkip.dyndns.org
But this will stopt working if www.whatismyip.com or
checkip.dyndns.org disappear or change.
See http://www.go2linux.org/what-is-my-public-ip-address-with-linux
for some other ideas.
I don't know if the socket api can give you a better solution, but I
think http://checkip.dyndns.org/ is quite safe.
|
|
0
|
|
|
|
Reply
|
danmath06 (17)
|
10/16/2008 6:03:51 PM
|
|
On 16 oct, 12:11, asit <lipu...@gmail.com> wrote:
> On Oct 15, 9:17 am, danmat...@gmail.com wrote:
>
> > asit <lipu...@gmail.com> wrote:
> > > I am developing an application in which I need to know my ip address
> > > irespective of my internet connection(PPPoE, LAN, etc).
>
> > > Somebody told me that I can query a web server over internet to know
> > > my address. How can I do thins in C ????.
>
> > If I understand right, what you want is not you own actual IP but the
> > IP everybody sees from the internet. For example my IP is 10.0.0.31
> > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
> > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
> > Is this what you want to find out??
>
> Yes...
Try this, get_public_ip takes an array of at least 16 chars and
returns a string with your public ip.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#define REC_SIZE 1000
int get_public_ip ( char * ip ){
int sock;
int ret, position= 0;
struct hostent *host;
struct sockaddr_in server_addr;
char received[REC_SIZE], *curr_ip;
/* Connect to HTTP Server at checkip.dyndns.org */
host = gethostbyname("checkip.dyndns.org");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) return -1;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if ( connect (sock, (struct sockaddr *)&server_addr, sizeof(struct
sockaddr)) == -1) return - 1;
/* Request Main Page */
char request[]= "GET / HTTP/1.1\r\n\r\n";
ret= send (sock, request, sizeof (request), 0);
if (ret != sizeof (request)) return -1;
/* Receive Page */
while ( (ret= recv (sock, received + position , REC_SIZE - position,
0)) > 0)
position+= ret;
if ( ret != 0) return -1;
received[REC_SIZE -1]= '\0';
close (sock);
/* Search for "Current IP Address:" */
curr_ip= strstr (received, "Current IP Address:");
if (curr_ip == NULL) return -1;
memset (ip, 0, 16);
ret= sscanf (curr_ip, "%*[^0-9]%15[0-9.]", ip);
ip[15]='\0';
if ( ret != 1) return -1;
return 0;
};
int main () {
char ip[20];
if (get_public_ip (ip)){
puts ("Could not get current Public IP");
} else {
printf ("Public IP is: %s\n", ip);
}
return 0;
}
|
|
0
|
|
|
|
Reply
|
danmath06 (17)
|
10/16/2008 9:12:32 PM
|
|
On Oct 16, 7:11=A0am, asit <lipu...@gmail.com> wrote:
> > If I understand right, what you want is not you own actual IP but the
> > IP everybody sees from the internet. For example my IP is 10.0.0.31
> > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
> > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
> > Is this what you want to find out??
>
> Yes...
Presumably, you are going to tell the IP to someone. So why not just
connect to them and say "my IP is whatever IP you see this connection
coming from". This ensure that you give them the correct answer for
them to reach you.
DS
|
|
0
|
|
|
|
Reply
|
David
|
10/17/2008 1:16:24 AM
|
|
In article <d40a6f7d-cddf-4656-853d-52073d3094e3@l42g2000hsc.googlegroups.com>,
<danmath06@gmail.com> wrote:
....
>lynx -dump checkip.dyndns.org
>
>But this will stopt working if www.whatismyip.com or
>checkip.dyndns.org disappear or change.
Right. That's the crux of it. There doesn't seem to be anyway to do it
that doesn't depend on knowing the name of something external.
>See http://www.go2linux.org/what-is-my-public-ip-address-with-linux
>for some other ideas.
Interesting read, but all of them are just variations on the theme of
checking something like whatismyip or dyndns.
>I don't know if the socket api can give you a better solution, but I
>think http://checkip.dyndns.org/ is quite safe.
Absolutely. Unless/until they change it.
Of course it then follows that the only thoroughly reliable ways to do
this is to do it yourself - i.e., have some machine of your own be on
the public net. One of the posters in the go2linux page had that
situation - where he could get what he needed directly from ifconfig,
because his machine was actually on the public net.
P.S. Another way to go about this is to try to query the information
from your own router - it obviously knows the external IP address. The
trick is figuring out a good automated way to get it to divulge the
information.
|
|
0
|
|
|
|
Reply
|
gazelle3 (1598)
|
10/17/2008 1:56:59 PM
|
|
In article <076440ba-6bbf-4639-9009-4c42512e9275@y71g2000hsa.googlegroups.com>,
David Schwartz <davids@webmaster.com> wrote:
>On Oct 16, 7:11�am, asit <lipu...@gmail.com> wrote:
>
>> > If I understand right, what you want is not you own actual IP but the
>> > IP everybody sees from the internet. For example my IP is 10.0.0.31
>> > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
>> > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
>> > Is this what you want to find out??
>>
>> Yes...
>
>Presumably, you are going to tell the IP to someone. So why not just
>connect to them and say "my IP is whatever IP you see this connection
>coming from". This ensure that you give them the correct answer for
>them to reach you.
>
>DS
There are a lot of practical ways that that can fail.
|
|
0
|
|
|
|
Reply
|
gazelle3 (1598)
|
10/17/2008 1:58:21 PM
|
|
Kenny McCormack wrote:
> In article <076440ba-6bbf-4639-9009-4c42512e9275@y71g2000hsa.googlegroups.com>,
> David Schwartz <davids@webmaster.com> wrote:
>> On Oct 16, 7:11 am, asit <lipu...@gmail.com> wrote:
>>
>>>> If I understand right, what you want is not you own actual IP but the
>>>> IP everybody sees from the internet. For example my IP is 10.0.0.31
>>>> and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
>>>> page says: "Your IP Address Is 190.138.107.47" which is my public IP.
>>>> Is this what you want to find out??
>>> Yes...
>> Presumably, you are going to tell the IP to someone. So why not just
>> connect to them and say "my IP is whatever IP you see this connection
>> coming from". This ensure that you give them the correct answer for
>> them to reach you.
>>
>
> There are a lot of practical ways that that can fail.
>
Such as?
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9880)
|
10/17/2008 7:04:03 PM
|
|
gazelle@shell.xmission.com (Kenny McCormack) writes:
> In article <d40a6f7d-cddf-4656-853d-52073d3094e3@l42g2000hsc.googlegroups.com>,
> <danmath06@gmail.com> wrote:
> ...
>>lynx -dump checkip.dyndns.org
>>
>>But this will stopt working if www.whatismyip.com or
>>checkip.dyndns.org disappear or change.
>
> Right. That's the crux of it. There doesn't seem to be anyway to do it
> that doesn't depend on knowing the name of something external.
Even if a reliable external source were available, access to it could
be blocked or, worse, intercepted and the result altered.
> Of course it then follows that the only thoroughly reliable ways to do
> this is to do it yourself - i.e., have some machine of your own be on
> the public net. One of the posters in the go2linux page had that
> situation - where he could get what he needed directly from ifconfig,
> because his machine was actually on the public net.
>
> P.S. Another way to go about this is to try to query the information
> from your own router - it obviously knows the external IP address. The
> trick is figuring out a good automated way to get it to divulge the
> information.
That would still fail if there are two (or more) levels of NAT.
If you *really* must know a public IP address corresponding to the
machine, you should probably be on the public Internet in the first
place, and not behind a NAT gateway.
--
M�ns Rullg�rd
mans@mansr.com
|
|
0
|
|
|
|
Reply
|
mans (443)
|
10/17/2008 7:05:18 PM
|
|
M�ns Rullg�rd wrote:
> gazelle@shell.xmission.com (Kenny McCormack) writes:
>>
>> P.S. Another way to go about this is to try to query the information
>> from your own router - it obviously knows the external IP address. The
>> trick is figuring out a good automated way to get it to divulge the
>> information.
>
> That would still fail if there are two (or more) levels of NAT.
>
> If you *really* must know a public IP address corresponding to the
> machine, you should probably be on the public Internet in the first
> place, and not behind a NAT gateway.
>
Or use a fixed IP.
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9880)
|
10/17/2008 7:15:56 PM
|
|
On Oct 18, 12:15 am, Ian Collins <ian-n...@hotmail.com> wrote:
> M=E5ns Rullg=E5rd wrote:
> > gaze...@shell.xmission.com (Kenny McCormack) writes:
>
> >> P.S. Another way to go about this is to try to query the information
> >> from your own router - it obviously knows the external IP address. Th=
e
> >> trick is figuring out a good automated way to get it to divulge the
> >> information.
>
> > That would still fail if there are two (or more) levels of NAT.
>
> > If you *really* must know a public IP address corresponding to the
> > machine, you should probably be on the public Internet in the first
> > place, and not behind a NAT gateway.
>
> Or use a fixed IP.
>
> --
> Ian Collins
Thanx everyone...finally my problem is solved...
|
|
0
|
|
|
|
Reply
|
lipun4u (226)
|
10/17/2008 9:42:45 PM
|
|
On Oct 17, 6:58=A0am, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> >Presumably, you are going to tell the IP to someone. So why not just
> >connect to them and say "my IP is whatever IP you see this connection
> >coming from". This ensure that you give them the correct answer for
> >them to reach you.
> There are a lot of practical ways that that can fail.
Actually, it's precisely because there is no way that can fail that
you should use it. Note that they still may not be able to reach you,
but it won't be because they don't have the right address.
DS
|
|
0
|
|
|
|
Reply
|
David
|
10/18/2008 1:12:01 AM
|
|
In article <7bc2a167-8af8-425e-835d-cd6c9460ff2a@w1g2000prk.googlegroups.com>,
asit <lipun4u@gmail.com> writes:
> On Oct 15, 9:17 am, danmat...@gmail.com wrote:
>> asit <lipu...@gmail.com> wrote:
>> > I am developing an application in which I need to know my ip address
>> > irespective of my internet connection(PPPoE, LAN, etc).
>>
>> > Somebody told me that I can query a web server over internet to know
>> > my address. How can I do thins in C ????.
>>
>> If I understand right, what you want is not you own actual IP but the
>> IP everybody sees from the internet. For example my IP is 10.0.0.31
>> and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
>> page says: "Your IP Address Is 190.138.107.47" which is my public IP.
>> Is this what you want to find out??
>
> Yes...
There's a protocol to do this called STUN - Simple Traversal
of User Datagram Protocol (UDP) Through Network Address
Translators (NATs), RFC 3489.
It's most commonly used to tunnel VoIP out through NAT,
but it includes provision to get your external IP address,
as protocols such as SIP need to include this in the payload.
You need to talk to an external STUN server -- there are
several public ones. A simple request and retrieving the
MAPPED-ADDRESS from the response will give you what you
want. This is the first part of the STUN discovery process to
determine if you are behind a NAT, and if so what type of NAT.
There's an opensource implementation of client and server
http://sourceforge.net/projects/stun/
--
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]
|
|
0
|
|
|
|
Reply
|
andrew
|
10/18/2008 1:22:06 PM
|
|
|
17 Replies
31 Views
(page loaded in 0.655 seconds)
|