//
//Relevent network ioctl functions
//
//SIOCGIFADDR: /* Get interface address */
//SIOCGIFBRDADDR: /* Get the broadcast address */
//SIOCGIFNETMASK: /* Get the netmask for the interface */
int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr, int request)
{
int fd;
int rv; // return value
strncpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0)
rv = fd;
else
rv = ioctl(fd, request, ifr);
return rv;
}
int get_local_address_ioctl(char *ifname, struct in_addr *addr,
int request)
{
struct ifreq ifr = { {{0}} };
struct sockaddr_in *tcpip_address;
int rv; // return value
rv = get_local_ifr_ioctl(ifname, &ifr, request);
if (rv >= 0) {
tcpip_address = (struct sockaddr_in *) &ifr.ifr_addr;
memcpy(addr, &tcpip_address->sin_addr, sizeof(struct in_addr));
}
return rv;
}
int get_local_ip_address(char *ifname, struct in_addr *addr)
{
return get_local_address_ioctl(ifname, addr, SIOCGIFADDR);
}
int get_local_ip_broadcast_address(char *ifname, struct in_addr *addr)
{
return get_local_address_ioctl(ifname, addr, SIOCGIFBRDADDR);
}
int get_local_ip_address_mask(char *ifname, struct in_addr *mask)
{
return get_local_address_ioctl(ifname, mask, SIOCGIFNETMASK);
}
阅读(2530) | 评论(0) | 转发(0) |