Chinaunix

标题: 获取本机ip和网卡mac等信息的代码 [打印本页]

作者: gadfly    时间: 2003-06-06 17:02
标题: 获取本机ip和网卡mac等信息的代码
我以前贴过,好像被删了。老有人问。我就再贴了

支持多网卡。很久以前写的,参照几本资料,忘了是哪些。

  1. #include <stdio.h>;
  2. #include <sys/types.h>;
  3. #include <sys/param.h>;

  4. #include <sys/ioctl.h>;
  5. #include <sys/socket.h>;
  6. #include <net/if.h>;
  7. #include <netinet/in.h>;
  8. #include <net/if_arp.h>;

  9. #define MAXINTERFACES   16

  10. main (argc, argv)
  11. register int argc;
  12. register char *argv[];
  13. {
  14.    register int fd, intrface, retn = 0;
  15.    struct ifreq buf[MAXINTERFACES];
  16.    struct arpreq arp;
  17.    struct ifconf ifc;

  18.    if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >;= 0) {
  19.       ifc.ifc_len = sizeof buf;
  20.       ifc.ifc_buf = (caddr_t) buf;
  21.       if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) {
  22.          intrface = ifc.ifc_len / sizeof (struct ifreq);
  23.          printf("interface num is intrface=%d\n\n\n",intrface);
  24.          while (intrface-- >; 0)
  25.           {
  26.             printf ("net device %s\n", buf[intrface].ifr_name);

  27. /*Jugde whether the net card status is promisc  */
  28.             if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface]))) {
  29.                if (buf[intrface].ifr_flags & IFF_PROMISC) {
  30.                   puts ("the interface is PROMISC");
  31.                   retn++;
  32.                }
  33.             } else {
  34.                char str[256];

  35.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  36.                perror (str);
  37.             }

  38. /*Jugde whether the net card status is up       */
  39.             if (buf[intrface].ifr_flags & IFF_UP) {
  40.                 puts("the interface status is UP");
  41.                }
  42.             else {
  43.                 puts("the interface status is DOWN");
  44.             }

  45. /*Get IP of the net card */
  46.             if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
  47.                 {
  48.                  puts ("IP address is:");
  49.                  puts(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr));
  50.                  puts("");
  51.                    //puts (buf[intrface].ifr_addr.sa_data);
  52.                 }
  53.             else {
  54.                char str[256];

  55.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  56.                perror (str);
  57.            }
  58. /* this section can't get Hardware Address,I don't know whether the reason is module driver
  59. //          ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr;
  60.             arp.arp_pa.sa_family = AF_INET;
  61.             arp.arp_ha.sa_family = AF_INET;
  62.             ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr.s_addr;
  63.             if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
  64.                 {
  65.                  puts ("HW address is:");

  66.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  67.                                 (unsigned char)arp.arp_ha.sa_data[0],
  68.                                 (unsigned char)arp.arp_ha.sa_data[1],
  69.                                 (unsigned char)arp.arp_ha.sa_data[2],
  70.                                 (unsigned char)arp.arp_ha.sa_data[3],
  71.                                 (unsigned char)arp.arp_ha.sa_data[4],
  72.                                 (unsigned char)arp.arp_ha.sa_data[5]);

  73.                  puts("");
  74.                  puts("");
  75.                 }

  76. */

  77. /*Get HW ADDRESS of the net card */
  78.             if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
  79.                 {
  80.                  puts ("HW address is:");

  81.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  82.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
  83.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
  84.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
  85.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
  86.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
  87.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);

  88.                  puts("");
  89.                  puts("");
  90.                 }

  91.             else {
  92.                char str[256];

  93.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  94.                perror (str);
  95.            }
  96.         }
  97.       } else
  98.          perror ("cpm: ioctl");

  99.    } else
  100.       perror ("cpm: socket");

  101.     close (fd);
  102.     return retn;
  103. }
复制代码

作者: gadfly    时间: 2003-06-06 18:18
标题: 获取本机ip和网卡mac等信息的代码
另外有人问我solaris怎么有问题,我查阅了一下,确实如此。

