-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
193 lines (161 loc) · 3.81 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#else
#include <sys/time.h>
#endif
#include <pthread.h>
#include "misc.h"
void log_socket_error(const char* msg)
{
#ifdef WIN32
printf(msg);
printf(": WSAGetLastError(): %d", WSAGetLastError());
#else
perror(msg);
#endif
}
#ifdef WIN32
static const unsigned __int64 epoch = 116444736000000000;
int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
FILETIME file_time;
SYSTEMTIME system_time;
ULARGE_INTEGER ularge;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
ularge.LowPart = file_time.dwLowDateTime;
ularge.HighPart = file_time.dwHighDateTime;
tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
return 0;
}
#endif
bool GetAbsTime(struct timespec *Abstime, int MillisecondsFromNow)
{
struct timeval now;
if (!gettimeofday(&now, NULL)) {
now.tv_sec += MillisecondsFromNow / 1000;
now.tv_usec += (MillisecondsFromNow % 1000) * 1000;
if (now.tv_usec >= 1000000) {
now.tv_sec++;
now.tv_usec -= 1000000;
}
Abstime->tv_sec = now.tv_sec;
Abstime->tv_nsec = now.tv_usec * 1000;
return true;
}
return false;
}
// --- cTimeMs ---------------------------------------------------------------
cTimeMs::cTimeMs(int Ms)
{
Set(Ms);
}
uint64_t cTimeMs::Now(void)
{
struct timeval t;
if (gettimeofday(&t, NULL) == 0)
return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
return 0;
}
void cTimeMs::Set(int Ms)
{
begin = Now() + Ms;
}
bool cTimeMs::TimedOut(void)
{
return Now() >= begin;
}
uint64_t cTimeMs::Elapsed(void)
{
return Now() - begin;
}
#ifdef WIN32
#ifdef __MINGW32__
#define __try if(1)
#define __leave goto out
#define __finally if(1)
#endif
bool IsUserAdmin( bool* pbAdmin )
{
#if WINVER < 0x0500
HANDLE hAccessToken = NULL;
PBYTE pInfoBuffer = NULL;
DWORD dwInfoBufferSize = 1024; // starting buffer size
PTOKEN_GROUPS ptgGroups = NULL;
#endif
PSID psidAdministrators = NULL;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = {SECURITY_NT_AUTHORITY};
BOOL bResult = FALSE;
__try
{
// initialization of the security id
if( !AllocateAndInitializeSid(
&siaNtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0,0,0,0,0,0, &psidAdministrators ) )
__leave;
#if WINVER < 0x0500
// code for the OS < W2k
// open process token
if( !OpenProcessToken( GetCurrentProcess(),TOKEN_QUERY,&hAccessToken ) )
__leave;
do // lets make a buffer for the information from the token
{
if( pInfoBuffer )
delete pInfoBuffer;
pInfoBuffer = new BYTE[dwInfoBufferSize];
if( !pInfoBuffer )
__leave;
SetLastError( 0 );
if( !GetTokenInformation( hAccessToken, TokenGroups,
pInfoBuffer, dwInfoBufferSize, &dwInfoBufferSize ) &&
( ERROR_INSUFFICIENT_BUFFER != GetLastError() ) )
__leave;
else
ptgGroups = (PTOKEN_GROUPS)pInfoBuffer;
}
while( GetLastError() ); // if we encounter an error, then the
// initializing buffer too small,
// lets make it bigger
// lets check the security id one by one, while we find one we need
for( UINT i = 0; i < ptgGroups->GroupCount; i++ )
{
if( EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid) )
{
*pbAdmin = TRUE;
bResult = TRUE;
__leave;
}
}
#else
// this is code for the W2K
bResult = CheckTokenMembership( NULL, psidAdministrators, pbAdmin );
#endif
}
__finally
{
#ifdef __MINGW32__
out:
#endif
#if WINVER < 0x0500
if( hAccessToken )
CloseHandle( hAccessToken );
if( pInfoBuffer )
delete pInfoBuffer;
#endif
if( psidAdministrators )
FreeSid( psidAdministrators );
}
return bResult;
}
bool IsVistaOrHigher()
{
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
return (osvi.dwMajorVersion >= 6);
}
#endif