Logo Search packages:      
Sourcecode: nbsmtp version File versions  Download package

int get_socket ( servinfo_t serverinfo  ) 

Opens a connection to the server.

Parameters:
[in,out] serverinfo A pointer to a servinfo_t struct with the host information
Returns:
0 if an error ocurs and the socket description if everything is allright

Definition at line 575 of file original.c.

References servinfo::host, log_msg(), servinfo::port, and servinfo::sockfd.

Referenced by send_mail().

{
#ifdef HAVE_INET6
      struct addrinfo hints,*res,*res0;
      char *servname;
      int error;
#else
      struct hostent *servp;
      struct sockaddr_in addr;
#endif

#ifdef HAVE_INET6
      memset(&hints,0,sizeof(hints));

      hints.ai_family = PF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      asprintf(&servname,"%d",serverinfo->port);

      error = getaddrinfo(serverinfo->host,servname,&hints,&res0);

      if(error)
      {
            log_msg(LOG_DEBUG,"Error after getaddrinfo, maybe DNS problem");
      }

      serverinfo->sockfd = -1;

      for ( res = res0 ; res ; res = res->ai_next )
      {
            serverinfo->sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);

            if (serverinfo->sockfd<0)
            {
                  continue;
            }

            if (connect(serverinfo->sockfd,res->ai_addr,res->ai_addrlen)<0)
            {
                  close(serverinfo->sockfd);
                  serverinfo->sockfd = -1;
                  continue;
            }

            break;
      }

      if (serverinfo->sockfd<0)
      {
            log_msg(LOG_ERR,"Couldn't connect to host");
            return 0;
      }

      freeaddrinfo(res0);
      free(servname);
#else
      servp = gethostbyname(serverinfo->host);

      if(servp == NULL)
      {
            return 0;
      }

      if ((serverinfo->sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
            return 0;
      }

      addr.sin_family = AF_INET;
      addr.sin_port = htons(serverinfo->port);
      addr.sin_addr = *((struct in_addr *)servp->h_addr);
      memset(&(addr.sin_zero),0,sizeof(addr.sin_zero));

      if (connect(serverinfo->sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1)
      {
            log_msg(LOG_ERR,"Couldn't connect to host");
            return 0;
      }
#endif

#ifdef HAVE_SSL
      if (serverinfo->use_tls==True && serverinfo->use_starttls==False)
      {
            if (nbsmtp_sslinit(serverinfo) < 0)
            {
                  return 0;
            }
      }

      log_msg(LOG_INFO,"Creating%sconnection to host (%s:%d)",
                  (serverinfo->use_tls==True?" SSL ":" "),
                  serverinfo->host,serverinfo->port);
#else
      log_msg(LOG_INFO,"Creating connection to host (%s:%d)",serverinfo->host,serverinfo->port);
#endif

      return serverinfo->sockfd;
}


Generated by  Doxygen 1.6.0   Back to index