2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 DLL
5 * PURPOSE: Namespace APIs
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/09-2000 Created
10 #define __NO_CTYPE_INLINES
19 #ifndef MAX_HOSTNAME_LEN
20 #define MAX_HOSTNAME_LEN 256
23 /* Name resolution APIs */
30 WSAAddressToStringA(IN LPSOCKADDR lpsaAddress
,
31 IN DWORD dwAddressLength
,
32 IN LPWSAPROTOCOL_INFOA lpProtocolInfo
,
33 OUT LPSTR lpszAddressString
,
34 IN OUT LPDWORD lpdwAddressStringLength
)
47 WSAAddressToStringW(IN LPSOCKADDR lpsaAddress
,
48 IN DWORD dwAddressLength
,
49 IN LPWSAPROTOCOL_INFOW lpProtocolInfo
,
50 OUT LPWSTR lpszAddressString
,
51 IN OUT LPDWORD lpdwAddressStringLength
)
64 WSAEnumNameSpaceProvidersA(IN OUT LPDWORD lpdwBufferLength
,
65 OUT LPWSANAMESPACE_INFOA lpnspBuffer
)
78 WSAEnumNameSpaceProvidersW(IN OUT LPDWORD lpdwBufferLength
,
79 OUT LPWSANAMESPACE_INFOW lpnspBuffer
)
92 WSAGetServiceClassInfoA(IN LPGUID lpProviderId
,
93 IN LPGUID lpServiceClassId
,
94 IN OUT LPDWORD lpdwBufferLength
,
95 OUT LPWSASERVICECLASSINFOA lpServiceClassInfo
)
108 WSAGetServiceClassInfoW(IN LPGUID lpProviderId
,
109 IN LPGUID lpServiceClassId
,
110 IN OUT LPDWORD lpdwBufferLength
,
111 OUT LPWSASERVICECLASSINFOW lpServiceClassInfo
)
124 WSAGetServiceClassNameByClassIdA(IN LPGUID lpServiceClassId
,
125 OUT LPSTR lpszServiceClassName
,
126 IN OUT LPDWORD lpdwBufferLength
)
139 WSAGetServiceClassNameByClassIdW(IN LPGUID lpServiceClassId
,
140 OUT LPWSTR lpszServiceClassName
,
141 IN OUT LPDWORD lpdwBufferLength
)
154 WSAInstallServiceClassA(IN LPWSASERVICECLASSINFOA lpServiceClassInfo
)
167 WSAInstallServiceClassW(IN LPWSASERVICECLASSINFOW lpServiceClassInfo
)
180 WSALookupServiceBeginA(IN LPWSAQUERYSETA lpqsRestrictions
,
181 IN DWORD dwControlFlags
,
182 OUT LPHANDLE lphLookup
)
195 WSALookupServiceBeginW(IN LPWSAQUERYSETW lpqsRestrictions
,
196 IN DWORD dwControlFlags
,
197 OUT LPHANDLE lphLookup
)
210 WSALookupServiceEnd(IN HANDLE hLookup
)
223 WSALookupServiceNextA(IN HANDLE hLookup
,
224 IN DWORD dwControlFlags
,
225 IN OUT LPDWORD lpdwBufferLength
,
226 OUT LPWSAQUERYSETA lpqsResults
)
239 WSALookupServiceNextW(IN HANDLE hLookup
,
240 IN DWORD dwControlFlags
,
241 IN OUT LPDWORD lpdwBufferLength
,
242 OUT LPWSAQUERYSETW lpqsResults
)
255 WSARemoveServiceClass(IN LPGUID lpServiceClassId
)
268 WSASetServiceA(IN LPWSAQUERYSETA lpqsRegInfo
,
269 IN WSAESETSERVICEOP essOperation
,
270 IN DWORD dwControlFlags
)
283 WSASetServiceW(IN LPWSAQUERYSETW lpqsRegInfo
,
284 IN WSAESETSERVICEOP essOperation
,
285 IN DWORD dwControlFlags
)
298 WSAStringToAddressA(IN LPSTR AddressString
,
299 IN INT AddressFamily
,
300 IN LPWSAPROTOCOL_INFOA lpProtocolInfo
,
301 OUT LPSOCKADDR lpAddress
,
302 IN OUT LPINT lpAddressLength
)
306 LPWSAPROTOCOL_INFOW lpProtoInfoW
= NULL
;
308 len
= MultiByteToWideChar(CP_ACP
,
315 szTemp
= HeapAlloc(GetProcessHeap(),
317 len
* sizeof(WCHAR
));
319 MultiByteToWideChar(CP_ACP
,
328 len
= WSAPROTOCOL_LEN
+1;
329 lpProtoInfoW
= HeapAlloc(GetProcessHeap(),
331 len
* sizeof(WCHAR
) );
335 sizeof(LPWSAPROTOCOL_INFOA
));
337 MultiByteToWideChar(CP_ACP
,
339 lpProtocolInfo
->szProtocol
,
341 lpProtoInfoW
->szProtocol
,
345 ret
= WSAStringToAddressW(szTemp
,
351 HeapFree(GetProcessHeap(),
356 HeapFree(GetProcessHeap(),
360 WSASetLastError(ret
);
371 WSAStringToAddressW(IN LPWSTR AddressString
,
372 IN INT AddressFamily
,
373 IN LPWSAPROTOCOL_INFOW lpProtocolInfo
,
374 OUT LPSOCKADDR lpAddress
,
375 IN OUT LPINT lpAddressLength
)
382 SOCKADDR_IN
*sockaddr
= (SOCKADDR_IN
*) lpAddress
;
384 if (!lpAddressLength
|| !lpAddress
)
387 if (AddressString
==NULL
)
390 /* Set right adress family */
391 if (lpProtocolInfo
!=NULL
)
392 sockaddr
->sin_family
= lpProtocolInfo
->iAddressFamily
;
394 else sockaddr
->sin_family
= AddressFamily
;
397 if (AddressFamily
== AF_INET
)
399 if (*lpAddressLength
< (INT
)sizeof(SOCKADDR_IN
))
401 *lpAddressLength
= sizeof(SOCKADDR_IN
);
410 // translate now ip string to ip
412 /* rest sockaddr.sin_addr.s_addr
413 for we need to be sure it is zero when we come to while */
414 memset(lpAddress
,0,sizeof(SOCKADDR_IN
));
416 /* Set right adress family */
417 sockaddr
->sin_family
= AF_INET
;
419 /* Get port number */
420 pos
= wcscspn(AddressString
,L
":") + 1;
422 if (pos
< (int)wcslen(AddressString
))
423 sockaddr
->sin_port
= wcstol(&AddressString
[pos
],
428 sockaddr
->sin_port
= 0;
434 while (pos
< (int)wcslen(AddressString
))
436 inetaddr
= (inetaddr
<<8) + ((UCHAR
)wcstol(&AddressString
[pos
],
439 pos
+= wcscspn( &AddressString
[pos
],L
".") +1 ;
443 sockaddr
->sin_addr
.s_addr
= inetaddr
;
449 WSASetLastError(res
);
454 void check_hostent(struct hostent
**he
)
456 struct hostent
*new_he
;
458 WS_DbgPrint(MID_TRACE
,("*he: %x\n",*he
));
462 new_he
= HeapAlloc(GlobalHeap
,
464 sizeof(struct hostent
) + MAX_HOSTNAME_LEN
+ 1);
466 new_he
->h_name
= (PCHAR
)(new_he
+ 1);
467 new_he
->h_aliases
= 0;
468 new_he
->h_addrtype
= 0; // AF_INET
469 new_he
->h_length
= 0; // sizeof(in_addr)
470 new_he
->h_addr_list
= HeapAlloc(GlobalHeap
,
474 RtlZeroMemory(new_he
->h_addr_list
,
480 void populate_hostent(struct hostent
*he
, char* name
, DNS_A_DATA addr
)
484 //he = HeapAlloc(GlobalHeap, 0, sizeof(struct hostent));
485 //he->h_name = HeapAlloc(GlobalHeap, 0, MAX_HOSTNAME_LEN+1);
491 if( !he
->h_aliases
) {
492 he
->h_aliases
= HeapAlloc(GlobalHeap
, 0, sizeof(char *));
493 he
->h_aliases
[0] = 0;
495 he
->h_addrtype
= AF_INET
;
496 he
->h_length
= sizeof(IN_ADDR
); //sizeof(struct in_addr);
498 if( he
->h_addr_list
[0] )
505 he
->h_addr_list
[0] = HeapAlloc(GlobalHeap
,
507 MAX_HOSTNAME_LEN
+ 1);
509 WS_DbgPrint(MID_TRACE
,("he->h_addr_list[0] %x\n", he
->h_addr_list
[0]));
511 RtlCopyMemory(he
->h_addr_list
[0],
512 (char*)&addr
.IpAddress
,
513 sizeof(addr
.IpAddress
));
515 he
->h_addr_list
[1] = 0;
519 #define HFREE(x) if(x) { HeapFree(GlobalHeap, 0, (x)); x=0; }
520 void free_hostent(struct hostent
*he
)
528 next
= he
->h_aliases
[0];
529 while(next
) { HFREE(next
); next
++; }
533 next
= he
->h_addr_list
[0];
534 while(next
) { HFREE(next
); next
++; }
536 HFREE(he
->h_addr_list
);
537 HFREE(he
->h_aliases
);
542 /* WinSock 1.1 compatible name resolution APIs */
549 gethostbyaddr(IN CONST CHAR FAR
* addr
,
555 return (LPHOSTENT
)NULL
;
559 Assumes rfc 1123 - adam *
561 addr[0] = inet_addr(name);
562 strcpy( hostname, name );
563 if(addr[0] == 0xffffffff) return NULL;
564 he.h_addr_list = (void *)addr;
565 he.h_name = hostname;
567 he.h_addrtype = AF_INET;
568 he.h_length = sizeof(addr);
572 From the MSDN Platform SDK: Windows Sockets 2
573 "The gethostbyname function cannot resolve IP address strings passed to it.
574 Such a request is treated exactly as if an unknown host name were passed."
577 Defferring to the the documented behaviour, rather than the unix behaviour
578 What if the hostname is in the HOSTS file? see getservbyname
583 /* DnsQuery -- lib/dnsapi/dnsapi/query.c */
584 /* see ws2_32.h, winsock2.h*/
585 /*getnetworkparameters - iphlp api */
589 servent -- w32api/include/winsock2.h
590 PWINSOCK_THREAD_BLOCK -- ws2_32.h
591 dllmain.c -- threadlocal memory allocation / deallocation
596 /* lib/adns/src/adns.h XXX */
606 #define h_addr h_addr_list[0]
616 struct hostent defined in w32api/include/winsock2.h
619 void free_servent(struct servent
* s
)
622 char* next
= s
->s_aliases
[0];
623 while(next
) { HFREE(next
); next
++; }
633 gethostbyname(IN CONST CHAR FAR
* name
)
642 typedef enum addr_type addr_type
;
646 DNS_STATUS dns_status
= {0};
647 /* include/WinDNS.h -- look up DNS_RECORD on MSDN */
652 PWINSOCK_THREAD_BLOCK p
= NtCurrentTeb()->WinSockData
;
656 WSASetLastError( WSANOTINITIALISED
);
660 check_hostent(&p
->Hostent
); /*XXX alloc_hostent*/
662 /* Hostname NULL - behave like gethostname */
665 ret
= gethostname(p
->Hostent
->h_name
, MAX_HOSTNAME_LEN
);
671 WSASetLastError( WSAHOST_NOT_FOUND
); //WSANO_DATA ??
675 /* Is it an IPv6 address? */
676 found
= strstr(name
, ":");
683 /* Is it an IPv4 address? */
684 if (!isalpha(name
[0]))
690 addr
= GH_RFC1123_DNS
;
692 /* Broken out in case we want to get fancy later */
697 WSASetLastError(STATUS_NOT_IMPLEMENTED
);
702 WSASetLastError(WSAEFAULT
);
706 /* Note: If passed an IP address, MSDN says that gethostbyname()
707 treats it as an unknown host.
708 This is different from the unix implementation. Use inet_addr()
712 /* DNS_TYPE_A: include/WinDNS.h */
713 /* DnsQuery -- lib/dnsapi/dnsapi/query.c */
714 dns_status
= DnsQuery_A(name
,
718 /* extra dns servers */ &dp
,
723 //ASSERT(dp->wType == DNS_TYPE_A);
724 //ASSERT(dp->wDataLength == sizeof(DNS_A_DATA));
727 curr
!= NULL
&& curr
->wType
!= DNS_TYPE_A
;
730 WS_DbgPrint(MID_TRACE
,("wType: %i\n", curr
->wType
));
736 WS_DbgPrint(MID_TRACE
,("populating hostent\n"));
737 WS_DbgPrint(MID_TRACE
,("pName is (%s)\n", curr
->pName
));
738 populate_hostent(p
->Hostent
, (PCHAR
)curr
->pName
, curr
->Data
.A
);
739 DnsRecordListFree(dp
, DnsFreeRecordList
);
744 DnsRecordListFree(dp
, DnsFreeRecordList
);
748 WS_DbgPrint(MID_TRACE
,("Called DnsQuery, but host not found. Err: %i\n",
750 WSASetLastError(WSAHOST_NOT_FOUND
);
756 WSASetLastError(WSANO_RECOVERY
);
761 WSASetLastError(WSANO_RECOVERY
);
770 gethostname(OUT CHAR FAR
* name
,
773 DWORD size
= namelen
;
775 int ret
= GetComputerNameExA(ComputerNameDnsHostname
,
780 WSASetLastError(WSAEFAULT
);
785 name
[namelen
-1] = '\0';
792 * XXX arty -- Partial implementation pending a better one. This one will
793 * do for normal purposes.#include <ws2_32.h>
795 * Return the address of a static LPPROTOENT corresponding to the named
796 * protocol. These structs aren't very interesting, so I'm not too ashamed
797 * to have this function work on builtins for now.
803 getprotobyname(IN CONST CHAR FAR
* name
)
805 static CHAR
*udp_aliases
= 0;
806 static PROTOENT udp
= { "udp", &udp_aliases
, 17 };
807 static CHAR
*tcp_aliases
= 0;
808 static PROTOENT tcp
= { "tcp", &tcp_aliases
, 6 };
810 if(!_stricmp(name
, "udp"))
814 else if (!_stricmp( name
, "tcp"))
827 getprotobynumber(IN INT number
)
831 return (LPPROTOENT
)NULL
;
834 #define SKIPWS(ptr,act) \
835 {while(*ptr && isspace(*ptr)) ptr++; if(!*ptr) act;}
836 #define SKIPANDMARKSTR(ptr,act) \
837 {while(*ptr && !isspace(*ptr)) ptr++; \
838 if(!*ptr) {act;} else { *ptr = 0; ptr++; }}
842 DecodeServEntFromString(IN PCHAR ServiceString
,
843 OUT PCHAR
*ServiceName
,
844 OUT PCHAR
*PortNumberStr
,
845 OUT PCHAR
*ProtocolStr
,
851 WS_DbgPrint(MAX_TRACE
, ("Parsing service ent [%s]\n", ServiceString
));
853 SKIPWS(ServiceString
, return FALSE
);
854 *ServiceName
= ServiceString
;
855 SKIPANDMARKSTR(ServiceString
, return FALSE
);
856 SKIPWS(ServiceString
, return FALSE
);
857 *PortNumberStr
= ServiceString
;
858 SKIPANDMARKSTR(ServiceString
, ;);
860 while( *ServiceString
&& NAliases
< MaxAlias
- 1 )
862 SKIPWS(ServiceString
, break);
865 SKIPANDMARKSTR(ServiceString
, ;);
866 if( strlen(ServiceString
) )
868 WS_DbgPrint(MAX_TRACE
, ("Alias: %s\n", ServiceString
));
869 *Aliases
++ = ServiceString
;
876 *ProtocolStr
= strchr(*PortNumberStr
,'/');
877 if( !*ProtocolStr
) return FALSE
;
878 **ProtocolStr
= 0; (*ProtocolStr
)++;
880 WS_DbgPrint(MAX_TRACE
, ("Parsing done: %s %s %s %d\n",
881 *ServiceName
, *ProtocolStr
, *PortNumberStr
,
887 #define ADJ_PTR(p,b1,b2) p = (p - b1) + b2
894 getservbyname(IN CONST CHAR FAR
* name
,
895 IN CONST CHAR FAR
* proto
)
899 CHAR ServiceDBData
[BUFSIZ
] = { 0 };
900 PCHAR SystemDirectory
= ServiceDBData
; /* Reuse this stack space */
901 PCHAR ServicesFileLocation
= "\\drivers\\etc\\services";
902 PCHAR ThisLine
= 0, NextLine
= 0, ServiceName
= 0, PortNumberStr
= 0,
903 ProtocolStr
= 0, Comment
= 0;
904 PCHAR Aliases
[WS2_INTERNAL_MAX_ALIAS
] = { 0 };
905 UINT i
,SizeNeeded
= 0,
906 SystemDirSize
= sizeof(ServiceDBData
) - 1;
907 DWORD ReadSize
= 0, ValidData
= 0;
908 PWINSOCK_THREAD_BLOCK p
= NtCurrentTeb()->WinSockData
;
912 WSASetLastError( WSANOTINITIALISED
);
918 WSASetLastError( WSANO_RECOVERY
);
922 if( !GetSystemDirectoryA( SystemDirectory
, SystemDirSize
) )
924 WSASetLastError( WSANO_RECOVERY
);
925 WS_DbgPrint(MIN_TRACE
, ("Could not get windows system directory.\n"));
926 return NULL
; /* Can't get system directory */
929 strncat(SystemDirectory
,
930 ServicesFileLocation
,
933 ServicesFile
= CreateFileA(SystemDirectory
,
938 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_SEQUENTIAL_SCAN
,
941 if( ServicesFile
== INVALID_HANDLE_VALUE
)
943 WSASetLastError( WSANO_RECOVERY
);
947 /* Scan the services file ...
949 * We will read up to BUFSIZ bytes per pass, until the buffer does not
950 * contain a full line, then we will try to read more.
952 * We fall from the loop if the buffer does not have a line terminator.
957 ReadFile(ServicesFile
,
958 ServiceDBData
+ ValidData
,
959 sizeof( ServiceDBData
) - ValidData
,
963 ValidData
+= ReadSize
;
965 NextLine
= ThisLine
= ServiceDBData
;
967 /* Find the beginning of the next line */
968 while(NextLine
< ServiceDBData
+ ValidData
&&
969 *NextLine
!= '\r' && *NextLine
!= '\n' )
974 /* Zero and skip, so we can treat what we have as a string */
975 if( NextLine
>= ServiceDBData
+ ValidData
)
978 *NextLine
= 0; NextLine
++;
980 Comment
= strchr( ThisLine
, '#' );
981 if( Comment
) *Comment
= 0; /* Terminate at comment start */
983 if(DecodeServEntFromString(ThisLine
,
988 WS2_INTERNAL_MAX_ALIAS
) &&
989 !strcmp( ServiceName
, name
) &&
990 (proto
? !strcmp( ProtocolStr
, proto
) : TRUE
) )
993 WS_DbgPrint(MAX_TRACE
,("Found the service entry.\n"));
995 SizeNeeded
= sizeof(WINSOCK_GETSERVBYNAME_CACHE
) +
996 (NextLine
- ThisLine
);
1000 /* Get rid of everything we read so far */
1001 while( NextLine
<= ServiceDBData
+ ValidData
&&
1002 isspace( *NextLine
) )
1007 WS_DbgPrint(MAX_TRACE
,("About to move %d chars\n",
1008 ServiceDBData
+ ValidData
- NextLine
));
1010 memmove(ServiceDBData
,
1012 ServiceDBData
+ ValidData
- NextLine
);
1013 ValidData
-= NextLine
- ServiceDBData
;
1014 WS_DbgPrint(MAX_TRACE
,("Valid bytes: %d\n", ValidData
));
1017 /* This we'll do no matter what */
1018 CloseHandle( ServicesFile
);
1022 WS_DbgPrint(MAX_TRACE
,("Not found\n"));
1023 WSASetLastError( WSANO_DATA
);
1027 if( !p
->Getservbyname
|| p
->Getservbyname
->Size
< SizeNeeded
)
1029 /* Free previous getservbyname buffer, allocate bigger */
1030 if( p
->Getservbyname
)
1031 HeapFree(GlobalHeap
, 0, p
->Getservbyname
);
1032 p
->Getservbyname
= HeapAlloc(GlobalHeap
, 0, SizeNeeded
);
1033 if( !p
->Getservbyname
)
1035 WS_DbgPrint(MIN_TRACE
,("Couldn't allocate %d bytes\n",
1037 WSASetLastError( WSATRY_AGAIN
);
1040 p
->Getservbyname
->Size
= SizeNeeded
;
1044 memmove(p
->Getservbyname
->Data
,
1046 NextLine
- ThisLine
);
1048 ADJ_PTR(ServiceName
,ThisLine
,p
->Getservbyname
->Data
);
1049 ADJ_PTR(ProtocolStr
,ThisLine
,p
->Getservbyname
->Data
);
1050 WS_DbgPrint(MAX_TRACE
, ("ServiceName: %s, Protocol: %s\n",
1054 for( i
= 0; Aliases
[i
]; i
++ )
1056 ADJ_PTR(Aliases
[i
],ThisLine
,p
->Getservbyname
->Data
);
1057 WS_DbgPrint(MAX_TRACE
,("Aliase %d: %s\n", i
, Aliases
[i
]));
1060 memcpy(p
->Getservbyname
,
1064 /* Create the struct proper */
1065 p
->Getservbyname
->ServerEntry
.s_name
= ServiceName
;
1066 p
->Getservbyname
->ServerEntry
.s_aliases
= p
->Getservbyname
->Aliases
;
1067 p
->Getservbyname
->ServerEntry
.s_port
= htons(atoi(PortNumberStr
));
1068 p
->Getservbyname
->ServerEntry
.s_proto
= ProtocolStr
;
1070 return &p
->Getservbyname
->ServerEntry
;
1079 getservbyport(IN INT port
,
1080 IN CONST CHAR FAR
* proto
)
1083 HANDLE ServicesFile
;
1084 CHAR ServiceDBData
[BUFSIZ
] = { 0 };
1085 PCHAR SystemDirectory
= ServiceDBData
; /* Reuse this stack space */
1086 PCHAR ServicesFileLocation
= "\\drivers\\etc\\services";
1087 PCHAR ThisLine
= 0, NextLine
= 0, ServiceName
= 0, PortNumberStr
= 0,
1088 ProtocolStr
= 0, Comment
= 0;
1089 PCHAR Aliases
[WS2_INTERNAL_MAX_ALIAS
] = { 0 };
1090 UINT i
,SizeNeeded
= 0,
1091 SystemDirSize
= sizeof(ServiceDBData
) - 1;
1092 DWORD ReadSize
= 0, ValidData
= 0;
1093 PWINSOCK_THREAD_BLOCK p
= NtCurrentTeb()->WinSockData
;
1097 WSASetLastError( WSANOTINITIALISED
);
1103 WSASetLastError( WSANO_RECOVERY
);
1107 if( !GetSystemDirectoryA( SystemDirectory
, SystemDirSize
) )
1109 WSASetLastError( WSANO_RECOVERY
);
1110 WS_DbgPrint(MIN_TRACE
, ("Could not get windows system directory.\n"));
1111 return NULL
; /* Can't get system directory */
1114 strncat(SystemDirectory
,
1115 ServicesFileLocation
,
1118 ServicesFile
= CreateFileA(SystemDirectory
,
1123 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_SEQUENTIAL_SCAN
,
1126 if( ServicesFile
== INVALID_HANDLE_VALUE
)
1128 WSASetLastError( WSANO_RECOVERY
);
1132 /* Scan the services file ...
1134 * We will read up to BUFSIZ bytes per pass, until the buffer does not
1135 * contain a full line, then we will try to read more.
1137 * We fall from the loop if the buffer does not have a line terminator.
1142 ReadFile(ServicesFile
,
1143 ServiceDBData
+ ValidData
,
1144 sizeof( ServiceDBData
) - ValidData
,
1147 ValidData
+= ReadSize
;
1149 NextLine
= ThisLine
= ServiceDBData
;
1151 /* Find the beginning of the next line */
1152 while( NextLine
< ServiceDBData
+ ValidData
&&
1153 *NextLine
!= '\r' && *NextLine
!= '\n' ) NextLine
++;
1155 /* Zero and skip, so we can treat what we have as a string */
1156 if( NextLine
>= ServiceDBData
+ ValidData
)
1159 *NextLine
= 0; NextLine
++;
1161 Comment
= strchr( ThisLine
, '#' );
1162 if( Comment
) *Comment
= 0; /* Terminate at comment start */
1164 if(DecodeServEntFromString(ThisLine
,
1169 WS2_INTERNAL_MAX_ALIAS
) &&
1170 (htons(atoi( PortNumberStr
)) == port
) &&
1171 (proto
? !strcmp( ProtocolStr
, proto
) : TRUE
) )
1174 WS_DbgPrint(MAX_TRACE
,("Found the port entry.\n"));
1177 SizeNeeded
= sizeof(WINSOCK_GETSERVBYPORT_CACHE
) +
1178 (NextLine
- ThisLine
);
1182 /* Get rid of everything we read so far */
1183 while( NextLine
<= ServiceDBData
+ ValidData
&&
1184 isspace( *NextLine
) )
1189 WS_DbgPrint(MAX_TRACE
,("About to move %d chars\n",
1190 ServiceDBData
+ ValidData
- NextLine
));
1192 memmove(ServiceDBData
,
1194 ServiceDBData
+ ValidData
- NextLine
);
1195 ValidData
-= NextLine
- ServiceDBData
;
1196 WS_DbgPrint(MAX_TRACE
,("Valid bytes: %d\n", ValidData
));
1199 /* This we'll do no matter what */
1200 CloseHandle( ServicesFile
);
1204 WS_DbgPrint(MAX_TRACE
,("Not found\n"));
1205 WSASetLastError( WSANO_DATA
);
1209 if( !p
->Getservbyport
|| p
->Getservbyport
->Size
< SizeNeeded
)
1211 /* Free previous getservbyport buffer, allocate bigger */
1212 if( p
->Getservbyport
)
1213 HeapFree(GlobalHeap
, 0, p
->Getservbyport
);
1214 p
->Getservbyport
= HeapAlloc(GlobalHeap
,
1217 if( !p
->Getservbyport
)
1219 WS_DbgPrint(MIN_TRACE
,("Couldn't allocate %d bytes\n",
1221 WSASetLastError( WSATRY_AGAIN
);
1224 p
->Getservbyport
->Size
= SizeNeeded
;
1227 memmove(p
->Getservbyport
->Data
,
1229 NextLine
- ThisLine
);
1231 ADJ_PTR(PortNumberStr
,ThisLine
,p
->Getservbyport
->Data
);
1232 ADJ_PTR(ProtocolStr
,ThisLine
,p
->Getservbyport
->Data
);
1233 WS_DbgPrint(MAX_TRACE
, ("Port Number: %s, Protocol: %s\n",
1234 PortNumberStr
, ProtocolStr
));
1236 for( i
= 0; Aliases
[i
]; i
++ )
1238 ADJ_PTR(Aliases
[i
],ThisLine
,p
->Getservbyport
->Data
);
1239 WS_DbgPrint(MAX_TRACE
,("Aliases %d: %s\n", i
, Aliases
[i
]));
1242 memcpy(p
->Getservbyport
,Aliases
,sizeof(Aliases
));
1244 /* Create the struct proper */
1245 p
->Getservbyport
->ServerEntry
.s_name
= ServiceName
;
1246 p
->Getservbyport
->ServerEntry
.s_aliases
= p
->Getservbyport
->Aliases
;
1247 p
->Getservbyport
->ServerEntry
.s_port
= port
;
1248 p
->Getservbyport
->ServerEntry
.s_proto
= ProtocolStr
;
1250 WS_DbgPrint(MID_TRACE
,("s_name: %s\n", ServiceName
));
1252 return &p
->Getservbyport
->ServerEntry
;
1262 inet_addr(IN CONST CHAR FAR
* cp
)
1264 * FUNCTION: Converts a string containing an IPv4 address to an unsigned long
1266 * cp = Pointer to string with address to convert
1268 * Binary representation of IPv4 address, or INADDR_NONE
1280 if (strcmp(p
, " ") == 0)
1283 for (i
= 0; i
<= 3; i
++)
1285 u
+= (strtoul(p
, &p
, 0) << (i
* 8));
1305 inet_ntoa(IN IN_ADDR in
)
1310 p
= ((PWINSOCK_THREAD_BLOCK
)NtCurrentTeb()->WinSockData
)->Intoa
;
1311 _itoa(in
.S_un
.S_addr
& 0xFF, b
, 10);
1313 _itoa((in
.S_un
.S_addr
>> 8) & 0xFF, b
, 10);
1316 _itoa((in
.S_un
.S_addr
>> 16) & 0xFF, b
, 10);
1319 _itoa((in
.S_un
.S_addr
>> 24) & 0xFF, b
, 10);
1323 return (CHAR FAR
*)p
;