1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-03 12:11:33 +01: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>
* http.c (connect_server): Properly handle a single A record that

View File

@ -711,71 +711,70 @@ start_server()
static int
connect_server( const char *server, ushort port )
{
int sd;
#ifdef __MINGW32__
struct hostent *hp;
struct sockaddr_in ad;
int sock,i=0;
struct sockaddr_in addr;
struct hostent *host=NULL;
unsigned long l;
init_sockets ();
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;
memset(&addr,0,sizeof(addr));
addr.sin_family = AF_INET;
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;
sd = socket(AF_INET, SOCK_STREAM, 0);
if( sd == -1 )
}
#else
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
log_error("error creating socket\n");
return -1;
}
#endif
/* Try all A records until one responds. TODO: do this on the
MINGW32 side as well. */
#ifdef __MINGW32__
/* 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])
{
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;
i++;
@ -783,12 +782,22 @@ connect_server( const char *server, ushort port )
if(host->h_addr_list[i]==0)
{
sock_close(sd);
sock_close(sock);
return -1;
}
}
else
{
memcpy(&addr.sin_addr,&l,sizeof(l));
#endif
return sd;
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr))!=0)
{
sock_close(sock);
return -1;
}
}
return sock;
}