summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorDavid Shaw <dshaw@jabberwocky.com>2002-10-17 14:45:58 +0200
committerDavid Shaw <dshaw@jabberwocky.com>2002-10-17 14:45:58 +0200
commit9d32f6f06b8f0bad14c5007dffe69cf48e85be96 (patch)
treeb62591a0d0f829e0f06ef0025f2f9b2e60d62656 /util
parent* g10.c (main): Handle --strict and --no-strict from the command line (diff)
downloadgnupg2-9d32f6f06b8f0bad14c5007dffe69cf48e85be96.tar.xz
gnupg2-9d32f6f06b8f0bad14c5007dffe69cf48e85be96.zip
* http.c (connect_server): Try all A records for names with multiple
addresses until one answers for both MINGW32 and not MINGW32.
Diffstat (limited to 'util')
-rw-r--r--util/ChangeLog6
-rw-r--r--util/http.c141
2 files changed, 81 insertions, 66 deletions
diff --git a/util/ChangeLog b/util/ChangeLog
index 26dbed6fe..e9caf465a 100644
--- a/util/ChangeLog
+++ b/util/ChangeLog
@@ -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
diff --git a/util/http.c b/util/http.c
index e76ab940c..fa3d512e9 100644
--- a/util/http.c
+++ b/util/http.c
@@ -711,84 +711,93 @@ start_server()
static int
connect_server( const char *server, ushort port )
{
- int sd;
+ int sock,i=0;
+ struct sockaddr_in addr;
+ struct hostent *host=NULL;
+ unsigned long l;
+
+ memset(&addr,0,sizeof(addr));
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+
#ifdef __MINGW32__
- struct hostent *hp;
- struct sockaddr_in ad;
- 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;
- }
+ init_sockets ();
- if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
- log_error ("error creating socket: ex=%d\n",
- (int)WSAGetLastError ());
- return -1;
+ if((sock=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
+ {
+ log_error("error creating socket: ec=%d\n",(int)WSAGetLastError());
+ return -1;
}
-
- if( connect (sd, (struct sockaddr *)&ad, sizeof (ad) ) ) {
- sock_close (sd);
- return -1;
+#else
+ if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
+ {
+ log_error("error creating socket\n");
+ return -1;
}
+#endif
+
+#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
- struct sockaddr_in addr;
- struct hostent *host;
- int i=0;
-
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- host = gethostbyname((char*)server);
- if( !host )
- return -1;
-
- sd = socket(AF_INET, SOCK_STREAM, 0);
- if( sd == -1 )
+ 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;
+ }
- /* Try all A records until one responds. TODO: do this on the
- MINGW32 side as well. */
+ if(host->h_length != 4 )
+ {
+ log_error ("%s: illegal address length\n", server);
+ sock_close(sock);
+ return -1;
+ }
- while(host->h_addr_list[i])
- {
- addr.sin_addr = *(struct in_addr*)host->h_addr_list[i];
+ /* Try all A records until one responds. */
+ while(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)
- break;
+ if(connect(sock,(struct sockaddr *)&addr,sizeof(addr))==0)
+ break;
- i++;
- }
+ i++;
+ }
- if(host->h_addr_list[i]==0)
- {
- sock_close(sd);
- return -1;
- }
+ if(host->h_addr_list[i]==0)
+ {
+ 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;
}