int hs_getdevicecurrip(char *ip, char *devName)
{
int sockfd;
if (-1 == (sockfd = socket(PF_INET, SOCK_STREAM, 0)))
{
perror("socket");
return -1;
}
struct ifreq req;
struct sockaddr_in *host;
bzero(&req, sizeof(struct ifreq));
strcpy(req.ifr_name, devName);
if (ioctl(sockfd, SIOCGIFADDR, &req))
{
close(sockfd);
return -1;
}
else
{
host = (struct sockaddr_in *)&req.ifr_addr;
strcpy(ip, inet_ntoa(host->sin_addr));
if (inet_addr(ip) > 0)
{
close(sockfd);
return 0;
}
}
close(sockfd);
return -1;
}
联系客服