]>
code.delx.au - pulseaudio/blob - polyp/socket-util.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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.
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.
16 You should have received a copy of the GNU Lesser 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
33 #include <sys/types.h>
35 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/tcp.h>
40 #include <netinet/ip.h>
41 #include <sys/socket.h>
45 #include "socket-util.h"
50 void pa_socket_peer_to_string(int fd
, char *c
, size_t l
) {
53 assert(c
&& l
&& fd
>= 0);
55 if (fstat(fd
, &st
) < 0) {
56 snprintf(c
, l
, "Invalid client fd");
60 if (S_ISSOCK(st
.st_mode
)) {
63 struct sockaddr_in in
;
64 struct sockaddr_un un
;
66 socklen_t sa_len
= sizeof(sa
);
68 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
70 if (sa
.sa
.sa_family
== AF_INET
) {
71 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
73 snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
78 ntohs(sa
.in
.sin_port
));
80 } else if (sa
.sa
.sa_family
== AF_LOCAL
) {
81 snprintf(c
, l
, "UNIX socket client");
86 snprintf(c
, l
, "Unknown network client");
88 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
89 snprintf(c
, l
, "STDIN/STDOUT client");
93 snprintf(c
, l
, "Unknown client");
96 int pa_socket_low_delay(int fd
) {
102 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &priority
, sizeof(priority
)) < 0)
109 int pa_socket_tcp_low_delay(int fd
) {
114 ret
= pa_socket_low_delay(fd
);
118 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
120 if (setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, &on
, sizeof(on
)) < 0)
122 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
)) < 0)
127 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || \
129 tos
= IPTOS_LOWDELAY
;
131 if (setsockopt(fd
, SOL_IP
, IP_TOS
, &tos
, sizeof(tos
)) < 0)
133 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
)) < 0)
142 int pa_socket_set_rcvbuf(int fd
, size_t l
) {
145 /* if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0) { */
146 /* pa_log(__FILE__": SO_RCVBUF: %s\n", strerror(errno)); */
153 int pa_socket_set_sndbuf(int fd
, size_t l
) {
156 /* if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0) { */
157 /* pa_log(__FILE__": SO_SNDBUF: %s\n", strerror(errno)); */
164 int pa_unix_socket_is_stale(const char *fn
) {
165 struct sockaddr_un sa
;
166 int fd
= -1, ret
= -1;
168 if ((fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
169 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
173 sa
.sun_family
= AF_LOCAL
;
174 strncpy(sa
.sun_path
, fn
, sizeof(sa
.sun_path
)-1);
175 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
177 if (connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
178 if (errno
== ECONNREFUSED
)
190 int pa_unix_socket_remove_stale(const char *fn
) {
193 if ((r
= pa_unix_socket_is_stale(fn
)) < 0)
194 return errno
!= ENOENT
? -1 : 0;
199 /* Yes, here is a race condition. But who cares? */