Networking Forums

Networking Forums > Computer Networking > Linux Networking > finding local ip

Reply
Thread Tools Display Modes

finding local ip

 
 
Robert Harris
Guest
Posts: n/a

 
      02-20-2006, 03:16 PM
shyam wrote:
> hi all
>
> how do i find out the ip address of the local machine programatically?
>
> thanks shyam
>


Call ioctl with parameter SIOCGIFHWADDR. Like:

char mac_address[6];

{
struct ifreq ifr = {{{0}}};
int sock = socket(PF_INET, SOCK_DGRAM, 0);

if (sock == -1) {
[ error ]
}
strcpy(ifr.ifr_name, "eth0");
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
[ error ]
}
memcpy(mac_address, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
close(sock);
}

Robert
 
Reply With Quote
 
 
 
 
Floyd L. Davidson
Guest
Posts: n/a

 
      02-20-2006, 04:11 PM
"shyam" <(E-Mail Removed)> wrote:
>how do i find out the ip address of the local machine programatically?


Here is a short demo program that will find the IP addresses
(and other information) for all interfaces, or you can choose to
sort by interface type, name, whatever. This is a generic
program, and I've posted it to Usenet many times over several
years and incorporated ideas into it from the comments.

/* display info about network interfaces */

#define _BSD_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <net/if_arp.h>

#define ifaddr(x) (*(struct in_addr *) &x->ifr_addr.sa_data[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))

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);

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(ifaddr(ifr)));

/*
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 <(E-Mail Removed)>
*/

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(ifaddr(ifr)))) {
printf("Netmask: %s\n", inet_ntoa(ifaddr(ifr)));
}

if (ifr->ifr_flags & IFF_BROADCAST) {
if (0 == ioctl(sockfd, SIOCGIFBRDADDR, ifr) &&
strcmp("0.0.0.0", inet_ntoa(ifaddr(ifr)))) {
printf("Broadcast: %s\n", inet_ntoa(ifaddr(ifr)));
}
}

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) (E-Mail Removed)
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing XP login from Local to Domain While Maintaining Local User. TomTech Windows Networking 13 10-03-2007 01:28 AM
how to create a local share on local computer muelle60 Windows Networking 10 10-04-2006 06:45 PM
Howto redirect traffic from local machine to internet back to local machine? Martin Kahlert Linux Networking 0 11-25-2005 07:40 AM
Finding out how many CAL's I have Shabam Windows Networking 3 07-19-2004 02:59 AM
Finding the ip address your really using. T Home Networking 5 08-06-2003 08:11 AM



1 2 3 4 5 6 7 8 9 10 11