Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
153 user(s) are online (133 user(s) are browsing Forums)

Members: 1
Guests: 152

Mr_byte, more...

Support us!

Headlines

 
  Register To Post  

How to get my local IP address? (using program code)
Home away from home
Home away from home


See User information
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.
Go to top
Re: How to get my local IP address?
Not too shy to talk
Not too shy to talk


See User information
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.”
Go to top
Re: How to get my local IP address?
Home away from home
Home away from home


See User information
@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.
Go to top
Re: How to get my local IP address?
Home away from home
Home away from home


See User information
@ChrisH


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

See SDK:Examples/PPP for various usage examples.


Go to top
Re: How to get my local IP address?
Not too shy to talk
Not too shy to talk


See User information
Type shownetstatus command to shell window?

Go to top
Re: How to get my local IP address?
Just can't stay away
Just can't stay away


See User information
Yes : c:shownetstatus

--
AmigaONE X1000 and Radeon RX 560
Go to top
Re: How to get my local IP address?
Just popping in
Just popping in


See User information
He meant from own program code

Go to top
Re: How to get my local IP address?
Home away from home
Home away from home


See User information
@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.
Go to top
Re: How to get my local IP address?
Home away from home
Home away from home


See User information
@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.

Go to top
Re: How to get my local IP address?
Just can't stay away
Just can't stay away


See User information
@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
Go to top
Re: How to get my local IP address?
Home away from home
Home away from home


See User information
@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.
Go to top
Re: How to get my local IP address?
Home away from home
Home away from home


See User information
@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.
Go to top
Re: How to get my local IP address?
Just can't stay away
Just can't stay away


See User information
@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
Go to top
Re: How to get my local IP address? (using program code)
Just can't stay away
Just can't stay away


See User information
The ShowNetStatus source code is available in the Roadshow SDK if it's of any help.

Go to top
Re: How to get my local IP address? (using program code)
Just can't stay away
Just can't stay away


See User information
@ChrisH

a TCP/IP module soon to be available?

Philippe 'Elwood' FERRUCCI
Sam460ex 1.10 Ghz
http://elwoodb.free.fr
Go to top
Re: How to get my local IP address? (using program code)
Home away from home
Home away from home


See User information
@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.
Go to top
Re: How to get my local IP address? (using program code)
Just popping in
Just popping in


See User information
How do you get the default gateway and DNS's?

Workbench Explorer - A better way to browse drawers
Go to top
Re: How to get my local IP address? (using program code)
Just can't stay away
Just can't stay away


See User information
@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 ) );

Go to top
Re: How to get my local IP address? (using program code)
Just popping in
Just popping in


See User information
@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.

Go to top
Re: How to get my local IP address? (using program code)
Just popping in
Just popping in


See User information
Got it. Thanks.

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

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project