Convert all osi_timeval_t to osi_timeval32_t
[openafs.git] / src / util / uuid.c
index 4fc3ad0..5d44f18 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  * Copyright (c) 2002 Kungliga Tekniska Högskolan
  * (Royal Institute of Technology, Stockholm, Sweden).
  * All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
- * 
+ *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
- * 
+ *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 
+ *
  * 3. Neither the name of the Institute nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  */
 
 #include <afsconfig.h>
-#ifdef KERNEL
-#include "afs/param.h"
-#else
 #include <afs/param.h>
-#endif
 
+#ifndef KERNEL
+# include <roken.h>
+
+# ifdef AFS_NT40_ENV
+#  include <rpc.h>
+# else
+#  include <net/if.h>
+#  if !defined(AFS_LINUX20_ENV) && !defined(AFS_ARM_DARWIN_ENV)
+#   include <netinet/if_ether.h>
+#  endif
+# endif
+
+#include "afsutil.h"
 
-#ifdef KERNEL
-#include "afs/sysincludes.h"
-#include "afsincludes.h"
-#define uuid_memcmp(A,B,C)     memcmp(A, B, C)
-#define uuid_memcpy(A,B,C)     memcpy(A, B, C)
-#else /* KERNEL */
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#include <process.h>
 #else
-#include <sys/file.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#ifndef ITIMER_REAL
-#include <sys/time.h>
-#endif /* ITIMER_REAL */
-#include <net/if.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+# include "afs/sysincludes.h"
+# include "afsincludes.h"
 #endif
-#include <stdlib.h>
+#ifdef UKERNEL
+# include "rx/rx_prototypes.h"
 #endif
