Logo Search packages:      
Sourcecode: dbus version File versions

int _dbus_connect_tcp_socket ( const char *  host,
dbus_uint32_t  port,
DBusError error 
)

Creates a socket and connects to a socket at the given host and port. The connection fd is returned, and is set up as nonblocking.

Parameters:
host the host name to connect to
port the prot to connect to
error return location for error code
Returns:
connection file descriptor or -1 on error

Definition at line 592 of file dbus-sysdeps.c.

References _dbus_error_from_errno(), _dbus_set_fd_nonblocking(), _dbus_strerror(), dbus_set_error(), and NULL.

Referenced by _dbus_transport_new_for_tcp_socket().

{
  int fd;
  struct sockaddr_in addr;
  struct hostent *he;
  struct in_addr *haddr;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
  fd = socket (AF_INET, SOCK_STREAM, 0);
  
  if (fd < 0)
    {
      dbus_set_error (error,
                      _dbus_error_from_errno (errno),
                      "Failed to create socket: %s",
                      _dbus_strerror (errno)); 
      
      return -1;
    }

  if (host == NULL)
    host = "localhost";

  he = gethostbyname (host);
  if (he == NULL) 
    {
      dbus_set_error (error,
                      _dbus_error_from_errno (errno),
                      "Failed to lookup hostname: %s",
                      host);
      close (fd);
      return -1;
    }
  
  haddr = ((struct in_addr *) (he->h_addr_list)[0]);

  _DBUS_ZERO (addr);
  memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons (port);
  
  if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
    {      
      dbus_set_error (error,
                       _dbus_error_from_errno (errno),
                      "Failed to connect to socket %s: %s:%d",
                      host, _dbus_strerror (errno), port);

      close (fd);
      fd = -1;
      
      return -1;
    }

  if (!_dbus_set_fd_nonblocking (fd, error))
    {
      close (fd);
      fd = -1;

      return -1;
    }

  return fd;
}


Generated by  Doxygen 1.6.0   Back to index