/*******************************************************************************
$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;
       sfd = socket (PF_INET, SOCK_STREAM, 0);
       if (sfd < 0)
       {
               SYSLOG_ERR ("BLOG: EXTR: socket err in %s\n", __func__);
               return FALSE;
       }
       g_strlcpy (ifr.ifr_name, "eth0", 16);
       ret = ioctl (sfd, SIOCGIFFLAGS, &ifr);
       if (ret < 0)
       {
               SYSLOG_ERR ("BLOG: EXTR: ioctl err in %s\n", __func__);
       }
       if (((ifr.ifr_flags) & IFF_UP) && ((ifr.ifr_flags) & IFF_RUNNING))
       {
               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__);
               }
               if (((ifr.ifr_flags) & IFF_UP) && ((ifr.ifr_flags) & IFF_RUNNING))
               {
                       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__);
                       }
                       if (((ifr.ifr_flags) & IFF_UP) && ((ifr.ifr_flags) & IFF_RUNNING))
                       {
                               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__);
                               }
                               if (((ifr.ifr_flags) & IFF_UP) && ((ifr.ifr_flags) & IFF_RUNNING))
                               {
                                       goto ok;
                               }
                               else
                               {
                                       goto err;
                               }
                       }
               }
       }
err:
       close (sfd);
       return FALSE;
ok:
       close (sfd);
       return TRUE;
}
 
----------------------------------------------------------
 
#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
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Z_man/archive/2009/03/17/3998250.aspx