Re: getting all interface addresses in the system
Re: getting all interface addresses in the system
- Subject: Re: getting all interface addresses in the system
- From: Josh Graessley <email@hidden>
- Date: Tue, 6 Sep 2005 13:43:39 -0700
The sa_data in a sockaddr is not a string. The sockaddr is a general
structure that has more or less a header that is shared among all
socket address structures. Depending on the value of the sa_family,
you need to cast the sockaddr to the correct socket address type. Use
struct sockaddr_in for AF_INET, struct sockaddr_in6 for AF_INET6, etc.
Also, the ifaddr_address function will copy as much data as will fit.
In the event that you don't supply a large enough buffer, you can
still find out the size as long as the buffer is at least the size of
the sockaddr header (sa_len/sa_family). You can look at sa_len if you
get back EMSGSIZE.
-josh
On Sep 5, 2005, at 8:30 PM, Herry Wiputra wrote:
Hi,
I've been trying to get all the interface addresses in my system
with the interface KPI. Unfortunately, all I've been getting is
empty addresses.
This is the output from ifconfig -a:
<snip>
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 134.14.55.116 netmask 0xffffff00 broadcast 134.14.55.255
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 192.168.0.116 netmask 0xffffff00 broadcast 192.168.0.255
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
lladdr 00:0a:95:ff:fe:79:5c:fc
media: autoselect <full-duplex> status: inactive
supported media: autoselect <full-duplex>
<snip>
This is what I did to get the interface addresses:
ifnet_t *list = NULL;
ifaddr_t *addresses = NULL;
uint32_t count = 0;
struct sockaddr out_addr;
int i = 0;
int j = 0;
int err = 0;
struct in_addr pin;
if (!ifnet_list_get(AF_INET, &list, &count)) {
for (i = 0; i < count; i++) {
if (!ifnet_get_address_list(list[i], &addresses)) {
for (j = 0; addresses[j] != NULL; j++) {
if ((err = ifaddr_address(addresses[j], &out_addr, sizeof(struct
sockaddr))) == 0) {
if (inet_aton(out_addr.sa_data, &pin))
PRINTF("inet[%d]->address[%d]: size = %d, data = %u, type = %d
\n", i, j, (int)out_addr.sa_len, pin.s_addr, (int)out_addr.sa_family);
else
PRINTF("inet[%d]->address[%d]: size = %d, data = %s, data addr
= %p, type = %d\n", i, j, (int)out_addr.sa_len, out_addr.sa_data,
out_addr.sa_data, (int)out_addr.sa_family);
}
else if (err == 40) {
if ((err = ifaddr_address(addresses[j], &out_addr, 20)) == 0) {
if (inet_aton(out_addr.sa_data, &pin))
PRINTF("inet[%d]->address[%d]: size = %d, data = %u, type = %d\n",
i, j, (int)out_addr.sa_len, pin.s_addr, (int)out_addr.sa_family);
else
PRINTF("inet[%d]->address[%d]: size = %d, data = %s, data addr = %
p, type = %d\n", i, j, (int)out_addr.sa_len, out_addr.sa_data,
out_addr.sa_data, (int)out_addr.sa_family);
}
else
PRINTF("inet[%d]->address[%d]: still fail with larger sa_len, err =
%d\n", i, j, err);
}
else
PRINTF("inet[%d]->address[%d]: NULL sock address, err = %d\n", i,
j, err);
if ((err = ifaddr_dstaddress(addresses[j], &out_addr, sizeof(struct
sockaddr))) == 0) {
if (inet_aton(out_addr.sa_data, &pin))
PRINTF("inet[%d]->dstaddress[%d]: size = %d, data = %u, type = %
d\n", i, j, (int)out_addr.sa_len, pin.s_addr, (int)
out_addr.sa_family);
else
PRINTF("inet[%d]->dstaddress[%d]: size = %d, data = %s, data
addr = %p, type = %d\n", i, j, (int)out_addr.sa_len,
out_addr.sa_data, out_addr.sa_data, (int)out_addr.sa_family);
}
else
PRINTF("inet[%d]->dstaddress[%d]: NULL sock dst address, err = %d
\n", i, j, err);
if ((err = ifaddr_netmask(addresses[j], &out_addr, sizeof(struct
sockaddr))) == 0) {
if (inet_aton(out_addr.sa_data, &pin))
PRINTF("inet[%d]->netmask[%d]: size = %d, data = %u, type = %d
\n", i, j, (int)out_addr.sa_len, pin.s_addr, (int)out_addr.sa_family);
else
PRINTF("inet[%d]->netmask[%d]: size = %d, data = %s, data addr
= %p, type = %d\n", i, j, (int)out_addr.sa_len, out_addr.sa_data,
out_addr.sa_data, (int)out_addr.sa_family);
}
else
PRINTF("inet[%d]->netmask[%d]: NULL netmask address, err = %d\n",
i, j, err);
}
}
}
}
And this is the output that I got:
dc 01aaec60 inet[0]->address[0]: still fail with larger sa_len,
err = 45
dd 01aaec60 inet[0]->dstaddress[0]: NULL sock dst address, err = 45
de 01aaec60 inet[0]->netmask[0]: size = 11, data = , data addr =
0xce1b9e2, type = 0
e1 01aaec60 inet[0]->address[1]: still fail with larger sa_len,
err = 45
e2 01aaec60 inet[0]->dstaddress[1]: NULL sock dst address, err = 45
e3 01aaec60 inet[0]->netmask[1]: NULL netmask address, err = 40
e6 01aaec60 inet[0]->address[2]: size = 16, data = , data addr =
0xce1b9e2, type = 2
e7 01aaec60 inet[0]->dstaddress[2]: size = 16, data = , data addr
= 0xce1b9e2, type = 2
e8 01aaec60 inet[0]->netmask[2]: size = 7, data = , data addr =
0xce1b9e2, type = 2
eb 01aaec60 inet[0]->address[3]: still fail with larger sa_len,
err = 45
ec 01aaec60 inet[0]->dstaddress[3]: NULL sock dst address, err = 45
ed 01aaec60 inet[0]->netmask[3]: NULL netmask address, err = 40
f6 01aaec60 inet[1]->address[0]: still fail with larger sa_len,
err = 45
f7 01aaec60 inet[1]->dstaddress[0]: NULL sock dst address, err = 45
f8 01aaec60 inet[1]->netmask[0]: size = 11, data = , data addr =
0xce1b9e2, type = 0
fb 01aaec60 inet[1]->address[1]: still fail with larger sa_len,
err = 45
fc 01aaec60 inet[1]->dstaddress[1]: NULL sock dst address, err = 45
fd 01aaec60 inet[1]->netmask[1]: NULL netmask address, err = 40
100 01aaec60 inet[1]->address[2]: size = 16, data = , data addr =
0xce1b9e2, type = 2
101 01aaec60 inet[1]->dstaddress[2]: size = 16, data = , data addr
= 0xce1b9e2, type = 2
102 01aaec60 inet[1]->netmask[2]: size = 7, data = , data addr =
0xce1b9e2, type = 2
Anyone has any idea why the sa_data field is always empty?
Am I doing the right thing?
Thanks,
Herry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40apple.com
This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden