Commit c69345aa authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Make VTCP_Connect() able to support async connections where

the calling code is responsible for poll(2)'ing the fd for
connection completion.
parent 46bfa187
......@@ -51,6 +51,7 @@ int VTCP_check_hup(int sock);
#ifdef SOL_SOCKET
void VTCP_name(const struct suckaddr *addr, char *abuf, unsigned alen,
char *pbuf, unsigned plen);
int VTCP_connected(int s);
int VTCP_connect(const struct suckaddr *name, int msec);
void VTCP_close(int *s);
void VTCP_set_read_timeout(int s, double seconds);
......
......@@ -212,10 +212,30 @@ VTCP_nonblocking(int sock)
*/
int
VTCP_connect(const struct suckaddr *name, int msec)
VTCP_connected(int s)
{
int s, i, k;
int k;
socklen_t l;
/* Find out if we got a connection */
l = sizeof k;
AZ(getsockopt(s, SOL_SOCKET, SO_ERROR, &k, &l));
/* An error means no connection established */
errno = k;
if (k) {
AZ(close(s));
return (-1);
}
(void)VTCP_blocking(s);
return (s);
}
int
VTCP_connect(const struct suckaddr *name, int msec)
{
int s, i;
struct pollfd fds[1];
const struct sockaddr *sa;
socklen_t sl;
......@@ -233,7 +253,7 @@ VTCP_connect(const struct suckaddr *name, int msec)
return (s);
/* Set the socket non-blocking */
if (msec > 0)
if (msec != 0)
(void)VTCP_nonblocking(s);
i = connect(s, sa, sl);
......@@ -244,6 +264,14 @@ VTCP_connect(const struct suckaddr *name, int msec)
return (-1);
}
if (msec < 0) {
/*
* Caller is responsible for waiting and
* calling VTCP_connected
*/
return (s);
}
assert(msec > 0);
/* Exercise our patience, polling for write */
fds[0].fd = s;
......@@ -258,19 +286,7 @@ VTCP_connect(const struct suckaddr *name, int msec)
return (-1);
}
/* Find out if we got a connection */
l = sizeof k;
AZ(getsockopt(s, SOL_SOCKET, SO_ERROR, &k, &l));
/* An error means no connection established */
errno = k;
if (k) {
AZ(close(s));
return (-1);
}
(void)VTCP_blocking(s);
return (s);
return (VTCP_connected(s));
}
/*--------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment