rem: soap_set_sender_error(soap, tcp_error(soap), "TCP socket failed in tcp_connect()", SOAP_TCP_ERROR);
add: soap_set_sender_error(soap, tcp_error(soap), "socket failed in tcp_connect()", SOAP_TCP_ERROR);
context: tcp_connect(struct soap *soap, const char *endpoint, const char *host, int port){ struct sockaddr_in sockaddr;/* WR[ */#ifdef WITH_IPV6  struct addrinfo *addrinfo;  struct addrinfo hints;  struct addrinfo resaddr;  struct sockaddr_storage addrstorage;  int err;#endif /* WITH_IPV6 *//* ]WR */  register int fd;#ifndef WITH_LEAN  int len = SOAP_BUFLEN;  int set = 1;#endif  if (soap_valid_socket(soap->socket))    soap->fclosesocket(soap, (SOAP_SOCKET)soap->socket);  soap->socket = SOAP_INVALID_SOCKET;  if (tcp_init(soap))  { soap->errnum = 0;    soap_set_sender_error(soap, tcp_error(soap), "TCP initialization failed in tcp_connect()", SOAP_TCP_ERROR);    return SOAP_INVALID_SOCKET;  }  soap->errmode = 0;/* WR[ */#ifdef WITH_IPV6  memset((void*)&hints, 0, sizeof(hints));  hints.ai_family = PF_UNSPEC;  hints.ai_socktype = SOCK_STREAM;  soap->errmode = 2;  if (soap->proxy_host)    err = getaddrinfo(soap->proxy_host, soap_int2s(soap, soap->proxy_port), &hints, &addrinfo);  else    err = getaddrinfo(host, soap_int2s(soap, port), &hints, &addrinfo);  if (addrinfo)  { resaddr = *addrinfo;    addrstorage = *((struct sockaddr_storage*)addrinfo->ai_addr);    resaddr.ai_addr = (struct sockaddr*)&addrstorage;    freeaddrinfo(addrinfo);  }  if (err)  { soap_set_sender_error(soap, gai_strerror(err), "TCP getaddrinfo on proxy host failed in tcp_connect()", SOAP_TCP_ERROR);    return SOAP_INVALID_SOCKET;  }  fd = (int)socket(resaddr.ai_family, resaddr.ai_socktype, resaddr.ai_protocol); /* modified to use fd */  soap->errmode = 0;#else /* WITH_IPV6 *//* ]WR */  fd = (int)socket(AF_INET, SOCK_STREAM, 0);/* WR[ */#endif /* WITH_IPV6 *//* ]WR */  if (fd < 0)  { soap->errnum = soap_socket_errno;    soap_set_sender_error(soap, tcp_error(soap), "TCP socket failed in tcp_connect()", SOAP_TCP_ERROR);    return SOAP_INVALID_SOCKET;  }#ifdef SOCKET_CLOSE_ON_EXEC#ifdef WIN32#ifndef UNDER_CE  SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0);#endif#else  fcntl (fd, F_SETFD, 1);#endif#endif#ifndef WITH_LEAN  if (soap->connect_flags & SO_LINGER)  { struct linger linger;    memset((void*)&linger, 0, sizeof(linger));    linger.l_onoff = 1;    linger.l_linger = 0;    if (setsockopt((SOAP_SOCKET)fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(struct linger)))    { soap->errnum = soap_socket_errno;      soap_set_sender_error(soap, tcp_error(soap), "TCP setsockopt SO_LINGER failed in tcp_connect()", SOAP_TCP_ERROR);      soap->fclosesocket(soap, (SOAP_SOCKET)fd);      return SOAP_INVALID_SOCKET;    }  }  if ((soap->connect_flags & ~SO_LINGER) && setsockopt((SOAP_SOCKET)fd, SOL_SOCKET, soap->connect_flags & ~SO_LINGER, (char*)&set, sizeof(int)))  { soap->errnum = soap_socket_errno;    soap_set_sender_error(soap, tcp_error(soap), "TCP setsockopt failed in tcp_connect()", SOAP_TCP_ERROR);    soap->fclosesocket(soap, (SOAP_SOCKET)fd);    return SOAP_INVALID_SOCKET;  }  if (soap->keep_alive && setsockopt((SOAP_SOCKET)fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&set, sizeof(int)))  { soap->errnum = soap_socket_errno;    soap_set_sender_error(soap, tcp_error(soap), "TCP setsockopt SO_KEEPALIVE failed in tcp_connect()", SOAP_TCP_ERROR);    soap->fclosesocket(soap, (SOAP_SOCKET)fd);    return SOAP_INVALID_SOCKET;  }  if (setsockopt((SOAP_SOCKET)fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))  { soap->errnum = soap_socket_errno;    soap_set_sender_error(soap, tcp_error(soap), "TCP setsockopt SO_SNDBUF failed in tcp_connect()", SOAP_TCP_ERROR);    soap->fclosesocket(soap, (SOAP_SOCKET)fd);    return SOAP_INVALID_SOCKET;  }  if (setsockopt((SOAP_SOCKET)fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))  { soap->errnum = soap_socket_errno;    soap_set_sender_error(soap, tcp_error(soap), "TCP setsockopt SO_RCVBUF failed in tcp_connect()", SOAP_TCP_ERROR);    soap->fclosesocket(soap, (SOAP_SOCKET)fd);    return SOAP_INVALID_SOCKET;  }#ifdef TCP_NODELAY  if (setsockopt((SOAP_SOCKET)fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))  { soap->errnum = soap_socket_errno;    soap_set_sender_error(soap, tcp_error(soap), "TCP setsockopt TCP_NODELAY failed in tcp_connect()", SOAP_TCP_ERROR);    soap->fclosesocket(soap, (SOAP_SOCKET)fd);    return SOAP_INVALID_SOCKET;  }#endif#endif/* WR[ */#ifndef WITH_IPV6/* ]WR */  memset((void*)&sockaddr, 0, sizeof(sockaddr));  sockaddr.sin_family = AF_INET;  DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Open socket %d to host='%s'\n", fd, host));  soap->errmode = 2;  if (soap->proxy_host)  { if (soap->fresolve(soap, soap->proxy_host, &sockaddr.sin_addr))    { soap_set_sender_error(soap, tcp_error(soap), "TCP get proxy host by name failed in tcp_connect()", SOAP_TCP_ERROR);      soap->fclosesocket(soap, (SOAP_SOCKET)fd);      return SOAP_INVALID_SOCKET;    }    sockaddr.sin_port = htons((short)soap->proxy_port);  }  else  { if (soap->fresolve(soap, host, &sockaddr.sin_addr))    { soap_set_sender_error(soap, tcp_error(soap), "TCP get host by name failed in tcp_connect()", SOAP_TCP_ERROR);      soap->fclosesocket(soap, (SOAP_SOCKET)fd);      return SOAP_INVALID_SOCKET;    }    sockaddr.sin_port = htons((short)port);  }  soap->errmode = 0;/* WR[ */#endif /* WITH_IPV6 *//* ]WR */#ifndef WITH_LEAN  if (soap->connect_timeout)#if defined(WIN32)  { u_long nonblocking = 1;    ioctlsocket((SOAP_SOCKET)fd, FIONBIO, &nonblocking);  }/* WR[ */#elif defined(VXWORKS)  { vx_nonblocking = TRUE;    ioctl((SOAP_SOCKET)fd, FIONBIO, (int)(&vx_nonblocking)); /* modified to use fd */  }/* ]WR */#else    fcntl((SOAP_SOCKET)fd, F_SETFL, fcntl((SOAP_SOCKET)fd, F_GETFL)|O_NONBLOCK);#endif  else#if defined(WIN32)  { u_long blocking = 0;    ioctlsocket((SOAP_SOCKET)fd, FIONBIO, &blocking);  }/* WR[ */#elif defined(VXWORKS)  { vx_nonblocking = FALSE;    ioctl((SOAP_SOCKET)fd, FIONBIO, (int)(&vx_nonblocking)); /* modified to use fd */  }/* ]WR */#else    fcntl((SOAP_SOCKET)fd, F_SETFL, fcntl((SOAP_SOCKET)fd, F_GETFL)&~O_NONBLOCK);#endif#endif  for (;;)  { /* WR[ */#ifdef WITH_IPV6    if (connect((SOAP_SOCKET)fd, resaddr.ai_addr, resaddr.ai_addrlen)) /* modified to use fd */#else /* WITH_IPV6 */    if (connect((SOAP_SOCKET)fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)))#endif /* WITH_IPV6 *//* ]WR */    { #ifndef WITH_LEAN      if (soap->connect_timeout && (soap_socket_errno == SOAP_EINPROGRESS || soap_socket_errno == SOAP_EWOULDBLOCK))      { struct timeval timeout;#if defined(SOCKLEN_T)        SOCKLEN_T n = sizeof(struct sockaddr_in);#elif defined(__socklen_t_defined) || defined(_SOCKLEN_T) || defined(CYGWIN) || defined(__FreeBSD__)        socklen_t n = sizeof(struct sockaddr_in);#elif defined(WIN32) || defined(__APPLE__) || defined(HP_UX) || defined(SUN_OS) || defined(OPENSERVER) || defined(TRU64) || defined(VXWORKS)        int n = sizeof(struct sockaddr_in);#else        size_t n = sizeof(struct sockaddr_in);#endif        fd_set fds;        if (soap->connect_timeout > 0)        { timeout.tv_sec = soap->connect_timeout;          timeout.tv_usec = 0;        }        else        { timeout.tv_sec = -soap->connect_timeout/1000000;          timeout.tv_usec = -soap->connect_timeout%1000000;        }        FD_ZERO(&fds);        FD_SET((SOAP_SOCKET)fd, &fds);        for (;;)        { int r = select((SOAP_SOCKET)(fd + 1), NULL, &fds, NULL, &timeout);          if (r > 0)	    break;          if (!r)          { soap->errnum = 0;            DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connect timeout\n"));            soap_set_sender_error(soap, "Timeout", "TCP connect failed in tcp_connect()", SOAP_TCP_ERROR);            soap->fclosesocket(soap, (SOAP_SOCKET)fd);            return SOAP_INVALID_SOCKET;          }          if (soap_socket_errno != SOAP_EINTR)          { soap->errnum = soap_socket_errno;            DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));            soap_set_sender_error(soap, tcp_error(soap), "TCP connect failed in tcp_connect()", SOAP_TCP_ERROR);            soap->fclosesocket(soap, (SOAP_SOCKET)fd);            return SOAP_INVALID_SOCKET;          }        }	n = sizeof(soap->errnum);        if (!getsockopt((SOAP_SOCKET)fd, SOL_SOCKET, SO_ERROR, (char*)&soap->errnum, &n) && !soap->errnum)          break;        DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));        soap->errnum = soap_socket_errno;        soap_set_sender_error(soap, tcp_error(soap), "TCP connect failed in tcp_connect()", SOAP_TCP_ERROR);        soap->fclosesocket(soap, (SOAP_SOCKET)fd);        return SOAP_INVALID_SOCKET;      }      else#endif      if (soap_socket_errno != SOAP_EINTR)      { soap->errnum = soap_socket_errno;        DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));        soap_set_sender_error(soap, tcp_error(soap), "TCP connect failed in tcp_connect()", SOAP_TCP_ERROR);        soap->fclosesocket(soap, (SOAP_SOCKET)fd);        return SOAP_INVALID_SOCKET;      }    }      else      break;  }#ifndef WITH_LEAN  if (soap->connect_timeout)#if defined(WIN32)  { u_long blocking = 0;    ioctlsocket((SOAP_SOCKET)fd, FIONBIO, &blocking);  }/* WR[ */#elif defined(VXWORKS)  { vx_nonblocking = FALSE;    ioctl((SOAP_SOCKET)fd, FIONBIO, (int)(&vx_nonblocking)); /* modified to use fd */  }/* ]WR */#else    fcntl((SOAP_SOCKET)fd, F_SETFL, fcntl((SOAP_SOCKET)fd, F_GETFL)&~O_NONBLOCK);#endif#endif  soap->socket = fd;#ifdef WITH_OPENSSL  soap->imode &= ~SOAP_ENC_SSL;  soap->omode &= ~SOAP_ENC_SSL;  if (!strncmp(endpoint, "https:", 6))  { int r;    if (soap->proxy_host)    { short v;      unsigned int k = soap->omode; /* make sure we only parse HTTP */      size_t n = soap->count; /* save the content length */      soap->omode &= ~SOAP_ENC; /* mask IO and ENC */      soap->omode |= SOAP_IO_BUFFER;      soap_begin_send(soap);      DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connecting to proxy server\n"));      sprintf(soap->tmpbuf, "CONNECT %s:%d HTTP/%s", host, port, soap->http_version);      if ((soap->error = soap->fposthdr(soap, soap->tmpbuf, NULL)))        return SOAP_INVALID_SOCKET;#ifndef WITH_LEAN      if (soap->proxy_userid && soap->proxy_passwd && strlen(soap->proxy_userid) + strlen(soap->proxy_passwd) < 761)      { sprintf(soap->tmpbuf + 262, "%s:%s", soap->proxy_userid, soap->proxy_passwd);        strcpy(soap->tmpbuf, "Basic ");        soap_s2base64(soap, (const unsigned char*)(soap->tmpbuf + 262), soap->tmpbuf + 6, strlen(soap->tmpbuf + 262));        if ((soap->error = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf)))          return soap->error;      }#endif      if ((soap->error = soap->fposthdr(soap, NULL, NULL))       || soap_flush(soap))        return SOAP_INVALID_SOCKET;      soap->omode = k;      k = soap->imode;      soap->imode &= ~SOAP_ENC; /* mask IO and ENC */      v = soap->version; /* preserve */      if (soap_begin_recv(soap))        return SOAP_INVALID_SOCKET;      soap->version = v; /* restore */      soap->imode = k; /* restore */      soap->count = n; /* restore */      soap_begin_send(soap);    }    if (!soap->ctx && (soap->error = soap->fsslauth(soap)))    { soap_set_sender_error(soap, "SSL error", "SSL authentication failed in tcp_connect(): check password, key file, and ca file.", SOAP_SSL_ERROR);      return SOAP_INVALID_SOCKET;    }    soap->ssl = SSL_new(soap->ctx);    if (!soap->ssl)    { soap->error = SOAP_SSL_ERROR;      return SOAP_INVALID_SOCKET;    }    if (soap->session)    { if (!strcmp(soap->session_host, host) && soap->session_port == port)        SSL_set_session(soap->ssl, soap->session);      SSL_SESSION_free(soap->session);      soap->session = NULL;    }    soap->imode |= SOAP_ENC_SSL;    soap->omode |= SOAP_ENC_SSL;    soap->bio = BIO_new_socket((SOAP_SOCKET)fd, BIO_NOCLOSE);    SSL_set_bio(soap->ssl, soap->bio, soap->bio);#ifndef WITH_LEAN    if (soap->connect_timeout)#ifdef WIN32    { u_long nonblocking = 1;      ioctlsocket((SOAP_SOCKET)fd, FIONBIO, &nonblocking);    }#else      fcntl((SOAP_SOCKET)fd, F_SETFL, fcntl((SOAP_SOCKET)fd, F_GETFL)|O_NONBLOCK);#endif#endif    for (;;)    { if ((r = SSL_connect(soap->ssl)) <= 0)      { int err = SSL_get_error(soap->ssl, r);        if (err != SSL_ERROR_NONE && err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)        { soap_set_sender_error(soap, ssl_error(soap, r), "SSL connect failed in tcp_connect()", SOAP_SSL_ERROR);          return SOAP_INVALID_SOCKET;        }        if (soap->connect_timeout)        { struct timeval timeout;          fd_set fds;          if (soap->connect_timeout > 0)          { timeout.tv_sec = soap->connect_timeout;            timeout.tv_usec = 0;          }          else          { timeout.tv_sec = -soap->connect_timeout/1000000;            timeout.tv_usec = -soap->connect_timeout%1000000;          }          FD_ZERO(&fds);          FD_SET((SOAP_SOCKET)(soap->socket), &fds);          for (;;)          { int r = select((SOAP_SOCKET)(soap->socket + 1), &fds, NULL, &fds, &timeout);            if (r > 0)	      break;            if (!r)            { soap->errnum = 0;              DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connect timeout\n"));              soap_set_sender_error(soap, "Timeout", "TCP connect failed in tcp_connect()", SOAP_TCP_ERROR);              return SOAP_INVALID_SOCKET;            }          }	  continue;        }      }      break;    }#ifndef WITH_LEAN    if (soap->connect_timeout)#ifdef WIN32    { u_long blocking = 0;      ioctlsocket((SOAP_SOCKET)fd, FIONBIO, &blocking);    }#else      fcntl((SOAP_SOCKET)fd, F_SETFL, fcntl((SOAP_SOCKET)fd, F_GETFL)&~O_NONBLOCK);#endif#endif    if (soap->require_server_auth)    { X509 *peer;      int err;      if ((err = SSL_get_verify_result(soap->ssl)) != X509_V_OK)      { soap_set_sender_error(soap, X509_verify_cert_error_string(err), "SSL certificate presented by peer cannot be verified in tcp_connect()", SOAP_SSL_ERROR);        return SOAP_INVALID_SOCKET;      }      peer = SSL_get_peer_certificate(soap->ssl);      if (!peer)      { soap_set_sender_error(soap, "SSL error", "No SSL certificate was presented by the peer in tcp_connect()", SOAP_SSL_ERROR);        return SOAP_INVALID_SOCKET;      }      X509_NAME_get_text_by_NID(X509_get_subject_name(peer), NID_commonName, soap->msgbuf, sizeof(soap->msgbuf));      X509_free(peer);      if (soap_tag_cmp(soap->msgbuf, host))      { soap_set_sender_error(soap, "SSL error", "SSL certificate host name mismatch in tcp_connect()", SOAP_SSL_ERROR);        return SOAP_INVALID_SOCKET;      }    }  }#endif  return fd;}