Logo Search packages:      
Sourcecode: dbus version File versions

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

Creates a socket and binds it to the given path, then listens on the socket. The socket is set to be nonblocking.

Parameters:
host the host name to listen on
port the prot to listen on
error return location for errors
Returns:
the listening file descriptor or -1 on error

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

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

Referenced by _dbus_server_new_for_tcp_socket().

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

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

  he = gethostbyname (host);
  if (he == NULL) 
    {
      dbus_set_error (error,
                      _dbus_error_from_errno (errno),
                      "Failed to lookup hostname: %s",
                      host);
      close (listen_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 (bind (listen_fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
    {
      dbus_set_error (error, _dbus_error_from_errno (errno),
                      "Failed to bind socket \"%s:%d\": %s",
                      host, port, _dbus_strerror (errno));
      close (listen_fd);
      return -1;
    }

  if (listen (listen_fd, 30 /* backlog */) < 0)
    {
      dbus_set_error (error, _dbus_error_from_errno (errno),  
                      "Failed to listen on socket \"%s:%d\": %s",
                      host, port, _dbus_strerror (errno));
      close (listen_fd);
      return -1;
    }

  if (!_dbus_set_fd_nonblocking (listen_fd, error))
    {
      close (listen_fd);
      return -1;
    }
  
  return listen_fd;
}


Generated by  Doxygen 1.6.0   Back to index