comparison libaudacious/beepctrl.c @ 1436:c70b68bcf527 trunk

[svn] - add framework for later handling tcp:// connections
author nenolod
date Fri, 28 Jul 2006 00:13:02 -0700
parents bcd1ebd0a7c1
children dd2b97f79a20
comparison
equal deleted inserted replaced
1435:cc3b6df66e69 1436:c70b68bcf527
25 #include <glib.h> 25 #include <glib.h>
26 #include <sys/types.h> 26 #include <sys/types.h>
27 #include <sys/stat.h> 27 #include <sys/stat.h>
28 #include <sys/socket.h> 28 #include <sys/socket.h>
29 #include <sys/un.h> 29 #include <sys/un.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
30 #include <errno.h> 32 #include <errno.h>
31 #include <stdio.h> 33 #include <stdio.h>
32 #include <stdlib.h> 34 #include <stdlib.h>
33 #include <string.h> 35 #include <string.h>
34 #include "beepctrl.h" 36 #include "beepctrl.h"
35 #include "audacious/controlsocket.h" 37 #include "audacious/controlsocket.h"
36 38 #include "libaudacious/configdb.h"
39
40 /* overrides audacious_get_session_uri(). */
41 static gchar *session_uri = NULL;
37 42
38 #ifdef HAVE_UNISTD_H 43 #ifdef HAVE_UNISTD_H
39 #include <unistd.h> 44 #include <unistd.h>
40 #endif 45 #endif
41 46
290 remote_read_ack(fd); 295 remote_read_ack(fd);
291 close(fd); 296 close(fd);
292 return data; 297 return data;
293 } 298 }
294 299
300 gchar *
301 audacious_get_session_uri(gint session)
302 {
303 ConfigDb *db;
304 gchar *value = NULL;
305
306 db = bmp_cfg_db_open();
307
308 if (session_uri != NULL)
309 return session_uri;
310
311 bmp_cfg_db_get_string(db, NULL, "session_uri_base", &value);
312
313 if (value == NULL)
314 return g_strdup_printf("unix://localhost/%s/%s_%s.%d", g_get_tmp_dir(),
315 CTRLSOCKET_NAME, g_get_user_name(), session);
316
317 bmp_cfg_db_close(db);
318
319 return value;
320 }
321
322 gint
323 audacious_determine_session_type(gint session)
324 {
325 gchar *uri;
326
327 uri = audacious_get_session_uri(session);
328
329 if (!g_strncasecmp(uri, "tcp://", 6))
330 return AUDACIOUS_TYPE_TCP;
331 else
332 return AUDACIOUS_TYPE_UNIX;
333
334 return AUDACIOUS_TYPE_UNIX;
335 }
336
337 /* tcp://192.168.100.1:5900/zyzychynxi389xvmfewqaxznvnw */
338 void
339 audacious_decode_tcp_uri(gint session, gchar *in, gchar **host, gint *port, gchar **key)
340 {
341 gchar *workbuf = NULL, *keybuf = NULL;
342 gint iport;
343
344 /* split out the host/port and key */
345 sscanf(in, "tcp://%s/%s", workbuf, keybuf);
346
347 *key = keybuf;
348
349 if (strchr(workbuf, ':') == NULL)
350 {
351 *host = workbuf;
352 *port = 37370 + session;
353 }
354 else
355 {
356 gchar *hostbuf = NULL;
357 sscanf(workbuf, "%s:%d", hostbuf, &iport);
358
359 *port = iport + session;
360 }
361 }
362
363 /* unix://localhost/tmp/audacious_nenolod.0 */
364 void
365 audacious_decode_unix_uri(gint session, gchar *in, gchar **out)
366 {
367 gchar *workbuf = NULL, *pathbuf = NULL;
368
369 /* retrieve the pathbuf */
370 sscanf(in, "unix://%s/%s", workbuf, pathbuf);
371
372 *out = pathbuf;
373 }
374
295 gint 375 gint
296 xmms_connect_to_session(gint session) 376 xmms_connect_to_session(gint session)
297 { 377 {
298 gint fd; 378 gint fd;
299 uid_t stored_uid, euid; 379 gint type = audacious_determine_session_type(session);
300 struct sockaddr_un saddr; 380 gchar *uri = audacious_get_session_uri(session);
301 381
302 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) { 382 if (type == AUDACIOUS_TYPE_UNIX)
303 saddr.sun_family = AF_UNIX; 383 {
304 stored_uid = getuid(); 384 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
305 euid = geteuid(); 385 {
306 setuid(euid); 386 uid_t stored_uid, euid;
307 g_snprintf(saddr.sun_path, 108, "%s/%s_%s.%d", g_get_tmp_dir(), 387 struct sockaddr_un saddr;
308 CTRLSOCKET_NAME, g_get_user_name(), session); 388 gchar *path;
309 setreuid(stored_uid, euid); 389
310 if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != -1) 390 saddr.sun_family = AF_UNIX;
311 return fd; 391 stored_uid = getuid();
312 } 392 euid = geteuid();
393 setuid(euid);
394
395 audacious_decode_unix_uri(session, uri, &path);
396
397 /*
398 g_snprintf(saddr.sun_path, 108, "%s/%s_%s.%d", g_get_tmp_dir(),
399 CTRLSOCKET_NAME, g_get_user_name(), session);
400 */
401 g_strlcpy(saddr.sun_path, path, 108);
402 setreuid(stored_uid, euid);
403 if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != -1)
404 return fd;
405 }
406 }
407 else
408 {
409 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1)
410 {
411 struct hostent *hp;
412 struct sockaddr_in saddr;
413 gchar *host, *key;
414 gint port;
415
416 audacious_decode_tcp_uri(session, uri, &host, &port, &key);
417
418 /* resolve it */
419 if ((hp = gethostbyname(host)) == NULL)
420 {
421 close(fd);
422 return -1;
423 }
424
425 memset(&saddr, '\0', sizeof(saddr));
426 saddr.sin_family = AF_INET;
427 saddr.sin_port = htons(port);
428 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
429
430 if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != -1)
431 return fd;
432 }
433 }
434
313 close(fd); 435 close(fd);
314 return -1; 436 return -1;
315 } 437 }
316 438
317 void 439 void