How to retrieve local IP address from nw_interface_t?

Currently I see methods to get the name, index, and type of nw_interface_t objects. Is there a way to get the IP addresses attached to that interface?


I tried nw_path's nw_path_copy_effective_local_endpoint inside of a nw_path_monitor callback, but it always returns null.

Replies

To be clear, there’s no such thing as a “local IP address” for an interface. An interface has a set of local IP addresses, with an arbitrary mixture of IPv4 and IPv6.

The easiest way to get that set is to call

nw_interface_get_name
to get the BSD name for the interface and then use that to filter the results of
getidaddrs
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

But can individual call via ioctl be faster?

  struct sockaddr_in *addr;
	struct ifreq ifr;
	char *address;
	int sockfd;

	char *name = "en0";

	if(strlen(name)>=IFNAMSIZ)
		printf("device name is error.\n"),exit(0);

	strcpy(ifr.ifr_name,name);
	sockfd=socket(AF_INET,SOCK_DGRAM,0);

	//get inet addr
	if(ioctl(sockfd,SIOCGIFADDR,&ifr)<0)
		printf("ioctl error!!!\n"),exit(0);

	addr = (struct sockaddr_in *)&(ifr.ifr_addr);
	address=inet_ntoa(addr->sin_addr);

	printf("inet addr:%s\n",address);

But can individual call via ioctl be faster?

Why would you care about the performance of this? It’s not like you’re doing this in a tight loop, right? … Right?

Regardless, this code is wrong because it returns a single IPv4 address and, as I mentioned upthread, that’s not an accurate model of how the system works.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"