Topic options
View mode
Newest First
How to get my local IP address? (using program code)
Posted on:
2018/6/12 0:32
#1
Home away from home
Joined: 2007/5/19 14:23Last Login
: 1/2 17:30
From England
Group:
Registered Users
Can anyone give hints on a simple way to find my Amiga's local IP address? So far I only managed to get 127.0.0.1, which isn't very helpful! Other non-Amiga networking guides suggest getifaddrs() & other things which Roadshow doesn't have :( NOTE: Since it doesn't seem clear I posted this in the Developer forum, I am wanting to achieve this with code.
Edited by ChrisH on 2018/6/12 21:26:30
Author of the
PortablE programming language.
Re: How to get my local IP address?
Posted on:
2018/6/12 1:08
#2
Not too shy to talk
Joined: 2006/12/1 23:50Last Login
: Yesterday 19:11
From Skærbæk. Denmark
Group:
Registered Users
well if you want to get the ip addr from a shell you can use my util rIPConfig but if you want to get it from code you can use BSD Socket library call Queryinterface with IFQ_Address René
“The best thing about a boolean is even if you are wrong, you are only off by a bit.”
Re: How to get my local IP address?
Posted on:
2018/6/12 8:01
#3
Home away from home
Joined: 2007/5/19 14:23Last Login
: 1/2 17:30
From England
Group:
Registered Users
@rwo Thanks! QueryInterfaceTags() seems to be what I need... although it's rather poorly documented, so going to take me some time to work out how I'm supposed to use it. (e.g. "name -- Name of the interface to be queried" What "interface"? And how do I find it's name?!? e.g. "IFQ_Address (struct sockaddr *)" but how do I pass that parameter & get the return value as a tag list - using the same single data value?)
Author of the
PortablE programming language.
Re: How to get my local IP address?
Posted on:
2018/6/12 8:17
#4
Home away from home
Joined: 2006/12/4 23:15Last Login
: 10/14 16:04
Group:
Registered Users
@ChrisH You pass apointer to the struct and it will be filled in by the function. See SDK:Examples/PPP for various usage examples.
Re: How to get my local IP address?
Posted on:
2018/6/12 13:56
#5
Not too shy to talk
Joined: 2007/1/6 5:59Last Login
: 10/26 20:25
Group:
Registered Users
Type shownetstatus command to shell window?
Re: How to get my local IP address?
Posted on:
2018/6/12 19:38
#6
Just can't stay away
Joined: 2006/11/29 8:09Last Login
: Today 18:53
From Lyon, France
Group:
Registered Users
Yes : c:shownetstatus
-- AmigaONE X1000 and Radeon RX 560
Re: How to get my local IP address?
Posted on:
2018/6/12 19:46
#7
Just popping in
Joined: 2006/12/27 21:09Last Login
: 2/5 21:00
Group:
Registered Users
He meant from own program code
Re: How to get my local IP address?
Posted on:
2018/6/12 21:27
#8
Home away from home
Joined: 2007/5/19 14:23Last Login
: 1/2 17:30
From England
Group:
Registered Users
@all It looks like I can get the necessary network interface name using ObtainInterfaceList()... @broadblues Thanks, that will probably save me a few crashes... ...but QueryInterfaceTagList() is returning False when I try to use it, so I think I must still be missing something. Do I need to initialise the struct sockaddr? (But I tried a 'live' sockaddr, without luck.)
Edited by ChrisH on 2018/6/12 21:59:58 Edited by ChrisH on 2018/6/12 22:02:25
Author of the
PortablE programming language.
Re: How to get my local IP address?
Posted on:
2018/6/12 23:37
#9
Home away from home
Joined: 2006/12/4 23:15Last Login
: 10/14 16:04
Group:
Registered Users
@ChrisH
Quote:
but QueryInterfaceTagList() is returning False when I try to use it, so I think I must still be missing something.This bit of teh autodoc is what you missed
Quote:
RESULT success -- 0 for success, -1 for failure; the failure code will be stored in the 'errno' variable.
Re: How to get my local IP address?
Posted on:
2018/6/12 23:39
#10
Just can't stay away
Joined: 2007/2/23 13:49Last Login
: 11/16 0:49
From Finland, the land of Santa, sauna, sisu and salmiakki
Group:
Registered Users
@ChrisH
struct List * netiflist = NULL ;
struct Node * node = NULL ;
struct sockaddr addr ;
int32 res = 0 ;
SocketBase =( struct Library *) IExec -> OpenLibrary ( "bsdsocket.library" , 0 );
if ( SocketBase != NULL )
{
ISocket =( struct SocketIFace *) IExec -> GetInterface ( SocketBase , "main" , 1 , NULL );
if ( ISocket != NULL )
{
netiflist = ISocket -> ObtainInterfaceList ();
if ( netiflist != NULL )
{
node = IExec -> GetHead ( netiflist );
if ( node != NULL )
{
while ( node != NULL )
{
if ( node -> ln_Name != NULL )
{
printf ( "Network interface %s:\\n" , node -> ln_Name );
// QueryInterfaceTags returns 0 for success, -1 for error
res = ISocket -> QueryInterfaceTags ( node -> ln_Name ,
IFQ_Address ,& addr ,
TAG_DONE );
if ( res == 0 )
{
printf ( "IP: %u.%u.%u.%u\\n" ,( uint8 ) addr . sa_data [ 2 ],( uint8 ) addr . sa_data [ 3 ],( uint8 ) addr . sa_data [ 4 ],( uint8 ) addr . sa_data [ 5 ]);
}
else
{ printf ( "- Error: Can't query information (%ld)\\n" , res ); }
}
node = IExec -> GetSucc ( node );
}
}
else
{
printf ( "No network interfaces found\\n" );
}
ISocket -> ReleaseInterfaceList ( netiflist );
}
IExec -> DropInterface (( struct Interface *) ISocket );
}
IExec -> CloseLibrary ( SocketBase );
}
Edit: Added while loop to the code.
Edited by TSK on 2018/6/14 19:53:50
Rock lobster bit me - so I'm here forever X1000 + AmigaOS 4.1 FE "Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
Re: How to get my local IP address?
Posted on:
2018/6/13 19:01
#11
Home away from home
Joined: 2007/5/19 14:23Last Login
: 1/2 17:30
From England
Group:
Registered Users
@broadblues Thanks, that was it :) . I was in a hurry, so didn't see that hidden at the bottom. I'm not sure why anyone would call a return value "success" when True means failure & False means success. IMHO it would make much more sense to call it "error" or "failure"...
Author of the
PortablE programming language.
Re: How to get my local IP address?
Posted on:
2018/6/13 19:05
#12
Home away from home
Joined: 2007/5/19 14:23Last Login
: 1/2 17:30
From England
Group:
Registered Users
@TSK
Your code (probably) answers my next question, which would have been:
Quote:
Is it a good idea to only use the first Network Interface returned by ObtainInterfaceList() ? Although I suppose if you were being really picky, you'd interate through all the Network Interfaces until you got an IP Address. (I'm assuming QueryInterfaceTagList() will fail if the network interface has no IP address - I guess you'd need to test that as well. edit: Seems that ObtainInterfaceList() returns an empty list in that case.)
Edited by ChrisH on 2018/6/13 20:09:45 Edited by ChrisH on 2018/6/13 20:10:56 Edited by ChrisH on 2018/6/13 20:13:02
Author of the
PortablE programming language.
Re: How to get my local IP address?
Posted on:
2018/6/14 19:55
#13
Just can't stay away
Joined: 2007/2/23 13:49Last Login
: 11/16 0:49
From Finland, the land of Santa, sauna, sisu and salmiakki
Group:
Registered Users
@ChrisH I added while loop to the code in the previous post. It's possible to have multiple interfaces actually.
Rock lobster bit me - so I'm here forever X1000 + AmigaOS 4.1 FE "Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/15 13:36
#14
Just can't stay away
Joined: 2009/4/28 4:57Last Login
: Today 13:22
From Adelaide, Australia
Group:
Registered Users
The ShowNetStatus source code is available in the Roadshow SDK if it's of any help.
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/16 11:27
#15
Just can't stay away
Joined: 2006/12/9 10:44Last Login
: 11/11 18:51
From Lyon, France
Group:
Registered Users
@ChrisH
a TCP/IP module soon to be available?
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/17 15:03
#16
Home away from home
Joined: 2007/5/19 14:23Last Login
: 1/2 17:30
From England
Group:
Registered Users
@Elwood
PortablE has had TCP/IP support for years (but only for AmigaOS4)...
Otherwise I wouldn't have been able to write this:
http://cshandley.co.uk/enewsreader/
Author of the
PortablE programming language.
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/25 0:35
#17
Just popping in
Joined: 2015/9/29 0:42Last Login
: 2/25 18:04
From Bettendorf, IA, USA
Group:
Registered Users
How do you get the default gateway and DNS's?
Workbench Explorer - A better way to browse drawers
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/25 12:33
#18
Just can't stay away
Joined: 2007/2/6 13:57Last Login
: 11/16 10:39
From Donostia (SPAIN)
Group:
Registered Users
@mritter0 You can take a look into NetSpeedometer sources (os4depot). /* - Literally copied and pasted from SDK 53.20 ppp_dialer.c with very few minor changes, rather obscure subject not documented at all - I assume the "default gateway" to be unique in the system */ function void GetDefaultGateway(void) { ... } ... if ( ISocket->QueryInterfaceTags( Label_p, /* it can' t be longer than 15 chars */ IFQ_Address, &AddressBuffer, /* IFQ_DestinationAddress, &DestinationAddressBuffer, */ IFQ_NetMask, &NetMaskBuffer, IFQ_PrimaryDNSAddress, &PrimaryDNSAddressBuffer, IFQ_SecondaryDNSAddress, &SecondaryDNSAddressBuffer, IFQ_AddressBindType, &AddressBindType, /* --- */ IFQ_HardwareType, &HardwareType, IFQ_HardwareAddressSize, &HardwareAddressSize, IFQ_HardwareAddress, HardwareAddressText, IFQ_BPS, &BPS, IFQ_State, &State, /* --- */ IFQ_GetBytesIn, &GetBytesIn, IFQ_GetBytesOut, &GetBytesOut, TAG_DONE ) != 0 ) { ... strcpy( PrimaryDNSAddressText, ISocket->Inet_NtoA( PrimaryDNSAddressBuffer.sin_addr.s_addr ) ); strcpy( SecondaryDNSAddressText, ISocket->Inet_NtoA( SecondaryDNSAddressBuffer.sin_addr.s_addr ) );
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/25 18:04
#19
Just popping in
Joined: 2015/9/29 0:42Last Login
: 2/25 18:04
From Bettendorf, IA, USA
Group:
Registered Users
@jabirulo That is what I have been doing, but I always get 0.0.0.0 even though I have 8.8.8.8 and 8.8.4.4 specified in Prefs->Internet->Name resolution. In Routes I have 192.168.1.254 for Default Gateway Address. I always get 192.168.1.255. (Not sure I am looking in correct place for it). shownetstatus gives me the correct values.
Re: How to get my local IP address? (using program code)
Posted on:
2018/6/25 20:14
#20
Just popping in
Joined: 2015/9/29 0:42Last Login
: 2/25 18:04
From Bettendorf, IA, USA
Group:
Registered Users
Got it. Thanks.
Workbench Explorer - A better way to browse drawers
Currently Active Users Viewing This Thread:
2
(
0 members
and 2 Anonymous Users
)