-#include <sys/stat.h>
-#include <fcntl.h>
-#if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
-#include <netinet/if_ether.h>
-#endif
-#include "afsutil.h"
-
-#define uuid_memcmp(A,B,C)     memcmp(A,B,C)
-#define uuid_memcpy(A,B,C)     memcpy(A,B,C)
-#endif /* KERNEL */
-
 
 typedef struct {
     char eaddr[6];             /* 6 bytes of ethernet hardware address */
@@ -120,7 +98,7 @@ void uuid__get_os_time(uuid_time_t * os_time);
  * +--------------------------...-----+
  */
 
-afsUUID afs_uuid_g_nil_uuid = { 0 };
+afsUUID afs_uuid_g_nil_uuid;
 static uuid_time_t time_now, time_last;
 static u_short uuid_time_adjust, clock_seq;
 static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
@@ -130,7 +108,7 @@ static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
 afs_int32
 afs_uuid_equal(afsUUID * u1, afsUUID * u2)
 {
-    return (uuid_memcmp((void *)u1, (void *)u2, sizeof(afsUUID)) == 0);
+    return (memcmp(u1, u2, sizeof(afsUUID)) == 0);
 }
 
 afs_int32
@@ -138,8 +116,7 @@ afs_uuid_is_nil(afsUUID * u1)
 {
     if (!u1)
        return 1;
-    return (uuid_memcmp
-           ((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof(afsUUID)) == 0);
+    return afs_uuid_equal(u1, &afs_uuid_g_nil_uuid);
 }
 
 void
@@ -221,14 +198,21 @@ afsUUID_from_string(const char *str, afsUUID * uuid)
     return 0;
 }
 
-/*
+/**
  *    Converts a UUID from binary representation to a string representation.
+ *
+ *    @param[in]  uuid pointer to a afsUUID
+ *    @param[out] buf  format work buffer
+ *
+ *    @returns pointer to buffer containing string representation of "uuid"
  */
 
-int
-afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz)
+char *
+afsUUID_to_string(const afsUUID * uuid, struct uuid_fmtbuf *buf)
 {
-    snprintf(str, strsz, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
+    memset(buf, 0, sizeof(*buf));
+    snprintf(buf->buffer, sizeof(buf->buffer),
+            "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
             uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
             (unsigned char)uuid->clock_seq_hi_and_reserved,
             (unsigned char)uuid->clock_seq_low, (unsigned char)uuid->node[0],
@@ -236,19 +220,22 @@ afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz)
             (unsigned char)uuid->node[3], (unsigned char)uuid->node[4],
             (unsigned char)uuid->node[5]);
 
-    return 0;
+    return buf->buffer;
 }
 #endif
 
 afs_int32
 afs_uuid_create(afsUUID * uuid)
 {
+#ifdef AFS_NT40_ENV
+    UuidCreate((UUID *) uuid);
+#else /* AFS_NT40_ENV */
     uuid_address_t eaddr;
     afs_int32 got_no_time = 0, code;
 
     if (!uuid_init_done) {
        uuid_time_t t;
-       u_short *seedp, seed = 0;
+       u_short seedp[4], seed = 0;
        rand_m = 971;;
        rand_ia = 11113;
        rand_ib = 104322;
@@ -265,13 +252,15 @@ afs_uuid_create(afsUUID * uuid)
         * are multiple processes creating UUID's on a system, we add in the PID.
         */
        uuid__get_os_time(&t);
-       seedp = (u_short *) (&t);
-       seed ^= *seedp++;
-       seed ^= *seedp++;
-       seed ^= *seedp++;
-       seed ^= *seedp++;
+       memcpy(&seedp, &t, sizeof(seedp));
+       seed ^= seedp[0];
+       seed ^= seedp[1];
+       seed ^= seedp[2];
+       seed ^= seedp[3];
 #if defined(KERNEL) && defined(AFS_XBSD_ENV)
        rand_irand += seed + (afs_uint32) curproc->p_pid;
+#elif defined(UKERNEL)
+       rand_irand += seed + (afs_uint32) osi_getpid();
 #else
        rand_irand += seed + (afs_uint32) getpid();
 #endif
@@ -330,7 +319,8 @@ afs_uuid_create(afsUUID * uuid)
     uuid->clock_seq_low = clock_seq & 0xff;
     uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8;
     uuid->clock_seq_hi_and_reserved |= 0x80;
-    uuid_memcpy((void *)uuid->node, (void *)&eaddr, sizeof(uuid_address_t));
+    memcpy(uuid->node, &eaddr, sizeof(uuid_address_t));
+#endif /* AFS_NT40_ENV */
     return 0;
 }
 
@@ -401,7 +391,7 @@ extern struct interfaceAddr afs_cb_interface;
 static int
 uuid_get_address(uuid_address_p_t addr)
 {
-    uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
+    memcpy(addr->eaddr, &afs_cb_interface.addr_in[0], 4);
     addr->eaddr[4] = 0xaa;
     addr->eaddr[5] = 0x77;
     return 0;
@@ -410,7 +400,7 @@ uuid_get_address(uuid_address_p_t addr)
 void
 uuid__get_os_time(uuid_time_t * os_time)
 {
-    osi_timeval_t tp;
+    osi_timeval32_t tp;
 
     osi_GetTime(&tp);
     os_time->hi = tp.tv_sec;
@@ -424,38 +414,37 @@ static int
 uuid_get_address(uuid_address_p_t addr)
 {
     afs_int32 code;
-    afs_uint32 addr1;
-    struct hostent *he;
+    afs_uint32 addr1 = 0;
+    struct hostent *he = NULL;
 
     code = gethostname(hostName1, 64);
-    if (code) {
-       printf("gethostname() failed\n");
-#ifdef AFS_NT40_ENV
-       return ENOENT;
-#else
-       return errno;
+    if (!code)
+       he = gethostbyname(hostName1);
+
+    if (he)
+       memcpy(&addr1, he->h_addr_list[0], 4);
+#ifdef UKERNEL
+    else
+       addr1=rxi_getaddr();
 #endif
-    }
-    he = gethostbyname(hostName1);
-    if (!he) {
-       printf("Can't find address for '%s'\n", hostName1);
+
+    if (!addr1) {
 #ifdef AFS_NT40_ENV
        return ENOENT;
 #else
        return errno;
 #endif
-    } else {
-       uuid_memcpy(&addr1, he->h_addr_list[0], 4);
-       addr1 = ntohl(addr1);
-       uuid_memcpy(addr->eaddr, &addr1, 4);
-       addr->eaddr[4] = 0xaa;
-       addr->eaddr[5] = 0x77;
+    }
+
+    addr1 = ntohl(addr1);
+    memcpy(addr->eaddr, &addr1, 4);
+    addr->eaddr[4] = 0xaa;
+    addr->eaddr[5] = 0x77;
 #ifdef  UUID_DEBUG
-       printf("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
-              addr->eaddr[0], addr->eaddr[1], addr->eaddr[2], addr->eaddr[3],
-              addr->eaddr[4], addr->eaddr[5]);
+    printf("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
+          addr->eaddr[0], addr->eaddr[1], addr->eaddr[2], addr->eaddr[3],
+          addr->eaddr[4], addr->eaddr[5]);
 #endif
-    }
     return 0;
 }