public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* how to get IP address of current machine from C++ code?
       [not found] <200010210246.WAA08580@smarty.smart.net>
@ 2000-11-03 17:37 ` Christopher Friesen
  2000-11-03 21:09   ` David Ford
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher Friesen @ 2000-11-03 17:37 UTC (permalink / raw)
  To: linux-kernel

I would like to get the IP address of one of the interfaces of the
machine that I'm currently on from within some C++ code.  It looks like
I should be able to do this by doing an

ioctl(atoi(fd, SIOCGIFADDR, &ifr)

with the interface name set in the appropriate field in ifr, but I'm not
sure how I should be getting the proper value for fd.  I would
appreciate some help on this, or if there is a better way then I'd love
to hear it.

Thanks,

Chris


--
Chris Friesen                    | MailStop: 043/33/F10  
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: cfriesen@nortelnetworks.com
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: how to get IP address of current machine from C++ code?
  2000-11-03 17:37 ` how to get IP address of current machine from C++ code? Christopher Friesen
@ 2000-11-03 21:09   ` David Ford
  0 siblings, 0 replies; 2+ messages in thread
From: David Ford @ 2000-11-03 21:09 UTC (permalink / raw)
  To: Christopher Friesen; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 717 bytes --]

Christopher Friesen wrote:

> I would like to get the IP address of one of the interfaces of the
> machine that I'm currently on from within some C++ code.  It looks like
> I should be able to do this by doing an
>
> ioctl(atoi(fd, SIOCGIFADDR, &ifr)
>
> with the interface name set in the appropriate field in ifr, but I'm not
> sure how I should be getting the proper value for fd.  I would
> appreciate some help on this, or if there is a better way then I'd love
> to hear it.

This isn't c++, it's plain c, but it's easy to grok.  Called as: gi
[interface]

-d

--
"The difference between 'involvement' and 'commitment' is like an
eggs-and-ham breakfast: the chicken was 'involved' - the pig was
'committed'."



[-- Attachment #2: gi.c --]
[-- Type: text/plain, Size: 1500 bytes --]

/*
 * short hack to grab interface information
 * gcc -o gi gi.c; strip gi
 *
 * Blu3, Jan 1999
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIOCGIFCONF        0x8912          /* get iface list               */

int main(int argc, char *argv[])
{
int numreqs = 30, sd, n, search, tick, found=0;
struct ifconf ifc;
struct ifreq *ifr;
struct in_addr *ia;

//
// if there is an arg on the command line, print out the ip of that device
// only.  note the numreqs in the above, modify that as is desired.

search= (argc>1);
if(search && strlen(argv[1]) > 64) {
  fprintf(stderr, "specified device name too large, ignoring\n");
  search=0;
}

sd=socket(AF_INET, SOCK_STREAM, 0);
ifc.ifc_buf = NULL;
ifc.ifc_len = sizeof(struct ifreq) * numreqs;
ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) {
	perror("SIOCGIFCONF");
}
                                 
ifr = ifc.ifc_req;
for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
	ia= (struct in_addr *) ((ifr->ifr_ifru.ifru_addr.sa_data)+2);
	if(search)
		tick= strcmp(ifr->ifr_ifrn.ifrn_name, argv[1]);

	if(!search)
		fprintf(stdout, "%6s %-15s\n", ifr->ifr_ifrn.ifrn_name, inet_ntoa(*ia));
	  
	if (search && (tick==0)) {
		fprintf(stdout, "%s\n", inet_ntoa(*ia));
		found=1;
	}
	ifr++;
}

free(ifc.ifc_buf);
fprintf(stderr, "exiting with %i\n", found);
exit(found);
}

[-- Attachment #3: Card for David Ford --]
[-- Type: text/x-vcard, Size: 239 bytes --]

begin:vcard 
n:Ford;David
x-mozilla-html:TRUE
org:<img src="http://www.kalifornia.com/images/paradise.jpg">
adr:;;;;;;
version:2.1
email;internet:david@kalifornia.com
title:Blue Labs Developer
x-mozilla-cpt:;-12480
fn:David Ford
end:vcard

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-11-03 21:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <200010210246.WAA08580@smarty.smart.net>
2000-11-03 17:37 ` how to get IP address of current machine from C++ code? Christopher Friesen
2000-11-03 21:09   ` David Ford

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox