[gtk+] broadway: Allow binding http server to an ip



commit 2e76e5a543efa165dc2ae94cc1f5fcfe829e983c
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Jan 10 21:38:15 2013 +0100

    broadway: Allow binding http server to an ip

 gdk/broadway/broadway-server.c |   49 ++++++++++++++++++++++++++++++++++-----
 gdk/broadway/broadway-server.h |    3 +-
 gdk/broadway/broadwayd.c       |    4 ++-
 3 files changed, 47 insertions(+), 9 deletions(-)
---
diff --git a/gdk/broadway/broadway-server.c b/gdk/broadway/broadway-server.c
index 3b2c69d..3ee706f 100644
--- a/gdk/broadway/broadway-server.c
+++ b/gdk/broadway/broadway-server.c
@@ -19,6 +19,7 @@ typedef struct BroadwayWindow BroadwayWindow;
 struct _BroadwayServer {
   GObject parent_instance;
 
+  char *address;
   int port;
   GSocketService *service;
   BroadwayOutput *output;
@@ -123,6 +124,10 @@ broadway_server_init (BroadwayServer *server)
 static void
 broadway_server_finalize (GObject *object)
 {
+  BroadwayServer *server = BROADWAY_SERVER (object);
+
+  g_free (server->address);
+
   G_OBJECT_CLASS (broadway_server_parent_class)->finalize (object);
 }
 
@@ -1225,20 +1230,50 @@ handle_incoming_connection (GSocketService    *service,
 }
 
 BroadwayServer *
-broadway_server_new (int port, GError **error)
+broadway_server_new (char *address, int port, GError **error)
 {
   BroadwayServer *server;
+  GInetAddress *inet_address;
+  GSocketAddress *socket_address;
 
   server = g_object_new (BROADWAY_TYPE_SERVER, NULL);
   server->port = port;
+  server->address = g_strdup (address);
 
-  if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (server->service),
-					server->port,
-					G_OBJECT (server),
-					error))
+  if (address == NULL)
     {
-      g_prefix_error (error, "Unable to listen to port %d: ", server->port);
-      return NULL;
+      if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (server->service),
+					    server->port,
+					    G_OBJECT (server),
+					    error))
+	{
+	  g_prefix_error (error, "Unable to listen to port %d: ", server->port);
+	  return NULL;
+	}
+    }
+  else
+    {
+      inet_address = g_inet_address_new_from_string (address);
+      if (inet_address == NULL)
+	{
+	  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Invalid ip address %s: ", address);
+	  return NULL;
+	}
+      socket_address = g_inet_socket_address_new (inet_address, port);
+      g_object_unref (inet_address);
+      if (!g_socket_listener_add_address (G_SOCKET_LISTENER (server->service),
+					  socket_address,
+					  G_SOCKET_TYPE_STREAM,
+					  G_SOCKET_PROTOCOL_TCP,
+					  G_OBJECT (server),
+					  NULL,
+					  error))
+	{
+	  g_prefix_error (error, "Unable to listen to %s:%d: ", server->address, server->port);
+	  g_object_unref (socket_address);
+	  return NULL;
+	}
+      g_object_unref (socket_address);
     }
 
   g_signal_connect (server->service, "incoming",
diff --git a/gdk/broadway/broadway-server.h b/gdk/broadway/broadway-server.h
index a548f19..4ef9338 100644
--- a/gdk/broadway/broadway-server.h
+++ b/gdk/broadway/broadway-server.h
@@ -19,7 +19,8 @@ typedef struct _BroadwayServerClass BroadwayServerClass;
 #define BROADWAY_SERVER_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), BROADWAY_TYPE_SERVER, BroadwayServerClass))
 
 
-BroadwayServer     *broadway_server_new                      (int               port,
+BroadwayServer     *broadway_server_new                      (char             *address,
+							      int               port,
 							      GError          **error);
 gboolean            broadway_server_has_client               (BroadwayServer   *server);
 void                broadway_server_flush                    (BroadwayServer   *server);
diff --git a/gdk/broadway/broadwayd.c b/gdk/broadway/broadwayd.c
index 793393e..9a273c7 100644
--- a/gdk/broadway/broadwayd.c
+++ b/gdk/broadway/broadwayd.c
@@ -495,10 +495,12 @@ main (int argc, char *argv[])
   GSocketAddress *address;
   GSocketService *listener;
   char *path, *base;
+  char *http_address = NULL;
   int http_port = 0;
   int display = 1;
   const GOptionEntry entries[] = {
     { "port", 'p', 0, G_OPTION_ARG_INT, &http_port, "Httpd port", "PORT" },
+    { "address", 'a', 0, G_OPTION_ARG_STRING, &http_address, "Ip address to bind to ", "ADDRESS" },
     { NULL }
   };
 
@@ -528,7 +530,7 @@ main (int argc, char *argv[])
   if (http_port == 0)
     http_port = 8080 + (display - 1);
 
-  server = broadway_server_new (http_port, &error);
+  server = broadway_server_new (http_address, http_port, &error);
   if (server == NULL)
     {
       g_printerr ("%s\n", error->message);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]