]>
code.delx.au - pulseaudio/blob - src/socket-util.c
9 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <netinet/tcp.h>
14 #include <netinet/ip.h>
16 #include "socket-util.h"
19 void pa_socket_peer_to_string(int fd
, char *c
, size_t l
) {
22 assert(c
&& l
&& fd
>= 0);
24 if (fstat(fd
, &st
) < 0) {
25 snprintf(c
, l
, "Invalid client fd");
29 if (S_ISSOCK(st
.st_mode
)) {
32 struct sockaddr_in in
;
33 struct sockaddr_un un
;
35 socklen_t sa_len
= sizeof(sa
);
37 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
39 if (sa
.sa
.sa_family
== AF_INET
) {
40 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
42 snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
47 ntohs(sa
.in
.sin_port
));
49 } else if (sa
.sa
.sa_family
== AF_LOCAL
) {
50 snprintf(c
, l
, "UNIX socket client");
55 snprintf(c
, l
, "Unknown network client");
57 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
58 snprintf(c
, l
, "STDIN/STDOUT client");
62 snprintf(c
, l
, "Unknown client");
65 int pa_socket_low_delay(int fd
) {
70 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &priority
, sizeof(priority
)) < 0)
76 int pa_socket_tcp_low_delay(int fd
) {
81 ret
= pa_socket_low_delay(fd
);
84 /* if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
88 if (setsockopt(fd
, SOL_IP
, IP_TOS
, &tos
, sizeof(tos
)) < 0)
95 int pa_socket_set_rcvbuf(int fd
, size_t l
) {
98 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &l
, sizeof(l
)) < 0)
104 int pa_socket_set_sndbuf(int fd
, size_t l
) {
107 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &l
, sizeof(l
)) < 0)
113 int pa_unix_socket_is_stale(const char *fn
) {
114 struct sockaddr_un sa
;
115 int fd
= -1, ret
= -1;
117 if ((fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
118 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
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;
126 if (connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
127 if (errno
== ECONNREFUSED
)
139 int pa_unix_socket_remove_stale(const char *fn
) {
142 if ((r
= pa_unix_socket_is_stale(fn
)) < 0)
143 return errno
!= ENOENT
? -1 : 0;
148 /* Yes, here is a race condition. But who cares? */
155 int pa_unix_socket_make_secure_dir(const char *fn
) {
157 char *slash
, *dir
= strdup(fn
);
160 if (!(slash
= strrchr(dir
, '/')))
164 if (pa_make_secure_dir(dir
) < 0)
174 int pa_unix_socket_remove_secure_dir(const char *fn
) {
176 char *slash
, *dir
= strdup(fn
);
179 if (!(slash
= strrchr(dir
, '/')))