How to find a machine's IP address
Dave Higton (1515) 3526 posts |
I used to think it was easy… As far as I can see, I should look to see if Inet$LocalAddr exists. If it does, I can use that. If not, I have to find Inet$EtherIPAddr and resolve it, because it might be numeric or a machine name, so I can’t just parse numbers out of it. And, presumably, if I can’t resolve Inet$EtherIPAddr, it means that the machine isn’t actually on the LAN – something must have failed at startup. Is the above correct? Under what conditions is Inet$EtherIPAddr numeric, and when is it a name? |
Rick Murray (539) 13840 posts |
Given the name, Inet$EtherIPAddr, I’d wonder in what conditions is it not numeric and why? |
John Williams (567) 768 posts |
So here Inet$LocalAddr seems to be a hex number, and it looks vaguely familiar, but I can’t think why. Is the IP address read from CMOS by ReadCMOSIP in Internet.utils, and thus necessarily numeric? Whoops, no, that’s something else altogether!
So what on earth uses that?
Does that depend on how it’s been entered in Network configuration/Interfaces/Configure where a name can be used instead of the IP address? |
John Williams (567) 768 posts |
And thus if it’s not an IP address, look in Hosts to get the corresponding address, because it has to be in there if a name? |
Ronald May (387) 407 posts |
You are right that using an entry in the hosts file allows you tu use the text name which also turns up in Inet$EtherIPAddr |
Dave Higton (1515) 3526 posts |
I’m after anything I can use from BASIC. I really should go through all the combinations of network configuration settings and try them. My BBxM has a static IP address from host name, so Inet$LocalAddr exists (numeric) and Inet$EtherIPAddr exists (the machine’s name, not numeric). Experience supporting JDServer proved that Inet$LocalAddr does not always exist, though the specific case had a numeric Inet$EtherIPAddr. I don’t know if it is ever possible for Inet$LocalAddr to not exist AND Inet$EtherIPAddr to be non-numeric. |
Ronald May (387) 407 posts |
I’m after anything I can use from BASIC. I noticed that using *ifping or *gethost makes BASIC jump back to the CLI so you would either have to make the tempfile in the application obey file, or make an obey file with those commands and run that from BASIC. |
John Sandgrounder (1650) 574 posts |
With all of the combinations I have tried * on Raspberry Pi RC15 with USB networking, Inet$LocalAddr should exist with a valid address. If it does not, then the networking is going to fail anyway as the machine has not been set up properly. Inet$LocalAddr needs thinking about how to parse it. But, other than that, it is very easy to access from Basic
|
Chris Hall (132) 3554 posts |
If networking is not set up, you can still obtain the machine’s MAC address (except on Beagleboard, Pandaboard and IGEPv5 which do not have a MAC address, just a locally administered address). Even a model A Pi has a MAC address even though it has no network adapter. |
Sprow (202) 1158 posts |
Of course what nobody’s spotted yet is that the reason the question is hard to answer is because the question is nonsense. A machine doesn’t have an IP address, it’s an interface that does. A machine can have more than one (physical) interface, and some drivers allow you to have virtual (software) interfaces, and would hence have more than one IP address too. You can ask what the IP address of an interface is by opening a datagram socket (SOCK_DGRAM) then using an ioctl to request SIOCGIFADDR and SIOCGIFNETMASK. Both ifconfig and InetSetup’s status dialogue do this (in C), converting to BASIC and calling the appropriate Socket_ SWIs is certainly possible, but left as an exercise for masochists. |
Steve Pampling (1551) 8170 posts |
While it’s true that the interface has assigned IP’s1 the question Dave asked was “Under what conditions is Inet$EtherIPAddr numeric, and when is it a name?” to which the answer is Inet$EtherIPAddr numeric but the name can be resolved to the value stored in Inet$EtherIPAddr. However I thought that answer had pretty much been covered by John Williams with the list including his personal values for Inet$EtherIPAddr and Inet$HostName. 1A NIC could have a configuration with 192.168.112.13 and 224.0.23.63 simultantaneously. Specific combination quoted from a bit of “multicast network fault” which I spent 82 hours investigating and finally established that my first impression of it being a host system fault was actually totally correct. 2 1700 to 0100 |
Rick Murray (539) 13840 posts |
True – but it isn’t nonsense. Most people will be used to a “device” having an “address”. My router is 192.168.1.1, the Pi is 192.168.1.10 (via 192.168.1.19 (Vonets)). The IPCam
And what would
Uhhh… yeah… I’m still recovering from the beating-head-on-desk that got me to |
Steve Pampling (1551) 8170 posts |
OMFG. Resolver. You want broken? look no further. If you check the code of AntiSpam you will find it using it’s own facilities to do the work that Resolver can’t do correctly when AntiSpam is utilising DNS blacklist servers. Some years ago I actually ported some code to be able to run a DNS Blacklist lookup on RO (lord knows where that is, possibly on one of the RPC’s) IIRC it was GPL so mindbleach may have been needed. |
Rick Murray (539) 13840 posts |
Calm down, calm down. It was just an example of the joy of dealing with network stuff in BASIC. In another thread they’re discussing unicorns and Romans, and you and I both know that such things were alive and kicking when the RISC OS network stack was created. In fact, I would hazard a guess that the Resolver of which we speak predates the Dodo. Maybe also the evolutionary development of the Tardigrade? I dunno. There’s not so much cobwebs as trap doors and giant rolling stones… |
Steve Pampling (1551) 8170 posts |
Theoretical chill out weekend following the Tuesday 0900 – 2000 and then that 1700-0100 added on the end of the 0900-1700 on Wednesday.
Word association – DW, next regen, reveal on Sunday. |
Alan Adams (2486) 1149 posts |
Going right back to the OP: Inet$LocalAddr String 201a8c0 0201a8c0 is 2.1.168.192 in hex, i.e the 32-bit representation of the address, little-endian. |
Dave Higton (1515) 3526 posts |
Yes, it’s a string, but that string represents a hex number as you say, which is why I (rather loosely) said it’s a number. But I have evidence from another user that Inet$LocalAddr doesn’t always exist, so my question is about how to find the machine’s address (almost all machines we’re dealing with have only one interface and therefore only one address) when Inet$LocalAddr doesn’t exist. |
Colin (478) 2433 posts |
My pandaboard has 2 local addresses Inet$LocalAddr and Inet$LocalAddr1. I would open a socket and use ioctl as sprow suggested. |