Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get local ip address (not using ifconfig) ???

1 view
Skip to first unread message

Sagaert Johan

unread,
Sep 3, 2005, 5:05:41 PM9/3/05
to
Hi

how to get the local ip ?

i don't want to call the ifconfig and the parse the result.

what API's are available ?


Michael Rasmussen

unread,
Sep 3, 2005, 6:20:16 PM9/3/05
to

Do you mean that you want to write a C-program?
If so study the Berkley-socket.
--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/pks/lookup?op=get&search=0xE3E80917

mst

unread,
Sep 3, 2005, 6:38:09 PM9/3/05
to


Start with: man gethostbyname

--
remove MYSHOES to email

Floyd L. Davidson

unread,
Sep 3, 2005, 10:55:49 PM9/3/05
to
"Sagaert Johan" <sagaert.j AT belgacom.net> wrote:
>Hi
>
>how to get the local ip ?

You will have to define "local ip" first. Any given host might
have 20 "local ip" addresses.

Generally you either know the hostname, and want the IP address
associated with that particular name, or you want the IP address
associated with a particular interface (such as eth0, ppp0,
wlan0, etc.).

>i don't want to call the ifconfig and the parse the result.
>
>what API's are available ?

What are you writing? A shell script is one thing and a C
program is another...

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) fl...@apaflo.com

Sagaert Johan

unread,
Sep 4, 2005, 4:22:31 AM9/4/05
to

"mst" <mysw...@hotMYSHOESmail.com> wrote in message
news:20050903173...@linux.site...

I am programming gcc (m68k-elf-gcc) , my uclinux device has no dns support
so i wonder if gethostbyname will work.

maybe i have to get it by getting it from eth0 , but i don'nt know how.

I was used to program win32 before ,so this is all new to me .


Kenneth

unread,
Sep 4, 2005, 4:33:53 AM9/4/05
to

If it was me looking for how to find the info, I'd look at the source for
ifconfig, and see where it gets the info. Or maybe faster, do an
strace ifconfig -a
and see what system calls it uses.

Kasper Dupont

unread,
Sep 4, 2005, 8:43:02 AM9/4/05
to
Sagaert Johan wrote:
>
> maybe i have to get it by getting it from eth0 , but i don'nt know how.

http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#IP

--
Kasper Dupont
Note to self: Don't try to allocate
256000 pages with GFP_KERNEL on x86.

Ben Hutchings

unread,
Sep 21, 2005, 3:31:19 PM9/21/05
to
Sagaert Johan wrote:
> Hi
>
> how to get the local ip ?

There may well be several local IP addresses.

> i don't want to call the ifconfig and the parse the result.
>
> what API's are available ?

You can do it the same way ifconfig does:
1. Create a socket for the address family you're interested in
(typically AF_INET, for IPv4 addresses)
2. Use ioctl(sock, SIOCGIFCONF, ...) to find the names of the
interfaces
3. Use ioctl(sock, SIOCGIFADDR, ...) to find the IP address for each
interface

You will probably want to filter out e.g. the loopback interface, in
which case use the SIOCGIFHWADDR ioctl to find out the hardware type
(sa_family) of the interface.

Look at the socket(2) and netdevice(7) manual pages for a full
description of these APIs.

--
Ben Hutchings
Klipstein's 4th Law of Prototyping and Production:
A fail-safe circuit will destroy others.

0 new messages