#endif
#ifndef SOCK_NONBLOCK
- /* set socket non-blocking */
- (void)curlx_nonblock(ctx->sock, TRUE);
+ /* Set socket non-blocking, must be a non-blocking socket for
+ * a non-blocking connect. */
+ error = curlx_nonblock(ctx->sock, TRUE);
+ if(error < 0) {
+ result = CURLE_UNSUPPORTED_PROTOCOL;
+ ctx->error = SOCKERRNO;
+ goto out;
+ }
#else
- if(data->set.fopensocket)
- /* set socket non-blocking */
- (void)curlx_nonblock(ctx->sock, TRUE);
+ if(data->set.fopensocket) {
+ /* Set socket non-blocking, must be a non-blocking socket for
+ * a non-blocking connect. */
+ error = curlx_nonblock(ctx->sock, TRUE);
+ if(error < 0) {
+ result = CURLE_UNSUPPORTED_PROTOCOL;
+ ctx->error = SOCKERRNO;
+ goto out;
+ }
+ }
#endif
ctx->sock_connected = (ctx->addr.socktype != SOCK_DGRAM);
out:
/* most recent unix versions */
int flags;
flags = sfcntl(sockfd, F_GETFL, 0);
+ if(flags < 0)
+ return -1;
+ /* Check if the current file status flags have already satisfied
+ * the request, if so, it's no need to call fcntl() to replicate it.
+ */
+ if(!!(flags & O_NONBLOCK) == !!nonblock)
+ return 0;
if(nonblock)
- return sfcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
- return sfcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
+ flags |= O_NONBLOCK;
+ else
+ flags &= ~O_NONBLOCK;
+ return sfcntl(sockfd, F_SETFL, flags);
#elif defined(HAVE_IOCTL_FIONBIO)