Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3307026
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7194
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: C/C++

2012-12-30 14:11:48

1.检测接口的 inet_addr,netmask,broad_addr

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <sys/ioctl.h>
  11. #include <net/if.h>



  12. int main(int argc, char **argv)
  13. {
  14.     struct sockaddr_in *addr;
  15.     struct ifreq ifr;
  16.     char *name, *address;
  17.     int sockfd = -1;

  18.     if (argc != 2) {
  19.         printf("usage : %s interface \n", argv[0]);
  20.         exit(-1);
  21.     } else
  22.         name = argv[1];

  23.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  24.     strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
  25.     if (ioctl(sockfd, SIOCGIFADDR, &ifr) == -1) {
  26.         perror("ioctl error\n");
  27.         exit(1);
  28.     }

  29.     addr = (struct sockaddr_in *)&(ifr.ifr_addr);
  30.     address = inet_ntoa(addr->sin_addr);
  31.     printf("inet addr: %s\n", address);
  32.     if (ioctl(sockfd, SIOCGIFBRDADDR, &ifr) == -1) {
  33.         perror("ioctl error\n");
  34.         exit(1);
  35.     }

  36.     addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;
  37.     address = inet_ntoa(addr->sin_addr);
  38.     printf("broad addr: %s\n", address);
  39.     if (ioctl(sockfd, SIOCGIFNETMASK, &ifr) == -1) {
  40.         perror("ioctl error\n");
  41.         exit(1);
  42.     }

  43.     addr = (struct sockaddr_in *)&ifr.ifr_addr;
  44.     address = inet_ntoa(addr->sin_addr);
  45.     printf("inet mask: %s\n", address);

  46.     close(sockfd);

  47.     exit(0);
  48. }


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. #include <linux/ethtool.h>
  12. #include <linux/sockios.h>



  13. typedef unsigned short u16;
  14. typedef unsigned int u32;
  15. typedef unsigned char u8;


  16. int detect_mii(int skfd, char *ifname)
  17. {
  18.     struct ifreq ifr;
  19.     u16 *data, mii_val;
  20.     unsigned phy_id;

  21.     /* Get the vitals from the interface. */
  22.     strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  23.     if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
  24.         fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", ifname,
  25.                 strerror(errno));
  26.         close(skfd);

  27.         return 2;
  28.     }

  29.     data = (u16 *) (&ifr.ifr_data);
  30.     phy_id = data[0];
  31.     data[1] = 1;
  32.     if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0) {
  33.         fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name,
  34.                 strerror(errno));
  35.         close(skfd);

  36.         return 2;
  37.     }
  38.     mii_val = data[3];

  39.     return (((mii_val & 0x0016) == 0x0004) ? 0 : 1);
  40. }

  41. int detect_ethtool(int skfd, char *ifname)
  42. {
  43.     struct ifreq ifr;
  44.     struct ethtool_value edata;

  45.     memset(&ifr, 0, sizeof(ifr));
  46.     edata.cmd = ETHTOOL_GLINK;
  47.     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  48.     ifr.ifr_data = (char *)&edata;
  49.     if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1) {
  50.         printf("ETHTOOL_GLINK failed: %s\n", strerror(errno));
  51.         return 2;
  52.     }

  53.     return (edata.data ? 0 : 1);
  54. }

  55. int main(int argc, char **argv)
  56. {
  57.     int skfd = -1;
  58.     char *ifname;
  59.     int retval;

  60.     if (argv[1])
  61.         ifname = argv[1];
  62.     else
  63.         ifname = "eth0";

  64.     /* Open a socket. */
  65.     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  66.         printf("socket error\n");
  67.         exit(-1);
  68.     }
  69.     retval = detect_ethtool(skfd, ifname);
  70.     if (retval == 2)
  71.         retval = detect_mii(skfd, ifname);
  72.     close(skfd);

  73.     if (retval == 2)
  74.         printf("Could not determine status\n");
  75.     else if (retval == 1)
  76.         printf("Link down\n");
  77.     else if (retval == 0)
  78.         printf("Link up\n");

  79.     return 0;
  80. }


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. struct linktest_value {
  10.     unsigned int cmd;
  11.     unsigned int data;
  12. };

  13. static void usage(const char *pname)
  14. {
  15.     fprintf(stderr, "usage: %s \n", pname);
  16.     fprintf(stderr, "returns: \n");
  17.     fprintf(stderr, "\t 0: link detected\n");
  18.     fprintf(stderr, "\t%d: %s\n", ENODEV, strerror(ENODEV));
  19.     fprintf(stderr, "\t%d: %s\n", ENONET, strerror(ENONET));
  20.     fprintf(stderr, "\t%d: %s\n", EOPNOTSUPP, strerror(EOPNOTSUPP));

  21.     exit(EXIT_FAILURE);
  22. }

  23. static int linktest(const char *devname)
  24. {
  25.     struct ifreq ifr;
  26.     struct linktest_value edata;
  27.     int fd;

  28.     /* setup our control structures. */
  29.     memset(&ifr, 0, sizeof(ifr));
  30.     strcpy(ifr.ifr_name, devname);
  31.     /* open control socket. */
  32.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  33.     if (fd < 0) {
  34.         return -ECOMM;
  35.     }
  36.     errno = 0;
  37.     edata.cmd = LINKTEST_GLINK;
  38.     ifr.ifr_data = (caddr_t) & edata;
  39.     if (!ioctl(fd, SIOCETHTOOL, &ifr)) {
  40.         if (edata.data) {
  41.             fprintf(stdout, "link detected on %s\n", devname);
  42.             return 0;
  43.         } else {
  44.             errno = ENONET;
  45.         }
  46.     }
  47.     perror("linktest");

  48.     return errno;
  49. }

  50. int main(int argc, char *argv[])
  51. {
  52.     if (argc != 2) {
  53.         usage(argv[0]);
  54.     }

  55.     return linktest(argv[1]);
  56. }


4.调节音量

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/soundcard.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <math.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>

  11. #define BASE_VALUE 257

  12. int main(int argc, char *argv[])
  13. {
  14.     int mixer_fd = 0;
  15.     char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
  16.     int value, i;

  17.     if (argc < 3) {        
  18.         printf("\nusage:%s dev_no.[0..24] value[0..100]\n\n", argv[0]);
  19.         printf("eg. %s 0 100\n", argv[0]);
  20.         printf(" will change the volume to MAX volume.\n\n");
  21.         printf("The dev_no. are as below:\n");
  22.         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  23.             if (i % 3 == 0)
  24.                 printf("\n");
  25.             printf("%s:%d\t\t", names[i], i);
  26.         }
  27.         printf("\n\n");

  28.         exit(1);
  29.     }

  30.     if ((mixer_fd = open("/dev/mixer", O_RDWR))) {
  31.         printf("Mixer opened successfully,working...\n");
  32.         value = BASE_VALUE * atoi(argv[2]);
  33.         if (ioctl(mixer_fd, MIXER_WRITE(atoi(argv[1])), &value) == 0)
  34.             printf("successfully.....");
  35.         else
  36.             printf("unsuccessfully.....");
  37.         printf("done.\n");
  38.     } else
  39.         printf("can't open /dev/mixer error....\n");

  40.     exit(0);
  41. }
阅读(1874) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~