Commit Diff


commit - 69e1d1b6fb2f3fb532a3d70aeccc3fc32209afde
commit + 58cfcb902e49abdb76c80ab84121d509cf2dc82e
blob - 7fcf2564c2dc5997aee9db39299ebe81661f2d0c
blob + 009f30cdc9f0c7e9ca4cd4f75bebb9d875fdaf3c
--- ChangeLog
+++ ChangeLog
@@ -1,3 +1,7 @@
+2021-06-18  Omar Polo  <op@omarpolo.com>
+
+	* gemini.c (try_to_connect): use the async try_to_connect even when !HAVE_ASR_RUN
+
 2021-06-13  Omar Polo  <op@omarpolo.com>
 
 	* telescope.c (main): ignore SIGPIPE: prevent telescope from dying
blob - 986442cc5f9a7522e0257a7b308505eef946be21
blob + 3293bf279abb101b19418b65a4c9ab51ce428dd5
--- gemini.c
+++ gemini.c
@@ -52,8 +52,9 @@ struct req;
 
 static void		 die(void) __attribute__((__noreturn__));
 
-#if HAVE_ASR_RUN
 static void		 try_to_connect(int, short, void*);
+
+#if HAVE_ASR_RUN
 static void		 query_done(struct asr_result*, void*);
 static void		 async_conn_towards(struct req*);
 #else
@@ -103,8 +104,9 @@ struct req {
 	char			 buf[1024];
 	size_t			 off;
 
+	struct addrinfo		*servinfo, *p;
 #if HAVE_ASR_RUN
-	struct addrinfo		 hints, *servinfo, *p;
+	struct addrinfo		 hints;
 	struct event_asr	*asrev;
 #endif
 
@@ -138,7 +140,6 @@ die(void)
 	abort(); 		/* TODO */
 }
 
-#if HAVE_ASR_RUN
 static void
 try_to_connect(int fd, short ev, void *d)
 {
@@ -183,6 +184,7 @@ done:
 	setup_tls(req);
 }
 
+#if HAVE_ASR_RUN
 static void
 query_done(struct asr_result *res, void *d)
 {
@@ -219,9 +221,9 @@ async_conn_towards(struct req *req)
 static void
 blocking_conn_towards(struct req *req)
 {
-	struct addrinfo	 hints, *servinfo, *p;
+	struct addrinfo	 hints;
 	struct phos_uri	*url = &req->url;
-	int		 status, sock;
+	int		 status;
 	const char	*proto = "1965";
 
 	if (*url->port != '\0')
@@ -231,30 +233,15 @@ blocking_conn_towards(struct req *req)
 	hints.ai_family = AF_UNSPEC;
 	hints.ai_socktype = SOCK_STREAM;
 
-	if ((status = getaddrinfo(url->host, proto, &hints, &servinfo))) {
+	if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
 		close_with_errf(req, "failed to resolve %s: %s",
 		    url->host, gai_strerror(status));
 		return;
 	}
 
-	sock = -1;
-	for (p = servinfo; p != NULL; p = p->ai_next) {
-		if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
-			continue;
-		if (connect(sock, p->ai_addr, p->ai_addrlen) != -1)
-			break;
-		close(sock);
-	}
-	freeaddrinfo(servinfo);
-
-	if (sock == -1) {
-		close_with_errf(req, "couldn't connect to %s", url->host);
-		return;
-	}
-
-	req->fd = sock;
-	mark_nonblock(req->fd);
-	setup_tls(req);
+	req->fd = -1;
+	req->p = req->servinfo;
+	try_to_connect(0, 0, req);
 }
 #endif