as_uuid.cpp

changeset 1
d64aaa7d146f
child 3
c1941114ca88
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/as_uuid.cpp	Fri Nov 28 11:21:08 2008 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +//
     1.5 +//  OSSP asgui - Accounting system graphical user interface
     1.6 +//  Copyright (c) 2002-2004 The OSSP Project (http://www.ossp.org/)
     1.7 +//  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
     1.8 +//  Copyright (c) 2002-2004 Michael Schloh von Bennewitz <michael@schloh.com>
     1.9 +//  Copyright (c) 2002-2004 Cable & Wireless Telecommunications Services GmbH
    1.10 +//
    1.11 +//  This file is part of OSSP asgui, an accounting system graphical user
    1.12 +//  interface which can be found at http://www.ossp.org/pkg/tool/asgui/.
    1.13 +//
    1.14 +//  Permission to use, copy, modify, and distribute this software for
    1.15 +//  any purpose with or without fee is hereby granted, provided that
    1.16 +//  the above copyright notice and this permission notice appear in all
    1.17 +//  copies.
    1.18 +//
    1.19 +//  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    1.20 +//  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    1.21 +//  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.22 +//  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    1.23 +//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.24 +//  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.25 +//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    1.26 +//  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    1.27 +//  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.28 +//  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    1.29 +//  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.30 +//  SUCH DAMAGE.
    1.31 +//
    1.32 +//  as_uuid.cpp: ISO C++ implementation
    1.33 +//
    1.34 +
    1.35 +// System headers
    1.36 +#include <string>
    1.37 +#include <sys/socket.h>
    1.38 +
    1.39 +// Local headers
    1.40 +#include "as_uuid.h"
    1.41 +#include "as_rand.h"
    1.42 +
    1.43 +// All of these are for detecting
    1.44 +// the MAC address in setMac()
    1.45 +#ifdef HAVE_UNISTD_H
    1.46 +#include <unistd.h>
    1.47 +#endif // HAVE_UNISTD_H
    1.48 +#ifdef HAVE_SYS_SOCKIO_H
    1.49 +#include <sys/sockio.h>
    1.50 +#endif // HAVE_SYS_SOCKIO_H
    1.51 +#ifdef HAVE_NET_IF_H
    1.52 +#include <net/if.h>
    1.53 +#endif // HAVE_NET_IF_H
    1.54 +#ifdef HAVE_NETINET_IN_H
    1.55 +#include <netinet/in.h>
    1.56 +#endif // HAVE_NETINET_IN_H
    1.57 +
    1.58 +
    1.59 +namespace AS {
    1.60 +
    1.61 +//
    1.62 +// Generate a DCE standard UUID
    1.63 +//
    1.64 +void Uuid::genId(void)
    1.65 +{
    1.66 +    Rand Temprand;                  // For random numbers
    1.67 +    unsigned char szChardata[16];   // Intermediate data
    1.68 +
    1.69 +    // Generate random data and fill in our UUID member fields with it
    1.70 +    Temprand.genData(szChardata, sizeof(szChardata));
    1.71 +    setId(szChardata);
    1.72 +
    1.73 +    // Since we don't just want random data, lets take some clock sequences also
    1.74 +    this->clock_seq = (this->clock_seq & 0x3FFF) | 0x8000;
    1.75 +    this->time_hi_and_version = (this->time_hi_and_version & 0x0FFF) | 0x4000;
    1.76 +
    1.77 +    // In every case that we can, set the node ID to the real MAC address
    1.78 +    setMac(this->node);
    1.79 +
    1.80 +    setString(); // Set the human readable string
    1.81 +}
    1.82 +
    1.83 +//
    1.84 +// Helper method to set the UUID private member data
    1.85 +//
    1.86 +void Uuid::setId(const unsigned char *pkucData)
    1.87 +{
    1.88 +    const U8 *pOctet = pkucData;
    1.89 +    U32       Tmpdat;
    1.90 +
    1.91 +    Tmpdat = *pOctet++; // Tmpdat is our iterator
    1.92 +
    1.93 +    // Copy data chunks one octet at a time
    1.94 +    Tmpdat = (Tmpdat << 8) | *pOctet++;
    1.95 +    Tmpdat = (Tmpdat << 8) | *pOctet++;
    1.96 +    Tmpdat = (Tmpdat << 8) | *pOctet++;
    1.97 +    this->time_low = Tmpdat;
    1.98 +
    1.99 +    Tmpdat = *pOctet++;
   1.100 +    Tmpdat = (Tmpdat << 8) | *pOctet++;
   1.101 +    this->time_mid = Tmpdat;
   1.102 +
   1.103 +    Tmpdat = *pOctet++;
   1.104 +    Tmpdat = (Tmpdat << 8) | *pOctet++;
   1.105 +    this->time_hi_and_version = Tmpdat;
   1.106 +
   1.107 +    Tmpdat = *pOctet++;
   1.108 +    Tmpdat = (Tmpdat << 8) | *pOctet++;
   1.109 +    this->clock_seq = Tmpdat;
   1.110 +
   1.111 +    // Put in the MAC address
   1.112 +    memcpy(this->node, pOctet, 6);
   1.113 +}
   1.114 +
   1.115 +//
   1.116 +// Helper method to set up the MAC address node ID member data
   1.117 +//
   1.118 +int Uuid::setMac(unsigned char *pucNode)
   1.119 +{
   1.120 +    return 0;
   1.121 +}
   1.122 +
   1.123 +//
   1.124 +// Returns a formatted representation of a DCE standard UUID
   1.125 +//
   1.126 +std::string Uuid::getString(void)
   1.127 +{
   1.128 +    return m_Fmtstr;
   1.129 +}
   1.130 +
   1.131 +//
   1.132 +// Helper method to set the private formatted
   1.133 +// representation of a DCE standard UUID
   1.134 +//
   1.135 +void Uuid::setString(void)
   1.136 +{
   1.137 +    char szTemp[48];    // To perform intermediate manipulation
   1.138 +
   1.139 +    sprintf(szTemp, // The DCE standard UUID format
   1.140 +        "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
   1.141 +        this->time_low, this->time_mid, this->time_hi_and_version,
   1.142 +        this->clock_seq >> 8, this->clock_seq & 0xFF,
   1.143 +        node[0], node[1], node[2],
   1.144 +        node[3], node[4], node[5]);
   1.145 +
   1.146 +    m_Fmtstr = szTemp;  // Finally copy the temporary to our object
   1.147 +}
   1.148 +} // namespace AS

mercurial