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: 2025-07-18
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 source file on 2025-07-18, ver 1.14.21
22
29#ifdef _WIN32
30// enable "save" rand_s(), only defined on win32.
31#define _CRT_RAND_S
32#endif
33
34#include <sysdep.hpp>
35#include <UPnPsdk/port_sock.hpp>
36
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
62#ifdef _WIN32
63
64void get_system_time(uuid_time_t* uuid_time) {
65 ULARGE_INTEGER time;
66
67 GetSystemTimeAsFileTime((FILETIME*)&time);
68 /*
69 NT keeps time in FILETIME format which is 100ns ticks since
70 Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
71 The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
72 + 18 years and 5 leap days.
73 */
74 time.QuadPart +=
75 (unsigned __int64)(1000 * 1000 * 10) /* seconds */
76 * (unsigned __int64)(60 * 60 * 24) /* days */
77 * (unsigned __int64)(17 + 30 + 31 + 365 * 18 + 5); /* # of days */
78 *uuid_time = time.QuadPart;
79};
80
81void get_random_info(unsigned char seed[16]) {
83 int i;
84 for (i = 0; i < 16; i++) {
85 unsigned int number;
86 rand_s(&number);
87 seed[i] = (unsigned char)number;
88 }
89};
90
91#else /* _WIN32 */
92
93void get_system_time(uuid_time_t* uuid_time) {
94 struct timeval tp;
95
96 gettimeofday(&tp, (struct timezone*)0);
97 /* Offset between UUID formatted times and Unix formatted times.
98 * UUID UTC base time is October 15, 1582.
99 * Unix base time is January 1, 1970. */
100 *uuid_time = (uuid_time_t)tp.tv_sec * 10000000 +
101 (uuid_time_t)tp.tv_usec * 10 + 0x01B21DD213814000L;
102}
103
104constexpr int HOSTNAME_LENGTH{255};
105
106void get_random_info(unsigned char seed[16]) {
107 MD5_CTX c;
108 typedef struct {
109 /*struct sysinfo s; */
110 struct timeval t;
111 char hostname[HOSTNAME_LENGTH + 1];
112 } randomness;
113 randomness r;
114
115 /* Initialize memory area so that valgrind does not complain. */
116 memset(&r, 0, sizeof r);
117
118 /* Get some random stuff. */
119 gettimeofday(&r.t, (struct timezone*)0);
120 gethostname(r.hostname, HOSTNAME_LENGTH);
121
122 /* MD5 it */
123 MD5Init(&c);
124 MD5Update(&c, (unsigned char*)&r, sizeof r);
125 MD5Final(seed, &c);
126}
127
128#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:81
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:64
Get some system dependent information.
uint64_t uuid_time_t
UUID time.
Definition sysdep.hpp:48
node ID
Definition sysdep.hpp:51