ioctl用的不是一个选项,稍微改了一下,编译的时候,方法如下:
gcc getip.c -DSOLARIS -lsocket -lnsl

  1. #include <stdio.h>;
  2. #include <sys/types.h>;
  3. #include <sys/param.h>;

  4. #include <sys/ioctl.h>;
  5. #include <sys/socket.h>;
  6. #include <net/if.h>;
  7. #include <netinet/in.h>;
  8. #include <net/if_arp.h>;

  9. #ifdef SOLARIS
  10. #include <sys/sockio.h>;
  11. #endif

  12. #define MAXINTERFACES   16

  13. main (argc, argv)
  14. register int argc;
  15. register char *argv[];
  16. {
  17.    register int fd, intrface, retn = 0;
  18.    struct ifreq buf[MAXINTERFACES];
  19.    struct arpreq arp;
  20.    struct ifconf ifc;

  21.    if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >;= 0) {
  22.       ifc.ifc_len = sizeof buf;
  23.       ifc.ifc_buf = (caddr_t) buf;
  24.       if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) {
  25.          intrface = ifc.ifc_len / sizeof (struct ifreq);
  26.          printf("interface num is intrface=%d\n\n\n",intrface);
  27.          while (intrface-- >; 0)
  28.           {
  29.             printf ("net device %s\n", buf[intrface].ifr_name);

  30. /*Jugde whether the net card status is promisc  */
  31.             if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface]))) {
  32.                if (buf[intrface].ifr_flags & IFF_PROMISC) {
  33.                   puts ("the interface is PROMISC");
  34.                   retn++;
  35.                }
  36.             } else {
  37.                char str[256];

  38.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  39.                perror (str);
  40.             }

  41. /*Jugde whether the net card status is up       */
  42.             if (buf[intrface].ifr_flags & IFF_UP) {
  43.                 puts("the interface status is UP");
  44.                }
  45.             else {
  46.                 puts("the interface status is DOWN");
  47.             }

  48. /*Get IP of the net card */
  49.             if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
  50.                 {
  51.                  puts ("IP address is:");
  52.                  puts(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr));
  53.                  puts("");
  54.                    //puts (buf[intrface].ifr_addr.sa_data);
  55.                 }
  56.             else {
  57.                char str[256];

  58.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  59.                perror (str);
  60.            }
  61. /* this section can't get Hardware Address,I don't know whether the reason is module driver
  62. //          ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr;
  63.             arp.arp_pa.sa_family = AF_INET;
  64.             arp.arp_ha.sa_family = AF_INET;
  65.             ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr.s_addr;
  66.             if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
  67.                 {
  68.                  puts ("HW address is:");

  69.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  70.                                 (unsigned char)arp.arp_ha.sa_data[0],
  71.                                 (unsigned char)arp.arp_ha.sa_data[1],
  72.                                 (unsigned char)arp.arp_ha.sa_data[2],
  73.                                 (unsigned char)arp.arp_ha.sa_data[3],
  74.                                 (unsigned char)arp.arp_ha.sa_data[4],
  75.                                 (unsigned char)arp.arp_ha.sa_data[5]);

  76.                  puts("");
  77.                  puts("");
  78.                 }

  79. */

  80. /*Get HW ADDRESS of the net card */
  81. #ifdef SOLARIS
  82.             if (!(ioctl (fd,  SIOCGENADDR, (char *) &buf[intrface])))
  83.                 {
  84.                  puts ("HW address is:");

  85.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  86.                                 (unsigned char)buf[intrface].ifr_enaddr[0],
  87.                                 (unsigned char)buf[intrface].ifr_enaddr[1],
  88.                                 (unsigned char)buf[intrface].ifr_enaddr[2],
  89.                                 (unsigned char)buf[intrface].ifr_enaddr[3],
  90.                                 (unsigned char)buf[intrface].ifr_enaddr[4],
  91.                                 (unsigned char)buf[intrface].ifr_enaddr[5]);

  92.                  puts("");
  93.                  puts("");
  94.                 }
  95. #else
  96.             if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
  97.                 {
  98.                  puts ("HW address is:");

  99.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  100.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
  101.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
  102.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
  103.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
  104.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
  105.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);

  106.                  puts("");
  107.                  puts("");
  108.                 }
  109. #endif

  110.             else {
  111.                char str[256];

  112.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  113.                perror (str);
  114.            }
  115.         }
  116.       } else
  117.          perror ("cpm: ioctl");

  118.    } else
  119.       perror ("cpm: socket");

  120.     close (fd);
  121.     return retn;
  122. }
