|
Home > Archive > Unix Programming > February 2007 > Hai Can anyone give me idea to get the IP address of my camputer through a 'c' program
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 |
Hai Can anyone give me idea to get the IP address of my camputer through a 'c' program
|
|
|
| Hai,
Anybody can give me idea how to get the ip address of my machine
through a cprogram with out using the socket programming. Is there
any
system call to get the ip address of my machine that will be used in
the c program to display the IP address of my machine.
Regards,
Sunny
| |
| David Schwartz 2007-02-16, 7:20 am |
| On Feb 16, 2:06 am, "sunny" <kavuri.rajasek...@gmail.com> wrote:
> Anybody can give me idea how to get the ip address of my machine
> through a cprogram with out using the socket programming. Is there
> any
> system call to get the ip address of my machine that will be used in
> the c program to display the IP address of my machine.
The IP address of your machine is 127.0.0.1.
DS
| |
| Jens Thoms Toerring 2007-02-16, 7:20 am |
| sunny <kavuri.rajasekhar@gmail.com> wrote:
> Anybody can give me idea how to get the ip address of my machine
> through a cprogram with out using the socket programming. Is there
> any
> system call to get the ip address of my machine that will be used in
> the c program to display the IP address of my machine.
A computer can have several IP addresses (beside the canonical
127.0.0.1). It could have more than a single network card, with
a different IP address for each card. It could be on a LAN and
thus have an address in the LAN (like 192.168.17.25) and another
one it's visible as from the internet (the router forwarding
packets to that address and the machine itself not even being
aware of that). So which one of all those do you care about?
If you're looking for the addresses the machine knows about
you could parse the output of /sbin/ifconfig. If you want
to know about the address of a machine on a LAN as seem from
the internet you would need to have the name it's known under
and then could use the 'host' or 'dig' programs to figure out
the corresponding IP address.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
| |
| Christopher Layne 2007-02-16, 1:17 pm |
| sunny wrote:
> Hai,
>
> Anybody can give me idea how to get the ip address of my machine
> through a cprogram with out using the socket programming. Is there
> any
> system call to get the ip address of my machine that will be used in
> the c program to display the IP address of my machine.
>
>
> Regards,
> Sunny
Not going to happen without sockets. Also not going to possibly work on all
OSes.
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
extern int if_populate(char **dest, size_t ne, in_addr_t mask)
{
struct ifconf ifc;
struct ifreq *ifr;
struct sockaddr_in *sa;
struct in_addr addr;
char l_addr[256];
char *tmp;
int s;
unsigned int i, y;
ifc.ifc_len = sizeof(l_addr);
ifc.ifc_buf = l_addr;
s = socket(AF_INET, SOCK_STREAM, 0);
if ((ioctl(s, SIOCGIFCONF, &ifc)) == -1) {
perror("ioctl");
return -1;
}
close(s);
for (i = y = 0; i < (ifc.ifc_len / sizeof(struct ifreq)); i++) {
ifr = ifc.ifc_req + i;
sa = (struct sockaddr_in *)&ifr->ifr_addr;
addr.s_addr = sa->sin_addr.s_addr;
if ((ntohl(addr.s_addr) & mask) == mask && y < ne) {
tmp = inet_ntoa(addr);
dest[y] = malloc(strlen(tmp) + 1);
if (dest[y] == NULL)
abort();
strcpy(dest[y], tmp);
y++;
}
}
return y;
}
int main(int argc, char **argv)
{
char *sa_tab[4];
size_t i;
i = if_populate(sa_tab, sizeof sa_tab / sizeof *sa_tab,
inet_network(argc < 2 ? "0.0.0.0" : argv[1]));
while (i--)
fprintf(stdout, "%s\n", sa_tab[i]);
return 0;
}
|
|
|
|
|