You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sa...@apache.org on 2004/11/19 11:44:55 UTC

cvs commit: ws-axis/contrib/wsa4c/utils UUIDGen.hpp UUIDGen.cpp MacAddress.hpp MacAddress.cpp

sanjaya     2004/11/19 02:44:55

  Added:       contrib/wsa4c/utils UUIDGen.hpp UUIDGen.cpp MacAddress.hpp
                        MacAddress.cpp
  Log:
  adding UUID generating files
  
  Revision  Changes    Path
  1.1                  ws-axis/contrib/wsa4c/utils/UUIDGen.hpp
  
  Index: UUIDGen.hpp
  ===================================================================
  /*
  
   * Copyright 2001-2004 The Apache Software Foundation.
  
   * 
  
   * Licensed under the Apache License, Version 2.0 (the "License");
  
   * you may not use this file except in compliance with the License.
  
   * You may obtain a copy of the License at
  
   * 
  
   *      http://www.apache.org/licenses/LICENSE-2.0
  
   * 
  
   * Unless required by applicable law or agreed to in writing, software
  
   * distributed under the License is distributed on an "AS IS" BASIS,
  
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  
   * See the License for the specific language governing permissions and
  
   * limitations under the License.
  
   */
  //#include<iostream.h>
  #include<string.h>
  #include<time.h>
  #include<stdlib.h>
  #include<malloc.h>
  #include <conio.h>
  
  const unsigned int COUNT_START = -12219292800000;  // 15 October 1582
  const unsigned int ULONG_MAX = 4294967295;
  const unsigned int MAX_RAND = 16384;
  const char * pcHexaChars[16] = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};   
  
  class UUIDGen{
  public:
      UUIDGen();
      static char * nextUUID();
  private:
      static int hexToint(char cHex);    
      int nextInt(int iNumber);
      int next(int iBits);
      int iClockSequence;      
      
  };
  
  
  1.1                  ws-axis/contrib/wsa4c/utils/UUIDGen.cpp
  
  Index: UUIDGen.cpp
  ===================================================================
  /*
  
   * Copyright 2001-2004 The Apache Software Foundation.
  
   * 
  
   * Licensed under the Apache License, Version 2.0 (the "License");
  
   * you may not use this file except in compliance with the License.
  
   * You may obtain a copy of the License at
  
   * 
  
   *      http://www.apache.org/licenses/LICENSE-2.0
  
   * 
  
   * Unless required by applicable law or agreed to in writing, software
  
   * distributed under the License is distributed on an "AS IS" BASIS,
  
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  
   * See the License for the specific language governing permissions and
  
   * limitations under the License.
  
   */
  
  #include "UUIDGen.hpp"
  #include <iostream.h>
  #include "MacAddress.hpp"
  
  UUIDGen::UUIDGen()
  {
      iClockSequence = nextInt(MAX_RAND);
  }
  
  int UUIDGen::hexToint(char cHex)
  {
      switch(cHex)
      {
      case '0':
          return 0;
      case '1':
          return 1;
      case '2':
          return 2;
      case '3':
          return 3;
      case '4':
          return 4;
      case '5':
          return 5;
      case '6':
          return 6;
      case '7':
          return 7;
      case '8':
          return 8;
      case '9':
          return 9;
      case 'A':
          return 10;
      case 'B':
          return 11;
      case 'C':
          return 12;
      case 'D':
          return 13;
      case 'E':
          return 14;
      case 'F':
          return 15;
      }
  }  
  char * UUIDGen::nextUUID()
  {
     unsigned long ulCount;
  
     // the number of milliseconds since 1 January 1970
     time_t timeValue;
     time(&timeValue);
     unsigned long ulCurrentTime = timeValue;    
  
     // the number of milliseconds since 15 October 1582
     unsigned long ulCountMillis = ulCurrentTime + COUNT_START;
  
     // the result
     ulCount = ulCountMillis * 10000;
     
     char * pcHexString =(char*) malloc(32);
     strcpy(pcHexString,"");
     for (int i=30; i>=0; i--) 
     {
         if(i<16)
         {
  	       strcat(pcHexString,pcHexaChars[((ulCount >> i*4) & 0xF)]);
  		   //cout<<"0123456789ABCDEF"[((ulCount >> i*4) & 0xF)];
         }
         else
         {
             //left padding with zero
             strcat(pcHexString,"0");
             //cout<<"0";
         }
      }
  			 
      // the time_low field
  	char * pcTimeLow = (char*)malloc(9);
  	char * p = pcHexString;
      for (unsigned int j = 0; j <strlen(pcHexString)-8; j++)
          p++;
  	strcpy(pcTimeLow,p);	
  	*p='\0';
  	
  	// the time_mid field
  	char * pcTimeMid = (char*)malloc(5);
  	p= pcHexString;
      for (j = 0; j <strlen(pcHexString)-4; j++)
          p++;
  	strcpy(pcTimeMid,p);
  	*p='\0';
  
      // the time hi field multiplexed with the version
  	char * pcTimeHiAndVersion =(char*)malloc(5);
      p = pcHexString;
      for (j=0; j <strlen(pcHexString)-4; j++)
          p++;
      strcpy(pcTimeHiAndVersion,p);
      *pcTimeHiAndVersion = '1';
      *p='\0';        
  		
      // the clock_seq_low field
      char * pcClockSeqLow = (char*) malloc(3);
      // the clock_seq_hi_and_reserved field
      char * pcClockSeqHiAndReserved = (char*)malloc(3);
      strcpy(pcClockSeqLow,"");
      strcpy(pcClockSeqHiAndReserved,"");
      for (i=3; i>=0; i--) {   
          if(i>1)
  			strcat(pcClockSeqLow,pcHexaChars[((ulCount >> i*4) & 0xF)]);
          else
              strcat(pcClockSeqHiAndReserved,pcHexaChars[((ulCount >> i*4) & 0xF)]);
      }
     
      int * piClockSeqHi = new int(hexToint(*pcClockSeqLow));
      *piClockSeqHi = *piClockSeqHi & 0x3F;
      *piClockSeqHi = *piClockSeqHi | 0xB0;
              
      pcClockSeqHiAndReserved = (char*) malloc(3);
      strcpy(pcClockSeqHiAndReserved,"");
      for (i=1; i>=0; i--) 
      {   
  		strcat(pcClockSeqHiAndReserved,pcHexaChars[((*piClockSeqHi >> i*4) & 0xF)]);
      }
          
      char * pcMacAddress = (char*)malloc(13);
      MacAddress *pMacAddress = new MacAddress;
      pMacAddress->findMacAddress(pcMacAddress);
      
      char * pcUUIDString = (char*)malloc(37);
      strcpy(pcUUIDString,pcTimeLow);
      strcat(pcUUIDString,"-");
      strcat(pcUUIDString,pcTimeMid);
      strcat(pcUUIDString,"-");
      strcat(pcUUIDString,pcTimeHiAndVersion);
      strcat(pcUUIDString,"-");
      strcat(pcUUIDString,pcClockSeqHiAndReserved);
      strcat(pcUUIDString,pcClockSeqLow);
      strcat(pcUUIDString,"-");
      strcat(pcUUIDString,strdup(pcMacAddress));
  
      free(pcHexString);
      free(pcTimeLow);
      free(pcTimeMid);
      free(pcTimeHiAndVersion);
      free(pcClockSeqHiAndReserved);
      free(pcClockSeqLow);
      free(pcMacAddress);
      delete(pMacAddress);
      delete(piClockSeqHi);
      
  
      return pcUUIDString;
  
  } 
  
  int UUIDGen::nextInt(int iNumber)
  {
      if ((iNumber & -iNumber) == iNumber)  // i.e., n is a power of 2
           return (int)((iNumber * (long)next(31)) >> 31);
  
       int bits, val;
       do {
           bits = next(31);
           val = bits % iNumber;
       } while((bits - val + (iNumber-1) < 0) & val>0);
       return val;
  }
  
  int UUIDGen::next(int iBits)
  {
       time_t timeValue;
  	 time(&timeValue);      
       unsigned int seed = timeValue;
       seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
       return (int)(seed >> (48 - iBits));
  }
   
  
  
  1.1                  ws-axis/contrib/wsa4c/utils/MacAddress.hpp
  
  Index: MacAddress.hpp
  ===================================================================
  /*
  
   * Copyright 2001-2004 The Apache Software Foundation.
  
   * 
  
   * Licensed under the Apache License, Version 2.0 (the "License");
  
   * you may not use this file except in compliance with the License.
  
   * You may obtain a copy of the License at
  
   * 
  
   *      http://www.apache.org/licenses/LICENSE-2.0
  
   * 
  
   * Unless required by applicable law or agreed to in writing, software
  
   * distributed under the License is distributed on an "AS IS" BASIS,
  
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  
   * See the License for the specific language governing permissions and
  
   * limitations under the License.
  
   */
  
  #ifdef WIN32
  
  #include <snmp.h>
  #include <conio.h>
  #include <stdio.h>
  
  typedef BOOL(WINAPI * pSnmpExtensionInit) (
          IN DWORD dwTimeZeroReference,
          OUT HANDLE * hPollForTrapEvent,
          OUT AsnObjectIdentifier * supportedView);
  
  typedef BOOL(WINAPI * pSnmpExtensionTrap) (
          OUT AsnObjectIdentifier * enterprise,
          OUT AsnInteger * genericTrap,
          OUT AsnInteger * specificTrap,
          OUT AsnTimeticks * timeStamp,
          OUT RFC1157VarBindList * variableBindings);
  
  typedef BOOL(WINAPI * pSnmpExtensionQuery) (
          IN BYTE requestType,
          IN OUT RFC1157VarBindList * variableBindings,
          OUT AsnInteger * errorStatus,
          OUT AsnInteger * errorIndex);
  
  typedef BOOL(WINAPI * pSnmpExtensionInitEx) (
          OUT AsnObjectIdentifier * supportedView);
  #endif
  
  class MacAddress{
  
  public:
      static void findMacAddress(char * pachMacAddress);
  };
  
  
  1.1                  ws-axis/contrib/wsa4c/utils/MacAddress.cpp
  
  Index: MacAddress.cpp
  ===================================================================
  /*
  
   * Copyright 2001-2004 The Apache Software Foundation.
  
   * 
  
   * Licensed under the Apache License, Version 2.0 (the "License");
  
   * you may not use this file except in compliance with the License.
  
   * You may obtain a copy of the License at
  
   * 
  
   *      http://www.apache.org/licenses/LICENSE-2.0
  
   * 
  
   * Unless required by applicable law or agreed to in writing, software
  
   * distributed under the License is distributed on an "AS IS" BASIS,
  
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  
   * See the License for the specific language governing permissions and
  
   * limitations under the License.
  
   */
  #include "MacAddress.hpp"
  
  void MacAddress::findMacAddress(char * pachMacAddress)
  {
    HINSTANCE m_hInst;
    pSnmpExtensionInit m_Init;
    pSnmpExtensionInitEx m_InitEx;
    pSnmpExtensionQuery m_Query;
    pSnmpExtensionTrap m_Trap;
    HANDLE PollForTrapEvent;
    AsnObjectIdentifier SupportedView;
    UINT OID_ifEntryType[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 3};
    UINT OID_ifEntryNum[] = {1, 3, 6, 1, 2, 1, 2, 1};
    UINT OID_ipMACEntAddr[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 6};
    AsnObjectIdentifier MIB_ifMACEntAddr =
      { sizeof(OID_ipMACEntAddr) / sizeof(UINT), OID_ipMACEntAddr };
    AsnObjectIdentifier MIB_ifEntryType =
      {sizeof(OID_ifEntryType) / sizeof(UINT), OID_ifEntryType};
    AsnObjectIdentifier MIB_ifEntryNum =
      {sizeof(OID_ifEntryNum) / sizeof(UINT), OID_ifEntryNum};
    RFC1157VarBindList varBindList;
    RFC1157VarBind varBind[2];
    AsnInteger errorStatus;
    AsnInteger errorIndex;
    AsnObjectIdentifier MIB_NULL = {0, 0};
    int ret;
    int dtmp;
    int i = 0, j = 0;
    bool found = false;
   // char TempEthernet[13];
    m_Init = NULL;
    m_InitEx = NULL;
    m_Query = NULL;
    m_Trap = NULL;
  
    /* Load the SNMP dll and get the addresses of the functions
       necessary */
    m_hInst = LoadLibrary("inetmib1.dll");
    if (m_hInst < (HINSTANCE) HINSTANCE_ERROR)
    {
      m_hInst = NULL;
      return;
    }
    m_Init =
      (pSnmpExtensionInit) GetProcAddress(m_hInst, "SnmpExtensionInit");
    m_InitEx =
      (pSnmpExtensionInitEx) GetProcAddress(m_hInst,
                                            "SnmpExtensionInitEx");
    m_Query =
      (pSnmpExtensionQuery) GetProcAddress(m_hInst,
                                           "SnmpExtensionQuery");
    m_Trap =
      (pSnmpExtensionTrap) GetProcAddress(m_hInst, "SnmpExtensionTrap");
    m_Init(GetTickCount(), &PollForTrapEvent, &SupportedView);
  
    /* Initialize the variable list to be retrieved by m_Query */
    varBindList.list = varBind;
    varBind[0].name = MIB_NULL;
    varBind[1].name = MIB_NULL;
  
    /* Copy in the OID to find the number of entries in the
       Inteface table */
    varBindList.len = 1;        /* Only retrieving one item */
    SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryNum);
    ret =
      m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
              &errorIndex);
    //printf("# of adapters in this system : %in",
        // varBind[0].value.asnValue.number);
    varBindList.len = 2;
  
    /* Copy in the OID of ifType, the type of interface */
    SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryType);
  
    /* Copy in the OID of ifPhysAddress, the address */
    SNMP_oidcpy(&varBind[1].name, &MIB_ifMACEntAddr);
  
    do
    {
  
      /* Submit the query.  Responses will be loaded into varBindList.
         We can expect this call to succeed a # of times corresponding
         to the # of adapters reported to be in the system */
      ret =
        m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
                &errorIndex);
      if (!ret)
        ret = 1;
      else
          /* Confirm that the proper type has been returned */
        ret =
            SNMP_oidncmp(&varBind[0].name, &MIB_ifEntryType,
                         MIB_ifEntryType.idLength); if (!ret) {
      j++;
      dtmp = varBind[0].value.asnValue.number;
      //printf("Interface #%i type : %in", j, dtmp);
  
      /* Type 6 describes ethernet interfaces */
      if (dtmp == 6)
      {
  
        /* Confirm that we have an address here */
        ret =
            SNMP_oidncmp(&varBind[1].name, &MIB_ifMACEntAddr,
                         MIB_ifMACEntAddr.idLength);
        if ((!ret) && (varBind[1].value.asnValue.address.stream != NULL))
        {
          if((varBind[1].value.asnValue.address.stream[0] == 0x44)
            && (varBind[1].value.asnValue.address.stream[1] == 0x45)
            && (varBind[1].value.asnValue.address.stream[2] == 0x53)
            && (varBind[1].value.asnValue.address.stream[3] == 0x54)
            && (varBind[1].value.asnValue.address.stream[4] == 0x00))
          {
            /* Ignore all dial-up networking adapters */
            //printf("Interface #%i is a DUN adaptern", j);
            continue;
          }
          if ((varBind[1].value.asnValue.address.stream[0] == 0x00)
              && (varBind[1].value.asnValue.address.stream[1] == 0x00)
              && (varBind[1].value.asnValue.address.stream[2] == 0x00)
              && (varBind[1].value.asnValue.address.stream[3] == 0x00)
              && (varBind[1].value.asnValue.address.stream[4] == 0x00)
              && (varBind[1].value.asnValue.address.stream[5] == 0x00))
          {
            /* Ignore NULL addresses returned by other network
               interfaces */
            //printf("Interface #%i is a NULL addressn", j);
            continue;
          }
          sprintf(pachMacAddress, "%02x%02x%02x%02x%02x%02x",
                  varBind[1].value.asnValue.address.stream[0],
                  varBind[1].value.asnValue.address.stream[1],
                  varBind[1].value.asnValue.address.stream[2],
                  varBind[1].value.asnValue.address.stream[3],
                  varBind[1].value.asnValue.address.stream[4],
                  varBind[1].value.asnValue.address.stream[5]);
         // printf("MAC Address of interface #%i: %sn", j,
                // pachMacAddress);
          }
        }
      }
    } while (!ret);         /* Stop only on an error.  An error will occur
                               when we go exhaust the list of interfaces to
                               be examined */
    getch();
  
    FreeLibrary(m_hInst);
    /* Free the bindings */
    SNMP_FreeVarBind(&varBind[0]);
    SNMP_FreeVarBind(&varBind[1]);
  }