]> code.delx.au - pulseaudio/blob - src/pulsecore/parseaddr.c
Merge dead branch 'ossman'
[pulseaudio] / src / pulsecore / parseaddr.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <pulse/xmalloc.h>
30 #include <pulse/util.h>
31
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/macro.h>
34
35 #include "parseaddr.h"
36
37 /* Parse addresses in one of the following forms:
38 * HOSTNAME
39 * HOSTNAME:PORT
40 * [HOSTNAME]
41 * [HOSTNAME]:PORT
42 *
43 * Return a newly allocated string of the hostname and fill in *ret_port if specified */
44
45 static char *parse_host(const char *s, uint16_t *ret_port) {
46 pa_assert(s);
47 pa_assert(ret_port);
48
49 if (*s == '[') {
50 char *e;
51 if (!(e = strchr(s+1, ']')))
52 return NULL;
53
54 if (e[1] == ':')
55 *ret_port = atoi(e+2);
56 else if (e[1] != 0)
57 return NULL;
58
59 return pa_xstrndup(s+1, e-s-1);
60 } else {
61 char *e;
62
63 if (!(e = strrchr(s, ':')))
64 return pa_xstrdup(s);
65
66 *ret_port = atoi(e+1);
67 return pa_xstrndup(s, e-s);
68 }
69 }
70
71 int pa_parse_address(const char *name, pa_parsed_address *ret_p) {
72 const char *p;
73
74 pa_assert(name);
75 pa_assert(ret_p);
76
77 memset(ret_p, 0, sizeof(pa_parsed_address));
78 ret_p->type = PA_PARSED_ADDRESS_TCP_AUTO;
79
80 if (*name == '{') {
81 char hn[256], *pfx;
82 /* The URL starts with a host specification for detecting local connections */
83
84 if (!pa_get_host_name(hn, sizeof(hn)))
85 return -1;
86
87 pfx = pa_sprintf_malloc("{%s}", hn);
88 if (!pa_startswith(name, pfx)) {
89 pa_xfree(pfx);
90 /* Not local */
91 return -1;
92 }
93
94 p = name + strlen(pfx);
95 pa_xfree(pfx);
96 } else
97 p = name;
98
99 if (*p == '/')
100 ret_p->type = PA_PARSED_ADDRESS_UNIX;
101 else if (pa_startswith(p, "unix:")) {
102 ret_p->type = PA_PARSED_ADDRESS_UNIX;
103 p += sizeof("unix:")-1;
104 } else if (pa_startswith(p, "tcp:")) {
105 ret_p->type = PA_PARSED_ADDRESS_TCP4;
106 p += sizeof("tcp:")-1;
107 } else if (pa_startswith(p, "tcp4:")) {
108 ret_p->type = PA_PARSED_ADDRESS_TCP4;
109 p += sizeof("tcp4:")-1;
110 } else if (pa_startswith(p, "tcp6:")) {
111 ret_p->type = PA_PARSED_ADDRESS_TCP6;
112 p += sizeof("tcp6:")-1;
113 }
114
115 if (ret_p->type == PA_PARSED_ADDRESS_UNIX)
116 ret_p->path_or_host = pa_xstrdup(p);
117 else
118 if (!(ret_p->path_or_host = parse_host(p, &ret_p->port)))
119 return -1;
120
121 return 0;
122 }