- /* The event loop does not protect against spurious events. Verify that we are actually connected. */
- if (connect(c->socket, &c->address.sa, sizeof(c->address)) == 0)
- logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): redundant connect() unexpectedly succeeded", c->name, c->hostname);
- else if (!sockisconn(sockerrno)) {
- if (!sockalready(sockerrno)) {
- logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while checking connection status for %s (%s): %s", c->name, c->hostname, sockstrerror(sockerrno));
+ /*
+ The event loop does not protect against spurious events. Verify that we are actually connected
+ by issuing an empty send() call.
+
+ Note that the behavior of send() on potentially unconnected sockets differ between platforms:
+ +------------+-----------+-------------+-----------+
+ | Event | POSIX | Linux | Windows |
+ +------------+-----------+-------------+-----------+
+ | Spurious | ENOTCONN | EWOULDBLOCK | ENOTCONN |
+ | Failed | ENOTCONN | (cause) | ENOTCONN |
+ | Successful | (success) | (success) | (success) |
+ +------------+-----------+-------------+-----------+
+ */
+ if (send(c->socket, NULL, 0, 0) != 0) {
+ if (sockwouldblock(sockerrno))
+ return;
+ int socket_error;
+ if (!socknotconn(sockerrno))
+ socket_error = sockerrno;
+ else {
+ socklen_t len = sizeof socket_error;
+ getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&socket_error, &len);
+ }
+ if (socket_error) {
+ logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(socket_error));