c mac生成器(自動增長)

wzm10455發表於2014-11-23

最近公司需要,寫一個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數目。

相關文章