复制代码

作者: gadfly    时间: 2003-06-06 18:28
标题: 获取本机ip和网卡mac等信息的代码
但是用以上的方法,会报错类似
cpm: ioctl device hme0: No such file or directory

的错。我又查了一下,有资料说,solaris上对SIOCGENADDR ioctl 没有实现。

我就仔细看了看代码,试试以前被我注释的,发现被注释的可以在solaris用,但是不能在linux下用。

所以最终版本是下面这个
solaris上编译方式是:
gcc getip.c -DSOLARIS -lsocket -lnsl

linux直接用
gcc getip.c


  1. #include <stdio.h>;
  2. #include <sys/types.h>;
  3. #include <sys/param.h>;

  4. #include <sys/ioctl.h>;
  5. #include <sys/socket.h>;
  6. #include <net/if.h>;
  7. #include <netinet/in.h>;
  8. #include <net/if_arp.h>;

  9. #ifdef SOLARIS
  10. #include <sys/sockio.h>;
  11. #endif

  12. #define MAXINTERFACES   16

  13. main (argc, argv)
  14. register int argc;
  15. register char *argv[];
  16. {
  17.    register int fd, intrface, retn = 0;
  18.    struct ifreq buf[MAXINTERFACES];
  19.    struct arpreq arp;
  20.    struct ifconf ifc;

  21.    if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >;= 0) {
  22.       ifc.ifc_len = sizeof buf;
  23.       ifc.ifc_buf = (caddr_t) buf;
  24.       if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) {
  25.          intrface = ifc.ifc_len / sizeof (struct ifreq);
  26.          printf("interface num is intrface=%d\n\n\n",intrface);
  27.          while (intrface-- >; 0)
  28.           {
  29.             printf ("net device %s\n", buf[intrface].ifr_name);

  30. /*Jugde whether the net card status is promisc  */
  31.             if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface]))) {
  32.                if (buf[intrface].ifr_flags & IFF_PROMISC) {
  33.                   puts ("the interface is PROMISC");
  34.                   retn++;
  35.                }
  36.             } else {
  37.                char str[256];

  38.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  39.                perror (str);
  40.             }

  41. /*Jugde whether the net card status is up       */
  42.             if (buf[intrface].ifr_flags & IFF_UP) {
  43.                 puts("the interface status is UP");
  44.                }
  45.             else {
  46.                 puts("the interface status is DOWN");
  47.             }

  48. /*Get IP of the net card */
  49.             if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
  50.                 {
  51.                  puts ("IP address is:");
  52.                  puts(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr));
  53.                  puts("");
  54.                    //puts (buf[intrface].ifr_addr.sa_data);
  55.                 }
  56.             else {
  57.                char str[256];

  58.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  59.                perror (str);
  60.            }
  61. /* this section can't get Hardware Address,I don't know whether the reason is module driver*/
  62. //          ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr;
  63. #ifdef SOLARIS
  64.             arp.arp_pa.sa_family = AF_INET;
  65.             arp.arp_ha.sa_family = AF_INET;
  66.             ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr.s_addr;
  67.             if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
  68.                 {
  69.                  puts ("HW address is:");

  70.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  71.                                 (unsigned char)arp.arp_ha.sa_data[0],
  72.                                 (unsigned char)arp.arp_ha.sa_data[1],
  73.                                 (unsigned char)arp.arp_ha.sa_data[2],
  74.                                 (unsigned char)arp.arp_ha.sa_data[3],
  75.                                 (unsigned char)arp.arp_ha.sa_data[4],
  76.                                 (unsigned char)arp.arp_ha.sa_data[5]);

  77.                  puts("");
  78.                  puts("");
  79.                 }


  80. #else
  81. #if 0
  82. /*Get HW ADDRESS of the net card */
  83.             if (!(ioctl (fd,  SIOCGENADDR, (char *) &buf[intrface])))
  84.                 {
  85.                  puts ("HW address is:");

  86.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  87.                                 (unsigned char)buf[intrface].ifr_enaddr[0],
  88.                                 (unsigned char)buf[intrface].ifr_enaddr[1],
  89.                                 (unsigned char)buf[intrface].ifr_enaddr[2],
  90.                                 (unsigned char)buf[intrface].ifr_enaddr[3],
  91.                                 (unsigned char)buf[intrface].ifr_enaddr[4],
  92.                                 (unsigned char)buf[intrface].ifr_enaddr[5]);

  93.                  puts("");
  94.                  puts("");
  95.                 }
  96. #endif
  97.             if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
  98.                 {
  99.                  puts ("HW address is:");

  100.                  printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  101.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
  102.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
  103.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
  104.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
  105.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
  106.                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);

  107.                  puts("");
  108.                  puts("");
  109.                 }
  110. #endif

  111.             else {
  112.                char str[256];

  113.                sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  114.                perror (str);
  115.            }
  116.         }
  117.       } else
  118.          perror ("cpm: ioctl");

  119.    } else
  120.       perror ("cpm: socket");

  121.     close (fd);
  122.     return retn;
  123. }
