14 #include <ole32/ole32.h>
15 #include <storage32.h>
19 /***********************************************************************
23 * hinstDLL [I] handle to the DLL's instance
25 * lpvReserved [I] reserved, must be NULL
33 RPCRT4_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
36 case DLL_PROCESS_ATTACH:
39 case DLL_PROCESS_DETACH:
46 /*************************************************************************
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.
58 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
64 static char has_init = 0;
66 static int adjustment = 0;
67 static struct timeval last = {0, 0};
68 static UINT16 clock_seq;
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;
75 struct ifreq ifr, *ifrp;
81 /* Have we already tried to get the MAC address? */
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)
91 # define max(a,b) ((a) > (b) ? (a) : (b))
93 # define ifreq_size(i) max(sizeof(struct ifreq),\
94 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
96 # define ifreq_size(i) sizeof(struct ifreq)
97 # endif /* HAVE_SA_LEN */
99 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
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;
110 memset(buf, 0, sizeof(buf));
111 ifc.ifc_len = sizeof(buf);
113 /* get the ifconf interface */
114 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
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;
125 /* loop through the interfaces, looking for a valid one */
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)
134 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
137 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
139 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
141 /* XXX we don't have a way of getting the hardware address */
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])
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;
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 */
178 /* generate time element of GUID */
180 /* Assume that the gettimeofday() has microsecond granularity */
181 #define MAX_ADJUSTMENT 10
184 gettimeofday(&tv, 0);
185 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
186 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
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;
196 } else if ((tv.tv_sec == last.tv_sec) &&
197 (tv.tv_usec == last.tv_usec)) {
198 if (adjustment >= MAX_ADJUSTMENT)
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;
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;
214 /* pack the information into the GUID structure */
216 ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
218 ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
220 ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
222 ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
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;
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;
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;
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];
243 Print(MAX_TRACE, ("%s\n", PRINT_GUID(Uuid)));
249 /*************************************************************************
250 * RpcStringFreeA [RPCRT4.436]
252 * Frees a character string allocated by the RPC run-time library.
256 * S_OK if successful.
258 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
260 HeapFree( GetProcessHeap(), 0, *String);
265 /*************************************************************************
266 * UuidToStringA [RPCRT4.450]
268 * Converts a UUID to a string.
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
275 * S_OK if successful.
276 * S_OUT_OF_MEMORY if unsucessful.
278 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
280 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
283 /* FIXME: this should be RPC_S_OUT_OF_MEMORY */
285 return ERROR_OUTOFMEMORY;
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] );
293 return S_OK; /*FIXME: this should be RPC_S_OK */
296 /***********************************************************************
297 * NdrDllRegisterProxy (RPCRT4.@)
299 HRESULT WINAPI NdrDllRegisterProxy(
300 HMODULE hDll, /* [in] */
301 void **pProxyFileList, /* [???] FIXME: const ProxyFileInfo ** */
302 const CLSID *pclsid /* [in] */
304 Print(MIN_TRACE, ("(%x,%p,%s), stub!\n",hDll,pProxyFileList,PRINT_GUID(pclsid)));