]> code.delx.au - pulseaudio/blob - polyp/parseaddr.c
5e4c689ca2c58c373917358fbdb9c22c85f0d056
[pulseaudio] / polyp / parseaddr.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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.
10
11 polypaudio 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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <assert.h>
28 #include <stdlib.h>
29
30 #include "xmalloc.h"
31 #include "util.h"
32 #include "parseaddr.h"
33
34 /* Parse addresses in one of the following forms:
35 * HOSTNAME
36 * HOSTNAME:PORT
37 * [HOSTNAME]
38 * [HOSTNAME]:PORT
39 *
40 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
41
42 static char *parse_host(const char *s, uint16_t *ret_port) {
43 assert(s && ret_port);
44 if (*s == '[') {
45 char *e;
46 if (!(e = strchr(s+1, ']')))
47 return NULL;
48
49 if (e[1] == ':')
50 *ret_port = atoi(e+2);
51 else if (e[1] != 0)
52 return NULL;
53
54 return pa_xstrndup(s+1, e-s-1);
55 } else {
56 char *e;
57
58 if (!(e = strrchr(s, ':')))
59 return pa_xstrdup(s);
60
61 *ret_port = atoi(e+1);
62 return pa_xstrndup(s, e-s);
63 }
64 }
65
66 int pa_parse_address(const char *name, pa_parsed_address *ret_p) {
67 const char *p;
68 assert(name && ret_p);
69 memset(ret_p, 0, sizeof(pa_parsed_address));
70 ret_p->type = PA_PARSED_ADDRESS_TCP_AUTO;
71
72 if (*name == '{') {
73 char hn[256], *pfx;
74 /* The URL starts with a host specification for detecting local connections */
75
76 if (!pa_get_host_name(hn, sizeof(hn)))
77 return -1;
78
79 pfx = pa_sprintf_malloc("{%s}", hn);
80 if (!pa_startswith(name, pfx)) {
81 pa_xfree(pfx);
82 /* Not local */
83 return -1;
84 }
85
86 p = name + strlen(pfx);
87 pa_xfree(pfx);
88 } else
89 p = name;
90
91 if (*p == '/')
92 ret_p->type = PA_PARSED_ADDRESS_UNIX;
93 else if (pa_startswith(p, "unix:")) {
94 ret_p->type = PA_PARSED_ADDRESS_UNIX;
95 p += sizeof("unix:")-1;
96 } else if (pa_startswith(p, "tcp:") || pa_startswith(p, "tcp4:")) {
97 ret_p->type = PA_PARSED_ADDRESS_TCP4;
98 p += sizeof("tcp:")-1;
99 } else if (pa_startswith(p, "tcp6:")) {
100 ret_p->type = PA_PARSED_ADDRESS_TCP6;
101 p += sizeof("tcp6:")-1;
102 }
103
104 if (ret_p->type == PA_PARSED_ADDRESS_UNIX)
105 ret_p->path_or_host = pa_xstrdup(p);
106 else
107 if (!(ret_p->path_or_host = parse_host(p, &ret_p->port)))
108 return -1;
109
110
111 return 0;
112 }