Re: How can a server know its own IP address

From: Pascal Bourguignon (spam_at_mouse-potato.com)
Date: 09/28/05


Date: Wed, 28 Sep 2005 12:21:46 +0200


"au.faisal" <au.faisal@gmail.com> writes:
> I am a newbie in socket programming. Can anyone tell me how can a
> server know on which IP address it is running.
>
> To make it suppose a webserver is running on computer which has the IP
> address 131.100.0.1. How can the webserver get this IP server without
> hardcoding.

You've got a misconception here. Computers DO NOT HAVE IP addresses.
Interfaces HAVE.

And since there's always at least the lo0 interface, all computer that
has a physical network interface has at least two interfaces with each
one IP address.

Moreover, most in implementations several IP addresses can be assigned
to each physical interface.

So you should ask: how can I get the list of interfaces my computer
has, and how can I get the list of IP addresses one interface has?

> is there any function for that?

gethostbyname won't really help here, since it only returns what's
stored into the DNS database.

The user command to use is ifconfig(1)

There's no standard API to get the list of interfaces,
But there's one to get the list of IP addresses: getaddrinfo(3).

It returns a list of IP addresses, you'll have to filter out and
select the one you're interested in.

Note that most often, the servers are configured to answer to requests
from ANY address. And the TCP/IP protocol stack is designed to select
for outgoing packets the interface (therefore source address) that is
the most propicious to get a good connection to the destination address.

Which leaves me to wonder why you need to know "which IP address it is running"?
This is a rather meaningless question.

If you want to know on what IP address the given web server listens
to, the best way is to read it's configuration files.

For example, here:

$ grep -i listen /etc/httpd/httpd.conf
# Listen: Allows you to bind Apache to specific IP addresses and/or
#Listen 3000
#Listen 12.34.56.78:80
# is used to tell the server which IP address to listen to. It can either
# See also the <VirtualHost> and Listen directives.
# Port: The port to which the standalone server listens. For
## When we also provide SSL we have to listen to the
Listen 80
Listen 443

Since there's no specific IP address configured, my httpd server
listens on ALL the IP addresses of this host. What more do you need to know?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Small brave carnivores
Kill pine cones and mosquitoes
Fear vacuum cleaner


Relevant Pages

  • Re: Help with Subnetting
    ... to do the same thing but it would have to have multiple NICs. ... want a DMZ which is a third interface that brings you up on the list ... Here is a very simple example configuration that I'm sure IPTables ... Same to a different server using 5589. ...
    (comp.os.linux.networking)
  • Re: Remote Access Hassles
    ... Ethernet adapter LAN Interface: ... I should mention that I have been able to telnet to the Exchange server OK, ... so I suspect the problem is not with IP configuration. ... OWA or VPN from outside the Local LAN. ...
    (microsoft.public.windows.server.sbs)
  • Re: two interface two entry
    ... I have configured itself as DNS server for internal interface by giving it ... And I have configured modem as DNS server for external interface because it ... Windows IP Configuration ... Connection-specific DNS Suffix. ...
    (microsoft.public.windows.server.dns)
  • Re: Wireless NIC in FreeBSD 6.0 ?
    ... If for some set of options the client should use the value sup- plied by the server, but needs to use some default value if no value was supplied by the server, these values can be defined in the default statement. ... how can I configure the wireless interface to use DHCP in dhclient.conf? ... the default configuration for your wireless nic ... ...
    (freebsd-questions)
  • Re: Multicast question
    ... I have a new system running Solaris 10 set up as an NTP server. ... IT is synchronizing correctly but I cannot get it to multicast on any interface except the systems primary Ethernet interface, ... Basically multicast configuration is sort of configured backwards. ...
    (comp.protocols.time.ntp)