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