动态修改mac地址

来源:互联网 发布:ai for mac 破解版 编辑:程序博客网 时间:2024/05/22 15:57

平台arm9200  vxworks

#include "vxWorks.h"
#include "stdio.h"
#include "netinet/in.h"
#include "net/if.h"
#include "netinet/if_ether.h"
#include "sys/ioctl.h"
#include "ioLib.h"
#include "inetLib.h"
#include "string.h"
#include "netinet/in_var.h"
#include "ipProto.h"
#include "end.h"
#include "private/muxLibP.h"

/*set mac */

int setmac()
{
 struct in_ifaddr* ia;
 for (ia = in_ifaddr; ia != 0; ia = ia->ia_next)
 {
  struct ifnet* ifp = ia->ia_ifa.ifa_ifp;
  if (ifp != 0)
  {
   int level;
   IP_DRV_CTRL *pDrvCtrl;
   END_OBJ *pEnd;
   unsigned char PhyAddr[10]={0x08,0x09,0x10,0x11,0x12,0x13};

   if(ifp->if_type != M2_ifType_ethernetCsmacd)
   {
    continue;
   }
   level = intLock();

   pDrvCtrl = (IP_DRV_CTRL *)ifp->pCookie;

   pEnd = PCOOKIE_TO_ENDOBJ(pDrvCtrl->pIpCookie);

   pEnd->pFuncTable->ioctl(pEnd, EIOCSADDR, PhyAddr); /*此函数中会将PhyAddr中的值写到arm9200的寄存器中,但是ifShow看到的mac地址还是旧的,因为ifShow取的                                                                                                        不是寄存器中的MAC,而是一个全局变量ifnet中的MAC*/
   //printf("%x:%x:%x:%x:%x:%x\n", PhyAddr[0], PhyAddr[1], PhyAddr[2], PhyAddr[3], PhyAddr[4], PhyAddr[5]);

   intUnlock(level);
   break;
  }
 }
}

/*get mac*/

int getmac()
{
 struct in_ifaddr* ia;
 for (ia = in_ifaddr; ia != 0; ia = ia->ia_next)
 {
  struct ifnet* ifp = ia->ia_ifa.ifa_ifp;
  if (ifp != 0)
  {
   int level;
   IP_DRV_CTRL *pDrvCtrl;
   END_OBJ *pEnd;
   unsigned char PhyAddr[10];

   if(ifp->if_type != M2_ifType_ethernetCsmacd)
   {
    continue;
   }
   level = intLock();

   pDrvCtrl = (IP_DRV_CTRL *)ifp->pCookie;

   pEnd = PCOOKIE_TO_ENDOBJ(pDrvCtrl->pIpCookie);

   pEnd->pFuncTable->ioctl(pEnd, EIOCGADDR, PhyAddr);
   printf("%x:%x:%x:%x:%x:%x\n", PhyAddr[0], PhyAddr[1], PhyAddr[2], PhyAddr[3], PhyAddr[4], PhyAddr[5]);

   intUnlock(level);
  }
 }
}

 

 

原创粉丝点击