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