c mac生成器(自动增长)

最近公司需要,写一个mac生成器,自己也上网了解了一下,mac地址原则上要求唯一,对于同一网段来说mac地址唯一比较重要,不然会冲突;另外,同一种通信产品最好mac地址也要唯一,就这样,贴上我的代码。

#include "stdio.h"
#include "string.h"
#include <stdlib.h>
#include <direct.h> 
typedef unsigned char u_char;
#define ERROR_INPUT_NULL_PTR (1)
#define ERROR_INPUT_OUT_RANGE (2)
#define TRUE 1
#define FALSE 0
#define TEST_STARTMAC 1
#define TEST_LEAPYEAR 0
#define TEST_ISPRIME 0
#define MAX_NUM ((256)*(256)*(256))


static unsigned char startMac[10][6] = {
    {0xAA, 0xBB, 0xCC, 0x00, 0x00, 0x01},
    {0xAA, 0xBB, 0xCC, 0x00, 0x00, 0x01},
    {0xAA, 0xBB, 0xCC, 0x00, 0x00, 0xF0}, 
    {0xAA, 0xBB, 0xCC, 0x00, 0xFF, 0xFF}, 
    {0xAA, 0xBB, 0xCC, 0xFE, 0xFF, 0xFF}, 
    {0xAA, 0xBB, 0xCC, 0xFE, 0xFF, 0xFF}, 
    {0xAA, 0xBB, 0xCC, 0xFE, 0xFF, 0xFF}, 
    {0xAA, 0xBB, 0xCC, 0xFE, 0xFF, 0xFF}, 
    {0xAA, 0xBB, 0xCC, 0x00, 0x00, 0xFF}, 
    {0xAA, 0xBB, 0xCC, 0xFE, 0xFF, 0xFF}};
    
int ltStrToLower(char *str){
	int len = strlen(str);
	int i = 0;
	for(i == 0;i < len ;i++){
		if(str[i] >=65 && str[i] <= 90){
			str[i] = str[i] - 32;
		}
	}
	return 0;
}

/* 将可显示的Mac地址转换为内部的Mac地址  */
int nasCvtMacI(unsigned char *caMacStr,unsigned char *caMac)
{
    register int i,j;
    int l;
    char caTemp[32];
    char caTemp1[6];
    char *p;
    char *p1;
    strcpy(caTemp,(char *)caMacStr);
    i = 0;
    ltStrToLower(caTemp);
    
    j = 0;
    memset(caMac,0,6);
    memset(caTemp1,0,6);
    
    p1=strstr(caTemp,":");
    if(p1){
    	  p = caTemp;
		    while(caTemp[i]!=0 && j < 6){
		        if(caTemp[i]== ':') {
		            caTemp[i]=0;
		            sscanf(p,"%x",&l);
		            if(l<0 || l > 255) {
		                return (-1);
		            }
		            caMac[j] = (unsigned char)l;
		            p = &caTemp[i+1];
		            i++;
		            j++;
		        }
		        else {
		            i++;
		        }
		    }
		    if(j != 5) {
		        return (-1);
		    }else {
		        sscanf(p,"%x",&l);
		        if(l<0 || l > 255) {
		            return (-1);
		        }
		        caMac[j] = (unsigned char)l;
		    } 
		}else{
		  //aabbccddeeff
		     i=0;
		     while(caTemp[i]!=0 && j < 7){
		        if(i==1 || i==3 || i==5 || i==7 || i==9 || i==11  ) {
		            memset(caTemp1,0,6);
		            memcpy(caTemp1,&caTemp[i-1],2);
		            p = caTemp1;
		            sscanf(p,"%x",&l);
		            if(l<0 || l > 255) {
		                return (-1);
		            }
		            caMac[j] = (unsigned char)l;
		            i++;
		            j++;
		        }
		        else {
		            i++;
		        }
		    }

	  }       
    return 0;
}

/*get current dir*/
char* getcurrentdir( char * newPath){
	newPath = getcwd( NULL, 0 );  
    printf("new work path: %s\n", newPath); 
    return newPath;
}

