织梦CMS - 轻松建站从此开始!

罗索

Linux系统ioctl使用示例

jackyhwei 发布于 2010-05-05 12:54 点击:次 
Linux系统ioctl使用示例集:程序1:检测接口的inet_addr,netmask,broad_addr;程序2:检查接口的物理连接是否正常;程序3:更简单一点测试物理连接;程序4:调节音量
TAG:

These were writed and collected by kf701,you can use and modify them but NO WARRANTY.Contact with me : kf_701@21cn.com

程序1:检测接口的 inet_addr,netmask,broad_addr
程序2:检查接口的物理连接是否正常
程序3:更简单一点测试物理连接
程序4:调节音量

***************************程序1****************************************

  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <stdlib.h> 
  4. #include <errno.h> 
  5. #include <unistd.h> 
  6.  
  7. #include <sys/types.h> 
  8. #include <sys/socket.h> 
  9. #include <netinet/in.h> 
  10. #include <arpa/inet.h> 
  11.  
  12. #include <sys/ioctl.h> 
  13. #include <net/if.h> 
  14.  
  15. static void usage(){ 
  16.         printf("usage : ipconfig interface "); 
  17.         exit(0); 
  18.  
  19. int main(int argc,char **argv) 
  20.         struct sockaddr_in *addr; 
  21.         struct ifreq ifr; 
  22.         char *name,*address; 
  23.         int sockfd; 
  24.  
  25.         if(argc != 2) 
  26.                 usage(); 
  27.         else 
  28.                 name = argv[1]; 
  29.  
  30.         sockfd = socket(AF_INET,SOCK_DGRAM,0); 
  31.         strncpy(ifr.ifr_name,name,IFNAMSIZ-1); 
  32.  
  33.         if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1) 
  34.                 perror("ioctl error"),exit(1); 
  35.         addr = (struct sockaddr_in *)&(ifr.ifr_addr); 
  36.         address = inet_ntoa(addr->sin_addr); 
  37.         printf("inet addr: %s ",address); 
  38.  
  39.         if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1) 
  40.                 perror("ioctl error"),exit(1); 
  41.         addr = (struct sockaddr_in *)&ifr.ifr_broadaddr; 
  42.         address = inet_ntoa(addr->sin_addr); 
  43.         printf("broad addr: %s ",address); 
  44.  
  45.         if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1) 
  46.                 perror("ioctl error"),exit(1); 
  47.         addr = (struct sockaddr_in *)&ifr.ifr_addr; 
  48.         address = inet_ntoa(addr->sin_addr); 
  49.         printf("inet mask: %s ",address); 
  50.  
  51.         printf(" "); 
  52.         exit(0); 

