4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
37 #include "parseaddr.h"
39 /* Parse addresses in one of the following forms:
45 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
47 static char *parse_host(const char *s
, uint16_t *ret_port
) {
53 if (!(e
= strchr(s
+1, ']')))
57 *ret_port
= atoi(e
+2);
61 return pa_xstrndup(s
+1, e
-s
-1);
65 if (!(e
= strrchr(s
, ':')))
68 *ret_port
= atoi(e
+1);
69 return pa_xstrndup(s
, e
-s
);
73 int pa_parse_address(const char *name
, pa_parsed_address
*ret_p
) {
79 memset(ret_p
, 0, sizeof(pa_parsed_address
));
80 ret_p
->type
= PA_PARSED_ADDRESS_TCP_AUTO
;
84 /* The URL starts with a host specification for detecting local connections */
86 if (!pa_get_host_name(hn
, sizeof(hn
)))
89 pfx
= pa_sprintf_malloc("{%s}", hn
);
90 if (!pa_startswith(name
, pfx
)) {
96 p
= name
+ strlen(pfx
);
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;
114 if (ret_p
->type
== PA_PARSED_ADDRESS_UNIX
)
115 ret_p
->path_or_host
= pa_xstrdup(p
);
117 if (!(ret_p
->path_or_host
= parse_host(p
, &ret_p
->port
)))