:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / ole32 / rpcrt4_main.c
1 /*
2  *  RPCRT4
3  *
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 #include <sys/time.h>
11 #include <unistd.h>
12
13 #include <windows.h>
14 #include <ole32/ole32.h>
15 #include <storage32.h>
16
17 #include <debug.h>
18
19 /***********************************************************************
20  * RPCRT4_LibMain
21  *
22  * PARAMS
23  *     hinstDLL    [I] handle to the DLL's instance
24  *     fdwReason   [I]
25  *     lpvReserved [I] reserved, must be NULL
26  *
27  * RETURNS
28  *     Success: TRUE
29  *     Failure: FALSE
30  */
31
32 BOOL WINAPI
33 RPCRT4_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
34 {
35     switch (fdwReason) {
36     case DLL_PROCESS_ATTACH:
37         break;
38
39     case DLL_PROCESS_DETACH:
40         break;      
41     }
42
43     return TRUE;
44 }
45
46 /*************************************************************************
47  *           UuidCreate   [RPCRT4]
48  *
49  * Creates a 128bit UUID.
50  * Implemented according the DCE specification for UUID generation.
51  * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
52  * Copyright (C) 1996, 1997 Theodore Ts'o.
53  *
54  * RETURNS
55  *
56  *  S_OK if successful.
57  */
58 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
59 {
60 #if 1
61   UNIMPLEMENTED;
62   return S_OK;
63 #else
64    static char has_init = 0;
65    unsigned char a[6];
66    static int                      adjustment = 0;
67    static struct timeval           last = {0, 0};
68    static UINT16                   clock_seq;
69    struct timeval                  tv;
70    unsigned long long              clock_reg;
71    UINT clock_high, clock_low;
72    UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
73 #ifdef HAVE_NET_IF_H
74    int             sd;
75    struct ifreq    ifr, *ifrp;
76    struct ifconf   ifc;
77    char buf[1024];
78    int             n, i;
79 #endif
80    
81    /* Have we already tried to get the MAC address? */
82    if (!has_init) {
83 #ifdef HAVE_NET_IF_H
84       /* BSD 4.4 defines the size of an ifreq to be
85        * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
86        * However, under earlier systems, sa_len isn't present, so
87        *  the size is just sizeof(struct ifreq)
88        */
89 #ifdef HAVE_SA_LEN
90 #  ifndef max
91 #   define max(a,b) ((a) > (b) ? (a) : (b))
92 #  endif
93 #  define ifreq_size(i) max(sizeof(struct ifreq),\
94 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
95 # else
96 #  define ifreq_size(i) sizeof(struct ifreq)
97 # endif /* HAVE_SA_LEN */
98       
99       sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
100       if (sd < 0) {
101          /* if we can't open a socket, just use random numbers */
102          /* set the multicast bit to prevent conflicts with real cards */
103          a[0] = (rand() & 0xff) | 0x80;
104          a[1] = rand() & 0xff;
105          a[2] = rand() & 0xff;
106          a[3] = rand() & 0xff;
107          a[4] = rand() & 0xff;
108          a[5] = rand() & 0xff;
109       } else {
110          memset(buf, 0, sizeof(buf));
111          ifc.ifc_len = sizeof(buf);
112          ifc.ifc_buf = buf;
113          /* get the ifconf interface */
114          if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
115             close(sd);
116             /* no ifconf, so just use random numbers */
117             /* set the multicast bit to prevent conflicts with real cards */
118             a[0] = (rand() & 0xff) | 0x80;
119             a[1] = rand() & 0xff;
120             a[2] = rand() & 0xff;
121             a[3] = rand() & 0xff;
122             a[4] = rand() & 0xff;
123             a[5] = rand() & 0xff;
124          } else {
125             /* loop through the interfaces, looking for a valid one */
126             n = ifc.ifc_len;
127             for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
128                ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
129                strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
130                /* try to get the address for this interface */
131 # ifdef SIOCGIFHWADDR
132                if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
133                    continue;
134                memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
135 # else
136 #  ifdef SIOCGENADDR
137                if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
138                    continue;
139                memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
140 #  else
141                /* XXX we don't have a way of getting the hardware address */
142                close(sd);
143                a[0] = 0;
144                break;
145 #  endif /* SIOCGENADDR */
146 # endif /* SIOCGIFHWADDR */
147                /* make sure it's not blank */
148                if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
149                    continue;
150                                                                 
151                goto valid_address;
152             }
153             /* if we didn't find a valid address, make a random one */
154             /* once again, set multicast bit to avoid conflicts */
155             a[0] = (rand() & 0xff) | 0x80;
156             a[1] = rand() & 0xff;
157             a[2] = rand() & 0xff;
158             a[3] = rand() & 0xff;
159             a[4] = rand() & 0xff;
160             a[5] = rand() & 0xff;
161
162             valid_address:
163             close(sd);
164          }
165       }
166 #else
167       /* no networking info, so generate a random address */
168       a[0] = (rand() & 0xff) | 0x80;
169       a[1] = rand() & 0xff;
170       a[2] = rand() & 0xff;
171       a[3] = rand() & 0xff;
172       a[4] = rand() & 0xff;
173       a[5] = rand() & 0xff;
174 #endif /* HAVE_NET_IF_H */
175       has_init = 1;
176    }
177    
178    /* generate time element of GUID */
179    
180    /* Assume that the gettimeofday() has microsecond granularity */
181 #define MAX_ADJUSTMENT 10
182                      
183    try_again:
184    gettimeofday(&tv, 0);
185    if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
186       clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
187       clock_seq &= 0x1FFF;
188       last = tv;
189       last.tv_sec--;
190    }
191    if ((tv.tv_sec < last.tv_sec) ||
192        ((tv.tv_sec == last.tv_sec) &&
193         (tv.tv_usec < last.tv_usec))) {
194       clock_seq = (clock_seq+1) & 0x1FFF;
195       adjustment = 0;
196    } else if ((tv.tv_sec == last.tv_sec) &&
197               (tv.tv_usec == last.tv_usec)) {
198       if (adjustment >= MAX_ADJUSTMENT)
199           goto try_again;
200       adjustment++;
201    } else
202        adjustment = 0;
203    
204    clock_reg = tv.tv_usec*10 + adjustment;
205    clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
206    clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
207    
208    clock_high = clock_reg >> 32;
209    clock_low = clock_reg;
210    temp_clock_seq = clock_seq | 0x8000;
211    temp_clock_mid = (UINT16)clock_high;
212    temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
213    
214    /* pack the information into the GUID structure */
215    
216    ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
217    clock_low >>= 8;
218    ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
219    clock_low >>= 8;
220    ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
221    clock_low >>= 8;
222    ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
223    
224    ((unsigned char*)&Uuid->Data2)[1] = (unsigned char)temp_clock_mid;
225    temp_clock_mid >>= 8;
226    ((unsigned char*)&Uuid->Data2)[0] = (unsigned char)temp_clock_mid;
227    
228    ((unsigned char*)&Uuid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
229    temp_clock_hi_and_version >>= 8;
230    ((unsigned char*)&Uuid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
231       
232    ((unsigned char*)Uuid->Data4)[1] = (unsigned char)temp_clock_seq;
233    temp_clock_seq >>= 8;
234    ((unsigned char*)Uuid->Data4)[0] = (unsigned char)temp_clock_seq;
235    
236    ((unsigned char*)Uuid->Data4)[2] = a[0];
237    ((unsigned char*)Uuid->Data4)[3] = a[1];
238    ((unsigned char*)Uuid->Data4)[4] = a[2];
239    ((unsigned char*)Uuid->Data4)[5] = a[3];
240    ((unsigned char*)Uuid->Data4)[6] = a[4];
241    ((unsigned char*)Uuid->Data4)[7] = a[5];
242    
243    Print(MAX_TRACE, ("%s\n", PRINT_GUID(Uuid)));
244    
245    return S_OK;
246 #endif
247 }
248
249 /*************************************************************************
250  *           RpcStringFreeA   [RPCRT4.436]
251  *
252  * Frees a character string allocated by the RPC run-time library.
253  *
254  * RETURNS
255  *
256  *  S_OK if successful.
257  */
258 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
259 {
260   HeapFree( GetProcessHeap(), 0, *String);
261
262   return S_OK;
263 }
264
265 /*************************************************************************
266  *           UuidToStringA   [RPCRT4.450]
267  *
268  * Converts a UUID to a string.
269  *
270  * UUID format is 8 hex digits, followed by a hyphen then three groups of
271  * 4 hex digits each followed by a hyphen and then 12 hex digits
272  *
273  * RETURNS
274  *
275  *  S_OK if successful.
276  *  S_OUT_OF_MEMORY if unsucessful.
277  */
278 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
279 {
280   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
281
282
283   /* FIXME: this should be RPC_S_OUT_OF_MEMORY */
284   if(!(*StringUuid))
285     return ERROR_OUTOFMEMORY;
286
287        sprintf(*StringUuid, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
288                  Uuid->Data1, Uuid->Data2, Uuid->Data3,
289                  Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
290                  Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
291                  Uuid->Data4[6], Uuid->Data4[7] );
292
293   return S_OK; /*FIXME: this should be RPC_S_OK */
294 }
295
296 /***********************************************************************
297  *              NdrDllRegisterProxy (RPCRT4.@)
298  */
299 HRESULT WINAPI NdrDllRegisterProxy(
300   HMODULE hDll,          /* [in] */
301   void **pProxyFileList, /* [???] FIXME: const ProxyFileInfo ** */
302   const CLSID *pclsid    /* [in] */
303 ) {
304   Print(MIN_TRACE, ("(%x,%p,%s), stub!\n",hDll,pProxyFileList,PRINT_GUID(pclsid)));
305   return S_OK;
306 }