免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 13396 | 回复: 26
打印 上一主题 下一主题

获取本机ip和网卡mac等信息的代码 [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-06 17:02 |只看该作者 |倒序浏览
我以前贴过,好像被删了。老有人问。我就再贴了

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

  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. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 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. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 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. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 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
复制代码


晕,不是我置的顶吧?误操作?

论坛徽章:
0
5 [报告]
发表于 2003-06-07 14:48 |只看该作者

获取本机ip和网卡mac等信息的代码

很实用
加精华

论坛徽章:
0
6 [报告]
发表于 2003-06-09 17:56 |只看该作者

获取本机ip和网卡mac等信息的代码

为什么不用shell 呢?
ifconfig -a
set - -
Ip=$..............
MAC=$..........
这样不就很简单了吗?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-06-09 17:58 |只看该作者

获取本机ip和网卡mac等信息的代码

有些人要用程序实现。

感谢gadfly的热情和努力吧。

论坛徽章:
0
8 [报告]
发表于 2003-06-10 10:16 |只看该作者

获取本机ip和网卡mac等信息的代码

收藏先。。。。

多谢

论坛徽章:
0
9 [报告]
发表于 2003-06-10 11:23 |只看该作者

获取本机ip和网卡mac等信息的代码

save

论坛徽章:
0
10 [报告]
发表于 2003-06-11 10:29 |只看该作者

获取本机ip和网卡mac等信息的代码

谢谢……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP