Beispiel #1
0
int tcp_connect_sockaddr(

  struct sockaddr *sa,      /* I */
  size_t           sa_size) /* I */

  {
  int rc = PBSE_NONE;
  int stream = TRANSIENT_SOCKET_FAIL;
  char *err_msg = NULL;
  char local_err_buf[LOCAL_LOG_BUF];
  char *tmp_ip = NULL;
  int  retryCount = 5;

  errno = 0;

  while (retryCount-- >= 0)
    {
    if ((stream = socket_get_tcp_priv()) < 0)
      {
      /* FAILED */
      log_err(errno,__func__,"Failed when trying to get privileged port - socket_get_tcp_priv() failed");
      }
    else if ((rc = socket_connect_addr(&stream, sa, sa_size, 1, &err_msg)) != PBSE_NONE)
      {
      /* FAILED */
      if (errno != EINTR) //Interrupted system call is a retryable error so try it again.
        {
        retryCount = -1;
        }
      else
        {
        usleep(10000); //Catch a breath on a retryable error.
        }
      tmp_ip = inet_ntoa(((struct sockaddr_in *)sa)->sin_addr);
      snprintf(local_err_buf, LOCAL_LOG_BUF, "Failed when trying to open tcp connection - connect() failed [rc = %d] [addr = %s:%d]", rc, tmp_ip, htons(((struct sockaddr_in *)sa)->sin_port));
      log_err(-1,__func__,local_err_buf);
      if (err_msg != NULL)
        {
        log_err(-1,__func__,err_msg);
        free(err_msg);
        err_msg = NULL;
        }
      }
    else
      {
      /* SUCCESS */
      return(stream);
      }

    /* FAILURE */
    if (IS_VALID_STREAM(stream))
      {
      close(stream);
      stream = TRANSIENT_SOCKET_FAIL;
      }
    }

  return(stream);
  } /* END tcp_connect_sockaddr() */
int tcp_connect_sockaddr(

  struct sockaddr *sa,      /* I */
  size_t           sa_size) /* I */

  {
  char *id = "tcp_connect_sockaddr";
  int rc = PBSE_NONE;
  int stream = -1;
  char *err_msg = NULL;
  char local_err_buf[LOCAL_LOG_BUF];
  char *tmp_ip = NULL;
  /*
  int   stream = socket(AF_INET,SOCK_STREAM,0);
  
  if (stream < 0)
    {
    log_err(errno,id,"Failed when trying to open tcp connection - socket() failed");
    }
  else if (setsockopt(stream,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i)) < 0)
    {
    log_err(errno,id,"Failed when trying to open tcp connection - setsockopt() failed");
    }
  else if (bindresvport(stream,NULL) < 0)
    {
    log_err(errno,id,"Failed when trying to open tcp connection - bindresvport() failed");
    }
    */
  if ((stream = socket_get_tcp_priv()) < 0)
    {
    /* FAILED */
    log_err(errno,id,"Failed when trying to get privileged port - socket_get_tcp_priv() failed");
    }
  else if ((rc = socket_connect_addr(&stream, sa, sa_size, 1, &err_msg)) != PBSE_NONE)
    {
    /* FAILED */
    tmp_ip = inet_ntoa(((struct sockaddr_in *)sa)->sin_addr);
    snprintf(local_err_buf, LOCAL_LOG_BUF, "Failed when trying to open tcp connection - connect() failed [rc = %d] [addr = %s:%d]", rc, tmp_ip, htons(((struct sockaddr_in *)sa)->sin_port));
    log_err(errno,id,local_err_buf);
    if (err_msg != NULL)
      {
      log_err(errno,id,err_msg);
      free(err_msg);
      }
    }
  else
    {
    /* SUCCESS */
    return(stream);
    }

  /* FAILURE */
  if (IS_VALID_STREAM(stream))
    close(stream);

  return(-1);
  } /* END tcp_connect_sockaddr() */
int socket_connect(

    int   *local_socket,
    char  *dest_addr,
    int    dest_addr_len,
    int    dest_port,
    int    family,
    int    is_privileged,
    char **error_msg)

{
    struct sockaddr_in remote;
    size_t r_size = sizeof(struct sockaddr_in);

    memset(&remote, 0, r_size);
    remote.sin_family = family;
    memcpy(&remote.sin_addr, dest_addr, dest_addr_len);
    remote.sin_port = htons((unsigned short)dest_port);
    return socket_connect_addr(local_socket, (struct sockaddr *)&remote, r_size, is_privileged, error_msg);
}