PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/upnp123/upnp/src/uuid/sysdep.c

https://bitbucket.org/glassman/pm123
C | 152 lines | 79 code | 21 blank | 52 comment | 1 complexity | eaafa880e68f760ab6839c3b2e979967 MD5 | raw file
  1. /*
  2. * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
  3. * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
  4. * Digital Equipment Corporation, Maynard, Mass.
  5. * Copyright (c) 1998 Microsoft.
  6. * To anyone who acknowledges that this file is provided "AS IS"
  7. * without any express or implied warranty: permission to use, copy,
  8. * modify, and distribute this file for any purpose is hereby
  9. * granted without fee, provided that the above copyright notices and
  10. * this notice appears in all source code copies, and that none of
  11. * the names of Open Software Foundation, Inc., Hewlett-Packard
  12. * Company, or Digital Equipment Corporation be used in advertising
  13. * or publicity pertaining to distribution of the software without
  14. * specific, written prior permission. Neither Open Software
  15. * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital Equipment
  16. * Corporation makes any representations about the suitability of
  17. * this software for any purpose.
  18. */
  19. /*!
  20. * \file
  21. */
  22. #include "config.h"
  23. #include "sysdep.h"
  24. #include "UpnpInet.h"
  25. #include <string.h>
  26. #include <stdio.h>
  27. /*!
  28. * \brief System dependent call to get IEEE node ID.
  29. *
  30. * This sample implementation generates a random node ID.
  31. */
  32. void get_ieee_node_identifier(uuid_node_t *node)
  33. {
  34. unsigned char seed[16];
  35. static int inited = 0;
  36. static uuid_node_t saved_node;
  37. if (!inited) {
  38. get_random_info(seed);
  39. seed[0] |= 0x80;
  40. memcpy(&saved_node, seed, sizeof(uuid_node_t));
  41. inited = 1;
  42. };
  43. *node = saved_node;
  44. };
  45. /*!
  46. * \brief System dependent call to get the current system time.
  47. *
  48. * Returned as 100ns ticks since Oct 15, 1582, but resolution may be less
  49. * than 100ns.
  50. */
  51. #ifdef WIN32
  52. void get_system_time(uuid_time_t *uuid_time)
  53. {
  54. ULARGE_INTEGER time;
  55. GetSystemTimeAsFileTime((FILETIME *) & time);
  56. /*
  57. NT keeps time in FILETIME format which is 100ns ticks since
  58. Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
  59. The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
  60. + 18 years and 5 leap days.
  61. */
  62. time.QuadPart += (unsigned __int64)(1000 * 1000 * 10) /* seconds */
  63. *(unsigned __int64)(60 * 60 * 24) /* days */
  64. *(unsigned __int64)(17 + 30 + 31 + 365 * 18 + 5); /* # of days */
  65. *uuid_time = time.QuadPart;
  66. };
  67. void get_random_info(unsigned char seed[16])
  68. {
  69. MD5_CTX c;
  70. typedef struct {
  71. MEMORYSTATUS m;
  72. SYSTEM_INFO s;
  73. FILETIME t;
  74. LARGE_INTEGER pc;
  75. DWORD tc;
  76. DWORD l;
  77. char hostname[MAX_COMPUTERNAME_LENGTH + 1];
  78. } randomness;
  79. randomness r;
  80. /* Initialize memory area so that valgrind does not complain */
  81. memset(&r, 0, sizeof r);
  82. /* memory usage stats */
  83. GlobalMemoryStatus( &r.m );
  84. /* random system stats */
  85. GetSystemInfo( &r.s );
  86. /* 100ns resolution (nominally) time of day */
  87. GetSystemTimeAsFileTime( &r.t );
  88. /* high resolution performance counter */
  89. QueryPerformanceCounter( &r.pc );
  90. /* milliseconds since last boot */
  91. r.tc = GetTickCount();
  92. r.l = MAX_COMPUTERNAME_LENGTH + 1;
  93. GetComputerName( r.hostname, &r.l );
  94. /* MD5 it */
  95. MD5Init(&c);
  96. MD5Update(&c, (unsigned char *)(&r), sizeof r);
  97. MD5Final(seed, &c);
  98. };
  99. #else /* WIN32 */
  100. void get_system_time(uuid_time_t *uuid_time)
  101. {
  102. struct timeval tp;
  103. gettimeofday(&tp, (struct timezone *)0);
  104. /* Offset between UUID formatted times and Unix formatted times.
  105. * UUID UTC base time is October 15, 1582.
  106. * Unix base time is January 1, 1970. */
  107. *uuid_time =
  108. (uuid_time_t)tp.tv_sec * 10000000 +
  109. (uuid_time_t)tp.tv_usec * 10 +
  110. 0x01B21DD213814000LL;
  111. };
  112. void get_random_info(unsigned char seed[16])
  113. {
  114. MD5_CTX c;
  115. typedef struct {
  116. /*struct sysinfo s; */
  117. struct timeval t;
  118. char hostname[257];
  119. } randomness;
  120. randomness r;
  121. /* Initialize memory area so that valgrind does not complain. */
  122. memset(&r, 0, sizeof r);
  123. /* Get some random stuff. */
  124. gettimeofday(&r.t, (struct timezone *)0);
  125. gethostname(r.hostname, 256);
  126. /* MD5 it */
  127. MD5Init(&c);
  128. MD5Update(&c, (unsigned char *)&r, sizeof r);
  129. MD5Final(seed, &c);
  130. };
  131. #endif /* WIN32 */