16-bit resource size must be scaled by alignment.
[wine] / dlls / rpcrt4 / rpcrt4_main.c
1 /*
2  *  RPCRT4
3  *
4  */
5
6 #include "config.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <sys/time.h>
13 #include <unistd.h>
14
15 #include "windef.h"
16 #include "wine/windef16.h"
17 #include "winerror.h"
18 #include "winbase.h"
19 #include "rpc.h"
20
21 #ifdef HAVE_SYS_FILE_H
22 # include <sys/file.h>
23 #endif
24 #include <sys/ioctl.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
27 #endif
28 #ifdef HAVE_SYS_SOCKIO_H
29 # include <sys/sockio.h>
30 #endif
31 #ifdef HAVE_NET_IF_H
32 # include <net/if.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37
38 #include "debugtools.h"
39
40 DEFAULT_DEBUG_CHANNEL(ole);
41
42 /***********************************************************************
43  * RPCRT4_LibMain
44  *
45  * PARAMS
46  *     hinstDLL    [I] handle to the DLL's instance
47  *     fdwReason   [I]
48  *     lpvReserved [I] reserved, must be NULL
49  *
50  * RETURNS
51  *     Success: TRUE
52  *     Failure: FALSE
53  */
54
55 static DWORD RPCRT4_dwProcessesAttached = 0;
56
57 BOOL WINAPI
58 RPCRT4_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
59 {
60     switch (fdwReason) {
61     case DLL_PROCESS_ATTACH:
62         RPCRT4_dwProcessesAttached++;
63         break;
64
65     case DLL_PROCESS_DETACH:
66         RPCRT4_dwProcessesAttached--;
67         break;      
68     }
69
70     return TRUE;
71 }
72
73 /*************************************************************************
74  *           UuidCreate   [RPCRT4]
75  *
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.
80  *
81  * RETURNS
82  *
83  *  S_OK if successful.
84  */
85 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
86 {
87    static char has_init = 0;
88    unsigned char a[6];
89    static int                      adjustment = 0;
90    static struct timeval           last = {0, 0};
91    static UINT16                   clock_seq;
92    struct timeval                  tv;
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;
96 #ifdef HAVE_NET_IF_H
97    int             sd;
98    struct ifreq    ifr, *ifrp;
99    struct ifconf   ifc;
100    char buf[1024];
101    int             n, i;
102 #endif
103    
104    /* Have we already tried to get the MAC address? */
105    if (!has_init) {
106 #ifdef HAVE_NET_IF_H
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)
111        */
112 #ifdef HAVE_SA_LEN
113 #  ifndef max
114 #   define max(a,b) ((a) > (b) ? (a) : (b))
115 #  endif
116 #  define ifreq_size(i) max(sizeof(struct ifreq),\
117 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
118 # else
119 #  define ifreq_size(i) sizeof(struct ifreq)
120 # endif /* HAVE_SA_LEN */
121       
122       sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
123       if (sd < 0) {
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;
132       } else {
133          memset(buf, 0, sizeof(buf));
134          ifc.ifc_len = sizeof(buf);
135          ifc.ifc_buf = buf;
136          /* get the ifconf interface */
137          if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
138             close(sd);
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;
147          } else {
148             /* loop through the interfaces, looking for a valid one */
149             n = ifc.ifc_len;
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)
156                    continue;
157                memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
158 # else
159 #  ifdef SIOCGENADDR
160                if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
161                    continue;
162                memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
163 #  else
164                /* XXX we don't have a way of getting the hardware address */
165                close(sd);
166                a[0] = 0;
167                break;
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])
172                    continue;
173                                                                 
174                goto valid_address;
175             }
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;
184
185             valid_address:
186             close(sd);
187          }
188       }
189 #else
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 */
198       has_init = 1;
199    }
200    
201    /* generate time element of GUID */
202    
203    /* Assume that the gettimeofday() has microsecond granularity */
204 #define MAX_ADJUSTMENT 10
205                      
206    try_again:
207    gettimeofday(&tv, 0);
208    if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
209       clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
210       clock_seq &= 0x1FFF;
211       last = tv;
212       last.tv_sec--;
213    }
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;
218       adjustment = 0;
219    } else if ((tv.tv_sec == last.tv_sec) &&
220               (tv.tv_usec == last.tv_usec)) {
221       if (adjustment >= MAX_ADJUSTMENT)
222           goto try_again;
223       adjustment++;
224    } else
225        adjustment = 0;
226    
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;
230    
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;
236    
237    /* pack the information into the GUID structure */
238    
239    ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
240    clock_low >>= 8;
241    ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
242    clock_low >>= 8;
243    ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
244    clock_low >>= 8;
245    ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
246    
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;
250    
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;
254       
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;
258    
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];
265    
266    TRACE("%s", debugstr_guid(Uuid));
267    
268    return S_OK;
269 }
270
271 /*************************************************************************
272  *           RpcStringFreeA   [RPCRT4.436]
273  *
274  * Frees a character string allocated by the RPC run-time library.
275  *
276  * RETURNS
277  *
278  *  S_OK if successful.
279  */
280 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
281 {
282   HeapFree( GetProcessHeap(), 0, *String);
283
284   return S_OK;
285 }
286
287 /*************************************************************************
288  *           UuidToStringA   [RPCRT4.450]
289  *
290  * Converts a UUID to a string.
291  *
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
294  *
295  * RETURNS
296  *
297  *  S_OK if successful.
298  *  S_OUT_OF_MEMORY if unsucessful.
299  */
300 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
301 {
302   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
303
304
305   /* FIXME: this should be RPC_S_OUT_OF_MEMORY */
306   if(!(*StringUuid))
307     return ERROR_OUTOFMEMORY;
308
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] );
314
315   return S_OK; /*FIXME: this should be RPC_S_OK */
316 }