linux probe whether network line is attached to network card normally
2011-06-17 15:41
323 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
// ifname like "eth0".
// Notice: call this function need root privilege.
// return value:
// -1 -- error , details about error, pls check errno
// 1 -- interface link up
// 0 -- interface link down.
inline int GetNetlinkStatus(uint8_t *ifname)
{
int sockfd;
struct ifreq ifr;
struct ethtool_value edata;
edata.cmd = ETHTOOL_GLINK;
edata.data = 0;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, (char *)ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_data = (char *) &edata;
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return -1;
if(ioctl(sockfd, SIOCETHTOOL, &ifr) == -1) {
close(sockfd);
return -1;
}
close(sockfd);
return edata.data;
}
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
// ifname like "eth0".
// Notice: call this function need root privilege.
// return value:
// -1 -- error , details about error, pls check errno
// 1 -- interface link up
// 0 -- interface link down.
inline int GetNetlinkStatus(uint8_t *ifname)
{
int sockfd;
struct ifreq ifr;
struct ethtool_value edata;
edata.cmd = ETHTOOL_GLINK;
edata.data = 0;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, (char *)ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_data = (char *) &edata;
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return -1;
if(ioctl(sockfd, SIOCETHTOOL, &ifr) == -1) {
close(sockfd);
return -1;
}
close(sockfd);
return edata.data;
}
相关文章推荐
- 解决linux下非root用户频繁出现Authentication is required to set the network proxy used for downloading packages
- Howto Red hat enterprise Linux 5 configure the network card
- ccah-500 第28题 Which Linux commands help you to identify whether swapping is occurring
- Howto Red hat enterprise Linux 5 configure the network card
- Write one line C function to find whether a no is power of two
- 18 Command Line Tools to Monitor Linux Performance
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC
- Access to the requested object is only available from the local network phpmyadmin
- Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict,
- 15 Useful “ifconfig” Commands to Configure Network Interface in Linux
- How to make linux boot from network
- 挂载nfs文件系统出现:mount: RPC: Unable to send; errno = Network is unreachable
- 8 Command Line Tools to Monitor Linux Performance
- How to identify video formats from command line on Linux
- This is a prerequisite condition to test whether sufficient total swap space is available on system
- mysql本地訪问linuxserver,出现SQLSTATE[HY000] [1130] Host '127.0.0.1' is not allowed to connect to this
- linux下搭建android开发环境出错:Unable to run mksdcard SDK tool.
- 关于Cygwin中编译FFmpeg时执行sh文件出现arm-linux-androideabi-gcc is unable to create an executable file.
- 20 Command Line Tools to Monitor Linux Performance