Chinaunix首页 | 论坛 | 博客
  • 博客访问: 612484
  • 博文数量: 204
  • 博客积分: 5172
  • 博客等级: 上校
  • 技术积分: 2092
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 21:48
个人简介

一个毫无毅力之人的自勉

文章分类

全部博文(204)

文章存档

2014年(1)

2013年(54)

2012年(50)

2011年(94)

2010年(3)

2009年(3)

分类: LINUX

2011-07-27 12:41:26

  1. //facename.c 枚举出iface使用的是/proc/net/dev文件

  2. #include <sys/types.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <net/if.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>

  10. #define LINE_MAX 512
  11. #define PATH_PROC_NET_DEV "/proc/net/dev"

  12. int main(int argc, char *argv[])
  13. {
  14.         FILE * fp;
  15.         char line[LINE_MAX];
  16.         char * tmp,i=0;
  17.         char interfacename[32][32];
  18.         if(!(fp = fopen(PATH_PROC_NET_DEV, "r")))
  19.         {
  20.                 perror("fopen");
  21.                 exit(1);
  22.         }

  23.         fgets(line, sizeof(line), fp);
  24.         fgets(line, sizeof(line), fp);
  25.         while(fgets(line, sizeof(line), fp))
  26.         {
  27.                 if((tmp = strchr(line,':')))
  28.                 {
  29.                         *tmp = '\0';
  30.                         tmp = line;
  31.                         while(*tmp ==' ')
  32.                         {
  33.                                 tmp++;
  34.                         }
  35.                         strcpy(interfacename[i],tmp);
  36.                         printf("%s\n", tmp);
  37.                         i++;
  38.                 }
  39.         }
  40.         fclose(fp);
  41.         for(i=i-1;i>=0;i--)
  42.         {
  43.                 printf("%s\n", interfacename[i]);
  44.         }
  45.         exit(0);
  46. }

/****************************************************************************************/
//phy_ioctrl.c 使用ioctrl测试网络状态,
  1. #include <sys/types.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/ioctl.h>
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <net/if.h>

  8. struct ethtool_value
  9. {
  10.         __uint32_t cmd;
  11.         __uint32_t data;
  12. };

  13. int main(char argc,char**argv)
  14. {
  15.         struct ethtool_value edata;
  16.         int fd=-1,err=0;
  17.         struct ifreq ifr;

  18.         memset(&ifr,0,sizeof(ifr));
  19.         strcpy(ifr.ifr_name,"eth0");
  20.         fd = socket(AF_INET,SOCK_DGRAM,0);
  21.         if(fd<0)
  22.         {
  23.                 perror("cannot get control socket");
  24.                 return 70;
  25.         }

  26.         edata.cmd = 0x0000000a;
  27.         ifr.ifr_data = (caddr_t)&edata;
  28.         err = ioctl(fd,0x8946,&ifr);
  29.         if(err == 0)
  30.         {
  31.                 printf("Link detected:%s\n",edata.data? "yes":"no");
  32.         }
  33.         else if(errno != EOPNOTSUPP)
  34.         {
  35.                 perror("cannot get link status");
  36.         }
  37.         return 0;
  38. }

/*********************************************************************/
iface_name.c
  1. #include <sys/types.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <net/if.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>

  9. #define IF_MAX 30

  10. int main(int argc, char *argv[])
  11. {
  12.         int numreqs = 10;
  13.         int fd;
  14.         int i;
  15.         struct ifreq * ifr = NULL;
  16.         struct ifconf ifc;

  17.         if((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  18.         {
  19.                 perror("socket");
  20.                 exit(1);
  21.         }

  22.         ifc.ifc_len = sizeof(struct ifreq) * numreqs;
  23.         ifc.ifc_buf = malloc(ifc.ifc_len);
  24.         memset(ifc.ifc_buf, 0, ifc.ifc_len);

  25.         if(ioctl(fd, SIOCGIFCONF, &ifc) != 0)
  26.         {
  27.                 perror("SIOCGIFCONF");
  28.                 free(ifc.ifc_buf);
  29.                 close(fd);
  30.                 exit(1);
  31.         }

  32.         close(fd);

  33.         if(ifc.ifc_len == sizeof(struct ifreq) * numreqs)
  34.         printf("The system has at least %d interfaces, "
  35.                 "please increase the buffer.\n", numreqs);

  36.         ifr = ifc.ifc_req;

  37.         printf("Interfaces in the system (by SIOCGIFCONF):\n");
  38.         printf("--------------------------------------------------\n");
  39.         for(i=0; i<ifc.ifc_len; i+=sizeof(struct ifreq))
  40.         {
  41.                 printf("%s\n", ifr->ifr_name);
  42.                 ifr++;
  43.         }

  44.         free(ifc.ifc_buf);

  45.         exit(0);
  46. }
/**************************************************************************/




阅读(1827) | 评论(0) | 转发(0) |
0

上一篇:graphviz

下一篇:firefox通过ssh上网

给主人留下些什么吧!~~