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