4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33 #include <pulsecore/core-util.h>
35 #include "parseaddr.h"
37 /* Parse addresses in one of the following forms:
43 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
45 static char *parse_host(const char *s
, uint16_t *ret_port
) {
46 assert(s
&& ret_port
);
49 if (!(e
= strchr(s
+1, ']')))
53 *ret_port
= atoi(e
+2);
57 return pa_xstrndup(s
+1, e
-s
-1);
61 if (!(e
= strrchr(s
, ':')))
64 *ret_port
= atoi(e
+1);
65 return pa_xstrndup(s
, e
-s
);
69 int pa_parse_address(const char *name
, pa_parsed_address
*ret_p
) {
71 assert(name
&& ret_p
);
72 memset(ret_p
, 0, sizeof(pa_parsed_address
));
73 ret_p
->type
= PA_PARSED_ADDRESS_TCP_AUTO
;
77 /* The URL starts with a host specification for detecting local connections */
79 if (!pa_get_host_name(hn
, sizeof(hn
)))
82 pfx
= pa_sprintf_malloc("{%s}", hn
);
83 if (!pa_startswith(name
, pfx
)) {
89 p
= name
+ strlen(pfx
);
95 ret_p
->type
= PA_PARSED_ADDRESS_UNIX
;
96 else if (pa_startswith(p
, "unix:")) {
97 ret_p
->type
= PA_PARSED_ADDRESS_UNIX
;
98 p
+= sizeof("unix:")-1;
99 } else if (pa_startswith(p
, "tcp:") || pa_startswith(p
, "tcp4:")) {
100 ret_p
->type
= PA_PARSED_ADDRESS_TCP4
;
101 p
+= sizeof("tcp:")-1;
102 } else if (pa_startswith(p
, "tcp6:")) {
103 ret_p
->type
= PA_PARSED_ADDRESS_TCP6
;
104 p
+= sizeof("tcp6:")-1;
107 if (ret_p
->type
== PA_PARSED_ADDRESS_UNIX
)
108 ret_p
->path_or_host
= pa_xstrdup(p
);
110 if (!(ret_p
->path_or_host
= parse_host(p
, &ret_p
->port
)))