[MSWSOCK]
[reactos.git] / reactos / dll / win32 / mswsock / nsplookup.c
1 #include "precomp.h"
2
3 #include <stdlib.h>
4 #include <ws2spi.h>
5 #include <nspapi.h>
6 #include <windef.h>
7 #include <winuser.h>
8 #include <windns.h>
9 #include <guiddef.h>
10 #include <svcguid.h>
11 #include <iptypes.h>
12 #include <strsafe.h>
13 #include <winreg.h>
14
15 #include "mswhelper.h"
16
17 #define NDEBUG
18 #include <debug.h>
19
20 #define NSP_CALLID_DNS 0x0001
21 #define NSP_CALLID_HOSTNAME 0x0002
22 #define NSP_CALLID_HOSTBYNAME 0x0003
23 #define NSP_CALLID_SERVICEBYNAME 0x0004
24
25 #ifndef BUFSIZ
26 #define BUFSIZ 1024
27 #endif // BUFSIZ
28 #ifndef WS2_INTERNAL_MAX_ALIAS
29 #define WS2_INTERNAL_MAX_ALIAS 512
30 #endif // WS2_INTERNAL_MAX_ALIAS
31
32 //#define IP_LOCALHOST 0x0100007F
33
34 //#define NSP_REDIRECT
35
36 typedef struct {
37 WCHAR* hostnameW;
38 DWORD addr4;
39 WCHAR* servnameW;
40 WCHAR* servprotoW;
41 CHAR** servaliasesA; /* array */
42 WORD servport;
43 } WSHOSTINFOINTERN, *PWSHOSTINFOINTERN;
44
45 typedef struct {
46 GUID providerId; /* Provider-ID */
47 DWORD dwControlFlags; /* dwControlFlags (WSALookupServiceBegin) */
48 DWORD CallID; /* List for LookupServiceNext-Calls */
49 DWORD CallIDCounter; /* call-count of the current CallID. */
50 WCHAR* hostnameW; /* hostbyname */
51 #ifdef NSP_REDIRECT
52 HANDLE rdrLookup;
53 NSP_ROUTINE rdrproc;
54 #endif
55 } WSHANDLEINTERN, *PWSHANDLEINTERN;
56
57 static const GUID guid_NULL = {0};
58 static const GUID guid_HOSTNAME = SVCID_HOSTNAME;
59 static const GUID guid_INET_HOSTADDRBYINETSTRING = SVCID_INET_HOSTADDRBYINETSTRING;
60 static const GUID guid_INET_HOSTADDRBYNAME = SVCID_INET_HOSTADDRBYNAME;
61 static const GUID guid_INET_SERVICEBYNAME = SVCID_INET_SERVICEBYNAME;
62
63 /* GUIDs - maybe they should be loaded from registry? */
64 /* Namespace: 32 */
65 static const GUID guid_mswsock_TcpIp = {/*Data1:*/ 0x22059D40,
66 /*Data2:*/ 0x7E9E,
67 /*Data3:*/ 0x11CF,
68 /*Data4:*/ {0xAE, 0x5A, 0x00, 0xAA, 0x00, 0xA7, 0x11, 0x2B}};
69
70 /* {6642243A-3BA8-4AA6-BAA5-2E0BD71FDD83} */
71 /* Namespace: 15 */
72 static const GUID guid_mswsock_NLA = {/*Data1:*/ 0x6642243A,
73 /*Data2:*/ 0x3BA8,
74 /*Data3:*/ 0x4AA6,
75 /*Data4:*/ {0xBA, 0xA5, 0x2E, 0x0B, 0xD7, 0x1F, 0xDD, 0x83}};
76
77 #ifdef NSP_REDIRECT
78
79 typedef INT
80 (CALLBACK *lpRdrNSPStartup)(
81 LPGUID lpProviderId,
82 LPNSP_ROUTINE lpRout);
83
84 const rdrLib = "mswsock.dll-original";
85 lpRdrNSPStartup rdrNSPStartup;
86 HANDLE hLib;
87 NSP_ROUTINE rdrproc_tcpip;
88 NSP_ROUTINE rdrproc_nla;
89
90 #endif /* NSP_REDIRECT */
91
92 /* Forwards */
93 INT
94 WINAPI
95 mswNSPStartup(
96 LPGUID lpProviderId,
97 LPNSP_ROUTINE lpRout);
98
99 INT
100 NSP_LookupServiceBeginW(
101 PWSHANDLEINTERN data,
102 CHAR* hostnameA,
103 WCHAR* hostnameW,
104 DWORD CallID);
105
106 INT
107 NSP_LookupServiceNextW(
108 _In_ PWSHANDLEINTERN data,
109 _In_ DWORD CallID,
110 _Inout_ LPWSAQUERYSETW lpRes,
111 _Inout_ LPDWORD lpResLen);
112
113 INT
114 NSP_GetHostNameHeapAllocW(
115 _Out_ WCHAR** hostname);
116
117 INT
118 NSP_GetHostByNameHeapAllocW(
119 _In_ WCHAR* name,
120 _In_ GUID* lpProviderId,
121 _Out_ PWSHOSTINFOINTERN hostinfo);
122
123 INT
124 NSP_GetServiceByNameHeapAllocW(
125 _In_ WCHAR* nameW,
126 _In_ GUID* lpProviderId,
127 _Out_ PWSHOSTINFOINTERN hostinfo);
128
129 /* Implementations - Internal */
130
131 INT
132 WSAAPI
133 mwsNSPCleanUp(_In_ LPGUID lpProviderId)
134 {
135 //WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED);
136 //return ERROR_CALL_NOT_IMPLEMENTED;
137 return ERROR_SUCCESS;
138 }
139
140 INT
141 mwsNSPInit(VOID)
142 {
143 return ERROR_SUCCESS;
144 }
145
146 INT
147 WSAAPI
148 mwsNSPLookupServiceBegin(_In_ LPGUID lpProviderId,
149 _In_ LPWSAQUERYSETW lpqsRestrictions,
150 _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo,
151 _In_ DWORD dwControlFlags,
152 _Out_ LPHANDLE lphLookup)
153 {
154 PWSHANDLEINTERN pLook;
155 int wsaErr;
156
157 if (IsEqualGUID(lpProviderId, &guid_mswsock_TcpIp))
158 {
159 //OK
160 }
161 else if (IsEqualGUID(lpProviderId, &guid_mswsock_NLA))
162 {
163 WSASetLastError(WSASERVICE_NOT_FOUND);
164 return SOCKET_ERROR;
165 }
166 else
167 {
168 return ERROR_CALL_NOT_IMPLEMENTED;
169 }
170
171 /* allocate internal structure */
172 pLook = HeapAlloc(GetProcessHeap(), 0, sizeof(WSHANDLEINTERN));
173 if (!pLook)
174 {
175 WSASetLastError(WSAEFAULT);
176 return SOCKET_ERROR;
177 }
178
179 *lphLookup = (HANDLE)pLook;
180
181 RtlZeroMemory(pLook, sizeof(*pLook));
182
183 /* Anyway the ControlFlags "should" be needed
184 in NSPLookupServiceNext. (see doku) But
185 thats not the fact ATM. */
186 pLook->dwControlFlags = dwControlFlags;
187 pLook->providerId = *lpProviderId;
188
189 #ifdef NSP_REDIRECT
190
191 if (IsEqualGUID(lpProviderId, &guid_mswsock_TcpIp))
192 {
193 pLook->rdrproc = rdrproc_tcpip;
194 }
195 else if (IsEqualGUID(lpProviderId, &guid_mswsock_NLA))
196 {
197 pLook->rdrproc = rdrproc_nla;
198 }
199 else
200 {
201 return ERROR_CALL_NOT_IMPLEMENTED;
202 }
203
204 if (pLook->rdrproc.NSPLookupServiceBegin(lpProviderId,
205 lpqsRestrictions,
206 lpServiceClassInfo,
207 dwControlFlags,
208 &pLook->rdrLookup) == NO_ERROR)
209 {
210 wsaErr = NO_ERROR;
211 }
212 else
213 {
214 wsaErr = WSAGetLastError();
215 }
216
217 /*
218 if (res)
219 res = WSAGetLastError();
220 */
221
222 #else /* NSP_REDIRECT */
223
224 wsaErr = ERROR_CALL_NOT_IMPLEMENTED;
225 if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, &guid_NULL))
226 {
227 wsaErr = ERROR_CALL_NOT_IMPLEMENTED;
228 }
229 else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, &guid_HOSTNAME))
230 {
231 wsaErr = NSP_LookupServiceBeginW(pLook,
232 NULL,
233 NULL,
234 NSP_CALLID_HOSTNAME);
235 }
236 else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId,
237 &guid_INET_HOSTADDRBYNAME))
238 {
239 wsaErr = NSP_LookupServiceBeginW(pLook,
240 NULL,
241 lpqsRestrictions->lpszServiceInstanceName,
242 NSP_CALLID_HOSTBYNAME);
243 }
244 else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId,
245 &guid_INET_SERVICEBYNAME))
246 {
247 wsaErr = NSP_LookupServiceBeginW(pLook,
248 NULL,
249 lpqsRestrictions->lpszServiceInstanceName,
250 NSP_CALLID_SERVICEBYNAME);
251 }
252 else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId,
253 &guid_INET_HOSTADDRBYINETSTRING))
254 {
255 wsaErr = ERROR_CALL_NOT_IMPLEMENTED;
256 }
257
258 #endif /* NSP_REDIRECT */
259
260 if (wsaErr != NO_ERROR)
261 {
262 WSASetLastError(wsaErr);
263 return SOCKET_ERROR;
264 }
265 return NO_ERROR;
266 }
267
268 INT
269 WSAAPI
270 mwsNSPLookupServiceNext(_In_ HANDLE hLookup,
271 _In_ DWORD dwControlFlags,
272 _Inout_ LPDWORD lpdwBufferLength,
273 //_Out_writes_bytes_to_(*lpdwBufferLength, *lpdwBufferLength)
274 LPWSAQUERYSETW lpqsResults)
275 {
276 PWSHANDLEINTERN pLook = hLookup;
277 int wsaErr = 0;
278
279 #ifdef NSP_REDIRECT
280
281 INT res = pLook->rdrproc.NSPLookupServiceNext(pLook->rdrLookup,
282 dwControlFlags,
283 lpdwBufferLength,
284 lpqsResults);
285 wsaErr = WSAGetLastError();
286 if (res != ERROR_SUCCESS)
287 {
288 wsaErr = WSAGetLastError();
289
290 if (wsaErr == 0)
291 wsaErr = 0xFFFFFFFF;
292 }
293
294 #else /* NSP_REDIRECT */
295
296 if ((lpdwBufferLength == NULL) || (*lpdwBufferLength == 0))
297 {
298 wsaErr = WSA_NOT_ENOUGH_MEMORY;
299 goto End;
300 }
301
302 RtlZeroMemory(lpqsResults, *lpdwBufferLength);
303 lpqsResults->dwSize = sizeof(*lpqsResults);
304
305 wsaErr = NSP_LookupServiceNextW(pLook,
306 pLook->CallID,
307 lpqsResults,
308 lpdwBufferLength);
309
310
311 #endif /* NSP_REDIRECT */
312
313 End:
314 if (wsaErr != 0)
315 {
316 WSASetLastError(wsaErr);
317 return SOCKET_ERROR;
318 }
319 return NO_ERROR;
320 }
321
322 INT
323 WSAAPI
324 mwsNSPIoCtl(_In_ HANDLE hLookup,
325 _In_ DWORD dwControlCode,
326 _In_reads_bytes_(cbInBuffer) LPVOID lpvInBuffer,
327 _In_ DWORD cbInBuffer,
328 _Out_writes_bytes_to_(cbOutBuffer, *lpcbBytesReturned) LPVOID lpvOutBuffer,
329 _In_ DWORD cbOutBuffer,
330 _Out_ LPDWORD lpcbBytesReturned,
331 _In_opt_ LPWSACOMPLETION lpCompletion,
332 _In_ LPWSATHREADID lpThreadId)
333 {
334 WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED);
335 return ERROR_CALL_NOT_IMPLEMENTED;
336 }
337
338 INT
339 WSAAPI
340 mwsNSPLookupServiceEnd(_In_ HANDLE hLookup)
341 {
342 PWSHANDLEINTERN pLook;
343 HANDLE hHeap;
344 INT res;
345
346 res = NO_ERROR;
347 pLook = (PWSHANDLEINTERN)hLookup;
348 hHeap = GetProcessHeap();
349
350 #ifdef NSP_REDIRECT
351 res = pLook->rdrproc.NSPLookupServiceEnd(pLook->rdrLookup);
352 #endif
353
354 if (pLook->hostnameW != NULL)
355 HeapFree(hHeap, 0, pLook->hostnameW);
356
357 HeapFree(hHeap, 0, pLook);
358 return res;
359 }
360
361 INT
362 WSAAPI
363 mwsNSPSetService(_In_ LPGUID lpProviderId,
364 _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo,
365 _In_ LPWSAQUERYSETW lpqsRegInfo,
366 _In_ WSAESETSERVICEOP essOperation,
367 _In_ DWORD dwControlFlags)
368 {
369 WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED);
370 return ERROR_CALL_NOT_IMPLEMENTED;
371 }
372
373 INT
374 WSAAPI
375 mwsNSPInstallServiceClass(_In_ LPGUID lpProviderId,
376 _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo)
377 {
378 WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED);
379 return ERROR_CALL_NOT_IMPLEMENTED;
380 }
381
382 INT
383 WSAAPI
384 mwsNSPRemoveServiceClass(_In_ LPGUID lpProviderId,
385 _In_ LPGUID lpServiceClassId)
386 {
387 WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED);
388 return ERROR_CALL_NOT_IMPLEMENTED;
389 }
390
391 INT
392 WSAAPI
393 mwsNSPGetServiceClassInfo(_In_ LPGUID lpProviderId,
394 _In_ LPDWORD lpdwBufSize,
395 _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo)
396 {
397 WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED);
398 return ERROR_CALL_NOT_IMPLEMENTED;
399 }
400
401 /*
402 hostnameA / hostnameW
403 * only used by HOSTBYNAME
404 * only one should be set
405
406 */
407 INT
408 NSP_LookupServiceBeginW(PWSHANDLEINTERN data,
409 CHAR* hostnameA,
410 WCHAR* hostnameW,
411 DWORD CallID)
412 {
413 HANDLE hHeap;
414
415 if (data->CallID != 0)
416 return WSAEFAULT;
417
418 data->CallID = CallID;
419
420 if ((CallID == NSP_CALLID_HOSTBYNAME) ||
421 (CallID == NSP_CALLID_SERVICEBYNAME))
422 {
423 hHeap = GetProcessHeap();
424
425 if (data->hostnameW != NULL)
426 HeapFree(hHeap, 0, data->hostnameW);
427
428 if (hostnameA != NULL)
429 {
430 data->hostnameW = StrA2WHeapAlloc(hHeap, hostnameA);
431 }
432 else
433 {
434 data->hostnameW = StrCpyHeapAllocW(hHeap, hostnameW);
435 }
436 }
437
438 WSASetLastError(0);
439
440 return ERROR_SUCCESS;
441 }
442
443 INT
444 NSP_GetHostNameHeapAllocW(_Out_ WCHAR** hostname)
445 {
446 WCHAR* name;
447 HANDLE hHeap = GetProcessHeap();
448 DWORD bufCharLen = MAX_COMPUTERNAME_LENGTH + 1;
449 DWORD bufByteLen = bufCharLen * sizeof(WCHAR);
450
451 name = HeapAlloc(hHeap, 0, bufByteLen);
452
453 if (!GetComputerNameExW(ComputerNameDnsHostname,
454 name,
455 &bufCharLen))
456 {
457 HeapFree(hHeap, 0, name);
458 WSASetLastError(WSAEFAULT);
459 return SOCKET_ERROR;
460 }
461
462 *hostname = name;
463 return ERROR_SUCCESS;
464 }
465
466 INT
467 NSP_GetHostByNameHeapAllocW(_In_ WCHAR* name,
468 _In_ GUID* lpProviderId,
469 _Out_ PWSHOSTINFOINTERN hostinfo)
470 {
471 HANDLE hHeap = GetProcessHeap();
472 DNS_STATUS dns_status = {0};
473 /* include/WinDNS.h -- look up DNS_RECORD on MSDN */
474 PDNS_RECORD dp;
475 PDNS_RECORD curr;
476 INT result = ERROR_SUCCESS;
477
478 /* needed to be cleaned up if != NULL */
479 dp = NULL;
480
481 if (name == NULL)
482 {
483 result = ERROR_INVALID_PARAMETER;
484 goto cleanup;
485 }
486
487 /* DNS_TYPE_A: include/WinDNS.h */
488 /* DnsQuery -- lib/dnsapi/dnsapi/query.c */
489 dns_status = DnsQuery(name,
490 DNS_TYPE_A,
491 DNS_QUERY_STANDARD,
492 /* extra dns servers */ 0,
493 &dp,
494 0);
495
496 if (dns_status == ERROR_INVALID_NAME)
497 {
498 WSASetLastError(WSAEFAULT);
499 result = ERROR_INVALID_PARAMETER;
500 goto cleanup;
501 }
502
503 if ((dns_status != 0) || (dp == NULL))
504 {
505 result = WSAHOST_NOT_FOUND;
506 goto cleanup;
507 }
508
509 //ASSERT(dp->wType == DNS_TYPE_A);
510 //ASSERT(dp->wDataLength == sizeof(DNS_A_DATA));
511 curr = dp;
512 while ((curr->pNext != NULL) || (curr->wType != DNS_TYPE_A))
513 {
514 curr = curr->pNext;
515 }
516
517 if (curr->wType != DNS_TYPE_A)
518 {
519 result = WSASERVICE_NOT_FOUND;
520 goto cleanup;
521 }
522
523 hostinfo->hostnameW = StrCpyHeapAllocW(hHeap, curr->pName);
524 hostinfo->addr4 = curr->Data.A.IpAddress;
525 result = ERROR_SUCCESS;
526
527 cleanup:
528 if (dp != NULL)
529 DnsRecordListFree(dp, DnsFreeRecordList);
530
531 return result;
532 }
533
534 #define SKIPWS(ptr, act) \
535 {while(*ptr && isspace(*ptr)) ptr++; if(!*ptr) act;}
536
537 #define SKIPANDMARKSTR(ptr, act) \
538 {while(*ptr && !isspace(*ptr)) ptr++; \
539 if(!*ptr) {act;} else { *ptr = 0; ptr++; }}
540
541 static
542 BOOL
543 DecodeServEntFromString(IN PCHAR ServiceString,
544 OUT PCHAR *ServiceName,
545 OUT PCHAR *PortNumberStr,
546 OUT PCHAR *ProtocolStr,
547 IN PCHAR *Aliases,
548 IN DWORD MaxAlias)
549 {
550 UINT NAliases = 0;
551
552 //WS_DbgPrint(MAX_TRACE, ("Parsing service ent [%s]\n", ServiceString));
553
554 SKIPWS(ServiceString, return FALSE);
555 *ServiceName = ServiceString;
556 SKIPANDMARKSTR(ServiceString, return FALSE);
557 SKIPWS(ServiceString, return FALSE);
558 *PortNumberStr = ServiceString;
559 SKIPANDMARKSTR(ServiceString, ;);
560
561 while (*ServiceString && NAliases < MaxAlias - 1)
562 {
563 SKIPWS(ServiceString, break);
564 if (*ServiceString)
565 {
566 SKIPWS(ServiceString, ;);
567 if (strlen(ServiceString))
568 {
569 //WS_DbgPrint(MAX_TRACE, ("Alias: %s\n", ServiceString));
570 *Aliases++ = ServiceString;
571 NAliases++;
572 }
573 SKIPANDMARKSTR(ServiceString, ;);
574 }
575 }
576 *Aliases = NULL;
577
578 *ProtocolStr = strchr(*PortNumberStr, '/');
579
580 if (!*ProtocolStr)
581 return FALSE;
582
583 **ProtocolStr = 0;
584 (*ProtocolStr)++;
585
586 //WS_DbgPrint(MAX_TRACE, ("Parsing done: %s %s %s %d\n",
587 // *ServiceName, *ProtocolStr, *PortNumberStr,
588 // NAliases));
589
590 return TRUE;
591 }
592
593 HANDLE
594 WSAAPI
595 OpenNetworkDatabase(_In_ LPCWSTR Name)
596 {
597 PWSTR ExpandedPath;
598 PWSTR DatabasePath;
599 INT ErrorCode;
600 HKEY DatabaseKey;
601 DWORD RegType;
602 DWORD RegSize = 0;
603 size_t StringLength;
604 HANDLE ret;
605
606 ExpandedPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(WCHAR));
607 if (!ExpandedPath)
608 return INVALID_HANDLE_VALUE;
609
610 /* Open the database path key */
611 ErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
612 L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
613 0,
614 KEY_READ,
615 &DatabaseKey);
616 if (ErrorCode == NO_ERROR)
617 {
618 /* Read the actual path */
619 ErrorCode = RegQueryValueEx(DatabaseKey,
620 L"DatabasePath",
621 NULL,
622 &RegType,
623 NULL,
624 &RegSize);
625
626 DatabasePath = HeapAlloc(GetProcessHeap(), 0, RegSize);
627 if (!DatabasePath)
628 {
629 HeapFree(GetProcessHeap(), 0, ExpandedPath);
630 return INVALID_HANDLE_VALUE;
631 }
632
633 /* Read the actual path */
634 ErrorCode = RegQueryValueEx(DatabaseKey,
635 L"DatabasePath",
636 NULL,
637 &RegType,
638 (LPBYTE)DatabasePath,
639 &RegSize);
640
641 /* Close the key */
642 RegCloseKey(DatabaseKey);
643
644 /* Expand the name */
645 ExpandEnvironmentStrings(DatabasePath, ExpandedPath, MAX_PATH);
646
647 HeapFree(GetProcessHeap(), 0, DatabasePath);
648 }
649 else
650 {
651 /* Use defalt path */
652 GetSystemDirectory(ExpandedPath, MAX_PATH);
653 StringCchLength(ExpandedPath, MAX_PATH, &StringLength);
654 if (ExpandedPath[StringLength - 1] != L'\\')
655 {
656 /* It isn't, so add it ourselves */
657 StringCchCat(ExpandedPath, MAX_PATH, L"\\");
658 }
659 StringCchCat(ExpandedPath, MAX_PATH, L"DRIVERS\\ETC\\");
660 }
661
662 /* Make sure that the path is backslash-terminated */
663 StringCchLength(ExpandedPath, MAX_PATH, &StringLength);
664 if (ExpandedPath[StringLength - 1] != L'\\')
665 {
666 /* It isn't, so add it ourselves */
667 StringCchCat(ExpandedPath, MAX_PATH, L"\\");
668 }
669
670 /* Add the database name */
671 StringCchCat(ExpandedPath, MAX_PATH, Name);
672
673 /* Return a handle to the file */
674 ret = CreateFile(ExpandedPath,
675 FILE_READ_DATA,
676 FILE_SHARE_READ,
677 NULL,
678 OPEN_EXISTING,
679 FILE_ATTRIBUTE_NORMAL,
680 NULL);
681
682 HeapFree(GetProcessHeap(), 0, ExpandedPath);
683 return ret;
684 }
685
686 INT
687 NSP_GetServiceByNameHeapAllocW(_In_ WCHAR* nameW,
688 _In_ GUID* lpProviderId,
689 _Out_ PWSHOSTINFOINTERN hostinfo)
690 {
691 BOOL Found = FALSE;
692 HANDLE ServicesFile;
693 CHAR ServiceDBData[BUFSIZ * sizeof(WCHAR)] = {0};
694 PCHAR ThisLine = 0, NextLine = 0, ServiceName = 0, PortNumberStr = 0,
695 ProtocolStr = 0, Comment = 0, EndValid;
696 PCHAR Aliases[WS2_INTERNAL_MAX_ALIAS] = {0};
697 PCHAR* AliasPtr;
698 UINT i = 0;
699 DWORD ReadSize = 0;
700 HANDLE hHeap;
701 PCHAR nameA = NULL;
702 PCHAR nameServiceA = NULL;
703 PCHAR nameProtoA = NULL;
704 INT res = WSANO_RECOVERY;
705
706 if (!nameW)
707 {
708 res = WSANO_RECOVERY;
709 goto End;
710 }
711
712 hHeap = GetProcessHeap();
713 nameA = StrW2AHeapAlloc(hHeap, nameW);
714
715 /* nameA has the form <service-name>/<protocol>
716 we split these now */
717 nameProtoA = strchr(nameA, '/');
718 if (nameProtoA == NULL)
719 {
720 res = WSANO_RECOVERY;
721 goto End;
722 }
723
724 nameProtoA++;
725 i = (DWORD)(nameProtoA - nameA - 1);
726 nameServiceA = (PCHAR)HeapAlloc(hHeap, 0, i + 1);
727 StringCbCopyA(nameServiceA, i + 1, nameA);
728 nameServiceA[i] = '\0';
729
730 ServicesFile = OpenNetworkDatabase(L"services");
731 if (ServicesFile == INVALID_HANDLE_VALUE)
732 {
733 return WSANO_RECOVERY;
734 }
735
736 /* Scan the services file ...
737 *
738 * We will be share the buffer on the lines. If the line does not fit in
739 * the buffer, then moving it to the beginning of the buffer and read
740 * the remnants of line from file.
741 */
742
743 /* Initial Read */
744 ReadFile(ServicesFile,
745 ServiceDBData,
746 sizeof( ServiceDBData ) - 1,
747 &ReadSize,
748 NULL);
749
750 ThisLine = NextLine = ServiceDBData;
751 EndValid = ServiceDBData + ReadSize;
752 ServiceDBData[sizeof(ServiceDBData) - 1] = '\0';
753
754 while (ReadSize)
755 {
756 for (; *NextLine != '\r' && *NextLine != '\n'; NextLine++)
757 {
758 if (NextLine == EndValid)
759 {
760 int LineLen = NextLine - ThisLine;
761
762 if (ThisLine == ServiceDBData)
763 {
764 //WS_DbgPrint(MIN_TRACE,("Line too long"));
765 return WSANO_RECOVERY;
766 }
767
768 memmove(ServiceDBData, ThisLine, LineLen);
769
770 ReadFile(ServicesFile,
771 ServiceDBData + LineLen,
772 sizeof( ServiceDBData )-1 - LineLen,
773 &ReadSize,
774 NULL);
775
776 EndValid = ServiceDBData + LineLen + ReadSize;
777 NextLine = ServiceDBData + LineLen;
778 ThisLine = ServiceDBData;
779
780 if (!ReadSize) break;
781 }
782 }
783
784 *NextLine = '\0';
785 Comment = strchr(ThisLine, '#');
786
787 if (Comment)
788 *Comment = '\0'; /* Terminate at comment start */
789
790 if (DecodeServEntFromString(ThisLine,
791 &ServiceName,
792 &PortNumberStr,
793 &ProtocolStr,
794 Aliases,
795 WS2_INTERNAL_MAX_ALIAS) &&
796 (strlen(nameProtoA) == 0 || strcmp(ProtocolStr, nameProtoA) == 0))
797 {
798 Found = (strcmp(ServiceName, nameServiceA) == 0 || strcmp(PortNumberStr, nameServiceA) == 0);
799 AliasPtr = Aliases;
800 while ((!Found) && (*AliasPtr != NULL))
801 {
802 Found = (strcmp(*AliasPtr, nameServiceA) == 0);
803 AliasPtr++;
804 }
805 if (Found)
806 break;
807 }
808 NextLine++;
809 ThisLine = NextLine;
810 }
811
812 /* This we'll do no matter what */
813 CloseHandle(ServicesFile);
814
815 if (!Found)
816 {
817 return WSANO_DATA;
818 }
819
820 hostinfo->addr4 = 0;
821 hostinfo->servnameW = StrA2WHeapAlloc(hHeap, ServiceName);
822 hostinfo->servprotoW = StrA2WHeapAlloc(hHeap, ProtocolStr);
823 hostinfo->servaliasesA = StrAryCpyHeapAllocA(hHeap, (char**)&Aliases);
824 hostinfo->servport = atoi(PortNumberStr);
825
826 res = NO_ERROR;
827
828 End:
829 if (nameA != NULL)
830 HeapFree(hHeap, 0, nameA);
831
832 if (nameServiceA != NULL)
833 HeapFree(hHeap, 0, nameServiceA);
834
835 return res;
836 }
837
838 INT
839 NSP_LookupServiceNextW(_In_ PWSHANDLEINTERN data,
840 _In_ DWORD CallID,
841 _Inout_ LPWSAQUERYSETW lpRes,
842 _Inout_ LPDWORD lpResLen)
843 {
844 MSW_BUFFER buf;
845 WSHOSTINFOINTERN hostinfo;
846 INT result;
847 HANDLE hHeap = GetProcessHeap();
848 WCHAR* ServiceInstanceNameW = NULL;
849 /* cleanup-vars */
850 CHAR* ServiceInstanceNameA = NULL;
851 CHAR* ServiceProtocolNameA = NULL;
852
853 RtlZeroMemory(&hostinfo, sizeof(hostinfo));
854
855 /* init and build result-buffer */
856 mswBufferInit(&buf, (BYTE*)lpRes, *lpResLen);
857 mswBufferIncUsed(&buf, sizeof(*lpRes));
858
859 /* QueryDataSet-Size without "blob-data"-size! */
860 lpRes->dwSize = sizeof(*lpRes);
861 lpRes->dwNameSpace = NS_DNS;
862
863 if ((CallID == NSP_CALLID_HOSTNAME) ||
864 (CallID == NSP_CALLID_HOSTBYNAME) ||
865 (CallID == NSP_CALLID_SERVICEBYNAME))
866 {
867 if (data->CallIDCounter >= 1)
868 {
869 result = WSAENOMORE;
870 goto End;
871 }
872 }
873 else
874 {
875 result = WSANO_RECOVERY;
876 goto End;
877 }
878 data->CallIDCounter++;
879
880 if (CallID == NSP_CALLID_HOSTNAME)
881 {
882 result = NSP_GetHostNameHeapAllocW(&hostinfo.hostnameW);
883
884 if (result != ERROR_SUCCESS)
885 goto End;
886
887 hostinfo.addr4 = 0;
888 }
889 else if (CallID == NSP_CALLID_HOSTBYNAME)
890 {
891 result = NSP_GetHostByNameHeapAllocW(data->hostnameW,
892 &data->providerId,
893 &hostinfo);
894 if (result != ERROR_SUCCESS)
895 goto End;
896 }
897 else
898 {
899 ASSERT(CallID == NSP_CALLID_SERVICEBYNAME);
900 result = NSP_GetServiceByNameHeapAllocW(data->hostnameW,
901 &data->providerId,
902 &hostinfo);
903 if (result != ERROR_SUCCESS)
904 goto End;
905 }
906
907 if (((LUP_RETURN_BLOB & data->dwControlFlags) != 0) ||
908 ((LUP_RETURN_NAME & data->dwControlFlags) != 0))
909 {
910 if (CallID == NSP_CALLID_HOSTNAME || CallID == NSP_CALLID_HOSTBYNAME)
911 {
912 ServiceInstanceNameW = hostinfo.hostnameW;
913 ServiceInstanceNameA = StrW2AHeapAlloc(hHeap, ServiceInstanceNameW);
914 if (ServiceInstanceNameA == NULL)
915 {
916 result = WSAEFAULT;
917 goto End;
918
919 }
920 }
921 if (CallID == NSP_CALLID_SERVICEBYNAME)
922 {
923 ServiceInstanceNameW = hostinfo.servnameW;
924 ServiceInstanceNameA = StrW2AHeapAlloc(hHeap, ServiceInstanceNameW);
925 if (ServiceInstanceNameA == NULL)
926 {
927 result = WSAEFAULT;
928 goto End;
929
930 }
931 ServiceProtocolNameA = StrW2AHeapAlloc(hHeap, hostinfo.servprotoW);
932 if (ServiceProtocolNameA == NULL)
933 {
934 result = WSAEFAULT;
935 goto End;
936
937 }
938 }
939 }
940
941 if ((LUP_RETURN_ADDR & data->dwControlFlags) != 0)
942 {
943 if (!mswBufferAppendAddr_AddrInfoW(&buf, lpRes, hostinfo.addr4))
944 {
945 *lpResLen = buf.bytesUsed;
946 result = WSAEFAULT;
947 goto End;
948 }
949 }
950
951 if ((LUP_RETURN_BLOB & data->dwControlFlags) != 0)
952 {
953 if (CallID == NSP_CALLID_HOSTBYNAME)
954 {
955 /* Write data for PBLOB (hostent) */
956 if (!mswBufferAppendBlob_Hostent(&buf,
957 lpRes,
958 ServiceInstanceNameA,
959 hostinfo.addr4))
960 {
961 *lpResLen = buf.bytesUsed;
962 result = WSAEFAULT;
963 goto End;
964 }
965 }
966 else if (CallID == NSP_CALLID_SERVICEBYNAME)
967 {
968 /* Write data for PBLOB (servent) */
969 if (!mswBufferAppendBlob_Servent(&buf,
970 lpRes,
971 ServiceInstanceNameA,/* ServiceName */
972 hostinfo.servaliasesA,
973 ServiceProtocolNameA,
974 hostinfo.servport))
975 {
976 *lpResLen = buf.bytesUsed;
977 result = WSAEFAULT;
978 goto End;
979 }
980 }
981 else
982 {
983 result = WSANO_RECOVERY;
984 goto End;
985 }
986 }
987
988 if ((LUP_RETURN_NAME & data->dwControlFlags) != 0)
989 {
990 /* HostByName sets the ServiceInstanceName to a
991 (UNICODE)copy of hostent.h_name */
992 lpRes->lpszServiceInstanceName = (LPWSTR)mswBufferEndPtr(&buf);
993 if (!mswBufferAppendStrW(&buf, ServiceInstanceNameW))
994 {
995 lpRes->lpszServiceInstanceName = NULL;
996 *lpResLen = buf.bytesUsed;
997 result = WSAEFAULT;
998 goto End;
999 }
1000 }
1001
1002 *lpResLen = buf.bytesUsed;
1003
1004 result = ERROR_SUCCESS;
1005 End:
1006 /* cleanup */
1007 if (ServiceInstanceNameA != NULL)
1008 HeapFree(hHeap, 0, ServiceInstanceNameA);
1009
1010 if (ServiceProtocolNameA != NULL)
1011 HeapFree(hHeap, 0, ServiceProtocolNameA);
1012
1013 if (hostinfo.hostnameW != NULL)
1014 HeapFree(hHeap, 0, hostinfo.hostnameW);
1015
1016 if (hostinfo.servnameW != NULL)
1017 HeapFree(hHeap, 0, hostinfo.servnameW);
1018
1019 if (hostinfo.servprotoW != NULL)
1020 HeapFree(hHeap, 0, hostinfo.servprotoW);
1021
1022 return result;
1023 }
1024
1025 /* Implementations - Exports */
1026 /*
1027 * @implemented
1028 */
1029 int
1030 WINAPI
1031 NSPStartup(_In_ LPGUID lpProviderId,
1032 _Out_ LPNSP_ROUTINE lpRout)
1033 {
1034 INT ret;
1035
1036 if ((lpRout == NULL) ||
1037 (lpRout->cbSize != sizeof(NSP_ROUTINE)))
1038 {
1039 WSASetLastError(ERROR_INVALID_PARAMETER);
1040 return ERROR_INVALID_PARAMETER;
1041 }
1042
1043 mwsNSPInit();
1044
1045 /* set own Provider GUID - maybe we need
1046 here to set the original mswsock-GUID?! */
1047
1048 /* Win2k3 returns
1049 - Version 1.1
1050 - no NSPIoctl
1051 - sets cbSize to 44! */
1052 lpRout->dwMajorVersion = 1;
1053 lpRout->dwMinorVersion = 1;
1054 lpRout->cbSize = sizeof(*lpRout) - sizeof(lpRout->NSPIoctl);
1055 lpRout->NSPCleanup = &mwsNSPCleanUp;
1056 lpRout->NSPLookupServiceBegin = &mwsNSPLookupServiceBegin;
1057 lpRout->NSPLookupServiceNext = &mwsNSPLookupServiceNext;
1058 lpRout->NSPLookupServiceEnd = &mwsNSPLookupServiceEnd;
1059 lpRout->NSPSetService = &mwsNSPSetService;
1060 lpRout->NSPInstallServiceClass = &mwsNSPInstallServiceClass;
1061 lpRout->NSPRemoveServiceClass = &mwsNSPRemoveServiceClass;
1062 lpRout->NSPGetServiceClassInfo = &mwsNSPGetServiceClassInfo;
1063 lpRout->NSPIoctl = NULL;// &mwsNSPIoCtl;
1064
1065 ret = NO_ERROR;
1066
1067 return ret;
1068 }