复制代码

作者: gadfly    时间: 2003-06-06 18:33
标题: 获取本机ip和网卡mac等信息的代码
我看到另外一个网上资料,solaris下说可以用直接访问dlpi api的方式可以取到mac地址。我贴过来了,各位试试看。
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=4e11f68182497875&seekm=tardp79vv9o9d3%40corp.supernews.com#link2

  1. /*
  2. * mac_addr_dlpi.c
  3. *
  4. * Return the MAC (ie, ethernet hardware) address by using the dlpi api.
  5. *
  6. * compile with: gcc -c -D "OS" mac_addr_dlpi.c
  7. * with "OS" is one of AIX, SunOS, HPUX
  8. */

  9. /**********************************************************************/
  10. /* this section defines a list of the dlpi capable devices
  11. * this depends on the operating system
  12. */

  13. #undef DLPI_DEV

  14. #ifdef HPUX
  15. static char *dlpi_dev[] = {"/dev/dlpi", ""};
  16. #define DLPI_DEV
  17. #endif

  18. #ifdef AIX
  19. static char *dlpi_dev[] = {"/dev/dlpi/et", "/dev/dlpi/en",
  20. "/dev/dlpi/tr", "/dev/dlpi/fddi", ""};
  21. #define DLPI_DEV
  22. /* AIX: remember to set up /etc/pse.conf or /etc/dlpi.conf */
  23. #endif

  24. #ifdef SunOS
  25. static char *dlpi_dev[] = {"/dev/hme", "/dev/ie", "/dev/le", ""};
  26. #define DLPI_DEV
  27. #endif

  28. #ifndef DLPI_DEV
  29. static char *dlpi_dev[] = {"/dev/dlpi", ""};
  30. /* unknown OS - hope that this will work ??? */
  31. #define DLPI_DEV
  32. #endif

  33. /**********************************************************************/
  34. /*
  35. * implementation
  36. */

  37. #define INSAP 22
  38. #define OUTSAP 24

  39. #include <sys/types.h>;
  40. #include <fcntl.h>;
  41. #include <errno.h>;
  42. #include <stdio.h>;
  43. #include <string.h>;
  44. #include <signal.h>;
  45. #include <ctype.h>;
  46. #include <sys/stropts.h>;
  47. #include <sys/poll.h>;
  48. #include <sys/dlpi.h>;

  49. #define bcopy(source, destination, length) memcpy(destination, source, length)

  50. #define AREA_SZ 5000 /*&=&* buffer length in bytes *&=&*/
  51. static u_long ctl_area[AREA_SZ];
  52. static u_long dat_area[AREA_SZ];
  53. static struct strbuf ctl = {AREA_SZ, 0, (char *)ctl_area};
  54. static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
  55. #define GOT_CTRL 1
  56. #define GOT_DATA 2
  57. #define GOT_BOTH 3
  58. #define GOT_INTR 4
  59. #define GOT_ERR 128

  60. /*&=&* get a message from a stream; return type of message *&=&*/
  61. static int get_msg(int fd)
  62. {
  63.     int flags = 0;
  64.     int res, ret;
  65.     ctl_area[0] = 0;
  66.     dat_area[0] = 0;
  67.     ret = 0;
  68.     res = getmsg(fd, &ctl, &dat, &flags);
  69.     if(res < 0) {
  70.         if(errno == EINTR) {
  71.             return(GOT_INTR);
  72.         } else {
  73.             return(GOT_ERR);
  74.         }
  75.     }
  76.     if(ctl.len >; 0) {
  77.         ret |= GOT_CTRL;
  78.     }
  79.     if(dat.len >; 0) {
  80.         ret |= GOT_DATA;
  81.     } return(ret);
  82. }

  83. /*&=&* verify that dl_primitive in ctl_area = prim *&=&*/
  84. static int check_ctrl(int prim)
  85. {
  86.     dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
  87.     if(err_ack->;dl_primitive != prim) {
  88.         return GOT_ERR;
  89.     } return 0;
  90. }

  91. /*&=&* put a control message on a stream *&=&*/
  92. static int put_ctrl(int fd, int len, int pri)
  93. {
  94.     ctl.len = len;
  95.     if(putmsg(fd, &ctl, 0, pri) < 0) {
  96.         return GOT_ERR;
  97.     } return  0;
  98. }

  99. /*&=&* put a control + data message on a stream *&=&*/
  100. static int put_both(int fd, int clen, int dlen, int pri)
  101. {
  102.     ctl.len = clen;
  103.     dat.len = dlen;
  104.     if(putmsg(fd, &ctl, &dat, pri) < 0) {
  105.         return GOT_ERR;
  106.     } return  0;
  107. }

  108. /*&=&* open file descriptor and attach *&=&*/
  109. static int dl_open(const char *dev, int ppa, int *fd)
  110. {
  111.     dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
  112.     if((*fd = open(dev, O_RDWR)) == -1) {
  113.         return GOT_ERR;
  114.     }
  115.     attach_req->;dl_primitive = DL_ATTACH_REQ;
  116.     attach_req->;dl_ppa = ppa;
  117.     put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
  118.     get_msg(*fd);
  119.     return check_ctrl(DL_OK_ACK);
  120. }

  121. /*&=&* send DL_BIND_REQ *&=&*/
  122. static int dl_bind(int fd, int sap, u_char *addr)
  123. {
  124.     dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
  125.     dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
  126.     bind_req->;dl_primitive = DL_BIND_REQ;
  127.     bind_req->;dl_sap = sap;
  128.     bind_req->;dl_max_conind = 1;
  129.     bind_req->;dl_service_mode = DL_CLDLS;
  130.     bind_req->;dl_conn_mgmt = 0;
  131.     bind_req->;dl_xidtest_flg = 0;
  132.     put_ctrl(fd, sizeof(dl_bind_req_t), 0);
  133.     get_msg(fd);
  134.     if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
  135.         return GOT_ERR;
  136.     }
  137.     bcopy((u_char *)bind_ack + bind_ack->;dl_addr_offset, addr,
  138.         bind_ack->;dl_addr_length);
  139.     return 0;
  140. }

  141. /**********************************************************************/
  142. /*
  143. * interface:
  144. * function mac_addr_dlpi - get the mac address of the "first" interface
  145. *
  146. * parameter: addr: an array of six bytes, has to be allocated by the
  147. caller
  148. *
  149. * return: 0 if OK, -1 if the address could not be determined
  150. *
  151. */

  152. long mac_addr_dlpi ( u_char  *addr)
  153. {
  154.     int fd;
  155.     int ppa;
  156.     u_char mac_addr[25];
  157.     int i;

  158.     char **dev;

  159.     for (dev = dlpi_dev; **dev != '\0'; ++dev) {
  160.         for (ppa=0; ppa<10; ++ppa) {
  161.             if (GOT_ERR != dl_open(*dev, ppa, &fd)) {
  162.                 if (GOT_ERR != dl_bind(fd, INSAP, mac_addr)) {
  163.                     bcopy( mac_addr, addr, 6);
  164.                     return 0;
  165.                 }
  166.             }
  167.             close(fd);
  168.         }
  169.     } return -1;
  170. }


  171. /**********************************************************************/
  172. /*
  173. * Main (only for testing)
  174. */
  175. #ifdef MAIN
  176. int main( int argc, char **argv)
  177. {
  178.     long stat;
  179.     int i;
  180.     u_char addr[6];

  181.     stat = mac_addr_dlpi( addr);
  182.     if (0 == stat) {
  183.         printf( "MAC address = ");
  184.         for (i=0; i<6; ++i) {
  185.             printf("%2.2x", addr[i]);
  186.         }
  187.         printf( "\n");
  188.     }
  189.     else {
  190.         fprintf( stderr, "can't get MAC address\n");
  191.         exit( 1);
  192.     } return 0;
  193. }
  194. #endif