int  getMaxMac(unsigned char *caMac){
	FILE *fp;
	char aa[256];
	char mac[32];
	getcurrentdir(aa);
	chdir(aa);
	if(fp=fopen("mac.txt","r")){
       printf("open success\n");
       fgets(mac,32,fp); 
       if(strlen(mac)<17){
       	   printf("MaxMac not exist\n");
       	   	caMac[0]=startMac[0][0];
        	caMac[1]=startMac[0][1];
        	caMac[2]=startMac[0][2];
        	caMac[3]=startMac[0][3];
        	caMac[4]=startMac[0][4];
        	caMac[5]=startMac[0][5];
       }else{
       	   nasCvtMacI((unsigned char *)mac,caMac);
       	   printf("set Max Mac %s\n",mac);
       }
    }else{
  	   printf("open error\n");
  	   fclose(fp);
  	   return -1;
    }
      
    fclose(fp);
    return 0;
}

/*设置最大的mac地址*/
int setMaxMac(char * mac){
	FILE *fp;
	char aa[256];
	getcurrentdir(aa);
	chdir(aa);
	if(fp=fopen("mac.txt","w+")){
       printf("open success\n");
       fputs(mac,fp);
    }else{
  	   printf("open error\n");
  	   fclose(fp);
  	   return -1;
    }
    fclose(fp);
    return 0;
}

/* auto print mac */
int getMac(int step, int count)
{
	unsigned char MacMac[6];
	getMaxMac(MacMac);
    u_char mac[6] = {0};
    unsigned int myStep = 0;
    unsigned int total = 0;
    unsigned int index = 0;
    unsigned int tmp = 0;
    
    if ((step < 0 || step > 0xFF) || count <= 0)
    {
        return ERROR_INPUT_OUT_RANGE;
    }
    myStep = step + 1; /* 题目中Step=0表示连续,程序中表示连续*/ total = myStep * count;
    if (total > MAX_NUM || total < 0)
    {
        return ERROR_INPUT_OUT_RANGE;
    }
    if (total == 0)
    {
        printf(" %02x:%02x:%02x:%02x:%02x:%02x\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        return 0;
    }
    memcpy(mac, MacMac, 6);
    while (index < count)
    {
        printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        tmp = mac[5] + myStep;
        if (tmp > 0xFF) /* 达到char的上限,需要进位*/
        {
            mac[5] = tmp & 0xFF;
            if (mac[4] >= 0xFF)
            {
            mac[3]++;
            }
            mac[4]++;
        }
        else
        {
            mac[5] += myStep;
            index ++;
        }
    }
    sprintf((char *)MacMac,"%02x:%02x:%02x:%02x:%02x:%02x",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    setMaxMac((char *)MacMac);
    return 0;
}




/* main founction*/
int main(int argc, char* argv[]){
	
    
    int step[10] = {0, 0, 1, 2, 0xFE, 0xFF, 0xFF, 256, 255, 255}; 
    int count[10] = {0, 5, 5, 10, 10, 20, 0xFF, 10, 256*256, 256*257};
    int ret = 0; 
    int index;
    
    if(argc != 3){
    	printf("please enter ***.exe  1  1\n");
    	return 0; 
    } 
	getMac(atoi(argv[1]),atoi(argv[2]));
	//ret = getMac(startMac[0], 0, 20);
/*
    for (index = 2; index < 9; index ++) { 
        ret = getMac(startMac[index], step[index], count[index]);
        if (0 != ret) { 
		     if (ERROR_INPUT_NULL_PTR == ret) { 
                printf("\r\n Input startMac is NULL!\r\n"); } 
       	     else if (ERROR_INPUT_OUT_RANGE == ret) {
       	        printf("\r\n Input step or count is out of range!\r\n"); } 
         } 
         printf("\r\n");
	}*/
    return 0;
}

最终代码,可以在同一路径下面生成一个保存最大mac值得文本,并且使用程序时,参数1代表间隔,一般为0,参数2代表生成的mac数目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值