Exemple #1
0
int
vsf_privop_get_ftp_port_sock(struct vsf_session* p_sess,
                             unsigned short remote_port,
                             int use_port_sockaddr)
{
  static struct vsf_sysutil_sockaddr* p_sockaddr;
  const struct vsf_sysutil_sockaddr* p_connect_to;
  int retval;
  int i;
  int s = vsf_sysutil_get_ipsock(p_sess->p_local_addr);
  int port = 0;
  if (vsf_sysutil_is_port_reserved(remote_port))
  {
    die("Illegal port request");
  }
  if (tunable_connect_from_port_20)
  {
    port = tunable_ftp_data_port;
  }
  vsf_sysutil_activate_reuseaddr(s);
  /* A report of failure here on Solaris, presumably buggy address reuse
   * support? We'll retry.
   */
  for (i = 0; i < 2; ++i)
  {
    double sleep_for;
    vsf_sysutil_sockaddr_clone(&p_sockaddr, p_sess->p_local_addr);
    vsf_sysutil_sockaddr_set_port(p_sockaddr, port);
    retval = vsf_sysutil_bind(s, p_sockaddr);
    if (retval == 0)
    {
      break;
    }
    if (vsf_sysutil_get_error() != kVSFSysUtilErrADDRINUSE || i == 1)
    {
      die("vsf_sysutil_bind");
    }
    sleep_for = vsf_sysutil_get_random_byte();
    sleep_for /= 256.0;
    sleep_for += 1.0;
    vsf_sysutil_sleep(sleep_for);
  }
  if (use_port_sockaddr)
  {
    p_connect_to = p_sess->p_port_sockaddr;
  }
  else
  {
    vsf_sysutil_sockaddr_set_port(p_sess->p_remote_addr, remote_port);
    p_connect_to = p_sess->p_remote_addr;
  }
  retval = vsf_sysutil_connect_timeout(s, p_connect_to,
                                       tunable_connect_timeout);
  if (vsf_sysutil_retval_is_error(retval))
  {
    vsf_sysutil_close(s);
    s = -1;
  }
  return s;
}
Exemple #2
0
int
vsf_ftpdataio_get_port_fd(struct vsf_session* p_sess)
{
    int retval;
    int remote_fd;
    if (tunable_connect_from_port_20)
    {
        if (tunable_one_process_model)
        {
            remote_fd = vsf_one_process_get_priv_data_sock(p_sess);
        }
        else
        {
            remote_fd = vsf_two_process_get_priv_data_sock(p_sess);
        }
    }
    else
    {
        remote_fd = vsf_sysutil_get_ipsock(p_sess->p_port_sockaddr);
        if (vsf_sysutil_sockaddr_same_family(p_sess->p_port_sockaddr,
                                             p_sess->p_local_addr))
        {
            static struct vsf_sysutil_sockaddr* s_p_addr;
            vsf_sysutil_sockaddr_clone(&s_p_addr, p_sess->p_local_addr);
            retval = vsf_sysutil_bind(remote_fd, s_p_addr);
            if (retval != 0)
            {
                die("vsf_sysutil_bind");
            }
        }
    }
    retval = vsf_sysutil_connect_timeout(remote_fd, p_sess->p_port_sockaddr,
                                         tunable_connect_timeout);
    if (vsf_sysutil_retval_is_error(retval))
    {
        vsf_cmdio_write(p_sess, FTP_BADSENDCONN,
                        "Failed to establish connection.");
        vsf_sysutil_close(remote_fd);
        return -1;
    }
    init_data_sock_params(p_sess, remote_fd);
    return remote_fd;
}