The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/uuid/uuid.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
    3  *
    4  * %Begin-Header%
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, and the entire permission notice in its entirety,
   10  *    including the disclaimer of warranties.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote
   15  *    products derived from this software without specific prior
   16  *    written permission.
   17  * 
   18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
   21  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
   22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
   24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   25  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
   28  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
   29  * DAMAGE.
   30  * %End-Header%
   31  */
   32 
   33 #include <uuid/uuid.h>
   34 
   35 #include <stdint.h>
   36 #include <string.h>
   37 
   38 #include <sys/random.h>
   39 #include <sys/socket.h>
   40 #include <sys/systm.h>
   41 #include <sys/time.h>
   42 
   43 #include <net/if.h>
   44 #include <net/if_dl.h>
   45 #include <net/if_types.h>
   46 
   47 UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
   48 
   49 static void
   50 read_node(uint8_t *node)
   51 {
   52 #if NETWORKING
   53         struct ifnet *ifp;
   54         struct ifaddr *ifa;
   55         struct sockaddr_dl *sdl;
   56 
   57         ifnet_head_lock_shared();
   58         TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
   59                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
   60                         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
   61                         if (sdl && sdl->sdl_family == AF_LINK && sdl->sdl_type == IFT_ETHER) {
   62                                 memcpy(node, LLADDR(sdl), 6);
   63                                 ifnet_head_done();
   64                                 return;
   65                         }
   66                 }
   67         }
   68         ifnet_head_done();
   69 #endif /* NETWORKING */
   70 
   71         read_random(node, 6);
   72         node[0] |= 0x01;
   73 }
   74 
   75 static uint64_t
   76 read_time(void)
   77 {
   78         struct timespec tv;
   79 
   80         nanotime(&tv);
   81 
   82         return (tv.tv_sec * 10000000ULL) + (tv.tv_nsec / 100ULL) + 0x01B21DD213814000ULL;
   83 }
   84 
   85 void
   86 uuid_clear(uuid_t uu)
   87 {
   88         memset(uu, 0, sizeof(uuid_t));
   89 }
   90 
   91 int
   92 uuid_compare(const uuid_t uu1, const uuid_t uu2)
   93 {
   94         return memcmp(uu1, uu2, sizeof(uuid_t));
   95 }
   96 
   97 void
   98 uuid_copy(uuid_t dst, const uuid_t src)
   99 {
  100         memcpy(dst, src, sizeof(uuid_t));
  101 }
  102 
  103 void
  104 uuid_generate_random(uuid_t out)
  105 {
  106         read_random(out, sizeof(uuid_t));
  107 
  108         out[6] = (out[6] & 0x0F) | 0x40;
  109         out[8] = (out[8] & 0x3F) | 0x80;
  110 }
  111 
  112 void
  113 uuid_generate_time(uuid_t out)
  114 {
  115         uint64_t time;
  116 
  117         read_node(&out[10]);
  118         read_random(&out[8], 2);
  119 
  120         time = read_time();
  121         out[0] = (uint8_t)(time >> 24);
  122         out[1] = (uint8_t)(time >> 16);
  123         out[2] = (uint8_t)(time >> 8);
  124         out[3] = (uint8_t)time;
  125         out[4] = (uint8_t)(time >> 40);
  126         out[5] = (uint8_t)(time >> 32);
  127         out[6] = (uint8_t)(time >> 56);
  128         out[7] = (uint8_t)(time >> 48);
  129  
  130         out[6] = (out[6] & 0x0F) | 0x10;
  131         out[8] = (out[8] & 0x3F) | 0x80;
  132 }
  133 
  134 void
  135 uuid_generate(uuid_t out)
  136 {
  137         uuid_generate_random(out);
  138 }
  139 
  140 int
  141 uuid_is_null(const uuid_t uu)
  142 {
  143         return !memcmp(uu, UUID_NULL, sizeof(uuid_t));
  144 }
  145 
  146 int
  147 uuid_parse(const uuid_string_t in, uuid_t uu)
  148 {
  149         int n = 0;
  150 
  151         sscanf(in,
  152                 "%2hhx%2hhx%2hhx%2hhx-"
  153                 "%2hhx%2hhx-"
  154                 "%2hhx%2hhx-"
  155                 "%2hhx%2hhx-"
  156                 "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%n",
  157                 &uu[0], &uu[1], &uu[2], &uu[3],
  158                 &uu[4], &uu[5],
  159                 &uu[6], &uu[7],
  160                 &uu[8], &uu[9],
  161                 &uu[10], &uu[11], &uu[12], &uu[13], &uu[14], &uu[15], &n);
  162 
  163         return (n != 36 || in[n] != '\0' ? -1 : 0);
  164 }
  165 
  166 void
  167 uuid_unparse_lower(const uuid_t uu, uuid_string_t out)
  168 {
  169         snprintf(out,
  170                 sizeof(uuid_string_t),
  171                 "%02x%02x%02x%02x-"
  172                 "%02x%02x-"
  173                 "%02x%02x-"
  174                 "%02x%02x-"
  175                 "%02x%02x%02x%02x%02x%02x",
  176                 uu[0], uu[1], uu[2], uu[3],
  177                 uu[4], uu[5],
  178                 uu[6], uu[7],
  179                 uu[8], uu[9],
  180                 uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
  181 }
  182 
  183 void
  184 uuid_unparse_upper(const uuid_t uu, uuid_string_t out)
  185 {
  186         snprintf(out,
  187                 sizeof(uuid_string_t),
  188                 "%02X%02X%02X%02X-"
  189                 "%02X%02X-"
  190                 "%02X%02X-"
  191                 "%02X%02X-"
  192                 "%02X%02X%02X%02X%02X%02X",
  193                 uu[0], uu[1], uu[2], uu[3],
  194                 uu[4], uu[5],
  195                 uu[6], uu[7],
  196                 uu[8], uu[9],
  197                 uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
  198 }
  199 
  200 void
  201 uuid_unparse(const uuid_t uu, uuid_string_t out)
  202 {
  203         uuid_unparse_upper(uu, out);
  204 }

Cache object: ea03b894c553e40ad4bdb0ce91df840a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.