Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding subnet mask on Linux in C

I am interested in finding the subnet mask of the local host programatically in Linux preferably through the native C api.

like image 274
user1483918 Avatar asked Mar 28 '26 03:03

user1483918


2 Answers

getifaddrs() will give you a list of your network interfaces and the field ifa_netmask should be the mask for each.

like image 142
Duck Avatar answered Mar 29 '26 15:03

Duck


The ifconfig utility uses ioctl() with the SIOCGIFNETMASK command to get the netmask. But I think Duck's answer is more user-friendly.

like image 25
jedwards Avatar answered Mar 29 '26 16:03

jedwards