2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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
29 #include <pulse/xmalloc.h>
30 #include <pulse/util.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/macro.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
) {
51 if (!(e
= strchr(s
+1, ']')))
57 if (pa_atou(e
+2, &p
) < 0)
60 *ret_port
= (uint16_t) p
;
64 return pa_xstrndup(s
+1, (size_t) (e
-s
-1));
69 if (!(e
= strrchr(s
, ':')))
72 if (pa_atou(e
+1, &p
) < 0)
75 *ret_port
= (uint16_t) p
;
76 return pa_xstrndup(s
, (size_t) (e
-s
));
80 int pa_parse_address(const char *name
, pa_parsed_address
*ret_p
) {
86 memset(ret_p
, 0, sizeof(pa_parsed_address
));
87 ret_p
->type
= PA_PARSED_ADDRESS_TCP_AUTO
;
91 /* The URL starts with a host specification for detecting local connections */
93 if (!pa_get_host_name(hn
, sizeof(hn
)))
96 pfx
= pa_sprintf_malloc("{%s}", hn
);
97 if (!pa_startswith(name
, pfx
)) {
103 p
= name
+ strlen(pfx
);
109 ret_p
->type
= PA_PARSED_ADDRESS_UNIX
;
110 else if (pa_startswith(p
, "unix:")) {
111 ret_p
->type
= PA_PARSED_ADDRESS_UNIX
;
112 p
+= sizeof("unix:")-1;
113 } else if (pa_startswith(p
, "tcp:")) {
114 ret_p
->type
= PA_PARSED_ADDRESS_TCP4
;
115 p
+= sizeof("tcp:")-1;
116 } else if (pa_startswith(p
, "tcp4:")) {
117 ret_p
->type
= PA_PARSED_ADDRESS_TCP4
;
118 p
+= sizeof("tcp4:")-1;
119 } else if (pa_startswith(p
, "tcp6:")) {
120 ret_p
->type
= PA_PARSED_ADDRESS_TCP6
;
121 p
+= sizeof("tcp6:")-1;
124 if (ret_p
->type
== PA_PARSED_ADDRESS_UNIX
)
125 ret_p
->path_or_host
= pa_xstrdup(p
);
127 if (!(ret_p
->path_or_host
= parse_host(p
, &ret_p
->port
)))