******************************** 程序2*****************************************************

  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <errno.h> 
  4. #include <fcntl.h> 
  5. #include <getopt.h> 
  6. #include <sys/socket.h> 
  7. #include <sys/ioctl.h> 
  8. #include <net/if.h> 
  9. #include <stdlib.h> 
  10. #include <unistd.h> 
  11.  
  12. typedef unsigned short u16; 
  13. typedef unsigned int u32; 
  14. typedef unsigned char u8; 
  15.  
  16. #include <linux/ethtool.h> 
  17. #include <linux/sockios.h> 
  18.  
  19. int detect_mii(int skfd, char *ifname) 
  20.         struct ifreq ifr; 
  21.         u16 *data, mii_val; 
  22.         unsigned phy_id; 
  23.  
  24.         /* Get the vitals from the interface. */ 
  25.         strncpy(ifr.ifr_name, ifname, IFNAMSIZ); 
  26.         if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) 
  27.         { 
  28.                 fprintf(stderr, "SIOCGMIIPHY on %s failed: %s ", ifname, 
  29.                 strerror(errno)); 
  30.                 (void) close(skfd); 
  31.                 return 2; 
  32.         } 
  33.  
  34.         data = (u16 *)(&ifr.ifr_data); 
  35.         phy_id = data[0]; 
  36.         data[1] = 1; 
  37.  
  38.  
  39.         if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0) 
  40.         { 
  41.                 fprintf(stderr, "SIOCGMIIREG on %s failed: %s ", ifr.ifr_name, 
  42.                 strerror(errno)); 
  43.                 return 2; 
  44.         } 
  45.  
  46.         mii_val = data[3]; 
  47.  
  48.         return(((mii_val & 0x0016) == 0x0004) ? 0 : 1); 
  49.  
  50. int detect_ethtool(int skfd, char *ifname) 
  51.         struct ifreq ifr; 
  52.         struct ethtool_value edata; 
  53.  
  54.         memset(&ifr, 0, sizeof(ifr)); 
  55.         edata.cmd = ETHTOOL_GLINK; 
  56.  
  57.         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)-1); 
  58.         ifr.ifr_data = (char *) &edata; 
  59.  
  60.         if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1) 
  61.         { 
  62.                 printf("ETHTOOL_GLINK failed: %s ", strerror(errno)); 
  63.                 return 2; 
  64.         } 
  65.  
  66.         return (edata.data ? 0 : 1); 
  67.  
  68. int main(int argc, char **argv) 
  69.         int skfd = -1; 
  70.         char *ifname; 
  71.         int retval; 
  72.  
  73.         if( argv[1] ) 
  74.                 ifname = argv[1]; 
  75.         else 
  76.                 ifname = "eth0"
  77.  
  78.         /* Open a socket. */ 
  79.         if (( skfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ) 
  80.         { 
  81.                 printf("socket error "); 
  82.                 exit(-1); 
  83.         } 
  84.  
  85.         retval = detect_ethtool(skfd, ifname); 
  86.  
  87.         if (retval == 2) 
  88.                 retval = detect_mii(skfd, ifname); 
  89.  
  90.  
  91.         close(skfd); 
  92.  
  93.         if (retval == 2) 
  94.                 printf("Could not determine status "); 
  95.  
  96.         if (retval == 1) 
  97.                 printf("Link down "); 
  98.  
  99.         if (retval == 0) 
  100.                 printf("Link up "); 
  101.  
  102.         return retval; 

*******************************程序3*****************************************************

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <string.h> 
  4. #include <errno.h> 
  5. #include <net/if.h> 
  6. #include <linux/sockios.h> 
  7. #include <sys/ioctl.h> 
  8. #define LINKTEST_GLINK 0x0000000a 
  9.  
  10. struct linktest_value { 
  11.         unsigned int    cmd; 
  12.         unsigned int    data; 
  13. }; 
  14.  
  15. static 
  16. void 
  17. usage(const char * pname) 
  18.         fprintf(stderr, "usage: %s <device> ", pname); 
  19.         fprintf(stderr, "returns: "); 
  20.         fprintf(stderr, " 0: link detected "); 
  21.         fprintf(stderr, " %d: %s ", ENODEV, strerror(ENODEV)); 
  22.         fprintf(stderr, " %d: %s ", ENONET, strerror(ENONET)); 
  23.         fprintf(stderr, " %d: %s ", EOPNOTSUPP, strerror(EOPNOTSUPP)); 
  24.         exit(EXIT_FAILURE); 
  25.  
  26. static 
  27. int 
  28. linktest(const char * devname) 
  29.         struct ifreq ifr; 
  30.         struct linktest_value edata; 
  31.         int fd; 
  32.  
  33.         /* setup our control structures. */ 
  34.         memset(&ifr, 0, sizeof(ifr)); 
  35.         strcpy(ifr.ifr_name, devname); 
  36.  
  37.         /* open control socket. */ 
  38.         fd=socket(AF_INET, SOCK_DGRAM, 0); 
  39.         if(fd < 0 ) { 
  40.                 return -ECOMM; 
  41.         } 
  42.  
  43.         errno=0; 
  44.         edata.cmd = LINKTEST_GLINK; 
  45.         ifr.ifr_data = (caddr_t)&edata; 
  46.  
  47.         if(!ioctl(fd, SIOCETHTOOL, &ifr)) { 
  48.                 if(edata.data) { 
  49.                         fprintf(stdout, "link detected on %s ", devname); 
  50.                         return 0; 
  51.                 } else { 
  52.                         errno=ENONET; 
  53.                 } 
  54.         } 
  55.  
  56.         perror("linktest"); 
  57.         return errno; 
  58.  
  59. int 
  60. main(int argc, char *argv[]) 
  61.         if(argc != 2) { 
  62.                 usage(argv[0]); 
  63.         } 
  64.         return linktest(argv[1]); 

*************************************程序4*********************************************************

  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <fcntl.h> 
  4. #include <sys/ioctl.h> 
  5. #include <sys/soundcard.h> 
  6. #include <stdio.h> 
  7. #include <unistd.h> 
  8. #include <math.h> 
  9. #include <string.h> 
  10. #include <stdlib.h> 
  11.  
  12. #define BASE_VALUE 257 
  13.  
  14. int main(int argc,char *argv[]) 
  15.         int mixer_fd=0; 
  16.         char *names[SOUND_MIXER_NRDEVICES]=SOUND_DEVICE_LABELS; 
  17.         int value,i; 
  18.  
  19.         printf(" usage:%s dev_no.[0..24] value[0..100] ",argv[0]); 
  20.         printf("eg. %s 0 100 ",argv[0]); 
  21.         printf("    will change the volume to MAX volume. "); 
  22.         printf("The dev_no. are as below: "); 
  23.         for (i=0;i<SOUND_MIXER_NRDEVICES;i++){ 
  24.                 if (i%3==0) printf(" "); 
  25.                 printf("%s:%d ",names[i],i); 
  26.         } 
  27.         printf(" "); 
  28.  
  29.         if (argc<3) 
  30.                 exit(1); 
  31.  
  32.         if ((mixer_fd = open("/dev/mixer",O_RDWR))){ 
  33.                 printf("Mixer opened successfully,working... "); 
  34.                 value=BASE_VALUE*atoi(argv[2]); 
  35.  
  36.                 if (ioctl(mixer_fd,MIXER_WRITE(atoi(argv[1])),&value)==0) 
  37.                 printf("successfully....."); 
  38.                 else    printf("unsuccessfully....."); 
  39.                 printf("done. "); 
  40.          }else 
  41.                 printf("can't open /dev/mixer error.... "); 
  42.  
  43.         exit(0); 
(kf701)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201005/9331.html]
本文出处:百度博客 作者:kf701
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容