歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux資訊 >> 更多Linux

在SCO下編程獲取網卡MAC地址


  #include <stdio.h>
  #include <sys/types.h>
  #include <strings.h>
  #include <sys/stream.h>
  #include <sys/stropts.h>
  #include <sys/macstat.h>
  
  int main(int argc, char *argv[])
  {
  strUCt strioctl strioc;
  u_char ether_addr[6];
  int s;
  
  if (argc != 2) {
  fprintf(stderr, "usage: %s deviceFile\n", argv[0]);
  exit (1);
  }
  
  bzero(ether_addr, sizeof(ether_addr));
  
  if ((s = open(argv[1], 0)) < 0) {
  perror(argv[1]);
  exit(1);
  }
  strioc.ic_cmd = MACIOC_GETADDR;
  strioc.ic_timout = -1;
  strioc.ic_len = sizeof(ether_addr);
  strioc.ic_dp = (caddr_t) ether_addr;
  if (ioctl(s, I_STR, (char *)&strioc) < 0) {
  perror("I_STR: MACIOC_GETADDR");
  exit(1);
  }
  
  printf("%s is using address %02x:%02x:%02x:%02x:%02x:%02x\n",
  argv[1],
  ether_addr[0], ether_addr[1], ether_addr[2],
  ether_addr[3], ether_addr[4], ether_addr[5]
  );
  
  exit(0);
  }
  
  /********************************** end *********************************/
  
  注:
  執行方式如(假設編譯出的執行文件為getmac):
  ./getmac /dev/net0
  
  MDI(MAC Driver Interface)
  
  MACIOC_GETADDR
  向MDI driver發出請求從而獲取網卡當前MAC地址 (the current MAC address);
  
  MACIOC_GETRADDR
  向MDI driver發出請求從而獲取網卡廠商MAC地址(the factory MAC address);
  
  此程序適用於SCO OpenServer和UnixWare,在OpenServer 5.06上測試通過。
  
  我在嘗試使用MACIOC_GETRADDR時提示無效參數,我估計也許是與驅動中沒有提供相應的實現有關,因為我在偶然看到的注釋版權信息為
  /*
  * Copyright (C) The Santa Cruz Operation, 1993-1995.
  * This Module contains Proprietary Information of
  * The Santa Cruz Operation and should be treated
  * as Confidential.
  */
  的AT-2500TX驅動源碼中,相應部分只包含了MACIOC_GETADDR的實現,而沒有MACIOC_GETRADDR。




Copyright © Linux教程網 All Rights Reserved