复制代码


晕,不是我置的顶吧?误操作?
作者: 无双    时间: 2003-06-07 14:48
标题: 获取本机ip和网卡mac等信息的代码
很实用
加精华
作者: jackyzhou    时间: 2003-06-09 17:56
标题: 获取本机ip和网卡mac等信息的代码
为什么不用shell 呢?
ifconfig -a
set - -
Ip=$..............
MAC=$..........
这样不就很简单了吗?
作者: 蓝色键盘    时间: 2003-06-09 17:58
标题: 获取本机ip和网卡mac等信息的代码
有些人要用程序实现。

感谢gadfly的热情和努力吧。
作者: jerrypan    时间: 2003-06-10 10:16
标题: 获取本机ip和网卡mac等信息的代码
收藏先。。。。

多谢
作者: angel518    时间: 2003-06-10 11:23
标题: 获取本机ip和网卡mac等信息的代码
save
作者: pillow    时间: 2003-06-11 10:29
标题: 获取本机ip和网卡mac等信息的代码
谢谢……
作者: shangxd    时间: 2003-06-26 13:55
标题: 获取本机ip和网卡mac等信息的代码
可是怎么才能获得子网掩码呢?
作者: shangxd    时间: 2003-06-26 16:05
标题: 获取本机ip和网卡mac等信息的代码
我知道了,要知道子网掩码使用下面的ioctl
ioctl (rec, SIOCGIFNETMASK, &if_data)
作者: breadso    时间: 2003-07-05 12:08
标题: 获取本机ip和网卡mac等信息的代码
对于您的乐于助人首先表示感谢!

