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