]> code.delx.au - pulseaudio/blob - src/pulsecore/socket-util.c
673058e2a994638922a94ebbac23f4c74a9bbc56
[pulseaudio] / src / pulsecore / socket-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2004 Joe Marcus Clarke
8 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2 of the License,
13 or (at your option) any later version.
14
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 USA.
24 ***/
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <sys/stat.h>
41
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 #ifdef HAVE_SYS_UN_H
46 #include <sys/un.h>
47 #endif
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
50 #endif
51 #ifdef HAVE_NETINET_IN_SYSTM_H
52 #include <netinet/in_systm.h>
53 #endif
54 #ifdef HAVE_NETINET_IP_H
55 #include <netinet/ip.h>
56 #endif
57 #ifdef HAVE_NETINET_TCP_H
58 #include <netinet/tcp.h>
59 #endif
60 #ifdef HAVE_NETDB_H
61 #include <netdb.h>
62 #endif
63 #ifdef HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
65 #endif
66
67 #ifndef HAVE_INET_NTOP
68 #include "inet_ntop.h"
69 #endif
70
71 #include "winsock.h"
72
73 #include <pulse/xmalloc.h>
74
75 #include <pulsecore/core-error.h>
76 #include <pulsecore/core-util.h>
77 #include <pulsecore/log.h>
78
79 #include "socket-util.h"
80
81 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
82 struct stat st;
83
84 assert(c && l && fd >= 0);
85
86 #ifndef OS_IS_WIN32
87 if (fstat(fd, &st) < 0) {
88 snprintf(c, l, "Invalid client fd");
89 return;
90 }
91 #endif
92
93 #ifndef OS_IS_WIN32
94 if (S_ISSOCK(st.st_mode)) {
95 #endif
96 union {
97 struct sockaddr sa;
98 struct sockaddr_in in;
99 struct sockaddr_in6 in6;
100 #ifdef HAVE_SYS_UN_H
101 struct sockaddr_un un;
102 #endif
103 } sa;
104 socklen_t sa_len = sizeof(sa);
105
106 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
107
108 if (sa.sa.sa_family == AF_INET) {
109 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
110
111 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
112 ip >> 24,
113 (ip >> 16) & 0xFF,
114 (ip >> 8) & 0xFF,
115 ip & 0xFF,
116 ntohs(sa.in.sin_port));
117 return;
118 } else if (sa.sa.sa_family == AF_INET6) {
119 char buf[INET6_ADDRSTRLEN];
120 const char *res;
121
122 res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf));
123 if (res) {
124 snprintf(c, l, "TCP/IP client from [%s]:%u", buf, ntohs(sa.in6.sin6_port));
125 return;
126 }
127 #ifdef HAVE_SYS_UN_H
128 } else if (sa.sa.sa_family == AF_UNIX) {
129 snprintf(c, l, "UNIX socket client");
130 return;
131 #endif
132 }
133
134 }
135 #ifndef OS_IS_WIN32
136 snprintf(c, l, "Unknown network client");
137 return;
138 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
139 snprintf(c, l, "STDIN/STDOUT client");
140 return;
141 }
142 #endif /* OS_IS_WIN32 */
143
144 snprintf(c, l, "Unknown client");
145 }
146
147 int pa_socket_low_delay(int fd) {
148 #ifdef SO_PRIORITY
149 int priority;
150 assert(fd >= 0);
151
152 priority = 7;
153 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void*)&priority, sizeof(priority)) < 0)
154 return -1;
155 #endif
156
157 return 0;
158 }
159
160 int pa_socket_tcp_low_delay(int fd) {
161 int ret, tos, on;
162
163 assert(fd >= 0);
164
165 ret = pa_socket_low_delay(fd);
166
167 on = 1;
168 tos = 0;
169
170 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
171 #if defined(SOL_TCP)
172 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
173 #else
174 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
175 #endif
176 ret = -1;
177 #endif
178
179 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || \
180 defined(IPPROTO_IP))
181 tos = IPTOS_LOWDELAY;
182 #ifdef SOL_IP
183 if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
184 #else
185 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
186 #endif
187 ret = -1;
188 #endif
189
190 return ret;
191
192 }
193
194 int pa_socket_set_rcvbuf(int fd, size_t l) {
195 assert(fd >= 0);
196
197 /* if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&l, sizeof(l)) < 0) { */
198 /* pa_log("SO_RCVBUF: %s", strerror(errno)); */
199 /* return -1; */
200 /* } */
201
202 return 0;
203 }
204
205 int pa_socket_set_sndbuf(int fd, size_t l) {
206 assert(fd >= 0);
207
208 /* if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&l, sizeof(l)) < 0) { */
209 /* pa_log("SO_SNDBUF: %s", strerror(errno)); */
210 /* return -1; */
211 /* } */
212
213 return 0;
214 }
215
216 #ifdef HAVE_SYS_UN_H
217
218 int pa_unix_socket_is_stale(const char *fn) {
219 struct sockaddr_un sa;
220 int fd = -1, ret = -1;
221
222 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
223 pa_log("socket(): %s", pa_cstrerror(errno));
224 goto finish;
225 }
226
227 sa.sun_family = AF_UNIX;
228 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
229 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
230
231 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
232 if (errno == ECONNREFUSED)
233 ret = 1;
234 } else
235 ret = 0;
236
237 finish:
238 if (fd >= 0)
239 pa_close(fd);
240
241 return ret;
242 }
243
244 int pa_unix_socket_remove_stale(const char *fn) {
245 int r;
246
247 if ((r = pa_unix_socket_is_stale(fn)) < 0)
248 return errno != ENOENT ? -1 : 0;
249
250 if (!r)
251 return 0;
252
253 /* Yes, here is a race condition. But who cares? */
254 if (unlink(fn) < 0)
255 return -1;
256
257 return 0;
258 }
259
260 #else /* HAVE_SYS_UN_H */
261
262 int pa_unix_socket_is_stale(const char *fn) {
263 return -1;
264 }
265
266 int pa_unix_socket_remove_stale(const char *fn) {
267 return -1;
268 }
269
270 #endif /* HAVE_SYS_UN_H */