看了大作中提到的两种查询mac,ip地址等信息的例子代码,现在有以下问题还不太明白,希望不吝赐教:

1。程序中有如下的定义:#define MAXINTERFACES   16
用在这个地方:     
struct ifreq buf[MAXINTERFACES];
经过试验,如果实际的网卡(或接口)数字大于,16的话,那么ioctl会失败!
不知道,定义16 是为什么?
顺便想问问,Solrias,HP,linux,所能支持的最大的网卡个数是多少?
作者: shangxd    时间: 2003-07-05 21:17
标题: 获取本机ip和网卡mac等信息的代码
16应该是自己定义的吧,一般接口数不大会超过这个数目。
要知道LINUX最多支持多少个网卡的话可能要看内核的代码了。
作者: gadfly    时间: 2003-07-05 21:20
标题: 获取本机ip和网卡mac等信息的代码
我在solaris和linux上把MAXINTERFACES改成255都没有问题呀?

你什么环境?怎么改的?另外用perror打印出错误信息看看
作者: breadso    时间: 2003-07-05 21:24
标题: 获取本机ip和网卡mac等信息的代码
如果是作者自己定义的,那么这个程序会ioctl执行失败的可能!

我很想知道的是在,HP,SunOS,中,最多可以设多少个网卡?
每个网卡可以设多少个IP地址。
作者: gadfly    时间: 2003-07-05 21:31
标题: 获取本机ip和网卡mac等信息的代码
没看到我前面的回复?
作者: breadso    时间: 2003-07-05 21:42
标题: 获取本机ip和网卡mac等信息的代码
对不起,刚看到:)

现在这个问题我觉得十分的棘手!!

