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