]> code.delx.au - pulseaudio/blob - src/pulsecore/parseaddr.c
a49a09ed7d93df67ecefb1e3fec03ca5dff49c51
[pulseaudio] / src / pulsecore / parseaddr.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2.1 of the
11 License, or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <assert.h>
30 #include <stdlib.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulse/util.h>
35 #include <pulsecore/core-util.h>
36
37 #include "parseaddr.h"
38
39 /* Parse addresses in one of the following forms:
40 * HOSTNAME
41 * HOSTNAME:PORT
42 * [HOSTNAME]
43 * [HOSTNAME]:PORT
44 *
45 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
46
47 static char *parse_host(const char *s, uint16_t *ret_port) {
48 assert(s && ret_port);
49 if (*s == '[') {
50 char *e;
51 if (!(e = strchr(s+1, ']')))
52 return NULL;
53
54 if (e[1] == ':')
55 *ret_port = atoi(e+2);
56 else if (e[1] != 0)
57 return NULL;
58
59 return pa_xstrndup(s+1, e-s-1);
60 } else {
61 char *e;
62
63 if (!(e = strrchr(s, ':')))
64 return pa_xstrdup(s);
65
66 *ret_port = atoi(e+1);
67 return pa_xstrndup(s, e-s);
68 }
69 }
70
71 int pa_parse_address(const char *name, pa_parsed_address *ret_p) {
72 const char *p;
73 assert(name && ret_p);
74 memset(ret_p, 0, sizeof(pa_parsed_address));
75 ret_p->type = PA_PARSED_ADDRESS_TCP_AUTO;
76
77 if (*name == '{') {
78 char hn[256], *pfx;
79 /* The URL starts with a host specification for detecting local connections */
80
81 if (!pa_get_host_name(hn, sizeof(hn)))
82 return -1;
83
84 pfx = pa_sprintf_malloc("{%s}", hn);
85 if (!pa_startswith(name, pfx)) {
86 pa_xfree(pfx);
87 /* Not local */
88 return -1;
89 }
90
91 p = name + strlen(pfx);
92 pa_xfree(pfx);
93 } else
94 p = name;
95
96 if (*p == '/')
97 ret_p->type = PA_PARSED_ADDRESS_UNIX;
98 else if (pa_startswith(p, "unix:")) {
99 ret_p->type = PA_PARSED_ADDRESS_UNIX;
100 p += sizeof("unix:")-1;
101 } else if (pa_startswith(p, "tcp:") || pa_startswith(p, "tcp4:")) {
102 ret_p->type = PA_PARSED_ADDRESS_TCP4;
103 p += sizeof("tcp:")-1;
104 } else if (pa_startswith(p, "tcp6:")) {
105 ret_p->type = PA_PARSED_ADDRESS_TCP6;
106 p += sizeof("tcp6:")-1;
107 }
108
109 if (ret_p->type == PA_PARSED_ADDRESS_UNIX)
110 ret_p->path_or_host = pa_xstrdup(p);
111 else
112 if (!(ret_p->path_or_host = parse_host(p, &ret_p->port)))
113 return -1;
114
115
116 return 0;
117 }