// 检测网络连接
// routeName: 网络连接名称,如ppp0、eth0等
// 返回值: 网络正常返回0,异常返回-1
int CheckNetLink(const char *routeName)
{
register int fd, intrface;
struct ifreq buf[16];
struct ifconf ifc;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc))
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
while (intrface-- > 0)
{
if (strcmp(buf[intrface].ifr_name, routeName) == 0)
{
ioctl(fd, SIOCGIFFLAGS, (char *) &buf[intrface]);
if ((buf[intrface].ifr_flags & IFF_UP) && (buf[intrface].ifr_flags & IFF_RUNNING))
{
shutdown(fd, SHUT_RDWR);
close(fd);
//printf("\n%s is UP & RUNNING\n", routeName);
return OK;
}
else
{
shutdown(fd, SHUT_RDWR);
close(fd);
//printf("\n%s is DOWN\n", routeName);
return ERROR;
}
}
}
shutdown(fd, SHUT_RDWR);
close(fd);
//printf("\n%s is not exist\n", routeName);
return ERROR;
}
}
return ERROR;
} |
评论暂时关闭