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