|
Home > Archive > Unix Programming > September 2005 > How can a server know its own IP address
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How can a server know its own IP address
|
|
| au.faisal 2005-09-28, 7:58 am |
| Hi guys
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.
is there any function for that?
Thanks
Faisal
| |
| Ravi Uday 2005-09-28, 7:58 am |
|
au.faisal wrote:
> Hi guys
>
> 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.
>
man gethostbyname
> is there any function for that?
>
> Thanks
> Faisal
>
| |
| Pascal Bourguignon 2005-09-28, 7:58 am |
| "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
| |
| Jonathan Bartlett 2005-09-28, 6:02 pm |
| au.faisal wrote:
> Hi guys
>
> I am a newbie in socket programming. Can anyone tell me how can a
> server know on which IP address it is running.
It's configuration file. If it isn't specified, that usually means it
is running on all available IP addresses.
> 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.
>
> is there any function for that?
Either the socket listens for all addresses (IIRC it's 0.0.0.0 to
specify) or it is specified in a config file.
Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
| |
| Floyd L. Davidson 2005-09-28, 6:02 pm |
| "au.faisal" <au.faisal@gmail.com> wrote:
>Hi guys
>
>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.
>
>is there any function for that?
A given computer may have many interfaces and even more IP
addresses.
If you want a shell script, /ifconfig/ is the right command.
Otherwise, here is a C program which demonstrates how to
discover any or even all interfaces and IP addresses. It can
also be configured to look for only one specific type of
interface, or even just one specific individual interface.
The code should be portable to almost any system, but may
require changes in which headers are include and in which
order.
/*
* display info about network interfaces
*/
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))
static int
get_addr(int sock, char * ifname, struct sockaddr * ifaddr) {
struct ifreq *ifr;
struct ifreq ifrr;
struct sockaddr_in sa;
ifr = &ifrr;
ifrr.ifr_addr.sa_family = AF_INET;
strncpy(ifrr.ifr_name, ifname, sizeof(ifrr.ifr_name));
if (ioctl(sock, SIOCGIFADDR, ifr) < 0) {
printf("No %s interface.\n", ifname);
return -1;
}
*ifaddr = ifrr.ifr_addr;
printf("Address for %s: %s\n", ifname, inet_ntoa(inaddrr(ifr_addr.sa_data)));
return 0;
}
int main(void)
{
unsigned char *u;
int sockfd, size = 1;
struct ifreq *ifr;
struct ifconf ifc;
struct sockaddr_in sa;
if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
fprintf(stderr, "Cannot open socket.\n");
exit(EXIT_FAILURE);
}
ifc.ifc_len = IFRSIZE;
ifc.ifc_req = NULL;
do {
++size;
/* realloc buffer size until no overflow occurs */
if (NULL == (ifc.ifc_req = realloc(ifc.ifc_req, IFRSIZE))) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
ifc.ifc_len = IFRSIZE;
if (ioctl(sockfd, SIOCGIFCONF, &ifc)) {
perror("ioctl SIOCFIFCONF");
exit(EXIT_FAILURE);
}
} while (IFRSIZE <= ifc.ifc_len);
/* this is an alternate way to get info... */
{
struct sockaddr ifa;
get_addr(sockfd, "ppp0", &ifa);
}
ifr = ifc.ifc_req;
for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr) {
if (ifr->ifr_addr.sa_data == (ifr+1)->ifr_addr.sa_data) {
continue; /* duplicate, skip it */
}
if (ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
continue; /* failed to get flags, skip it */
}
printf("Interface: %s\n", ifr->ifr_name);
printf("IP Address: %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
/*
This won't work on HP-UX 10.20 as there's no SIOCGIFHWADDR ioctl. You'll
need to use DLPI or the NETSTAT ioctl on /dev/lan0, etc (and you'll need
to be root to use the NETSTAT ioctl. Also this is deprecated and doesn't
work on 11.00).
On Digital Unix you can use the SIOCRPHYSADDR ioctl according to an old
utility I have. Also on SGI I think you need to use a raw socket, e.g. s
= socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP)
Dave
From: David Peter <dave.peter@eu.citrix.com>
*/
if (0 == ioctl(sockfd, SIOCGIFHWADDR, ifr)) {
/* Select which hardware types to process.
*
* See list in system include file included from
* /usr/include/net/if_arp.h (For example, on
* Linux see file /usr/include/linux/if_arp.h to
* get the list.)
*/
switch (ifr->ifr_hwaddr.sa_family) {
default:
printf("\n");
continue;
case ARPHRD_NETROM: case ARPHRD_ETHER: case ARPHRD_PPP:
case ARPHRD_EETHER: case ARPHRD_IEEE802: break;
}
u = (unsigned char *) &ifr->ifr_addr.sa_data;
if (u[0] + u[1] + u[2] + u[3] + u[4] + u[5]) {
printf("HW Address: %2.2x.%2.2x.%2.2x.%2.2x.%2.2x.%2.2x\n",
u[0], u[1], u[2], u[3], u[4], u[5]);
}
}
if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) &&
strcmp("255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
printf("Netmask: %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
}
if (ifr->ifr_flags & IFF_BROADCAST) {
if (0 == ioctl(sockfd, SIOCGIFBRDADDR, ifr) &&
strcmp("0.0.0.0", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
printf("Broadcast: %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
}
}
if (0 == ioctl(sockfd, SIOCGIFMTU, ifr)) {
printf("MTU: %u\n", ifr->ifr_mtu);
}
if (0 == ioctl(sockfd, SIOCGIFMETRIC, ifr)) {
printf("Metric: %u\n", ifr->ifr_metric);
}
printf("\n");
}
close(sockfd);
return EXIT_SUCCESS;
}
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com
| |
| Bjorn Reese 2005-09-28, 6:02 pm |
| au.faisal wrote:
> I am a newbie in socket programming. Can anyone tell me how can a
> server know on which IP address it is running.
Use getsockname() once a connection has been established.
--
mail1dotstofanetdotdk
| |
| David Schwartz 2005-09-28, 6:02 pm |
|
"Bjorn Reese" <breese@see.signature> wrote in message
news:433aee29$0$12109$ba624c82@nntp02.dk.telia.net...
> au.faisal wrote:
[vbcol=seagreen]
> Use getsockname() once a connection has been established.
This is the right answer if the question is "how can a server know what
IP address a client is reaching it on". That may or may not be the OP's
question.
DS
|
|
|
|
|