]> code.delx.au - pulseaudio/blob - src/pulsecore/parseaddr.c
merge 'lennart' branch back into trunk.
[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 <stdlib.h>
30
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.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 pa_assert(s);
49 pa_assert(ret_port);
50
51 if (*s == '[') {
52 char *e;
53 if (!(e = strchr(s+1, ']')))
54 return NULL;
55
56 if (e[1] == ':')
57 *ret_port = atoi(e+2);
58 else if (e[1] != 0)
59 return NULL;
60
61 return pa_xstrndup(s+1, e-s-1);
62 } else {
63 char *e;
64
65 if (!(e = strrchr(s, ':')))
66 return pa_xstrdup(s);
67
68 *ret_port = atoi(e+1);
69 return pa_xstrndup(s, e-s);
70 }
71 }
72
73 int pa_parse_address(const char *name, pa_parsed_address *ret_p) {
74 const char *p;
75
76 pa_assert(name);
77 pa_assert(ret_p);
78
79 memset(ret_p, 0, sizeof(pa_parsed_address));
80 ret_p->type = PA_PARSED_ADDRESS_TCP_AUTO;
81
82 if (*name == '{') {
83 char hn[256], *pfx;
84 /* The URL starts with a host specification for detecting local connections */
85
86 if (!pa_get_host_name(hn, sizeof(hn)))
87 return -1;
88
89 pfx = pa_sprintf_malloc("{%s}", hn);
90 if (!pa_startswith(name, pfx)) {
91 pa_xfree(pfx);
92 /* Not local */
93 return -1;
94 }
95
96 p = name + strlen(pfx);
97 pa_xfree(pfx);
98 } else
99 p = name;
100
101 if (*p == '/')
102 ret_p->type = PA_PARSED_ADDRESS_UNIX;
103 else if (pa_startswith(p, "unix:")) {
104 ret_p->type = PA_PARSED_ADDRESS_UNIX;
105 p += sizeof("unix:")-1;
106 } else if (pa_startswith(p, "tcp:") || pa_startswith(p, "tcp4:")) {
107 ret_p->type = PA_PARSED_ADDRESS_TCP4;
108 p += sizeof("tcp:")-1;
109 } else if (pa_startswith(p, "tcp6:")) {
110 ret_p->type = PA_PARSED_ADDRESS_TCP6;
111 p += sizeof("tcp6:")-1;
112 }
113
114 if (ret_p->type == PA_PARSED_ADDRESS_UNIX)
115 ret_p->path_or_host = pa_xstrdup(p);
116 else
117 if (!(ret_p->path_or_host = parse_host(p, &ret_p->port)))
118 return -1;
119
120 return 0;
121 }