f9451af8d5c739a73b5eb6d5068355b4c0a6f7b7
[profile/ivi/pulseaudio.git] / 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
18 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
19     struct stat st;
20
21     assert(c && l && fd >= 0);
22     
23     if (fstat(fd, &st) < 0) {
24         snprintf(c, l, "Invalid client fd");
25         return;
26     }
27
28     if (S_ISSOCK(st.st_mode)) {
29         union {
30             struct sockaddr sa;
31             struct sockaddr_in in;
32             struct sockaddr_un un;
33         } sa;
34         socklen_t sa_len = sizeof(sa);
35         
36         if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
37
38             if (sa.sa.sa_family == AF_INET) {
39                 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
40                 
41                 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
42                          ip >> 24,
43                          (ip >> 16) & 0xFF,
44                          (ip >> 8) & 0xFF,
45                          ip & 0xFF,
46                          ntohs(sa.in.sin_port));
47                 return;
48             } else if (sa.sa.sa_family == AF_LOCAL) {
49                 snprintf(c, l, "UNIX socket client");
50                 return;
51             }
52
53         }
54         snprintf(c, l, "Unknown network client");
55         return;
56     } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
57         snprintf(c, l, "STDIN/STDOUT client");
58         return;
59     }
60
61     snprintf(c, l, "Unknown client");
62 }
63
64 int pa_socket_low_delay(int fd) {
65     int priority;
66     assert(fd >= 0);
67
68     priority = 7;
69     if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
70         return -1;
71
72     return 0;
73 }
74
75 int pa_socket_tcp_low_delay(int fd) {
76     int ret, tos;
77
78     assert(fd >= 0);
79
80     ret = pa_socket_low_delay(fd);
81     
82 /*     on = 1; */
83 /*     if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
84 /*         ret = -1; */
85
86     tos = IPTOS_LOWDELAY;
87     if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
88         ret = -1;
89
90     return ret;
91
92 }
93
94 int pa_socket_set_rcvbuf(int fd, size_t l) {
95     assert(fd >= 0);
96
97     if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0)
98         return -1;
99
100     return 0;
101 }
102
103 int pa_socket_set_sndbuf(int fd, size_t l) {
104     assert(fd >= 0);
105
106     if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0)
107         return -1;
108
109     return 0;
110 }
111
112 int pa_unix_socket_is_stale(const char *fn) {
113     struct sockaddr_un sa;
114     int fd = -1, ret = -1;
115
116     if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
117         fprintf(stderr, "socket(): %s\n", strerror(errno));
118         goto finish;
119     }
120
121     sa.sun_family = AF_LOCAL;
122     strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
123     sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
124
125     if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
126         if (errno == ECONNREFUSED)
127             ret = 1;
128     } else
129         ret = 0;
130
131 finish:
132     if (fd >= 0)
133         close(fd);
134
135     return ret;
136 }
137
138 int pa_unix_socket_remove_stale(const char *fn) {
139     int r;
140     
141     if ((r = pa_unix_socket_is_stale(fn)) < 0)
142         return errno != ENOENT ? -1 : 0;
143
144     if (!r)
145         return 0;
146         
147     /* Yes, here is a race condition. But who cares? */
148     if (unlink(fn) < 0)
149         return -1;
150
151     return 0;
152 }