]>
code.delx.au - pulseaudio/blob - src/polypcore/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>
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
47 #ifdef HAVE_NETINET_IN_SYSTM_H
48 #include <netinet/in_systm.h>
50 #ifdef HAVE_NETINET_IP_H
51 #include <netinet/ip.h>
53 #ifdef HAVE_NETINET_TCP_H
54 #include <netinet/tcp.h>
59 #ifdef HAVE_ARPA_INET_H
60 #include <arpa/inet.h>
63 #ifndef HAVE_INET_NTOP
64 #include "inet_ntop.h"
69 #include <polyp/xmalloc.h>
71 #include <polypcore/core-error.h>
72 #include <polypcore/core-util.h>
73 #include <polypcore/log.h>
75 #include "socket-util.h"
77 void pa_socket_peer_to_string(int fd
, char *c
, size_t l
) {
80 assert(c
&& l
&& fd
>= 0);
83 if (fstat(fd
, &st
) < 0) {
84 snprintf(c
, l
, "Invalid client fd");
90 if (S_ISSOCK(st
.st_mode
)) {
94 struct sockaddr_in in
;
95 struct sockaddr_in6 in6
;
97 struct sockaddr_un un
;
100 socklen_t sa_len
= sizeof(sa
);
102 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
104 if (sa
.sa
.sa_family
== AF_INET
) {
105 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
107 snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
112 ntohs(sa
.in
.sin_port
));
114 } else if (sa
.sa
.sa_family
== AF_INET6
) {
115 char buf
[INET6_ADDRSTRLEN
];
118 res
= inet_ntop(AF_INET6
, &sa
.in6
.sin6_addr
, buf
, sizeof(buf
));
120 snprintf(c
, l
, "TCP/IP client from [%s]:%u", buf
, ntohs(sa
.in6
.sin6_port
));
124 } else if (sa
.sa
.sa_family
== AF_UNIX
) {
125 snprintf(c
, l
, "UNIX socket client");
132 snprintf(c
, l
, "Unknown network client");
134 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
135 snprintf(c
, l
, "STDIN/STDOUT client");
138 #endif /* OS_IS_WIN32 */
140 snprintf(c
, l
, "Unknown client");
143 int pa_socket_low_delay(int fd
) {
149 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, (void*)&priority
, sizeof(priority
)) < 0)
156 int pa_socket_tcp_low_delay(int fd
) {
161 ret
= pa_socket_low_delay(fd
);
166 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
168 if (setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, (void*)&on
, sizeof(on
)) < 0)
170 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&on
, sizeof(on
)) < 0)
175 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || \
177 tos
= IPTOS_LOWDELAY
;
179 if (setsockopt(fd
, SOL_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
181 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
190 int pa_socket_set_rcvbuf(int fd
, size_t l
) {
193 /* if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&l, sizeof(l)) < 0) { */
194 /* pa_log(__FILE__": SO_RCVBUF: %s", strerror(errno)); */
201 int pa_socket_set_sndbuf(int fd
, size_t l
) {
204 /* if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&l, sizeof(l)) < 0) { */
205 /* pa_log(__FILE__": SO_SNDBUF: %s", strerror(errno)); */
214 int pa_unix_socket_is_stale(const char *fn
) {
215 struct sockaddr_un sa
;
216 int fd
= -1, ret
= -1;
218 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
219 pa_log(__FILE__
": socket(): %s", pa_cstrerror(errno
));
223 sa
.sun_family
= AF_UNIX
;
224 strncpy(sa
.sun_path
, fn
, sizeof(sa
.sun_path
)-1);
225 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
227 if (connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
228 if (errno
== ECONNREFUSED
)
240 int pa_unix_socket_remove_stale(const char *fn
) {
243 if ((r
= pa_unix_socket_is_stale(fn
)) < 0)
244 return errno
!= ENOENT
? -1 : 0;
249 /* Yes, here is a race condition. But who cares? */
256 #else /* HAVE_SYS_UN_H */
258 int pa_unix_socket_is_stale(const char *fn
) {
262 int pa_unix_socket_remove_stale(const char *fn
) {
266 #endif /* HAVE_SYS_UN_H */