Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Get IP from Network Device

Moderators: larsivi kris

Posted: 08/29/07 14:03:56

I there is no way to get IP from a network device in linux ??

i have this code

#include <string.h>

#include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h>

char *getIP(char *dev){

int fd; struct ifreq ifr;

fd = socket(AF_INET, SOCK_DGRAM, 0);

ifr.ifr_addr.sa_family = AF_INET;

strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);

ioctl(fd, SIOCGIFADDR, &ifr);

close(fd);

return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);

}

But i can't port it for D :S Some one can help me please e need this for a project.

Author Message

Posted: 09/05/07 14:50:36

Regarding the socket/inet_ntoa and other calls, there are either definitions for these functions, or equivalents in tango. (see tango/sys/linux/socket)

However, I find no trace of ioctl anywhere in the tango tree. Remember that C functions can just be prototyped in D, so you can prototype it yourself, but I think ioctl is an important function for many low level coding projects.

Tango guys, can ioctl be added? More specifically is there a reason why ioctl isn't there?

Posted: 09/05/07 18:26:42

I believe magician was able to get his program working (maybe he could post the result here?).

As for ioctl, Sean may have a good reason, but things tend to not go in until after they're requested.

Posted: 09/05/07 18:42:08

I can add netinet/* and net/*, but sys/ioctl.h is not a POSIX header--it's a Linux header. I'll add it to tango.sys.linux, if that works? You'll also need to import tango.stdc.posix.stropts to get the ioctl() routine, which will be a new header as well.

Posted: 09/11/07 18:04:18

The Posix headers related to this have been added in changeset [2564]. I'll take care of the linux headers separately.