$function_name: check_online_status
$function_decs: check online status
$function_arguments:
$function_return:
TRUE: online
FALSE: offline
$function_remark:
*******************************************************************************/
gboolean check_online_status()
{
gint ret = 0;
gint sfd;
struct ifreq ifr;
if (sfd < 0)
{
SYSLOG_ERR ("BLOG: EXTR: socket err in %s\n", __func__);
return FALSE;
}
ret = ioctl (sfd, SIOCGIFFLAGS, &ifr);
if (ret < 0)
{
SYSLOG_ERR ("BLOG: EXTR: ioctl err in %s\n", __func__);
}
{
goto ok;
}
else
{
g_strlcpy (ifr.ifr_name, "ath0", 16);
ret = ioctl (sfd, SIOCGIFFLAGS, &ifr);
if (ret < 0)
{
SYSLOG_ERR ("BLOG: EXTR: ioctl err in %s\n", __func__);
}
{
goto ok;
}
else
{
/* 穷举开始 */
g_strlcpy (ifr.ifr_name, "wlan0", 16);
ret = ioctl (sfd, SIOCGIFFLAGS, &ifr);
if (ret < 0)
{
SYSLOG_ERR ("BLOG: EXTR: ioctl err in %s\n", __func__);
}
{
goto ok;
}
else
{
g_strlcpy (ifr.ifr_name, "wifi0", 16);
ret = ioctl (sfd, SIOCGIFFLAGS, &ifr);
if (ret < 0)
{
SYSLOG_ERR ("BLOG: EXTR: ioctl err in %s\n", __func__);
}
{
goto ok;
}
else
{
goto err;
}
}
}
}
close (sfd);
return FALSE;
close (sfd);
return TRUE;
#include <err.h>;
#include <errno.h>;
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
#include <unistd.h>;
#include <sys/socket.h>;
#include <net/if.h>;
#include <net/if_types.h>;
#include <net/if_media.h>;
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) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
err(1, "SIOCGIFMEDIA");
if (ifmr.ifm_count == 0) {
warnx("%s: no media types?", name);
return;
}
if (media_list == NULL)
err(1, "malloc");
ifmr.ifm_ulist = media_list;
err(1, "SIOCGIFMEDIA");
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_TOKEN:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("inserted");
else
printf("no ring");
break;
if (ifmr.ifm_status & IFM_ACTIVE)
printf("active");
else
printf("no carrier");
break;
if (ifmr.ifm_status & IFM_ACTIVE)
printf("associated");
else
printf("no carrier");
break;
}
putchar('\n');
}
return 0;
}
[/code]
firewall# test vr0
status: active
firewall# test vr1
status: active
firewall# test vr2
test: SIOCGIFMEDIA: Device not configured