网络在线测试2

#include <ctype.h>;
#include <err.h>;
#include <errno.h>;
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
#include <unistd.h>;

#include <sys/ioctl.h>;
#include <sys/socket.h>;
#include <net/if.h>;
#include <net/if_types.h>;
#include <net/if_media.h>;

int
main(int argc,char **argv)
{
int s;
struct ifreq ifr;
struct ifmediareq ifmr;
int *media_list;
char name[IFNAMSIZ];

if(2 != argc){
printf("Usage:/ntest cardname(e.g: vr0)/n");
return 1;
}

strncpy(name, argv[1], sizeof(name));

ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
err(1, "socket");

(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));

if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
err(1, "SIOCGIFMEDIA");
if (ifmr.ifm_count == 0) {
warnx("%s: no media types?", name);
return;
}

media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
if (media_list == NULL)
err(1, "malloc");
ifmr.ifm_ulist = media_list;

if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
err(1, "SIOCGIFMEDIA");

if (ifmr.ifm_status & IFM_AVALID) {
printf("/tstatus: ");
switch (IFM_TYPE(ifmr.ifm_active)) {
case IFM_ETHER:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("active");
else
printf("no carrier");
break;

case IFM_FDDI:
case IFM_TOKEN:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("inserted");
else
printf("no ring");
break;

case IFM_ATM:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("active");
else
printf("no carrier");
break;

case IFM_IEEE80211:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("associated");
else
printf("no carrier");
break;
}
putchar('/n');
}

free(media_list);
return 0;
}
[/code]

firewall# gcc test.c -o test
firewall# test vr0
status: active
firewall# test vr1
status: active
firewall# test vr2
test: SIOCGIFMEDIA: Device not configured

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值