How to get an IP address from network interfaces
dangbinghoo
dangbinghoo at gmail.com
Fri Apr 22 05:28:52 UTC 2022
On Thursday, 21 April 2022 at 07:38:04 UTC, Alexander Zhirov
wrote:
> On Thursday, 21 April 2022 at 07:20:30 UTC, dangbinghoo wrote:
>> On Thursday, 21 April 2022 at 07:04:18 UTC, Alexander Zhirov
>> wrote:
>>> I want to get the IP address of the network interface. There
>>> is both a wireless interface and a wired one. Is it possible,
>>> knowing the name of the network interface, to get its IP
>>> address?
>>
>> ```d
>> import core.sys.posix.sys.ioctl;
>> import core.sys.posix.arpa.inet;
>>
>> import core.stdc.string;
>> import core.stdc.stdio;
>> import core.stdc.errno;
>> import core.sys.posix.stdio;
>> import core.sys.posix.unistd;
>>
>> string ip;
>>
>>
>> int get(string if_name)
>> {
>> int s = socket(AF_INET, SOCK_DGRAM, 0);
>> if (s < 0) {
>> fprintf(stderr, "Create socket failed!errno=%d", errno);
>> return -1;
>> }
>>
>> ifreq ifr;
>> uint nIP, nNetmask, nBroadIP;
>>
>> strcpy(ifr.ifr_name.ptr, std.string.toStringz(if_name));
>> try {
>> if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
>> return -2;
>> }
>> }
>> catch (Exception e) {
>> writeln("Error operation on netif " ~ if_name);
>> return -2;
>> }
>> memcpy(macaddr.ptr, cast(char *)ifr.ifr_hwaddr.sa_data.ptr,
>> 6);
>>
>> if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
>> nIP = 0;
>> }
>> else {
>> nIP = *cast(uint*)(&ifr.ifr_broadaddr.sa_data[2]);
>> ip = fromStringz(inet_ntoa(*cast(in_addr*)&nIP)).idup;
>> }
>> }
>>
>> ```
>
>
>
> Gives a lot of errors when compiling
>
> ```d
> app.d(19): Error: function
> `std.stdio.makeGlobal!"core.stdc.stdio.stderr".makeGlobal` at
> /usr/include/dlang/dmd/std/stdio.d(5198) conflicts with
> variable `core.stdc.stdio.stderr` at
> /usr/include/dlang/dmd/core/stdc/stdio.d(927)
> app.d(19): Error: function
> `core.stdc.stdio.fprintf(shared(_IO_FILE)* stream, scope
> const(char*) format, scope const ...)` is not callable using
> argument types `(void, string, int)`
> app.d(19): cannot pass argument
> `makeGlobal(StdFileHandle _iob)()` of type `void` to parameter
> `shared(_IO_FILE)* stream`
> app.d(23): Error: undefined identifier `ifreq`
> app.d(26): Error: undefined identifier `string` in package `std`
> app.d(39): Error: undefined identifier `macaddr`
> app.d(48): Error: undefined identifier `fromStringz`
> ```
```d
struct ifreq {
private union ifr_ifrn_ {
char[IFNAMSIZ] ifrn_name; /* if name, e.g. "en0" */
}
ifr_ifrn_ ifr_ifrn;
private union ifr_ifru_ {
sockaddr ifru_addr;
sockaddr ifru_dstaddr;
sockaddr ifru_broadaddr;
sockaddr ifru_netmask;
sockaddr ifru_hwaddr;
short ifru_flags;
int ifru_ivalue;
int ifru_mtu;
ifmap ifru_map;
byte[IFNAMSIZ] ifru_slave; /* Just fits the size */
byte[IFNAMSIZ] ifru_newname;
byte * ifru_data;
}
ifr_ifru_ ifr_ifru;
// NOTE: alias will not work : alias ifr_ifrn.ifrn_name
ifr_name;
@property ref ifr_name() { return ifr_ifrn.ifrn_name; } /*
interface name */
@property ref ifr_hwaddr() { return ifr_ifru.ifru_hwaddr; }
/* MAC address */
@property ref ifr_addr() { return ifr_ifru.ifru_addr; } /*
address */
@property ref ifr_dstaddr() { return ifr_ifru.ifru_dstaddr;
} /* other end of p-p lnk */
@property ref ifr_broadaddr() { return
ifr_ifru.ifru_broadaddr; } /* broadcast address */
@property ref ifr_netmask() { return ifr_ifru.ifru_netmask;
} /* interface net mask */
@property ref ifr_flags() { return ifr_ifru.ifru_flags; } /*
flags */
@property ref ifr_metric() { return ifr_ifru.ifru_ivalue; }
/* metric */
@property ref ifr_mtu() { return ifr_ifru.ifru_mtu; } /* mtu
*/
@property ref ifr_map() { return ifr_ifru.ifru_map; } /*
device map */
@property ref ifr_slave() { return ifr_ifru.ifru_slave; } /*
slave device */
@property ref ifr_data() { return ifr_ifru.ifru_data; } /*
for use by interface */
@property ref ifr_ifindex() { return ifr_ifru.ifru_ivalue; }
/* interface index */
@property ref ifr_bandwidth() { return ifr_ifru.ifru_ivalue;
} /* link bandwidth */
@property ref ifr_qlen() { return ifr_ifru.ifru_ivalue; } /*
queue length */
@property ref ifr_newname() { return ifr_ifru.ifru_newname;
} /* New name */
}
```
More information about the Digitalmars-d-learn
mailing list