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