1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-04-17 15:44:34 +02:00

* http.c (connect_server): Try all A records for names with multiple

addresses until one answers for both MINGW32 and not MINGW32.
This commit is contained in:
David Shaw 2002-10-17 12:45:58 +00:00
parent e613304ae7
commit 9d32f6f06b
2 changed files with 88 additions and 73 deletions

View File

@ -1,3 +1,9 @@
2002-10-17 David Shaw <dshaw@jabberwocky.com>
* http.c (connect_server): Try all A records for names with
multiple addresses until one answers for both MINGW32 and not
MINGW32.
2002-10-10 David Shaw <dshaw@jabberwocky.com> 2002-10-10 David Shaw <dshaw@jabberwocky.com>
* http.c (connect_server): Properly handle a single A record that * http.c (connect_server): Properly handle a single A record that

View File

@ -711,71 +711,70 @@ start_server()
static int static int
connect_server( const char *server, ushort port ) connect_server( const char *server, ushort port )
{ {
int sd; int sock,i=0;
#ifdef __MINGW32__ struct sockaddr_in addr;
struct hostent *hp; struct hostent *host=NULL;
struct sockaddr_in ad;
unsigned long l; unsigned long l;
init_sockets (); memset(&addr,0,sizeof(addr));
memset (&ad, 0, sizeof(ad));
ad.sin_family = AF_INET;
ad.sin_port = htons(port);
if( (l = inet_addr (server)) != SOCKET_ERROR ) {
memcpy (&ad.sin_addr, &l, sizeof(l));
}
else if( (hp = gethostbyname (server)) ) {
if( hp->h_addrtype != AF_INET ) {
log_error ("%s: unknown address family\n", server);
return -1;
}
if ( hp->h_length != 4 ) {
log_error ("%s: illegal address length\n", server);
return -1;
}
memcpy (&ad.sin_addr, hp->h_addr, hp->h_length);
}
else {
log_error ("%s: host not found: ec=%d\n",
server, (int)WSAGetLastError ());
return -1;
}
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
log_error ("error creating socket: ex=%d\n",
(int)WSAGetLastError ());
return -1;
}
if( connect (sd, (struct sockaddr *)&ad, sizeof (ad) ) ) {
sock_close (sd);
return -1;
}
#else
struct sockaddr_in addr;
struct hostent *host;
int i=0;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
host = gethostbyname((char*)server);
if( !host ) #ifdef __MINGW32__
init_sockets ();
if((sock=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
{
log_error("error creating socket: ec=%d\n",(int)WSAGetLastError());
return -1; return -1;
}
sd = socket(AF_INET, SOCK_STREAM, 0); #else
if( sd == -1 ) if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
log_error("error creating socket\n");
return -1; return -1;
}
#endif
/* Try all A records until one responds. TODO: do this on the #ifdef __MINGW32__
MINGW32 side as well. */ /* Win32 gethostbyname doesn't handle IP addresses internally, so we
try inet_addr first on that platform only. */
if((l=inet_addr(server))==SOCKET_ERROR)
#endif
if((host=gethostbyname(server))==NULL)
{
#ifdef __MINGW32__
log_error("%s: host not found: ec=%d\n",server,(int)WSAGetLastError());
#else
log_error("%s: host not found\n",server);
#endif
sock_close(sock);
return -1;
}
if(host)
{
if(host->h_addrtype != AF_INET)
{
log_error ("%s: unknown address family\n", server);
sock_close(sock);
return -1;
}
if(host->h_length != 4 )
{
log_error ("%s: illegal address length\n", server);
sock_close(sock);
return -1;
}
/* Try all A records until one responds. */
while(host->h_addr_list[i]) while(host->h_addr_list[i])
{ {
addr.sin_addr = *(struct in_addr*)host->h_addr_list[i]; memcpy(&addr.sin_addr,host->h_addr_list[i],host->h_length);
if(connect( sd, (struct sockaddr *)&addr, sizeof addr) == 0) if(connect(sock,(struct sockaddr *)&addr,sizeof(addr))==0)
break; break;
i++; i++;
@ -783,12 +782,22 @@ connect_server( const char *server, ushort port )
if(host->h_addr_list[i]==0) if(host->h_addr_list[i]==0)
{ {
sock_close(sd); sock_close(sock);
return -1; return -1;
} }
}
else
{
memcpy(&addr.sin_addr,&l,sizeof(l));
#endif if(connect(sock,(struct sockaddr *)&addr,sizeof(addr))!=0)
return sd; {
sock_close(sock);
return -1;
}
}
return sock;
} }