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, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/arpa-inet.h>
33 #include "parseaddr.h"
35 /* Parse addresses in one of the following forms:
41 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
43 static char *parse_host(const char *s, uint16_t *ret_port) {
49 if (!(e = strchr(s+1, ']')))
55 if (pa_atou(e+2, &p) < 0)
58 *ret_port = (uint16_t) p;
62 return pa_xstrndup(s+1, (size_t) (e-s-1));
67 if (!(e = strrchr(s, ':')))
70 if (pa_atou(e+1, &p) < 0)
73 *ret_port = (uint16_t) p;
74 return pa_xstrndup(s, (size_t) (e-s));
78 int pa_parse_address(const char *name, pa_parsed_address *ret_p) {
84 memset(ret_p, 0, sizeof(pa_parsed_address));
85 ret_p->type = PA_PARSED_ADDRESS_TCP_AUTO;
90 /* The URL starts with a host id for detecting local connections */
91 if (!(id = pa_machine_id()))
94 pfx = pa_sprintf_malloc("{%s}", id);
97 if (!pa_startswith(name, pfx)) {
103 p = name + strlen(pfx);
110 ret_p->type = PA_PARSED_ADDRESS_UNIX;
112 if (strlen(p) >= 3 && p[1] == ':' && p[2] == '\\' && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z')))
113 ret_p->type = PA_PARSED_ADDRESS_UNIX;
115 else if (pa_startswith(p, "unix:")) {
116 ret_p->type = PA_PARSED_ADDRESS_UNIX;
117 p += sizeof("unix:")-1;
118 } else if (pa_startswith(p, "tcp:")) {
119 ret_p->type = PA_PARSED_ADDRESS_TCP4;
120 p += sizeof("tcp:")-1;
121 } else if (pa_startswith(p, "tcp4:")) {
122 ret_p->type = PA_PARSED_ADDRESS_TCP4;
123 p += sizeof("tcp4:")-1;
124 } else if (pa_startswith(p, "tcp6:")) {
125 ret_p->type = PA_PARSED_ADDRESS_TCP6;
126 p += sizeof("tcp6:")-1;
129 if (ret_p->type == PA_PARSED_ADDRESS_UNIX)
130 ret_p->path_or_host = pa_xstrdup(p);
132 if (!(ret_p->path_or_host = parse_host(p, &ret_p->port)))
138 bool pa_is_ip_address(const char *a) {
139 char buf[INET6_ADDRSTRLEN];
143 if (inet_pton(AF_INET6, a, buf) >= 1)
146 if (inet_pton(AF_INET, a, buf) >= 1)
152 bool pa_is_ip6_address(const char *a) {
153 char buf[INET6_ADDRSTRLEN];
157 if (inet_pton(AF_INET6, a, buf) >= 1)