]> code.delx.au - pulseaudio/blob - src/polypcore/parseaddr.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / 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 <polypcore/xmalloc.h>
31 #include <polypcore/util.h>
32
33 #include "parseaddr.h"
34
35 /* Parse addresses in one of the following forms:
36 * HOSTNAME
37 * HOSTNAME:PORT
38 * [HOSTNAME]
39 * [HOSTNAME]:PORT
40 *
41 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
42
43 static char *parse_host(const char *s, uint16_t *ret_port) {
44 assert(s && ret_port);
45 if (*s == '[') {
46 char *e;
47 if (!(e = strchr(s+1, ']')))
48 return NULL;
49
50 if (e[1] == ':')
51 *ret_port = atoi(e+2);
52 else if (e[1] != 0)
53 return NULL;
54
55 return pa_xstrndup(s+1, e-s-1);
56 } else {
57 char *e;
58
59 if (!(e = strrchr(s, ':')))
60 return pa_xstrdup(s);
61
62 *ret_port = atoi(e+1);
63 return pa_xstrndup(s, e-s);
64 }
65 }
66
67 int pa_parse_address(const char *name, pa_parsed_address *ret_p) {
68 const char *p;
69 assert(name && ret_p);
70 memset(ret_p, 0, sizeof(pa_parsed_address));
71 ret_p->type = PA_PARSED_ADDRESS_TCP_AUTO;
72
73 if (*name == '{') {
74 char hn[256], *pfx;
75 /* The URL starts with a host specification for detecting local connections */
76
77 if (!pa_get_host_name(hn, sizeof(hn)))
78 return -1;
79
80 pfx = pa_sprintf_malloc("{%s}", hn);
81 if (!pa_startswith(name, pfx)) {
82 pa_xfree(pfx);
83 /* Not local */
84 return -1;
85 }
86
87 p = name + strlen(pfx);
88 pa_xfree(pfx);
89 } else
90 p = name;
91
92 if (*p == '/')
93 ret_p->type = PA_PARSED_ADDRESS_UNIX;
94 else if (pa_startswith(p, "unix:")) {
95 ret_p->type = PA_PARSED_ADDRESS_UNIX;
96 p += sizeof("unix:")-1;
97 } else if (pa_startswith(p, "tcp:") || pa_startswith(p, "tcp4:")) {
98 ret_p->type = PA_PARSED_ADDRESS_TCP4;
99 p += sizeof("tcp:")-1;
100 } else if (pa_startswith(p, "tcp6:")) {
101 ret_p->type = PA_PARSED_ADDRESS_TCP6;
102 p += sizeof("tcp6:")-1;
103 }
104
105 if (ret_p->type == PA_PARSED_ADDRESS_UNIX)
106 ret_p->path_or_host = pa_xstrdup(p);
107 else
108 if (!(ret_p->path_or_host = parse_host(p, &ret_p->port)))
109 return -1;
110
111
112 return 0;
113 }