]> code.delx.au - pulseaudio/blob - polyp/socket-util.c
add latency measurement support to tunnel module
[pulseaudio] / polyp / socket-util.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 General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 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 <stdarg.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <sys/un.h>
34 #include <netinet/in.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <netinet/tcp.h>
39 #include <netinet/ip.h>
40 #include <netdb.h>
41
42 #include "socket-util.h"
43 #include "util.h"
44 #include "xmalloc.h"
45 #include "log.h"
46
47 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
48 struct stat st;
49
50 assert(c && l && fd >= 0);
51
52 if (fstat(fd, &st) < 0) {
53 snprintf(c, l, "Invalid client fd");
54 return;
55 }
56
57 if (S_ISSOCK(st.st_mode)) {
58 union {
59 struct sockaddr sa;
60 struct sockaddr_in in;
61 struct sockaddr_un un;
62 } sa;
63 socklen_t sa_len = sizeof(sa);
64
65 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
66
67 if (sa.sa.sa_family == AF_INET) {
68 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
69
70 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
71 ip >> 24,
72 (ip >> 16) & 0xFF,
73 (ip >> 8) & 0xFF,
74 ip & 0xFF,
75 ntohs(sa.in.sin_port));
76 return;
77 } else if (sa.sa.sa_family == AF_LOCAL) {
78 snprintf(c, l, "UNIX socket client");
79 return;
80 }
81
82 }
83 snprintf(c, l, "Unknown network client");
84 return;
85 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
86 snprintf(c, l, "STDIN/STDOUT client");
87 return;
88 }
89
90 snprintf(c, l, "Unknown client");
91 }
92
93 int pa_socket_low_delay(int fd) {
94 int priority;
95 assert(fd >= 0);
96
97 priority = 7;
98 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
99 return -1;
100
101 return 0;
102 }
103
104 int pa_socket_tcp_low_delay(int fd) {
105 int ret, tos, on;
106
107 assert(fd >= 0);
108
109 ret = pa_socket_low_delay(fd);
110
111 on = 1;
112 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
113 ret = -1;
114
115 tos = IPTOS_LOWDELAY;
116 if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
117 ret = -1;
118
119 return ret;
120
121 }
122
123 int pa_socket_set_rcvbuf(int fd, size_t l) {
124 assert(fd >= 0);
125
126 /* if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0) { */
127 /* pa_log(__FILE__": SO_RCVBUF: %s\n", strerror(errno)); */
128 /* return -1; */
129 /* } */
130
131 return 0;
132 }
133
134 int pa_socket_set_sndbuf(int fd, size_t l) {
135 assert(fd >= 0);
136
137 /* if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0) { */
138 /* pa_log(__FILE__": SO_SNDBUF: %s\n", strerror(errno)); */
139 /* return -1; */
140 /* } */
141
142 return 0;
143 }
144
145 int pa_unix_socket_is_stale(const char *fn) {
146 struct sockaddr_un sa;
147 int fd = -1, ret = -1;
148
149 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
150 pa_log(__FILE__": socket(): %s\n", strerror(errno));
151 goto finish;
152 }
153
154 sa.sun_family = AF_LOCAL;
155 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
156 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
157
158 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
159 if (errno == ECONNREFUSED)
160 ret = 1;
161 } else
162 ret = 0;
163
164 finish:
165 if (fd >= 0)
166 close(fd);
167
168 return ret;
169 }
170
171 int pa_unix_socket_remove_stale(const char *fn) {
172 int r;
173
174 if ((r = pa_unix_socket_is_stale(fn)) < 0)
175 return errno != ENOENT ? -1 : 0;
176
177 if (!r)
178 return 0;
179
180 /* Yes, here is a race condition. But who cares? */
181 if (unlink(fn) < 0)
182 return -1;
183
184 return 0;
185 }
186
187 int pa_unix_socket_make_secure_dir(const char *fn) {
188 int ret = -1;
189 char *slash, *dir = pa_xstrdup(fn);
190
191 if (!(slash = strrchr(dir, '/')))
192 goto finish;
193 *slash = 0;
194
195 if (pa_make_secure_dir(dir) < 0)
196 goto finish;
197
198 ret = 0;
199
200 finish:
201 pa_xfree(dir);
202 return ret;
203 }
204
205 int pa_unix_socket_remove_secure_dir(const char *fn) {
206 int ret = -1;
207 char *slash, *dir = pa_xstrdup(fn);
208
209 if (!(slash = strrchr(dir, '/')))
210 goto finish;
211 *slash = 0;
212
213 if (rmdir(dir) < 0)
214 goto finish;
215
216 ret = 0;
217
218 finish:
219 pa_xfree(dir);
220 return ret;
221 }
222
223 struct sockaddr *pa_resolve_server(const char *server, size_t *len, uint16_t nport) {
224 struct sockaddr *sa;
225 struct addrinfo hints, *result = NULL;
226 char *port, host[256], tmp[16];
227 assert(server && len);
228
229 snprintf(host, sizeof(host), "%s", server);
230 host[strcspn(host, ":")] = 0;
231
232 if ((port = strrchr(server, ':')))
233 port++;
234
235 if (!port)
236 snprintf(port = tmp, sizeof(tmp), "%u", nport);
237
238 memset(&hints, 0, sizeof(hints));
239 hints.ai_family = PF_UNSPEC;
240 hints.ai_socktype = SOCK_STREAM;
241 hints.ai_protocol = 0;
242
243 if (getaddrinfo(host, port, &hints, &result) != 0)
244 return NULL;
245 assert(result);
246
247 sa = pa_xmalloc(*len = result->ai_addrlen);
248 memcpy(sa, result->ai_addr, *len);
249
250 freeaddrinfo(result);
251
252 return sa;
253 }
254