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