Skip to content

Commit

Permalink
SOCKET: Implement missing non-blocking for TCP sockets opened with co…
Browse files Browse the repository at this point in the history
…nnect

Outbound connections are rare and most may have wanted explicit blocking
behavior, so no one noticed the missing non-blocking case.

Any place which did do outbound connects have explicitly added
SIM_SOCK_OPT_BLOCKING so that the prior behavior is preserved.

The SIM_SOCK_OPT_BLOCKING flag is no honored as it was originally
intended to be.
  • Loading branch information
markpizz committed Mar 18, 2020
1 parent b848cb1 commit 89cd649
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
11 changes: 8 additions & 3 deletions sim_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ if (sta == SOCKET_ERROR) /* bind error? */
if (!(opt_flags & SIM_SOCK_OPT_BLOCKING)) {
sta = sim_setnonblock (newsock); /* set nonblocking */
if (sta == SOCKET_ERROR) /* fcntl error? */
return sim_err_sock (newsock, "fcntl");
return sim_err_sock (newsock, "setnonblock");
}
sta = listen (newsock, 1); /* listen on socket */
if (sta == SOCKET_ERROR) /* listen error? */
Expand Down Expand Up @@ -989,7 +989,7 @@ if (!(opt_flags & SIM_SOCK_OPT_BLOCKING)) {
sta = sim_setnonblock (newsock); /* set nonblocking */
if (sta == SOCKET_ERROR) { /* fcntl error? */
p_freeaddrinfo (result);
return sim_err_sock (newsock, "fcntl");
return sim_err_sock (newsock, "setnonblock");
}
}
if ((!(opt_flags & SIM_SOCK_OPT_DATAGRAM)) && (opt_flags & SIM_SOCK_OPT_NODELAY)) {
Expand All @@ -1007,6 +1007,11 @@ if (!(opt_flags & SIM_SOCK_OPT_DATAGRAM)) {
if (sta == -1)
return sim_err_sock (newsock, "setsockopt KEEPALIVE");
}
if (!(opt_flags & SIM_SOCK_OPT_BLOCKING)) {
sta = sim_setnonblock (newsock); /* set nonblocking */
if (sta == SOCKET_ERROR) /* fcntl error? */
return sim_err_sock (newsock, "setnonblock");
}
sta = connect (newsock, result->ai_addr, result->ai_addrlen);
p_freeaddrinfo (result);
if (sta == SOCKET_ERROR) {
Expand Down Expand Up @@ -1076,7 +1081,7 @@ if (connectaddr != NULL) {
if (!(opt_flags & SIM_SOCK_OPT_BLOCKING)) {
sta = sim_setnonblock (newsock); /* set nonblocking */
if (sta == SOCKET_ERROR) /* fcntl error? */
return sim_err_sock (newsock, "fcntl");
return sim_err_sock (newsock, "setnonblock");
}

if ((opt_flags & SIM_SOCK_OPT_NODELAY)) {
Expand Down
2 changes: 1 addition & 1 deletion sim_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int sim_parse_addr_ex (const char *cptr, char *host, size_t hostlen, const char
SOCKET sim_master_sock_ex (const char *hostport, int *parse_status, int opt_flags);
#define sim_master_sock(hostport, parse_status) sim_master_sock_ex(hostport, parse_status, ((sim_switches & SWMASK ('U')) ? SIM_SOCK_OPT_REUSEADDR : 0))
SOCKET sim_connect_sock_ex (const char *sourcehostport, const char *hostport, const char *default_host, const char *default_port, int opt_flags);
#define sim_connect_sock(hostport, default_host, default_port) sim_connect_sock_ex(NULL, hostport, default_host, default_port, 0)
#define sim_connect_sock(hostport, default_host, default_port) sim_connect_sock_ex(NULL, hostport, default_host, default_port, SIM_SOCK_OPT_BLOCKING)
SOCKET sim_accept_conn_ex (SOCKET master, char **connectaddr, int opt_flags);
#define sim_accept_conn(master, connectaddr) sim_accept_conn_ex(master, connectaddr, 0)
int sim_check_conn (SOCKET sock, int rd);
Expand Down
24 changes: 18 additions & 6 deletions sim_tmxr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,9 @@ for (i = 0; i < mp->lines; i++) { /* check each line in se
(!lp->modem_control || (lp->modembits & TMXR_MDM_DTR))) {
snprintf (msg, sizeof (msg) - 1, "tmxr_poll_conn() - establishing outgoing connection to: %s", lp->destination);
tmxr_debug_connect_line (lp, msg);
lp->connecting = sim_connect_sock_ex (lp->datagram ? lp->port : NULL, lp->destination, "localhost", NULL, (lp->datagram ? SIM_SOCK_OPT_DATAGRAM : 0) | (lp->mp->packet ? SIM_SOCK_OPT_NODELAY : 0));
lp->connecting = sim_connect_sock_ex (lp->datagram ? lp->port : NULL, lp->destination, "localhost", NULL, (lp->datagram ? SIM_SOCK_OPT_DATAGRAM : 0) |
(lp->mp->packet ? SIM_SOCK_OPT_NODELAY : 0) |
SIM_SOCK_OPT_BLOCKING);
}

}
Expand Down Expand Up @@ -1354,7 +1356,9 @@ if ((lp->destination) && (!lp->serport)) {
if ((!lp->modem_control) || (lp->modembits & TMXR_MDM_DTR)) {
sprintf (msg, "tmxr_reset_ln_ex() - connecting to %s", lp->destination);
tmxr_debug_connect_line (lp, msg);
lp->connecting = sim_connect_sock_ex (lp->datagram ? lp->port : NULL, lp->destination, "localhost", NULL, (lp->datagram ? SIM_SOCK_OPT_DATAGRAM : 0) | (lp->packet ? SIM_SOCK_OPT_NODELAY : 0));
lp->connecting = sim_connect_sock_ex (lp->datagram ? lp->port : NULL, lp->destination, "localhost", NULL, (lp->datagram ? SIM_SOCK_OPT_DATAGRAM : 0) |
(lp->packet ? SIM_SOCK_OPT_NODELAY : 0) |
SIM_SOCK_OPT_BLOCKING);
}
}
tmxr_init_line (lp); /* initialize line state */
Expand Down Expand Up @@ -1644,7 +1648,9 @@ if (lp->mp && lp->modem_control) { /* This API ONLY works on mo

sprintf (msg, "tmxr_set_get_modem_bits() - establishing outgoing connection to: %s", lp->destination);
tmxr_debug_connect_line (lp, msg);
lp->connecting = sim_connect_sock_ex (lp->datagram ? lp->port : NULL, lp->destination, "localhost", NULL, (lp->datagram ? SIM_SOCK_OPT_DATAGRAM : 0) | (lp->packet ? SIM_SOCK_OPT_NODELAY : 0));
lp->connecting = sim_connect_sock_ex (lp->datagram ? lp->port : NULL, lp->destination, "localhost", NULL, (lp->datagram ? SIM_SOCK_OPT_DATAGRAM : 0) |
(lp->packet ? SIM_SOCK_OPT_NODELAY : 0) |
SIM_SOCK_OPT_BLOCKING);
}
}
}
Expand Down Expand Up @@ -2799,7 +2805,9 @@ while (*tptr) {
else
return sim_messagef (SCPE_ARG, "Unexpected specifier: %s\n", eptr);
}
sock = sim_connect_sock_ex (NULL, hostport, "localhost", NULL, (datagram ? SIM_SOCK_OPT_DATAGRAM : 0) | (packet ? SIM_SOCK_OPT_NODELAY : 0));
sock = sim_connect_sock_ex (NULL, hostport, "localhost", NULL, (datagram ? SIM_SOCK_OPT_DATAGRAM : 0) |
(packet ? SIM_SOCK_OPT_NODELAY : 0) |
SIM_SOCK_OPT_BLOCKING);
if (sock != INVALID_SOCKET)
sim_close_sock (sock);
else
Expand Down Expand Up @@ -2951,7 +2959,9 @@ while (*tptr) {
return sim_messagef (SCPE_ARG, "Missing listen port for Datagram socket\n");
}
lp->packet = packet;
sock = sim_connect_sock_ex (datagram ? listen : NULL, hostport, "localhost", NULL, (datagram ? SIM_SOCK_OPT_DATAGRAM : 0) | (packet ? SIM_SOCK_OPT_NODELAY : 0));
sock = sim_connect_sock_ex (datagram ? listen : NULL, hostport, "localhost", NULL, (datagram ? SIM_SOCK_OPT_DATAGRAM : 0) |
(packet ? SIM_SOCK_OPT_NODELAY : 0) |
SIM_SOCK_OPT_BLOCKING);
if (sock != INVALID_SOCKET) {
_mux_detach_line (lp, FALSE, TRUE);
lp->destination = (char *)malloc(1+strlen(hostport));
Expand Down Expand Up @@ -3066,7 +3076,9 @@ while (*tptr) {
else
return sim_messagef (SCPE_ARG, "Missing listen port for Datagram socket\n");
}
sock = sim_connect_sock_ex (datagram ? listen : NULL, hostport, "localhost", NULL, (datagram ? SIM_SOCK_OPT_DATAGRAM : 0) | (packet ? SIM_SOCK_OPT_NODELAY : 0));
sock = sim_connect_sock_ex (datagram ? listen : NULL, hostport, "localhost", NULL, (datagram ? SIM_SOCK_OPT_DATAGRAM : 0) |
(packet ? SIM_SOCK_OPT_NODELAY : 0) |
SIM_SOCK_OPT_BLOCKING);
if (sock != INVALID_SOCKET) {
_mux_detach_line (lp, FALSE, TRUE);
lp->destination = (char *)malloc(1+strlen(hostport));
Expand Down
2 changes: 1 addition & 1 deletion slirp_glue/sim_slirp.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ else {
if ((rnd_val & 0xFFFF) == 0)
++rnd_val;
sprintf (db_host, "localhost:%d", (int)(rnd_val & 0xFFFF));
slirp->db_chime = sim_connect_sock_ex (db_host, db_host, NULL, NULL, SIM_SOCK_OPT_DATAGRAM);
slirp->db_chime = sim_connect_sock_ex (db_host, db_host, NULL, NULL, SIM_SOCK_OPT_DATAGRAM | SIM_SOCK_OPT_BLOCKING);
} while (slirp->db_chime == INVALID_SOCKET);
memset (&pfd, 0, sizeof (pfd));
pfd.fd = slirp->db_chime;
Expand Down

0 comments on commit 89cd649

Please sign in to comment.