1。你把MAXINTERFACES设的足够大,当然不会有问题,
现在问题时,如果你的机器有17个以上的地址,那么在设MAXINTERFACES=16, 就会出问题:

在HP,SUN,上面,ioctl 都会返回-1,也就是出错。
所以,我想知道一个足够大的极限值呀?
或者有什么方法可以获得??

还有一个问题:对于获取ip地址,是否,一定要检查网卡的状态,是否为UP呢?
作者: gadfly    时间: 2003-07-05 22:03
标题: 获取本机ip和网卡mac等信息的代码
是这样子的,我刚看了ifconfig的处理方式,它是用试探的方法,也就是说它的buf是动态的。如果发现这个buf都满了,就再扩展buf的大小。上面的代码你可以自己改成动态分配的,或者通过参数输入。

取ip不需要判断状态,我这只是个示例,其实interface还有其它的一些属性。另外如果interface是down的话,这个代码的网卡信息是取不到的,
你自己可以试试看。
作者: breadso    时间: 2003-07-05 22:13
标题: 获取本机ip和网卡mac等信息的代码
我现在就是不知道,怎么去探测呀?(HP,Sun)

是根据ioctl的错误返回值吗?
在HP,Sun,中,如果分配的内存不足,那么errno = 22,是说参数出错,
但是这个信息好像不足以说明是否是因为,内存分配的不足,

**不知道您认为该如何去判断,继续探测的条件??

对了,Sun 2.6之前每个网卡最多支持256个地址,以后嘛,就可任意多了。
但是HP是什么情况呢?

还想问一下:调用 ioclt  用:SIOCGIFFLAGS,这个参数是干嘛用的?,(linux支持)
作者: gadfly    时间: 2003-07-05 22:26
标题: 获取本机ip和网卡mac等信息的代码
在linux中,是ioctl去取,如果发现返回的len是等于buf的len,就继续加大这个buf的len。

如果sun和hp是这种情况,你也完全可以根据这个errno,循环的增大到一定数量,可以设个最大的上限。

呵呵,由于我这没有这么多的interface,所以说这种情况,我也没法试。
这个是取网卡的一些状态属性的,状态信息很多,如:是否是混杂模式,
是否up, 是否接受广播包,是否支持多播等等。我上面的例子,只取了最常用的几种信息。这些在solaris上也支持的呀。
作者: breadso    时间: 2003-07-05 22:34
标题: 获取本机ip和网卡mac等信息的代码
SUN,HP:
情况与linux有区别,
errno时说有不正确的参数,

也就是说如果,你内存分配足够,但是其他的参数出错,也是不行的。

项测试的话很容易,你把你设置的maxifnum设小点,自己在加几个地址,就可以了。
作者: yanyf    时间: 2003-07-10 15:36
标题: 获取本机ip和网卡mac等信息的代码
为什么在FREEBSD4.5上编译第一段代码,遇到如下错误(在LINUX可以):
nic.c:57: warning: passing arg 1 of `puts' makes pointer from integer without a cast
nic.c:91: `SIOCGIFHWADDR' undeclared (first use in this function)
nic.c:91: (Each undeclared identifier is reported only once
nic.c:91: for each function it appears in.)
nic.c:96: structure has no member named `ifr_hwaddr'
nic.c:97: structure has no member named `ifr_hwaddr'
nic.c:98: structure has no member named `ifr_hwaddr'
nic.c:99: structure has no member named `ifr_hwaddr'
nic.c:100: structure has no member named `ifr_hwaddr'
nic.c:101: structure has no member named `ifr_hwaddr'

主要是在执行ioctl获取硬件地址时,不认识SIOCGIFHWADDR,是不是有头文件没包含?
作者: gadfly    时间: 2003-07-11 13:49
标题: 获取本机ip和网卡mac等信息的代码
估计freebsd不支持这个ioctl的选项。

试试solaris上的代码吧
作者: allenli007    时间: 2003-09-19 14:51
标题: 获取本机ip和网卡mac等信息的代码
好文
支持
作者: hongfengyue    时间: 2006-07-05 17:21
Mark
谢谢!
作者: h0tr0ck    时间: 2006-07-05 20:21
谢谢




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2