]> code.delx.au - pulseaudio/blob - src/socket-util.c
add alsa sink
[pulseaudio] / src / socket-util.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <sys/un.h>
9 #include <netinet/in.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <netinet/tcp.h>
14 #include <netinet/ip.h>
15
16 #include "socket-util.h"
17 #include "util.h"
18
19 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
20 struct stat st;
21
22 assert(c && l && fd >= 0);
23
24 if (fstat(fd, &st) < 0) {
25 snprintf(c, l, "Invalid client fd");
26 return;
27 }
28
29 if (S_ISSOCK(st.st_mode)) {
30 union {
31 struct sockaddr sa;
32 struct sockaddr_in in;
33 struct sockaddr_un un;
34 } sa;
35 socklen_t sa_len = sizeof(sa);
36
37 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
38
39 if (sa.sa.sa_family == AF_INET) {
40 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
41
42 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
43 ip >> 24,
44 (ip >> 16) & 0xFF,
45 (ip >> 8) & 0xFF,
46 ip & 0xFF,
47 ntohs(sa.in.sin_port));
48 return;
49 } else if (sa.sa.sa_family == AF_LOCAL) {
50 snprintf(c, l, "UNIX socket client");
51 return;
52 }
53
54 }
55 snprintf(c, l, "Unknown network client");
56 return;
57 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
58 snprintf(c, l, "STDIN/STDOUT client");
59 return;
60 }
61
62 snprintf(c, l, "Unknown client");
63 }
64
65 int pa_socket_low_delay(int fd) {
66 int priority;
67 assert(fd >= 0);
68
69 priority = 7;
70 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
71 return -1;
72
73 return 0;
74 }
75
76 int pa_socket_tcp_low_delay(int fd) {
77 int ret, tos;
78
79 assert(fd >= 0);
80
81 ret = pa_socket_low_delay(fd);
82
83 /* on = 1; */
84 /* if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
85 /* ret = -1; */
86
87 tos = IPTOS_LOWDELAY;
88 if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
89 ret = -1;
90
91 return ret;
92
93 }
94
95 int pa_socket_set_rcvbuf(int fd, size_t l) {
96 assert(fd >= 0);
97
98 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0)
99 return -1;
100
101 return 0;
102 }
103
104 int pa_socket_set_sndbuf(int fd, size_t l) {
105 assert(fd >= 0);
106
107 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0)
108 return -1;
109
110 return 0;
111 }
112
113 int pa_unix_socket_is_stale(const char *fn) {
114 struct sockaddr_un sa;
115 int fd = -1, ret = -1;
116
117 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
118 fprintf(stderr, "socket(): %s\n", strerror(errno));
119 goto finish;
120 }
121
122 sa.sun_family = AF_LOCAL;
123 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
124 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
125
126 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
127 if (errno == ECONNREFUSED)
128 ret = 1;
129 } else
130 ret = 0;
131
132 finish:
133 if (fd >= 0)
134 close(fd);
135
136 return ret;
137 }
138
139 int pa_unix_socket_remove_stale(const char *fn) {
140 int r;
141
142 if ((r = pa_unix_socket_is_stale(fn)) < 0)
143 return errno != ENOENT ? -1 : 0;
144
145 if (!r)
146 return 0;
147
148 /* Yes, here is a race condition. But who cares? */
149 if (unlink(fn) < 0)
150 return -1;
151
152 return 0;
153 }
154
155 int pa_unix_socket_make_secure_dir(const char *fn) {
156 int ret = -1;
157 char *slash, *dir = strdup(fn);
158 assert(dir);
159
160 if (!(slash = strrchr(dir, '/')))
161 goto finish;
162 *slash = 0;
163
164 if (pa_make_secure_dir(dir) < 0)
165 goto finish;
166
167 ret = 0;
168
169 finish:
170 free(dir);
171 return ret;
172 }
173
174 int pa_unix_socket_remove_secure_dir(const char *fn) {
175 int ret = -1;
176 char *slash, *dir = strdup(fn);
177 assert(dir);
178
179 if (!(slash = strrchr(dir, '/')))
180 goto finish;
181 *slash = 0;
182
183 if (rmdir(dir) < 0)
184 goto finish;
185
186 ret = 0;
187
188 finish:
189 free(dir);
190 return ret;
191 }