Reimplement several RpcServer.* methods in rpc_server.c.
[wine/testsucceed.git] / dlls / rpcrt4 / rpcrt4_main.c
blob94e23fce29aa12b2f7a4e0092781693f7e8176ea
1 /*
2 * RPCRT4
4 * Copyright 2000 Huw D M Davies for Codeweavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winerror.h"
36 #include "winbase.h"
37 #include "wine/unicode.h"
38 #include "rpc.h"
40 #include "ole2.h"
41 #include "rpcndr.h"
42 #include "rpcproxy.h"
44 #ifdef HAVE_SYS_FILE_H
45 # include <sys/file.h>
46 #endif
47 #ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
49 #endif
50 #ifdef HAVE_SYS_SOCKET_H
51 # include <sys/socket.h>
52 #endif
53 #ifdef HAVE_SYS_SOCKIO_H
54 # include <sys/sockio.h>
55 #endif
56 #ifdef HAVE_NET_IF_H
57 # include <net/if.h>
58 #endif
59 #ifdef HAVE_NETINET_IN_H
60 # include <netinet/in.h>
61 #endif
63 #include "rpc_binding.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(ole);
69 static UUID uuid_nil;
71 /***********************************************************************
72 * RPCRT4_LibMain
74 * PARAMS
75 * hinstDLL [I] handle to the DLL's instance
76 * fdwReason [I]
77 * lpvReserved [I] reserved, must be NULL
79 * RETURNS
80 * Success: TRUE
81 * Failure: FALSE
84 BOOL WINAPI
85 RPCRT4_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
87 switch (fdwReason) {
88 case DLL_PROCESS_ATTACH:
89 break;
91 case DLL_PROCESS_DETACH:
92 break;
95 return TRUE;
98 /*************************************************************************
99 * RpcStringFreeA [RPCRT4.@]
101 * Frees a character string allocated by the RPC run-time library.
103 * RETURNS
105 * S_OK if successful.
107 RPC_STATUS WINAPI RpcStringFreeA(LPSTR* String)
109 HeapFree( GetProcessHeap(), 0, *String);
111 return RPC_S_OK;
114 /*************************************************************************
115 * RpcStringFreeW [RPCRT4.@]
117 * Frees a character string allocated by the RPC run-time library.
119 * RETURNS
121 * S_OK if successful.
123 RPC_STATUS WINAPI RpcStringFreeW(LPWSTR* String)
125 HeapFree( GetProcessHeap(), 0, *String);
127 return RPC_S_OK;
130 /*************************************************************************
131 * UuidCompare [RPCRT4.@]
133 * (an educated-guess implementation)
135 * PARAMS
136 * UUID *Uuid1 [I] Uuid to compare
137 * UUID *Uuid2 [I] Uuid to compare
138 * RPC_STATUS *Status [O] returns RPC_S_OK
140 * RETURNS
141 * -1 if Uuid1 is less than Uuid2
142 * 0 if Uuid1 and Uuid2 are equal
143 * 1 if Uuid1 is greater than Uuid2
145 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
147 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
148 *Status = RPC_S_OK;
149 if (!Uuid1) Uuid1 = &uuid_nil;
150 if (!Uuid2) Uuid2 = &uuid_nil;
151 if (Uuid1 == Uuid2) return 0;
152 return memcmp(Uuid1, Uuid2, sizeof(UUID));
155 /*************************************************************************
156 * UuidEqual [RPCRT4.@]
158 * PARAMS
159 * UUID *Uuid1 [I] Uuid to compare
160 * UUID *Uuid2 [I] Uuid to compare
161 * RPC_STATUS *Status [O] returns RPC_S_OK
163 * RETURNS
164 * TRUE/FALSE
166 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
168 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
169 return !UuidCompare(Uuid1, Uuid2, Status);
172 /*************************************************************************
173 * UuidIsNil [RPCRT4.@]
175 * PARAMS
176 * UUID *Uuid [I] Uuid to compare
177 * RPC_STATUS *Status [O] retuns RPC_S_OK
179 * RETURNS
180 * TRUE/FALSE
182 int WINAPI UuidIsNil(UUID *uuid, RPC_STATUS *Status)
184 TRACE("(%s)\n", debugstr_guid(uuid));
185 *Status = RPC_S_OK;
186 if (!uuid) return TRUE;
187 return !memcmp(uuid, &uuid_nil, sizeof(UUID));
190 /*************************************************************************
191 * UuidCreateNil [RPCRT4.@]
193 * PARAMS
194 * UUID *Uuid [O] returns a nil UUID
196 * RETURNS
197 * RPC_S_OK
199 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
201 *Uuid = uuid_nil;
202 return RPC_S_OK;
205 /*************************************************************************
206 * UuidCreate [RPCRT4.@]
208 * Creates a 128bit UUID.
209 * Implemented according the DCE specification for UUID generation.
210 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
211 * Copyright (C) 1996, 1997 Theodore Ts'o.
213 * RETURNS
215 * S_OK if successful.
217 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
219 static char has_init = 0;
220 static unsigned char a[6];
221 static int adjustment = 0;
222 static struct timeval last = {0, 0};
223 static WORD clock_seq;
224 struct timeval tv;
225 unsigned long long clock_reg;
226 DWORD clock_high, clock_low;
227 WORD temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
228 #ifdef HAVE_NET_IF_H
229 int sd;
230 struct ifreq ifr, *ifrp;
231 struct ifconf ifc;
232 char buf[1024];
233 int n, i;
234 #endif
236 /* Have we already tried to get the MAC address? */
237 if (!has_init) {
238 #ifdef HAVE_NET_IF_H
239 /* BSD 4.4 defines the size of an ifreq to be
240 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
241 * However, under earlier systems, sa_len isn't present, so
242 * the size is just sizeof(struct ifreq)
244 #ifdef HAVE_SOCKADDR_SA_LEN
245 # ifndef max
246 # define max(a,b) ((a) > (b) ? (a) : (b))
247 # endif
248 # define ifreq_size(i) max(sizeof(struct ifreq),\
249 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
250 # else
251 # define ifreq_size(i) sizeof(struct ifreq)
252 # endif /* defined(HAVE_SOCKADDR_SA_LEN) */
254 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
255 if (sd < 0) {
256 /* if we can't open a socket, just use random numbers */
257 /* set the multicast bit to prevent conflicts with real cards */
258 a[0] = (rand() & 0xff) | 0x80;
259 a[1] = rand() & 0xff;
260 a[2] = rand() & 0xff;
261 a[3] = rand() & 0xff;
262 a[4] = rand() & 0xff;
263 a[5] = rand() & 0xff;
264 } else {
265 memset(buf, 0, sizeof(buf));
266 ifc.ifc_len = sizeof(buf);
267 ifc.ifc_buf = buf;
268 /* get the ifconf interface */
269 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
270 close(sd);
271 /* no ifconf, so just use random numbers */
272 /* set the multicast bit to prevent conflicts with real cards */
273 a[0] = (rand() & 0xff) | 0x80;
274 a[1] = rand() & 0xff;
275 a[2] = rand() & 0xff;
276 a[3] = rand() & 0xff;
277 a[4] = rand() & 0xff;
278 a[5] = rand() & 0xff;
279 } else {
280 /* loop through the interfaces, looking for a valid one */
281 n = ifc.ifc_len;
282 for (i = 0; i < n; i+= ifreq_size(ifr) ) {
283 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
284 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
285 /* try to get the address for this interface */
286 # ifdef SIOCGIFHWADDR
287 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
288 continue;
289 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
290 # else
291 # ifdef SIOCGENADDR
292 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
293 continue;
294 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
295 # else
296 /* XXX we don't have a way of getting the hardware address */
297 close(sd);
298 a[0] = 0;
299 break;
300 # endif /* SIOCGENADDR */
301 # endif /* SIOCGIFHWADDR */
302 /* make sure it's not blank */
303 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
304 continue;
306 goto valid_address;
308 /* if we didn't find a valid address, make a random one */
309 /* once again, set multicast bit to avoid conflicts */
310 a[0] = (rand() & 0xff) | 0x80;
311 a[1] = rand() & 0xff;
312 a[2] = rand() & 0xff;
313 a[3] = rand() & 0xff;
314 a[4] = rand() & 0xff;
315 a[5] = rand() & 0xff;
317 valid_address:
318 close(sd);
321 #else
322 /* no networking info, so generate a random address */
323 a[0] = (rand() & 0xff) | 0x80;
324 a[1] = rand() & 0xff;
325 a[2] = rand() & 0xff;
326 a[3] = rand() & 0xff;
327 a[4] = rand() & 0xff;
328 a[5] = rand() & 0xff;
329 #endif /* HAVE_NET_IF_H */
330 has_init = 1;
333 /* generate time element of GUID */
335 /* Assume that the gettimeofday() has microsecond granularity */
336 #define MAX_ADJUSTMENT 10
338 try_again:
339 gettimeofday(&tv, 0);
340 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
341 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
342 clock_seq &= 0x1FFF;
343 last = tv;
344 last.tv_sec--;
346 if ((tv.tv_sec < last.tv_sec) ||
347 ((tv.tv_sec == last.tv_sec) &&
348 (tv.tv_usec < last.tv_usec))) {
349 clock_seq = (clock_seq+1) & 0x1FFF;
350 adjustment = 0;
351 } else if ((tv.tv_sec == last.tv_sec) &&
352 (tv.tv_usec == last.tv_usec)) {
353 if (adjustment >= MAX_ADJUSTMENT)
354 goto try_again;
355 adjustment++;
356 } else
357 adjustment = 0;
359 clock_reg = tv.tv_usec*10 + adjustment;
360 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
361 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
363 clock_high = clock_reg >> 32;
364 clock_low = clock_reg;
365 temp_clock_seq = clock_seq | 0x8000;
366 temp_clock_mid = (WORD)clock_high;
367 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
369 /* pack the information into the GUID structure */
371 ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
372 clock_low >>= 8;
373 ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
374 clock_low >>= 8;
375 ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
376 clock_low >>= 8;
377 ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
379 ((unsigned char*)&Uuid->Data2)[1] = (unsigned char)temp_clock_mid;
380 temp_clock_mid >>= 8;
381 ((unsigned char*)&Uuid->Data2)[0] = (unsigned char)temp_clock_mid;
383 ((unsigned char*)&Uuid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
384 temp_clock_hi_and_version >>= 8;
385 ((unsigned char*)&Uuid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
387 ((unsigned char*)Uuid->Data4)[1] = (unsigned char)temp_clock_seq;
388 temp_clock_seq >>= 8;
389 ((unsigned char*)Uuid->Data4)[0] = (unsigned char)temp_clock_seq;
391 ((unsigned char*)Uuid->Data4)[2] = a[0];
392 ((unsigned char*)Uuid->Data4)[3] = a[1];
393 ((unsigned char*)Uuid->Data4)[4] = a[2];
394 ((unsigned char*)Uuid->Data4)[5] = a[3];
395 ((unsigned char*)Uuid->Data4)[6] = a[4];
396 ((unsigned char*)Uuid->Data4)[7] = a[5];
398 TRACE("%s\n", debugstr_guid(Uuid));
400 return RPC_S_OK;
404 /*************************************************************************
405 * UuidCreateSequential [RPCRT4.@]
407 * Creates a 128bit UUID by calling UuidCreate.
408 * New API in Win 2000
410 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
412 return UuidCreate (Uuid);
416 /*************************************************************************
417 * UuidHash [RPCRT4.@]
419 * Generates a hash value for a given UUID
421 * Code based on FreeDCE implementation
424 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
426 BYTE *data = (BYTE*)uuid;
427 short c0 = 0, c1 = 0, x, y;
428 int i;
430 TRACE("(%s)\n", debugstr_guid(uuid));
432 for (i=0; i<sizeof(UUID); i++) {
433 c0 += data[i];
434 c1 += c0;
437 x = -c1 % 255;
438 if (x < 0) x += 255;
440 y = (c1 - c0) % 255;
441 if (y < 0) y += 255;
443 *Status = RPC_S_OK;
444 return y*256 + x;
447 /*************************************************************************
448 * UuidToStringA [RPCRT4.@]
450 * Converts a UUID to a string.
452 * UUID format is 8 hex digits, followed by a hyphen then three groups of
453 * 4 hex digits each followed by a hyphen and then 12 hex digits
455 * RETURNS
457 * S_OK if successful.
458 * S_OUT_OF_MEMORY if unsucessful.
460 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, LPSTR* StringUuid)
462 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
464 if(!(*StringUuid))
465 return RPC_S_OUT_OF_MEMORY;
467 sprintf(*StringUuid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
468 Uuid->Data1, Uuid->Data2, Uuid->Data3,
469 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
470 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
471 Uuid->Data4[6], Uuid->Data4[7] );
473 return RPC_S_OK;
476 /*************************************************************************
477 * UuidToStringW [RPCRT4.@]
479 * Converts a UUID to a string.
481 * S_OK if successful.
482 * S_OUT_OF_MEMORY if unsucessful.
484 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, LPWSTR* StringUuid)
486 char buf[37];
488 sprintf(buf, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
489 Uuid->Data1, Uuid->Data2, Uuid->Data3,
490 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
491 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
492 Uuid->Data4[6], Uuid->Data4[7] );
494 *StringUuid = RPCRT4_strdupAtoW(buf);
496 if(!(*StringUuid))
497 return RPC_S_OUT_OF_MEMORY;
499 return RPC_S_OK;
502 static const BYTE hex2bin[] =
504 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
505 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
506 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
507 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
508 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
509 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
510 0,10,11,12,13,14,15 /* 0x60 */
513 /***********************************************************************
514 * UuidFromStringA (RPCRT4.@)
516 RPC_STATUS WINAPI UuidFromStringA(LPSTR str, UUID *uuid)
518 BYTE *s = (BYTE *)str;
519 int i;
521 if (!s) return UuidCreateNil( uuid );
523 if (strlen(s) != 36) return RPC_S_INVALID_STRING_UUID;
525 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
526 return RPC_S_INVALID_STRING_UUID;
528 for (i=0; i<36; i++)
530 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
531 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
534 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
536 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
537 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
538 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
539 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
541 /* these are just sequential bytes */
542 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
543 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
544 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
545 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
546 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
547 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
548 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
549 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
550 return RPC_S_OK;
554 /***********************************************************************
555 * UuidFromStringW (RPCRT4.@)
557 RPC_STATUS WINAPI UuidFromStringW(LPWSTR s, UUID *uuid)
559 int i;
561 if (!s) return UuidCreateNil( uuid );
563 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
565 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
566 return RPC_S_INVALID_STRING_UUID;
568 for (i=0; i<36; i++)
570 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
571 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
574 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
576 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
577 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
578 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
579 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
581 /* these are just sequential bytes */
582 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
583 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
584 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
585 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
586 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
587 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
588 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
589 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
590 return RPC_S_OK;
593 /***********************************************************************
594 * DllRegisterServer (RPCRT4.@)
597 HRESULT WINAPI RPCRT4_DllRegisterServer( void )
599 FIXME( "(): stub\n" );
600 return S_OK;