Login
Username:

Password:

Remember me



Lost Password?

Register now!
Sections
Who's Online
57 user(s) are online (44 user(s) are browsing Forums)

Members: 0
Guests: 57

more...
Support us!
Recent OS4 Files
OS4Depot.net





How to get my local IP address? (using program code)
Home away from home
Joined:
2007/5/19 13:23
From England
Posts: 3541
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 20:26:30
_________________
Author of the PortablE programming language.
I love using Amiga OS4.1
It is pitch black. You are likely to be eaten by a grue...
   Report Go to top

Re: How to get my local IP address?
Not too shy to talk
Joined:
2006/12/1 23:50
From Skærbæk. Denmark
Posts: 266
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.”
   Report Go to top

Re: How to get my local IP address?
Home away from home
Joined:
2007/5/19 13:23
From England
Posts: 3541
@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.
I love using Amiga OS4.1
It is pitch black. You are likely to be eaten by a grue...
   Report Go to top

Re: How to get my local IP address?
Just can't stay away
Joined:
2006/12/4 23:15
Posts: 1999
@ChrisH


You pass apointer to the struct and it will be filled in by the function.

See SDK:Examples/PPP for various usage examples.


_________________
Blender For OS4.x : Blues : Walker Broad
   Report Go to top

Re: How to get my local IP address?
Not too shy to talk
Joined:
2007/1/6 5:59
Posts: 318
Type shownetstatus command to shell window?

   Report Go to top

Re: How to get my local IP address?
Quite a regular
Joined:
2006/11/29 8:09
From Lyon, France
Posts: 954
Yes : c:shownetstatus

_________________
--
AmigaONE X1000 1,8Ghz, 2GB, Sapphire Radeon HD 7700, with Amiga OS 4.1 FE
http://www.amiga-ng.org
   Report Go to top

Re: How to get my local IP address?
Just popping in
Joined:
2006/12/27 21:09
Posts: 53
He meant from own program code

   Report Go to top

Re: How to get my local IP address?
Home away from home
Joined:
2007/5/19 13:23
From England
Posts: 3541
@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 20:59:58
Edited by ChrisH on 2018/6/12 21:02:25
_________________
Author of the PortablE programming language.
I love using Amiga OS4.1
It is pitch black. You are likely to be eaten by a grue...
   Report Go to top

Re: How to get my local IP address?
Just can't stay away
Joined:
2006/12/4 23:15
Posts: 1999
@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.

_________________
Blender For OS4.x : Blues : Walker Broad
   Report Go to top

Re: How to get my local IP address?
Just can't stay away
Joined:
2007/2/23 13:49
From Finland, the land of Santa, sauna, sisu and salmiakki
Posts: 1089
@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 18:53:50
_________________
Rock lobster bit me, so excuse me.
X1000 + AmigaOS 4.1 FE. Be authentic.
"Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
   Report Go to top

Re: How to get my local IP address?
Home away from home
Joined:
2007/5/19 13:23
From England
Posts: 3541
@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.
I love using Amiga OS4.1
It is pitch black. You are likely to be eaten by a grue...
   Report Go to top

Re: How to get my local IP address?
Home away from home
Joined:
2007/5/19 13:23
From England
Posts: 3541
@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 19:09:45
Edited by ChrisH on 2018/6/13 19:10:56
Edited by ChrisH on 2018/6/13 19:13:02
_________________
Author of the PortablE programming language.
I love using Amiga OS4.1
It is pitch black. You are likely to be eaten by a grue...
   Report Go to top

Re: How to get my local IP address?
Just can't stay away
Joined:
2007/2/23 13:49
From Finland, the land of Santa, sauna, sisu and salmiakki
Posts: 1089
@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 excuse me.
X1000 + AmigaOS 4.1 FE. Be authentic.
"Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
   Report Go to top

Re: How to get my local IP address? (using program code)
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 900
The ShowNetStatus source code is available in the Roadshow SDK if it's of any help.

   Report Go to top

Re: How to get my local IP address? (using program code)
Just can't stay away
Joined:
2006/12/9 10:44
From Lyon, France
Posts: 1326
@ChrisH

a TCP/IP module soon to be available?

_________________
Philippe 'Elwood' FERRUCCI
Sam460ex 1.10 Ghz
http://elwoodb.free.fr
   Report Go to top

Re: How to get my local IP address? (using program code)
Home away from home
Joined:
2007/5/19 13:23
From England
Posts: 3541
@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.
I love using Amiga OS4.1
It is pitch black. You are likely to be eaten by a grue...
   Report Go to top

Re: How to get my local IP address? (using program code)
Just popping in
Joined:
2015/9/28 23:42
From Bettendorf, IA, USA
Posts: 139
How do you get the default gateway and DNS's?

_________________
Workbench Explorer - A better way to browse drawers
   Report Go to top

Re: How to get my local IP address? (using program code)
Not too shy to talk
Joined:
2007/2/6 13:57
From Donostia (SPAIN)
Posts: 379
@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 ) );

   Report Go to top

Re: How to get my local IP address? (using program code)
Just popping in
Joined:
2015/9/28 23:42
From Bettendorf, IA, USA
Posts: 139
@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.

   Report Go to top

Re: How to get my local IP address? (using program code)
Just popping in
Joined:
2015/9/28 23:42
From Bettendorf, IA, USA
Posts: 139
Got it. Thanks.

_________________
Workbench Explorer - A better way to browse drawers
   Report Go to top





[Advanced Search]


Powered by XOOPS 2.0 © 2001-2016 The XOOPS Project