[REBOL] Re: How to get allocated ip number
From: holger::rebol::net at: 16-Oct-2002 8:04
On Wed, Oct 16, 2002 at 06:58:29AM -0500, G. Scott Jones wrote:
> Hi, Gabriele,
> From: "Gabriele Santilli"
> > I don't know if I understood the question correctly, but it is
> > possible to discover the address you are using when you connect to
> > something with:
> > port: open tcp://www.rebol.com:80
> > print port/local-ip
> > close port
Yes, that's basically correct.
First of all, the question "to discover THE address you are using"
is inaccurate, because hosts can have more than one address.
There is obviously the loopback address 127.0.0.1, plus usually at
least one other IP address, sometimes more than one, e.g. if your
machine is connected to a local network, or if you have more than
one simultaneous Internet connection.
Since the question is not unique, you cannot expect the answer to
be unique :-). REBOL tells you all IP addresses in use on your
system, just like OS calls or shell calls on your machine do. It is
up to you to figure out which IP address is relevant, based on what
you want to do.
If you want to know which IP address would be used locally if you
connect "to the Internet" then the easiest way is to actually connect
and look at your local IP, as Gabriele described. This is what you
should do if you want to implement protocols that require you to send
IP addresses across the connection (IRC DCC, FTP active mode etc.), or
which require multiple sockets with identical local IPs.
It is dangerous to assume that the IP address used on your end of
a connection is always your ISP-assigned IP addresses, because this
does not hold for locally forwarded connections (e.g. if your router
contains a transparent http proxy cache) or for connections through
VPNs. Actually connecting and then checking is the best way.
If you want to know the IP address of a particular interface (e.g.
for admin or logging purposes), then simply look at the interface list
and pick the interface you need, based on your knowledge of the topology
of your network.
> My ill-formed question concerned a slightly different area; although, I
> suspect that your answer may also be helpful to Michel.
> What I am curious about is the two letter code that preceeds the interface
> name, like:
In most cases these names are generated by the OS. For instance Unix has a
naming convention of "name followed by a number" for all interfaces (with
the exception of Linux often using lo instead of lo0 for loopback).
In the case where the OS provides such a name REBOL will use that.
Some operating systems do not provide a name, but some kind of number or
id. In that case REBOL generates a name: "lox" for loopback, and
for all other interfaces ("if" standing for "interface", because
there is no way for REBOL to know the actual type of interface in this case).
> If there is solid information somewhere about this coding convention, then
> Michel may be able to develop a generic approach to deciding what ip belongs
> to which tcp interface. Otherwise, I am guessing that your trick documented
> above is the easiest solution.
Unfortunately there is very little standardization, except for lo/lo0 being
loopback. PPP is usually pppx, except for platforms which do not have
interface names, but PPP can also be, e.g., tunx (for tunnel) depending on
config. Ethernet can be pretty much anything, a generic "ethx" (Linux,
Amiga etc.) or a prefix determined by the type of hardware used on the NIC,
e.g. on most BSDs "nex" for NE2000.
Your Internet connection therefore can have pretty much any name, except for
lo/lo0. On platforms that do not generate actual names the name is completely
unpredictable. For Unix/Amiga etc. the name is usually pppx for dialup and
for some types of dsl/cable connections (pppoe). If you are connected through
Ethernet to a cable/dsl modem or router and your ISP does not use pppoe (or
the router terminates pppoe internally) then the name can be anything though,
on Linux often ethx.