changeset 1436:c70b68bcf527 trunk

[svn] - add framework for later handling tcp:// connections
author nenolod
date Fri, 28 Jul 2006 00:13:02 -0700
parents cc3b6df66e69
children dd2b97f79a20
files ChangeLog libaudacious/beepctrl.c libaudacious/beepctrl.h
diffstat 3 files changed, 149 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Jul 27 23:15:43 2006 -0700
+++ b/ChangeLog	Fri Jul 28 00:13:02 2006 -0700
@@ -1,3 +1,12 @@
+2006-07-28 06:15:43 +0000  William Pitcock <nenolod@nenolod.net>
+  revision [1784]
+  - add incomplete media library UI -- still lots to do
+  
+
+  Changes:        Modified:
+  +182 -0         trunk/audacious/library/library.glade  
+
+
 2006-07-28 05:41:31 +0000  William Pitcock <nenolod@nenolod.net>
   revision [1782]
   - add sqlite/ directory
--- a/libaudacious/beepctrl.c	Thu Jul 27 23:15:43 2006 -0700
+++ b/libaudacious/beepctrl.c	Fri Jul 28 00:13:02 2006 -0700
@@ -27,13 +27,18 @@
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <arpa/inet.h>
+#include <netdb.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "beepctrl.h"
 #include "audacious/controlsocket.h"
+#include "libaudacious/configdb.h"
 
+/* overrides audacious_get_session_uri(). */
+static gchar *session_uri = NULL;
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -292,24 +297,141 @@
     return data;
 }
 
+gchar *
+audacious_get_session_uri(gint session)
+{
+    ConfigDb *db;
+    gchar *value = NULL;
+
+    db = bmp_cfg_db_open();
+
+    if (session_uri != NULL)
+	return session_uri;
+
+    bmp_cfg_db_get_string(db, NULL, "session_uri_base", &value);
+
+    if (value == NULL)
+        return g_strdup_printf("unix://localhost/%s/%s_%s.%d", g_get_tmp_dir(),
+		CTRLSOCKET_NAME, g_get_user_name(), session);
+
+    bmp_cfg_db_close(db);
+
+    return value;
+}
+
+gint
+audacious_determine_session_type(gint session)
+{
+    gchar *uri;
+
+    uri = audacious_get_session_uri(session);
+
+    if (!g_strncasecmp(uri, "tcp://", 6))
+        return AUDACIOUS_TYPE_TCP;
+    else
+        return AUDACIOUS_TYPE_UNIX;
+
+    return AUDACIOUS_TYPE_UNIX;
+}
+
+/* tcp://192.168.100.1:5900/zyzychynxi389xvmfewqaxznvnw */
+void
+audacious_decode_tcp_uri(gint session, gchar *in, gchar **host, gint *port, gchar **key)
+{
+    gchar *workbuf = NULL, *keybuf = NULL;
+    gint iport;
+
+    /* split out the host/port and key */
+    sscanf(in, "tcp://%s/%s", workbuf, keybuf);
+
+    *key = keybuf;
+
+    if (strchr(workbuf, ':') == NULL)
+    {
+        *host = workbuf;
+        *port = 37370 + session;
+    }
+    else
+    {
+        gchar *hostbuf = NULL;
+        sscanf(workbuf, "%s:%d", hostbuf, &iport);
+
+        *port = iport + session;
+    }
+}
+
+/* unix://localhost/tmp/audacious_nenolod.0 */
+void
+audacious_decode_unix_uri(gint session, gchar *in, gchar **out)
+{
+    gchar *workbuf = NULL, *pathbuf = NULL;
+
+    /* retrieve the pathbuf */
+    sscanf(in, "unix://%s/%s", workbuf, pathbuf);
+
+    *out = pathbuf;
+}
+
 gint
 xmms_connect_to_session(gint session)
 {
     gint fd;
-    uid_t stored_uid, euid;
-    struct sockaddr_un saddr;
+    gint type = audacious_determine_session_type(session);
+    gchar *uri = audacious_get_session_uri(session);
+
+    if (type == AUDACIOUS_TYPE_UNIX)
+    {
+        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
+        {
+            uid_t stored_uid, euid;
+            struct sockaddr_un saddr;
+	    gchar *path;
+
+            saddr.sun_family = AF_UNIX;
+            stored_uid = getuid();
+            euid = geteuid();
+            setuid(euid);
+	    
+	    audacious_decode_unix_uri(session, uri, &path);
 
-    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) {
-        saddr.sun_family = AF_UNIX;
-        stored_uid = getuid();
-        euid = geteuid();
-        setuid(euid);
-        g_snprintf(saddr.sun_path, 108, "%s/%s_%s.%d", g_get_tmp_dir(),
-                   CTRLSOCKET_NAME, g_get_user_name(), session);
-        setreuid(stored_uid, euid);
-        if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != -1)
-            return fd;
+/*
+            g_snprintf(saddr.sun_path, 108, "%s/%s_%s.%d", g_get_tmp_dir(),
+                       CTRLSOCKET_NAME, g_get_user_name(), session);
+ */
+	    g_strlcpy(saddr.sun_path, path, 108);
+            setreuid(stored_uid, euid);
+            if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != -1)
+                return fd;
+        }
     }
+    else
+    {
+        if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1)
+        {
+	    struct hostent *hp;
+	    struct sockaddr_in saddr;
+	    gchar *host, *key;
+	    gint port;
+
+	    audacious_decode_tcp_uri(session, uri, &host, &port, &key);
+
+            /* resolve it */
+            if ((hp = gethostbyname(host)) == NULL)
+            {
+                 close(fd);
+                 return -1;
+            }
+
+	    memset(&saddr, '\0', sizeof(saddr));
+            saddr.sin_family = AF_INET;
+            saddr.sin_port = htons(port);
+            memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
+
+            if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != -1)
+                return fd;
+        }
+    }
+
     close(fd);
     return -1;
 }
--- a/libaudacious/beepctrl.h	Thu Jul 27 23:15:43 2006 -0700
+++ b/libaudacious/beepctrl.h	Fri Jul 28 00:13:02 2006 -0700
@@ -26,6 +26,12 @@
 extern "C" {
 #endif
 
+    enum
+    {
+        AUDACIOUS_TYPE_UNIX,
+	AUDACIOUS_TYPE_TCP,
+    };
+
     /* Do NOT use this! This is only for control socket initialization now. */
     gint xmms_connect_to_session(gint session);