Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

How to get allocated ip number

 [1/10] from: michel:goasampis:free at: 15-Oct-2002 21:55


Hi all, excuse my bad english... I need to manipulate the ip number my PC obtain from the provider when connect to internet. To get it, I hack something with the 'get-modes function which give this result on my linux pc :
>>probe get-modes tcp:// 'interfaces
[ make object! [ name: "lo" addr: 127.0.0.1 netmask: 255.0.0.0 broadcast: none dest-addr: none flags: [loopback] ] make object! [ name: "eth0" addr: 192.168.1.118 netmask: 255.255.255.0 broadcast: 192.168.1.255 dest-addr: none flags: [broadcast multicast] ] make object! [ name: "ppp0" addr: 62.147.73.189 netmask: none broadcast: none dest-addr: 192.168.254.254 flags: [multicast point-to-point] ]] so I do : all-interfaces: get-modes tcp:// 'interfaces internet-interf: all-interfaces/3 my-ip: copy to-string internet-interf/addr but it isn't verry elegant, and does'nt work on all pc (if more or less interfaces are found). Please help me, I hope there is a more efficient way like my-ip: copy some/voodoo/path... Thank-You Michel, newbie but addicted

 [2/10] from: gscottjones:mchsi at: 16-Oct-2002 5:03


Hi, Michel, From: "Goasampis Michel"
> I need to manipulate the ip number my PC obtain from > the provider when connect to internet.
<<quoted lines omitted: 5>>
> name: "lo" > addr: 127.0.0.1
...
> so I do : > > all-interfaces: get-modes tcp:// 'interfaces > internet-interf: all-interfaces/3 > my-ip: copy to-string internet-interf/addr > > but it isn't verry elegant, and does'nt work on all pc (if more or less
interfaces are found).
> Please help me, I hope there is a more efficient way like > my-ip: copy some/voodoo/path...
... I hesitated to answer yesterday, in case someone else had a "whiz bang" suggestion. I do not speak with authority, but I believe that there are several issues here. In addition to having variable numbers of tcp interfaces, depending on the machine, the names given to an individual interface may not be consistent across platforms. If no one "knows" the answer to this question, then a little feedback will quickly resolve it. My platform returns names that look like: name: "lo0" name: "if17474259" The first evidently refers to the local (loopback) ip and the second must refer to my network interface card. I do not know how this number is generated (is it an encoding of the NIC MAC number?). Besides the variability in the number of interfaces and the variability in the naming of the interfaces, I thought your solution was fine because it a) works, and b) it is readily understood. You can avoid one variable intermediate by writing the full path out, like: internet-interf: all-interfaces/3/addr ; yields the desired IP tuple There are many other ways to manipulate the block containing the various IP interfaces. The next example shows how to iterate through all interfaces foreach interface all-interfaces [print interface/addr] If you know that you are looking for a particular class of IP, you could further refine the result similar to the following: foreach interface all-interfaces [ if find interface/name "ppp" [ print interface/addr ] ] will find interfaces that use the ppp protocol (namely, dial-up connections). Hope this helps. Please don't worry about the language issue. I've been speaking English for 43 years, and still haven't figured it out. :-) So please ask further questions, especially if I missed the point. --Scott Jones

 [3/10] from: g:santilli:tiscalinet:it at: 16-Oct-2002 13:28


Hi Scott, On Wednesday, October 16, 2002, 12:03:28 PM, you wrote: GSJ> interface may not be consistent across platforms. If no one "knows" the GSJ> answer to this question, then a little feedback will quickly resolve it. My 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 Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [4/10] from: gscottjones:mchsi at: 16-Oct-2002 6:58


Hi, Gabriele,
> GSJ> interface may not be consistent across platforms. If no one "knows"
the
> GSJ> answer to this question, then a little feedback will quickly resolve
it. My 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
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: lox ifxxxxxxxxx pppx I suspect that RT decided to give a two letter code to each type of tcp interface. lo = local (or loopback) ppp = point to point protocol if = ??? Given this, then my more fully articulated questions are: 1) Is there a place where these codes are documented? 2) Are ther other ones besides what I have listed above? 3) Do these codes differ on different platforms (even for seemingly the same class of tcp interface)? 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. I fear I still not be adequately articulating my question(s), but the question was more expressing curiousity over the schema that RT chose. Thanks, Gabriele, for the nifty trick to see which ip is being used for a given interaction. --Scott Jones

 [5/10] 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"
<<quoted lines omitted: 5>>
> > 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.
<<quoted lines omitted: 3>>
> ifxxxxxxxxx > pppx
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 ifxxxxxxxx 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. -- Holger Kruse [holger--ukiahmail--net]

 [6/10] from: g:santilli:tiscalinet:it at: 16-Oct-2002 15:17


Hi Scott, On Wednesday, October 16, 2002, 1:58:29 PM, you wrote: GSJ> What I am curious about is the two letter code that preceeds the interface GSJ> name, like: GSJ> lox GSJ> ifxxxxxxxxx GSJ> pppx GSJ> I suspect that RT decided to give a two letter code to each type of tcp GSJ> interface. I don't think so. That's the name returned by the OS, I think. (Well, maybe on some platform is REBOL that is giving it a name...) GSJ> If there is solid information somewhere about this coding convention, then GSJ> Michel may be able to develop a generic approach to deciding what ip belongs GSJ> to which tcp interface. Otherwise, I am guessing that your trick documented GSJ> above is the easiest solution. On linux, you get eth0, eth1 etc. for you ethernet adapters, ppp0, etc. for PPP connections, lo0 etc. for local loopback. There could be others, and I assume the name is given by the kernel (i.e. the module handling the interface). GSJ> I fear I still not be adequately articulating my question(s), but the GSJ> question was more expressing curiousity over the schema that RT chose. Dunno if it's really RT. On unix at least it is not; on Windows maybe the name is given by REBOL, or maybe the OS gives them names too; anyone knows? Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [7/10] from: gscottjones:mchsi at: 16-Oct-2002 10:28


Thanks, Holger! Excellent discussion that more than clarifies the answers to the questions that I have had for awhile. --Scott Jones

 [8/10] from: gscottjones:mchsi at: 16-Oct-2002 10:30


From: "Gabriele Santilli" ...
> I don't think so. That's the name returned by the OS, I think. > (Well, maybe on some platform is REBOL that is giving it a > name...)
... Thanks, Gabriele. Your answer foreshadows Holger's response. --Scott Jones

 [9/10] from: michel:goasampis:free at: 17-Oct-2002 22:03


Thanks a lot, all ! I'm surprise and grateful for the time you spend for this question. To "ping" a web server's port (Gabriele's answer) seems to be an efficient solution to get one active ip address among the possible interfaces a pc handles, and is, finally, appropriate to my purpose. Otherwise, the discussion about the different network situations (consequently about the differents interface's names) was verry enlightening, showing the difficulty to develope cross platform networking code. On this subject, if I need to get one working local network ip, do you think the following code is usefull for all platform ? print read foo: rejoin [dns:// read dns://] If it is, I can also surely get either local network or internet ip, and it's fine ! thanks again, Michel, less newbie than yesterday

 [10/10] from: g:santilli:tiscalinet:it at: 17-Oct-2002 20:15


Hi Goasampis, On Thursday, October 17, 2002, 10:03:31 PM, you wrote: GM> print read foo: rejoin [dns:// read dns://] If the hostname is set correctly, this should give you the correct address. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted