]> code.delx.au - pulseaudio/blob - polyp/socket-util.c
20380653d9a57e9146ac0a9058431d38cea3f426
[pulseaudio] / polyp / 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 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 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/un.h>
34 #include <netinet/in.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <netinet/tcp.h>
39 #include <netinet/ip.h>
40
41 #include "socket-util.h"
42 #include "util.h"
43 #include "xmalloc.h"
44 #include "log.h"
45
46 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
47 struct stat st;
48
49 assert(c && l && fd >= 0);
50
51 if (fstat(fd, &st) < 0) {
52 snprintf(c, l, "Invalid client fd");
53 return;
54 }
55
56 if (S_ISSOCK(st.st_mode)) {
57 union {
58 struct sockaddr sa;
59 struct sockaddr_in in;
60 struct sockaddr_un un;
61 } sa;
62 socklen_t sa_len = sizeof(sa);
63
64 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
65
66 if (sa.sa.sa_family == AF_INET) {
67 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
68
69 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
70 ip >> 24,
71 (ip >> 16) & 0xFF,
72 (ip >> 8) & 0xFF,
73 ip & 0xFF,
74 ntohs(sa.in.sin_port));
75 return;
76 } else if (sa.sa.sa_family == AF_LOCAL) {
77 snprintf(c, l, "UNIX socket client");
78 return;
79 }
80
81 }
82 snprintf(c, l, "Unknown network client");
83 return;
84 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
85 snprintf(c, l, "STDIN/STDOUT client");
86 return;
87 }
88
89 snprintf(c, l, "Unknown client");
90 }
91
92 int pa_socket_low_delay(int fd) {
93 int priority;
94 assert(fd >= 0);
95
96 priority = 7;
97 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
98 return -1;
99
100 return 0;
101 }
102
103 int pa_socket_tcp_low_delay(int fd) {
104 int ret, tos, on;
105
106 assert(fd >= 0);
107
108 ret = pa_socket_low_delay(fd);
109
110 on = 1;
111 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
112 ret = -1;
113
114 tos = IPTOS_LOWDELAY;
115 if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
116 ret = -1;
117
118 return ret;
119
120 }
121
122 int pa_socket_set_rcvbuf(int fd, size_t l) {
123 assert(fd >= 0);
124
125 /* if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0) { */
126 /* pa_log(__FILE__": SO_RCVBUF: %s\n", strerror(errno)); */
127 /* return -1; */
128 /* } */
129
130 return 0;
131 }
132
133 int pa_socket_set_sndbuf(int fd, size_t l) {
134 assert(fd >= 0);
135
136 /* if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0) { */
137 /* pa_log(__FILE__": SO_SNDBUF: %s\n", strerror(errno)); */
138 /* return -1; */
139 /* } */
140
141 return 0;
142 }
143
144 int pa_unix_socket_is_stale(const char *fn) {
145 struct sockaddr_un sa;
146 int fd = -1, ret = -1;
147
148 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
149 pa_log(__FILE__": socket(): %s\n", strerror(errno));
150 goto finish;
151 }
152
153 sa.sun_family = AF_LOCAL;
154 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
155 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
156
157 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
158 if (errno == ECONNREFUSED)
159 ret = 1;
160 } else
161 ret = 0;
162
163 finish:
164 if (fd >= 0)
165 close(fd);
166
167 return ret;
168 }
169
170 int pa_unix_socket_remove_stale(const char *fn) {
171 int r;
172
173 if ((r = pa_unix_socket_is_stale(fn)) < 0)
174 return errno != ENOENT ? -1 : 0;
175
176 if (!r)
177 return 0;
178
179 /* Yes, here is a race condition. But who cares? */
180 if (unlink(fn) < 0)
181 return -1;
182
183 return 0;
184 }
185
186 int pa_unix_socket_make_secure_dir(const char *fn) {
187 int ret = -1;
188 char *slash, *dir = pa_xstrdup(fn);
189
190 if (!(slash = strrchr(dir, '/')))
191 goto finish;
192 *slash = 0;
193
194 if (pa_make_secure_dir(dir) < 0)
195 goto finish;
196
197 ret = 0;
198
199 finish:
200 pa_xfree(dir);
201 return ret;
202 }
203
204 int pa_unix_socket_remove_secure_dir(const char *fn) {
205 int ret = -1;
206 char *slash, *dir = pa_xstrdup(fn);
207
208 if (!(slash = strrchr(dir, '/')))
209 goto finish;
210 *slash = 0;
211
212 if (rmdir(dir) < 0)
213 goto finish;
214
215 ret = 0;
216
217 finish:
218 pa_xfree(dir);
219 return ret;
220 }
221