16 #include "wine/windef16.h"
21 #ifdef HAVE_SYS_FILE_H
22 # include <sys/file.h>
24 #include <sys/ioctl.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
28 #ifdef HAVE_SYS_SOCKIO_H
29 # include <sys/sockio.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(ole);
42 /***********************************************************************
46 * hinstDLL [I] handle to the DLL's instance
48 * lpvReserved [I] reserved, must be NULL
55 static DWORD RPCRT4_dwProcessesAttached = 0;
58 RPCRT4_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
61 case DLL_PROCESS_ATTACH:
62 RPCRT4_dwProcessesAttached++;
65 case DLL_PROCESS_DETACH:
66 RPCRT4_dwProcessesAttached--;
73 /*************************************************************************
76 * Creates a 128bit UUID.
77 * Implemented according the DCE specification for UUID generation.
78 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
79 * Copyright (C) 1996, 1997 Theodore Ts'o.
85 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
87 static char has_init = 0;
89 static int adjustment = 0;
90 static struct timeval last = {0, 0};
91 static UINT16 clock_seq;
93 unsigned long long clock_reg;
94 UINT clock_high, clock_low;
95 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
98 struct ifreq ifr, *ifrp;
104 /* Have we already tried to get the MAC address? */
107 /* BSD 4.4 defines the size of an ifreq to be
108 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
109 * However, under earlier systems, sa_len isn't present, so
110 * the size is just sizeof(struct ifreq)
114 # define max(a,b) ((a) > (b) ? (a) : (b))
116 # define ifreq_size(i) max(sizeof(struct ifreq),\
117 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
119 # define ifreq_size(i) sizeof(struct ifreq)
120 # endif /* HAVE_SA_LEN */
122 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
124 /* if we can't open a socket, just use random numbers */
125 /* set the multicast bit to prevent conflicts with real cards */
126 a[0] = (rand() & 0xff) | 0x80;
127 a[1] = rand() & 0xff;
128 a[2] = rand() & 0xff;
129 a[3] = rand() & 0xff;
130 a[4] = rand() & 0xff;
131 a[5] = rand() & 0xff;
133 memset(buf, 0, sizeof(buf));
134 ifc.ifc_len = sizeof(buf);
136 /* get the ifconf interface */
137 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
139 /* no ifconf, so just use random numbers */
140 /* set the multicast bit to prevent conflicts with real cards */
141 a[0] = (rand() & 0xff) | 0x80;
142 a[1] = rand() & 0xff;
143 a[2] = rand() & 0xff;
144 a[3] = rand() & 0xff;
145 a[4] = rand() & 0xff;
146 a[5] = rand() & 0xff;
148 /* loop through the interfaces, looking for a valid one */
150 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
151 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
152 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
153 /* try to get the address for this interface */
154 # ifdef SIOCGIFHWADDR
155 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
157 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
160 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
162 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
164 /* XXX we don't have a way of getting the hardware address */
168 # endif /* SIOCGENADDR */
169 # endif /* SIOCGIFHWADDR */
170 /* make sure it's not blank */
171 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
176 /* if we didn't find a valid address, make a random one */
177 /* once again, set multicast bit to avoid conflicts */
178 a[0] = (rand() & 0xff) | 0x80;
179 a[1] = rand() & 0xff;
180 a[2] = rand() & 0xff;
181 a[3] = rand() & 0xff;
182 a[4] = rand() & 0xff;
183 a[5] = rand() & 0xff;
190 /* no networking info, so generate a random address */
191 a[0] = (rand() & 0xff) | 0x80;
192 a[1] = rand() & 0xff;
193 a[2] = rand() & 0xff;
194 a[3] = rand() & 0xff;
195 a[4] = rand() & 0xff;
196 a[5] = rand() & 0xff;
197 #endif /* HAVE_NET_IF_H */
201 /* generate time element of GUID */
203 /* Assume that the gettimeofday() has microsecond granularity */
204 #define MAX_ADJUSTMENT 10
207 gettimeofday(&tv, 0);
208 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
209 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
214 if ((tv.tv_sec < last.tv_sec) ||
215 ((tv.tv_sec == last.tv_sec) &&
216 (tv.tv_usec < last.tv_usec))) {
217 clock_seq = (clock_seq+1) & 0x1FFF;
219 } else if ((tv.tv_sec == last.tv_sec) &&
220 (tv.tv_usec == last.tv_usec)) {
221 if (adjustment >= MAX_ADJUSTMENT)
227 clock_reg = tv.tv_usec*10 + adjustment;
228 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
229 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
231 clock_high = clock_reg >> 32;
232 clock_low = clock_reg;
233 temp_clock_seq = clock_seq | 0x8000;
234 temp_clock_mid = (UINT16)clock_high;
235 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
237 /* pack the information into the GUID structure */
239 ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
241 ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
243 ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
245 ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
247 ((unsigned char*)&Uuid->Data2)[1] = (unsigned char)temp_clock_mid;
248 temp_clock_mid >>= 8;
249 ((unsigned char*)&Uuid->Data2)[0] = (unsigned char)temp_clock_mid;
251 ((unsigned char*)&Uuid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
252 temp_clock_hi_and_version >>= 8;
253 ((unsigned char*)&Uuid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
255 ((unsigned char*)Uuid->Data4)[1] = (unsigned char)temp_clock_seq;
256 temp_clock_seq >>= 8;
257 ((unsigned char*)Uuid->Data4)[0] = (unsigned char)temp_clock_seq;
259 ((unsigned char*)Uuid->Data4)[2] = a[0];
260 ((unsigned char*)Uuid->Data4)[3] = a[1];
261 ((unsigned char*)Uuid->Data4)[4] = a[2];
262 ((unsigned char*)Uuid->Data4)[5] = a[3];
263 ((unsigned char*)Uuid->Data4)[6] = a[4];
264 ((unsigned char*)Uuid->Data4)[7] = a[5];
266 TRACE("%s", debugstr_guid(Uuid));
271 /*************************************************************************
272 * RpcStringFreeA [RPCRT4.436]
274 * Frees a character string allocated by the RPC run-time library.
278 * S_OK if successful.
280 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
282 HeapFree( GetProcessHeap(), 0, *String);
287 /*************************************************************************
288 * UuidToStringA [RPCRT4.450]
290 * Converts a UUID to a string.
292 * UUID format is 8 hex digits, followed by a hyphen then three groups of
293 * 4 hex digits each followed by a hyphen and then 12 hex digits
297 * S_OK if successful.
298 * S_OUT_OF_MEMORY if unsucessful.
300 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
302 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
305 /* FIXME: this should be RPC_S_OUT_OF_MEMORY */
307 return ERROR_OUTOFMEMORY;
309 sprintf(*StringUuid, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
310 Uuid->Data1, Uuid->Data2, Uuid->Data3,
311 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
312 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
313 Uuid->Data4[6], Uuid->Data4[7] );
315 return S_OK; /*FIXME: this should be RPC_S_OK */