- Rewrite completely broken free_hostent and free_servent functions
- Readability improvements to check_hostent and populate_hostent

svn path=/trunk/; revision=56755
This commit is contained in:
Thomas Faber 2012-06-19 23:51:28 +00:00
parent 63e9d00bb0
commit da39b38e47

View file

@ -518,15 +518,13 @@ void check_hostent(struct hostent **he)
sizeof(struct hostent) + MAX_HOSTNAME_LEN + 1); sizeof(struct hostent) + MAX_HOSTNAME_LEN + 1);
new_he->h_name = (PCHAR)(new_he + 1); new_he->h_name = (PCHAR)(new_he + 1);
new_he->h_aliases = 0; new_he->h_aliases = NULL;
new_he->h_addrtype = 0; // AF_INET new_he->h_addrtype = 0; // AF_INET
new_he->h_length = 0; // sizeof(in_addr) new_he->h_length = 0; // sizeof(in_addr)
new_he->h_addr_list = HeapAlloc(GlobalHeap, new_he->h_addr_list = HeapAlloc(GlobalHeap,
0, HEAP_ZERO_MEMORY,
sizeof(char *) * 2); sizeof(char *) * 2);
RtlZeroMemory(new_he->h_addr_list,
sizeof(char *) * 2);
*he = new_he; *he = new_he;
} }
} }
@ -544,7 +542,7 @@ void populate_hostent(struct hostent *he, char* name, DNS_A_DATA addr)
if( !he->h_aliases ) { if( !he->h_aliases ) {
he->h_aliases = HeapAlloc(GlobalHeap, 0, sizeof(char *)); he->h_aliases = HeapAlloc(GlobalHeap, 0, sizeof(char *));
he->h_aliases[0] = 0; he->h_aliases[0] = NULL;
} }
he->h_addrtype = AF_INET; he->h_addrtype = AF_INET;
he->h_length = sizeof(IN_ADDR); //sizeof(struct in_addr); he->h_length = sizeof(IN_ADDR); //sizeof(struct in_addr);
@ -563,33 +561,33 @@ void populate_hostent(struct hostent *he, char* name, DNS_A_DATA addr)
WS_DbgPrint(MID_TRACE,("he->h_addr_list[0] %x\n", he->h_addr_list[0])); WS_DbgPrint(MID_TRACE,("he->h_addr_list[0] %x\n", he->h_addr_list[0]));
RtlCopyMemory(he->h_addr_list[0], RtlCopyMemory(he->h_addr_list[0],
(char*)&addr.IpAddress, &addr.IpAddress,
sizeof(addr.IpAddress)); sizeof(addr.IpAddress));
he->h_addr_list[1] = 0; he->h_addr_list[1] = NULL;
} }
#define HFREE(x) if(x) { HeapFree(GlobalHeap, 0, (x)); x=0; }
void free_hostent(struct hostent *he) void free_hostent(struct hostent *he)
{ {
if(he) int i;
if (he)
{ {
char *next = 0; if (he->h_name)
HFREE(he->h_name); HeapFree(GlobalHeap, 0, he->h_name);
if(he->h_aliases) if (he->h_aliases)
{ {
next = he->h_aliases[0]; for (i = 0; he->h_aliases[i]; i++)
while(next) { HFREE(next); next++; } HeapFree(GlobalHeap, 0, he->h_aliases[i]);
} HeapFree(GlobalHeap, 0, he->h_aliases);
if(he->h_addr_list) }
{ if (he->h_addr_list)
next = he->h_addr_list[0]; {
while(next) { HFREE(next); next++; } for (i = 0; he->h_addr_list[i]; i++)
} HeapFree(GlobalHeap, 0, he->h_addr_list[i]);
HFREE(he->h_addr_list); HeapFree(GlobalHeap, 0, he->h_addr_list);
HFREE(he->h_aliases); }
HFREE(he); HeapFree(GlobalHeap, 0, he);
} }
} }
@ -672,13 +670,22 @@ struct hostent defined in w32api/include/winsock2.h
void free_servent(struct servent* s) void free_servent(struct servent* s)
{ {
char* next; int i;
HFREE(s->s_name);
next = s->s_aliases[0]; if (s)
while(next) { HFREE(next); next++; } {
s->s_port = 0; if (s->s_name)
HFREE(s->s_proto); HeapFree(GlobalHeap, 0, s->s_name);
HFREE(s); if (s->s_aliases)
{
for (i = 0; s->s_aliases[i]; i++)
HeapFree(GlobalHeap, 0, s->s_aliases[i]);
HeapFree(GlobalHeap, 0, s->s_aliases);
}
if (s->s_proto)
HeapFree(GlobalHeap, 0, s->s_proto);
HeapFree(GlobalHeap, 0, s);
}
} }
/* This function is far from perfect but it works enough */ /* This function is far from perfect but it works enough */
@ -1052,7 +1059,7 @@ gethostname(OUT CHAR FAR* name,
* *
* @unimplemented * @unimplemented
*/ */
static CHAR *no_aliases = 0; static CHAR *no_aliases = 0;
static PROTOENT protocols[] = static PROTOENT protocols[] =
{ {
@ -1061,7 +1068,7 @@ static PROTOENT protocols[] =
{"udp", &no_aliases, IPPROTO_UDP}, {"udp", &no_aliases, IPPROTO_UDP},
{NULL, NULL, 0} {NULL, NULL, 0}
}; };
LPPROTOENT LPPROTOENT
EXPORT EXPORT
getprotobyname(IN CONST CHAR FAR* name) getprotobyname(IN CONST CHAR FAR* name)
@ -1203,14 +1210,14 @@ getservbyname(IN CONST CHAR FAR* name,
WSASetLastError( WSANO_RECOVERY ); WSASetLastError( WSANO_RECOVERY );
return NULL; return NULL;
} }
/* Scan the services file ... /* Scan the services file ...
* *
* We will be share the buffer on the lines. If the line does not fit in * We will be share the buffer on the lines. If the line does not fit in
* the buffer, then moving it to the beginning of the buffer and read * the buffer, then moving it to the beginning of the buffer and read
* the remnants of line from file. * the remnants of line from file.
*/ */
/* Initial Read */ /* Initial Read */
ReadFile(ServicesFile, ReadFile(ServicesFile,
ServiceDBData, ServiceDBData,
@ -1219,7 +1226,7 @@ getservbyname(IN CONST CHAR FAR* name,
ThisLine = NextLine = ServiceDBData; ThisLine = NextLine = ServiceDBData;
EndValid = ServiceDBData + ReadSize; EndValid = ServiceDBData + ReadSize;
ServiceDBData[sizeof(ServiceDBData) - 1] = '\0'; ServiceDBData[sizeof(ServiceDBData) - 1] = '\0';
while(ReadSize) while(ReadSize)
{ {
for(; *NextLine != '\r' && *NextLine != '\n'; NextLine++) for(; *NextLine != '\r' && *NextLine != '\n'; NextLine++)
@ -1227,7 +1234,7 @@ getservbyname(IN CONST CHAR FAR* name,
if(NextLine == EndValid) if(NextLine == EndValid)
{ {
int LineLen = NextLine - ThisLine; int LineLen = NextLine - ThisLine;
if(ThisLine == ServiceDBData) if(ThisLine == ServiceDBData)
{ {
WS_DbgPrint(MIN_TRACE,("Line too long")); WS_DbgPrint(MIN_TRACE,("Line too long"));
@ -1236,23 +1243,23 @@ getservbyname(IN CONST CHAR FAR* name,
} }
memmove(ServiceDBData, ThisLine, LineLen); memmove(ServiceDBData, ThisLine, LineLen);
ReadFile(ServicesFile, ServiceDBData + LineLen, ReadFile(ServicesFile, ServiceDBData + LineLen,
sizeof( ServiceDBData )-1 - LineLen, sizeof( ServiceDBData )-1 - LineLen,
&ReadSize, NULL ); &ReadSize, NULL );
EndValid = ServiceDBData + LineLen + ReadSize; EndValid = ServiceDBData + LineLen + ReadSize;
NextLine = ServiceDBData + LineLen; NextLine = ServiceDBData + LineLen;
ThisLine = ServiceDBData; ThisLine = ServiceDBData;
if(!ReadSize) break; if(!ReadSize) break;
} }
} }
*NextLine = '\0'; *NextLine = '\0';
Comment = strchr( ThisLine, '#' ); Comment = strchr( ThisLine, '#' );
if( Comment ) *Comment = '\0'; /* Terminate at comment start */ if( Comment ) *Comment = '\0'; /* Terminate at comment start */
if(DecodeServEntFromString(ThisLine, if(DecodeServEntFromString(ThisLine,
&ServiceName, &ServiceName,
&PortNumberStr, &PortNumberStr,
@ -1644,7 +1651,7 @@ getaddrinfo(const char FAR * nodename,
return WSAEINVAL; return WSAEINVAL;
if (nodename == NULL && servname == NULL) if (nodename == NULL && servname == NULL)
return WSAHOST_NOT_FOUND; return WSAHOST_NOT_FOUND;
if (!WSAINITIALIZED) if (!WSAINITIALIZED)
return WSANOTINITIALISED; return WSANOTINITIALISED;
@ -1681,7 +1688,7 @@ getaddrinfo(const char FAR * nodename,
/* Is it an IPv6 address? */ /* Is it an IPv6 address? */
if (strstr(nodename, ":")) if (strstr(nodename, ":"))
return WSAHOST_NOT_FOUND; return WSAHOST_NOT_FOUND;
/* Is it an IPv4 address? */ /* Is it an IPv4 address? */
addr = inet_addr(nodename); addr = inet_addr(nodename);
if (addr != INADDR_NONE) if (addr != INADDR_NONE)
@ -1720,7 +1727,7 @@ getaddrinfo(const char FAR * nodename,
{ {
/* accept only A records */ /* accept only A records */
if (currdns->wType != DNS_TYPE_A) continue; if (currdns->wType != DNS_TYPE_A) continue;
ai = new_addrinfo(ai); ai = new_addrinfo(ai);
if (ret == NULL) if (ret == NULL)
ret = ai; ret = ai;
@ -1771,7 +1778,7 @@ getaddrinfo(const char FAR * nodename,
if (ret == NULL) if (ret == NULL)
return WSAHOST_NOT_FOUND; return WSAHOST_NOT_FOUND;
if (hints && hints->ai_family != PF_UNSPEC && hints->ai_family != PF_INET) if (hints && hints->ai_family != PF_UNSPEC && hints->ai_family != PF_INET)
{ {
freeaddrinfo(ret); freeaddrinfo(ret);