head 1.12; access; symbols; locks; strict; comment @// @; 1.12 date 2004.05.27.21.48.41; author ms; state Exp; branches; next 1.11; 1.11 date 2003.11.20.11.31.20; author ms; state Exp; branches; next 1.10; 1.10 date 2003.02.04.19.39.44; author ms; state Exp; branches; next 1.9; 1.9 date 2003.01.24.19.44.40; author ms; state Exp; branches; next 1.8; 1.8 date 2003.01.22.20.44.40; author ms; state Exp; branches; next 1.7; 1.7 date 2003.01.22.18.59.46; author ms; state Exp; branches; next 1.6; 1.6 date 2003.01.22.11.37.22; author ms; state Exp; branches; next 1.5; 1.5 date 2003.01.21.14.41.15; author ms; state Exp; branches; next 1.4; 1.4 date 2003.01.17.17.22.58; author ms; state Exp; branches; next 1.3; 1.3 date 2003.01.16.11.51.19; author ms; state Exp; branches; next 1.2; 1.2 date 2002.12.03.11.54.03; author ms; state Exp; branches; next 1.1; 1.1 date 2002.12.03.10.01.56; author ms; state Exp; branches; next ; desc @@ 1.12 log @Modernize copyright text in source and graphic headers. @ text @// // OSSP asgui - Accounting system graphical user interface // Copyright (c) 2002-2004 The OSSP Project (http://www.ossp.org/) // Copyright (c) 2002-2004 Ralf S. Engelschall // Copyright (c) 2002-2004 Michael Schloh von Bennewitz // Copyright (c) 2002-2004 Cable & Wireless Telecommunications Services GmbH // // This file is part of OSSP asgui, an accounting system graphical user // interface which can be found at http://www.ossp.org/pkg/tool/asgui/. // // Permission to use, copy, modify, and distribute this software for // any purpose with or without fee is hereby granted, provided that // the above copyright notice and this permission notice appear in all // copies. // // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF // SUCH DAMAGE. // // as_uuid.cpp: ISO C++ implementation // // System headers #include #include // Local headers #include "as_uuid.h" #include "as_rand.h" // All of these are for detecting // the MAC address in setMac() #ifdef HAVE_UNISTD_H #include #endif // HAVE_UNISTD_H #ifdef HAVE_SYS_SOCKIO_H #include #endif // HAVE_SYS_SOCKIO_H #ifdef HAVE_NET_IF_H #include #endif // HAVE_NET_IF_H #ifdef HAVE_NETINET_IN_H #include #endif // HAVE_NETINET_IN_H namespace AS { // // Generate a DCE standard UUID // void Uuid::genId(void) { Rand Temprand; // For random numbers unsigned char szChardata[16]; // Intermediate data // Generate random data and fill in our UUID member fields with it Temprand.genData(szChardata, sizeof(szChardata)); setId(szChardata); // Since we don't just want random data, lets take some clock sequences also this->clock_seq = (this->clock_seq & 0x3FFF) | 0x8000; this->time_hi_and_version = (this->time_hi_and_version & 0x0FFF) | 0x4000; // In every case that we can, set the node ID to the real MAC address setMac(this->node); setString(); // Set the human readable string } // // Helper method to set the UUID private member data // void Uuid::setId(const unsigned char *pkucData) { const U8 *pOctet = pkucData; U32 Tmpdat; Tmpdat = *pOctet++; // Tmpdat is our iterator // Copy data chunks one octet at a time Tmpdat = (Tmpdat << 8) | *pOctet++; Tmpdat = (Tmpdat << 8) | *pOctet++; Tmpdat = (Tmpdat << 8) | *pOctet++; this->time_low = Tmpdat; Tmpdat = *pOctet++; Tmpdat = (Tmpdat << 8) | *pOctet++; this->time_mid = Tmpdat; Tmpdat = *pOctet++; Tmpdat = (Tmpdat << 8) | *pOctet++; this->time_hi_and_version = Tmpdat; Tmpdat = *pOctet++; Tmpdat = (Tmpdat << 8) | *pOctet++; this->clock_seq = Tmpdat; // Put in the MAC address memcpy(this->node, pOctet, 6); } // // Helper method to set up the MAC address node ID member data // int Uuid::setMac(unsigned char *pucNode) { return 0; } // // Returns a formatted representation of a DCE standard UUID // std::string Uuid::getString(void) { return m_Fmtstr; } // // Helper method to set the private formatted // representation of a DCE standard UUID // void Uuid::setString(void) { char szTemp[48]; // To perform intermediate manipulation sprintf(szTemp, // The DCE standard UUID format "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", this->time_low, this->time_mid, this->time_hi_and_version, this->clock_seq >> 8, this->clock_seq & 0xFF, node[0], node[1], node[2], node[3], node[4], node[5]); m_Fmtstr = szTemp; // Finally copy the temporary to our object } } // namespace AS @ 1.11 log @Handle the time consuming bureaucracy of changing a company name. @ text @d3 4 a6 4 // Copyright (c) 2002-2003 The OSSP Project (http://www.ossp.org/) // Copyright (c) 2002-2003 Ralf S. Engelschall // Copyright (c) 2002-2003 Michael Schloh von Bennewitz // Copyright (c) 2002-2003 Cable & Wireless Telecommunications Services GmbH @ 1.10 log @Replaced __u format datatypes in Uuid class with U from master header. @ text @a3 1 // Copyright (c) 2002-2003 Cable & Wireless Deutschland (http://www.cw.com/de/) d6 1 @ 1.9 log @Rename files, and do cleanups. @ text @d32 1 d36 1 d85 2 a86 2 const __u8 *pOctet = pkucData; __u32 Tmpdat; @ 1.8 log @Prepare for the battle of retreiving a MAC address from the OS. @ text @d29 1 a29 1 // as_uuidgen.cpp: ISO C++ implementation @ 1.7 log @Complete the UUID class for use in differentiating database entries. @ text @d33 1 d38 15 a66 1 setString(); // Uses field from internal data d71 5 d111 8 a122 2 // // Copy to outbound string and benefit from auto dealloc // auto_ptr szOut(new char[48]); @ 1.6 log @Bring to buildable status. @ text @d32 1 a32 16 /* * as_uuidgen.c --- generate a DCE-compatible uuid * * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o. * * This file may be redistributed under the terms of the GNU * Library General Public License. */ #include #include #include #include #include #include #include d35 1 a36 29 #ifdef HAVE_UNISTD_H #include #endif #ifdef HAVE_STDLIB_H #include #endif #ifdef TIME_WITH_SYS_TIME #include #endif /* TIME_WITH_SYS_TIME */ #ifdef HAVE_SYS_SOCKIO_H #include #endif /* HAVE_SYS_SOCKIO_H */ #ifdef HAVE_NET_IF_H #include #endif /* HAVE_NET_IF_H */ #ifdef HAVE_NETINET_IN_H #include #endif /* HAVE_NETINET_IN_H */ #ifndef max /* Define a general use max macro */ #define max(a,b) ((a) > (b) ? (a) : (b)) #endif /* max */ #ifdef HAVE_NET_IF_H /* Define the size of a interface address */ #define ifreq_size(i) max(sizeof(struct ifreq), sizeof((i).ifr_name)+(i).ifr_addr.sa_len) #endif /* HAVE_NET_IF_H */ d38 1 d40 4 a43 1 int Uuid::getRandfd(void) d45 2 a46 17 struct timeval tv; static int fd = -2; int i; if (fd == -2) { gettimeofday(&tv, 0); fd = open("/dev/urandom", O_RDONLY); if (fd == -1) fd = open("/dev/random", O_RDONLY | O_NONBLOCK); srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); } /* Crank the random number generator a few times */ gettimeofday(&tv, 0); for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) rand(); return fd; } d48 4 a51 138 /* * Generate a series of random bytes. Use /dev/urandom if possible, * and if not, use srandom/random. */ void Uuid::getRandbytes(void *buf, int nbytes) { int i, fd = getRandfd(); int lose_counter = 0; char *cp = (char *) buf; if (fd >= 0) { while (nbytes > 0) { i = read(fd, cp, nbytes); if ((i < 0) && ((errno == EINTR) || (errno == EAGAIN))) continue; if (i <= 0) { if (lose_counter++ == 8) break; continue; } nbytes -= i; cp += i; lose_counter = 0; } } /* XXX put something better here if no /dev/random! */ for (i = 0; i < nbytes; i++) *cp++ = rand() & 0xFF; return; } /* * Get the ethernet hardware address, if we can find it... */ int Uuid::getNodeid(unsigned char *node_id) { #if 0 // FIXME: Fix this broken code some other day #ifdef HAVE_NET_IF_H int sd; struct ifreq ifr, *ifrp; struct ifconf ifc; char buf[1024]; int n, i; unsigned char *a; sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sd < 0) { return -1; } memset(buf, 0, sizeof(buf)); ifc.ifc_len = sizeof(buf); ifc.ifc_buf = buf; if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) { close(sd); return -1; } n = ifc.ifc_len; for (i = 0; i < n; i+= ifreq_size(ifr)) { ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i); strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ); #ifdef HAVE_SIOCGIFHWADDR if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) continue; a = (unsigned char *) &ifr.ifr_hwaddr.sa_data; #else #ifdef HAVE_SIOCGENADDR if (ioctl(sd, SIOCGENADDR, &ifr) < 0) continue; a = (unsigned char *) ifr.ifr_enaddr; #else /* * Broken! We don't have a way of getting the hardware * address, so just return without doing anything else */ close(sd); return 0; #endif /* SIOCGENADDR */ #endif /* SIOCGIFHWADDR */ if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5]) continue; if (node_id) { memcpy(node_id, a, 6); close(sd); return 1; } } close(sd); #endif return 0; #endif // FIXME: Fix this broken code some other day } /* Assume that the gettimeofday() has microsecond granularity */ #define MAX_ADJUSTMENT 10 int Uuid::getClock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq) { static int adjustment = 0; static struct timeval last = {0, 0}; static __u16 clock_seq; struct timeval tv; unsigned long long clock_reg; try_again: gettimeofday(&tv, 0); if ((last.tv_sec == 0) && (last.tv_usec == 0)) { getRandbytes(&clock_seq, sizeof(clock_seq)); clock_seq &= 0x1FFF; last = tv; last.tv_sec--; } if ((tv.tv_sec < last.tv_sec) || ((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec))) { clock_seq = (clock_seq+1) & 0x1FFF; adjustment = 0; last = tv; } else if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) { if (adjustment >= MAX_ADJUSTMENT) goto try_again; adjustment++; } else { adjustment = 0; last = tv; } clock_reg = tv.tv_usec*10 + adjustment; clock_reg += ((unsigned long long) tv.tv_sec)*10000000; clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000; *clock_high = clock_reg >> 32; *clock_low = clock_reg; *ret_clock_seq = clock_seq; return 0; } d53 3 a55 25 void Uuid::genTime(uuid_t out) { static unsigned char node_id[6]; static int has_init = 0; uuid uu; __u32 clock_mid; if (!has_init) { if (getNodeid(node_id) <= 0) { getRandbytes(node_id, 6); /* * Set multicast bit, to prevent conflicts * with IEEE 802 addresses obtained from * network cards */ node_id[0] |= 0x80; } has_init = 1; } getClock(&clock_mid, &uu.time_low, &uu.clock_seq); uu.clock_seq |= 0x8000; uu.time_mid = (__u16) clock_mid; uu.time_hi_and_version = (clock_mid >> 16) | 0x1000; memcpy(uu.node, node_id, 6); packId(&uu, out); d58 4 a61 1 void Uuid::genRand(uuid_t out) d63 2 a64 2 uuid_t buf; uuid uu; d66 1 a66 2 getRandbytes(buf, sizeof(buf)); unpackId(buf, &uu); d68 5 a72 4 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000; uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000; packId(&uu, out); } d74 3 a76 13 /* * This is the generic front-end to uuid_generate_random and * uuid_generate_time. It uses uuid_generate_random only if * /dev/urandom is available, since otherwise we won't have * high-quality randomness. */ void Uuid::genId(uuid_t out) { if (getRandfd() >= 0) genRand(out); else genTime(out); } d78 3 a80 4 void Uuid::packId(const uuid *uu, uuid_t ptr) { __u32 tmp; unsigned char *out = ptr; d82 3 a84 23 tmp = uu->time_low; out[3] = (unsigned char) tmp; tmp >>= 8; out[2] = (unsigned char) tmp; tmp >>= 8; out[1] = (unsigned char) tmp; tmp >>= 8; out[0] = (unsigned char) tmp; tmp = uu->time_mid; out[5] = (unsigned char) tmp; tmp >>= 8; out[4] = (unsigned char) tmp; tmp = uu->time_hi_and_version; out[7] = (unsigned char) tmp; tmp >>= 8; out[6] = (unsigned char) tmp; tmp = uu->clock_seq; out[9] = (unsigned char) tmp; tmp >>= 8; out[8] = (unsigned char) tmp; d86 2 a87 1 memcpy(out+10, uu->node, 6); d90 4 a93 1 void Uuid::unpackId(const uuid_t in, uuid *uu) d95 3 a97 22 const __u8 *ptr = in; __u32 tmp; tmp = *ptr++; tmp = (tmp << 8) | *ptr++; tmp = (tmp << 8) | *ptr++; tmp = (tmp << 8) | *ptr++; uu->time_low = tmp; tmp = *ptr++; tmp = (tmp << 8) | *ptr++; uu->time_mid = tmp; tmp = *ptr++; tmp = (tmp << 8) | *ptr++; uu->time_hi_and_version = tmp; tmp = *ptr++; tmp = (tmp << 8) | *ptr++; uu->clock_seq = tmp; memcpy(uu->node, ptr, 6); d100 5 a104 1 void Uuid::unparseId(const uuid_t uu, char *out) d106 1 a106 1 uuid uuid; d108 1 a108 2 unpackId(uu, &uuid); sprintf(out, d110 6 a115 4 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, uuid.clock_seq >> 8, uuid.clock_seq & 0xFF, uuid.node[0], uuid.node[1], uuid.node[2], uuid.node[3], uuid.node[4], uuid.node[5]); d117 1 @ 1.5 log @First cut at a UUID class. @ text @d82 1 a82 1 static int Uuid::get_random_fd(void) d106 1 a106 1 static void Uuid::get_random_bytes(void *buf, int nbytes) d108 1 a108 1 int i, fd = get_random_fd(); d138 1 a138 1 static int Uuid::get_node_id(unsigned char *node_id) d199 1 a199 1 static int Uuid::get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq) d210 1 a210 1 get_random_bytes(&clock_seq, sizeof(clock_seq)); d241 1 a241 1 void Uuid::uuid_generate_time(uuid_t out) d245 1 a245 1 struct uuid uu; d249 2 a250 2 if (get_node_id(node_id) <= 0) { get_random_bytes(node_id, 6); d260 1 a260 1 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq); d265 1 a265 1 uuid_pack(&uu, out); d268 1 a268 1 void Uuid::uuid_generate_random(uuid_t out) d271 1 a271 1 struct uuid uu; d273 2 a274 2 get_random_bytes(buf, sizeof(buf)); uuid_unpack(buf, &uu); d278 1 a278 1 uuid_pack(&uu, out); d287 1 a287 1 void Uuid::uuid_generate(uuid_t out) d289 2 a290 2 if (get_random_fd() >= 0) uuid_generate_random(out); d292 1 a292 1 uuid_generate_time(out); d295 1 a295 1 void Uuid::uuid_pack(const struct uuid *uu, uuid_t ptr) d327 1 a327 1 void Uuid::uuid_unpack(const uuid_t in, struct uuid *uu) d353 1 a353 1 void Uuid::uuid_unparse(const uuid_t uu, char *out) d355 1 a355 1 struct uuid uuid; d357 1 a357 1 uuid_unpack(uu, &uuid); @ 1.4 log @Get CORBA::WString_var for free by defining wstring in IDL, and remove unused autoconf datatype size tests. @ text @d1 31 d49 1 a49 6 #include "as_uuidgen.h" /* Include whatever autoconf found */ #ifdef HAVE_CONFIG_H #include "ac_config.h" #endif d82 1 a82 1 static int get_random_fd(void) a101 1 d106 1 a106 1 static void get_random_bytes(void *buf, int nbytes) d138 1 a138 1 static int get_node_id(unsigned char *node_id) d199 1 a199 1 static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq) d241 1 a241 1 void uuid_generate_time(uuid_t out) d268 1 a268 1 void uuid_generate_random(uuid_t out) d287 1 a287 1 void uuid_generate(uuid_t out) d295 1 a295 1 void uuid_pack(const struct uuid *uu, uuid_t ptr) d327 1 a327 1 void uuid_unpack(const uuid_t in, struct uuid *uu) d353 1 a353 1 void uuid_unparse(const uuid_t uu, char *out) @ 1.3 log @Small buildconf and UUID fixes to get build running again. @ text @d32 3 @ 1.2 log @Bug fix pointer derefernce in GUID generator, and improve more portable build configuration network header file handling. @ text @d112 1 d165 1 @ 1.1 log @Starting point for inclusion of GUID generation logic. @ text @d10 2 d14 3 d20 10 a29 3 #ifndef max /* Define a general use max macro */ #define max(a,b) ((a) > (b) ? (a) : (b)) d32 3 d36 5 a40 1 #include /* For use in get_node_id() */ d42 9 a50 3 /* Define the size of a interface address */ #define ifreq_size(i) max(sizeof(struct ifreq), sizeof((i).ifr_name)+(i).ifr_addr.sa_len) #endif d132 1 a132 1 for (i = 0; i < n; i+= ifreq_size(*ifr) ) { @