UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
sysdep.cpp
Go to the documentation of this 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 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2024-08-17
8 * To anyone who acknowledges that this file is provided "AS IS"
9 * without any express or implied warranty: permission to use, copy,
10 * modify, and distribute this file for any purpose is hereby
11 * granted without fee, provided that the above copyright notices and
12 * this notice appears in all source code copies, and that none of
13 * the names of Open Software Foundation, Inc., Hewlett-Packard
14 * Company, or Digital Equipment Corporation be used in advertising
15 * or publicity pertaining to distribution of the software without
16 * specific, written prior permission. Neither Open Software
17 * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital Equipment
18 * Corporation makes any representations about the suitability of
19 * this software for any purpose.
20 */
21// Last compare with pupnp original source file on 2023-07-08, ver 1.14.17
22
29#ifdef _WIN32
30#define _CRT_RAND_S
31#endif
32
33#include <sysdep.hpp>
34#include <UPnPsdk/port_sock.hpp>
35
37#include <cstdio>
38#include <cstring>
40
42 unsigned char seed[16];
43 static int inited = 0;
44 static uuid_node_t saved_node;
45
46 if (!inited) {
47 get_random_info(seed);
48 seed[0] |= 0x80;
49 memcpy(&saved_node, seed, sizeof(uuid_node_t));
50 inited = 1;
51 };
52 *node = saved_node;
53}
54
55#ifdef _WIN32
56
57void get_system_time(uuid_time_t* uuid_time) {
58 ULARGE_INTEGER time;
59
60 GetSystemTimeAsFileTime((FILETIME*)&time);
61 /*
62 NT keeps time in FILETIME format which is 100ns ticks since
63 Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
64 The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
65 + 18 years and 5 leap days.
66 */
67 time.QuadPart +=
68 (unsigned __int64)(1000 * 1000 * 10) /* seconds */
69 * (unsigned __int64)(60 * 60 * 24) /* days */
70 * (unsigned __int64)(17 + 30 + 31 + 365 * 18 + 5); /* # of days */
71 *uuid_time = time.QuadPart;
72};
73
74void get_random_info(unsigned char seed[16]) {
76 int i;
77 for (i = 0; i < 16; i++) {
78 unsigned int number;
79 rand_s(&number);
80 seed[i] = (unsigned char)number;
81 }
82};
83
84#else /* _WIN32 */
85
86void get_system_time(uuid_time_t* uuid_time) {
87 struct timeval tp;
88
89 gettimeofday(&tp, (struct timezone*)0);
90 /* Offset between UUID formatted times and Unix formatted times.
91 * UUID UTC base time is October 15, 1582.
92 * Unix base time is January 1, 1970. */
93 *uuid_time = (uuid_time_t)tp.tv_sec * 10000000 +
94 (uuid_time_t)tp.tv_usec * 10 + 0x01B21DD213814000L;
95}
96
97void get_random_info(unsigned char seed[16]) {
98 MD5_CTX c;
99 typedef struct {
100 /*struct sysinfo s; */
101 struct timeval t;
102 char hostname[257];
103 } randomness;
104 randomness r;
105
106 /* Initialize memory area so that valgrind does not complain. */
107 memset(&r, 0, sizeof r);
108
109 /* Get some random stuff. */
110 gettimeofday(&r.t, (struct timezone*)0);
111 gethostname(r.hostname, 256);
112
113 /* MD5 it */
114 MD5Init(&c);
115 MD5Update(&c, (unsigned char*)&r, sizeof r);
116 MD5Final(seed, &c);
117}
118
119#endif /* _WIN32 */
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get time of day.
Timezone.
void MD5Update(MD5_CTX *, const void *, size_t)
MD5 Update.
Definition md5.cpp:82
void MD5Init(MD5_CTX *)
MD5 Initialisation.
Definition md5.cpp:70
void MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
MD5 Final.
Definition md5.cpp:119
MD5 Context.
Definition md5.hpp:38
Specifications to be portable with sockets between different platforms.
void get_random_info(unsigned char seed[16])
Get random information.
Definition sysdep.cpp:74
void get_ieee_node_identifier(uuid_node_t *node)
System dependent call to get IEEE node identifier.
Definition sysdep.cpp:41
void get_system_time(uuid_time_t *uuid_time)
System dependent call to get the current system time.
Definition sysdep.cpp:57
Get some system dependent information.
uint64_t uuid_time_t
UUID time.
Definition sysdep.hpp:48
node ID
Definition sysdep.hpp:51