a43d57b5b824eebbd012b297e43694504e2edbb4
[platform/upstream/libwebsockets.git] / lib / lws-plat-unix.c
1 #include "private-libwebsockets.h"
2
3 /*
4  * included from libwebsockets.c for unix builds
5  */
6
7 unsigned long long time_in_microseconds(void)
8 {
9         struct timeval tv;
10         gettimeofday(&tv, NULL);
11         return (tv.tv_sec * 1000000) + tv.tv_usec;
12 }
13
14 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
15                                                              void *buf, int len)
16 {
17         return read(context->fd_random, (char *)buf, len);
18 }
19
20 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
21 {
22         struct libwebsocket_pollfd fds;
23
24         /* treat the fact we got a truncated send pending as if we're choked */
25         if (wsi->truncated_send_len)
26                 return 1;
27
28         fds.fd = wsi->sock;
29         fds.events = POLLOUT;
30         fds.revents = 0;
31
32         if (poll(&fds, 1, 0) != 1)
33                 return 1;
34
35         if ((fds.revents & POLLOUT) == 0)
36                 return 1;
37
38         /* okay to send another packet without blocking */
39
40         return 0;
41 }
42
43 LWS_VISIBLE int
44 lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
45 {
46         return poll(fd, 1, 0);
47 }
48
49 /*
50  * This is just used to interrupt poll waiting
51  * we don't have to do anything with it.
52  */
53 static void lws_sigusr2(int sig)
54 {
55 }
56
57 /**
58  * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
59  * @context:    Websocket context
60  *
61  *      This function let a call to libwebsocket_service() waiting for a timeout
62  *      immediately return.
63  */
64 LWS_VISIBLE void
65 libwebsocket_cancel_service(struct libwebsocket_context *context)
66 {
67         char buf = 0;
68
69         if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
70                 lwsl_err("Cannot write to dummy pipe");
71 }
72
73 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
74 {
75         int syslog_level = LOG_DEBUG;
76
77         switch (level) {
78         case LLL_ERR:
79                 syslog_level = LOG_ERR;
80                 break;
81         case LLL_WARN:
82                 syslog_level = LOG_WARNING;
83                 break;
84         case LLL_NOTICE:
85                 syslog_level = LOG_NOTICE;
86                 break;
87         case LLL_INFO:
88                 syslog_level = LOG_INFO;
89                 break;
90         }
91         syslog(syslog_level, "%s", line);
92 }
93
94 LWS_VISIBLE int
95 lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
96 {
97         int n;
98         int m;
99         char buf;
100
101         /* stay dead once we are dead */
102
103         if (!context)
104                 return 1;
105
106         lws_libev_run(context);
107
108         context->service_tid = context->protocols[0].callback(context, NULL,
109                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
110
111 #ifdef LWS_OPENSSL_SUPPORT
112         /* if we know we have non-network pending data, do not wait in poll */
113         if (context->ssl_flag_buffered_reads)
114                 timeout_ms = 0;
115 #endif
116         n = poll(context->fds, context->fds_count, timeout_ms);
117         context->service_tid = 0;
118
119 #ifdef LWS_OPENSSL_SUPPORT
120         if (!context->ssl_flag_buffered_reads && n == 0) {
121 #else
122         if (n == 0) /* poll timeout */ {
123 #endif
124                 libwebsocket_service_fd(context, NULL);
125                 return 0;
126         }
127         
128 #ifdef LWS_OPENSSL_SUPPORT
129         /* any more will have to set it fresh this time around */
130         context->ssl_flag_buffered_reads = 0;
131 #endif
132
133         if (n < 0) {
134                 if (LWS_ERRNO != LWS_EINTR)
135                         return -1;
136                 return 0;
137         }
138
139         /* any socket with events to service? */
140
141         for (n = 0; n < context->fds_count; n++) {
142 #ifdef LWS_OPENSSL_SUPPORT
143                 struct libwebsocket *wsi;
144                 
145                 wsi = context->lws_lookup[context->fds[n].fd];
146                 if (wsi == NULL)
147                         continue;
148                 /* 
149                  * if he's not flowcontrolled, make sure we service ssl
150                  * pending read data
151                  */
152                 if (wsi->ssl && wsi->buffered_reads_pending) {
153                         lwsl_debug("wsi %p: forcing POLLIN\n", wsi);
154                         context->fds[n].revents |= context->fds[n].events & POLLIN;
155                         if (context->fds[n].revents & POLLIN)
156                                 wsi->buffered_reads_pending = 0;
157                         else
158                                 /* somebody left with pending SSL read data */
159                                 context->ssl_flag_buffered_reads = 1;
160                 }
161 #endif
162                 if (!context->fds[n].revents)
163                         continue;
164
165                 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
166                         if (read(context->fds[n].fd, &buf, 1) != 1)
167                                 lwsl_err("Cannot read from dummy pipe.");
168                         continue;
169                 }
170
171                 m = libwebsocket_service_fd(context, &context->fds[n]);
172                 if (m < 0)
173                         return -1;
174                 /* if something closed, retry this slot */
175                 if (m)
176                         n--;
177         }
178
179         return 0;
180 }
181
182 LWS_VISIBLE int
183 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
184 {
185         int optval = 1;
186         socklen_t optlen = sizeof(optval);
187
188 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
189     defined(__OpenBSD__)
190         struct protoent *tcp_proto;
191 #endif
192
193         if (context->ka_time) {
194                 /* enable keepalive on this socket */
195                 optval = 1;
196                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
197                                              (const void *)&optval, optlen) < 0)
198                         return 1;
199
200 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
201         defined(__CYGWIN__) || defined(__OpenBSD__)
202
203                 /*
204                  * didn't find a way to set these per-socket, need to
205                  * tune kernel systemwide values
206                  */
207 #else
208                 /* set the keepalive conditions we want on it too */
209                 optval = context->ka_time;
210                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
211                                              (const void *)&optval, optlen) < 0)
212                         return 1;
213
214                 optval = context->ka_interval;
215                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
216                                              (const void *)&optval, optlen) < 0)
217                         return 1;
218
219                 optval = context->ka_probes;
220                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
221                                              (const void *)&optval, optlen) < 0)
222                         return 1;
223 #endif
224         }
225
226         /* Disable Nagle */
227         optval = 1;
228 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
229     !defined(__OpenBSD__)
230         setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
231 #else
232         tcp_proto = getprotobyname("TCP");
233         setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
234 #endif
235
236         /* We are nonblocking... */
237         fcntl(fd, F_SETFL, O_NONBLOCK);
238
239         return 0;
240 }
241
242 LWS_VISIBLE void
243 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
244 {
245         if (info->gid != -1)
246                 if (setgid(info->gid))
247                         lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
248         if (info->uid != -1)
249                 if (setuid(info->uid))
250                         lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
251 }
252
253 LWS_VISIBLE int
254 lws_plat_init_fd_tables(struct libwebsocket_context *context)
255 {
256         if (lws_libev_init_fd_table(context))
257                 /* libev handled it instead */
258                 return 0;
259
260         if (pipe(context->dummy_pipe_fds)) {
261                 lwsl_err("Unable to create pipe\n");
262                 return 1;
263         }
264
265         /* use the read end of pipe as first item */
266         context->fds[0].fd = context->dummy_pipe_fds[0];
267         context->fds[0].events = LWS_POLLIN;
268         context->fds[0].revents = 0;
269         context->fds_count = 1;
270
271         context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
272         if (context->fd_random < 0) {
273                 lwsl_err("Unable to open random device %s %d\n",
274                                     SYSTEM_RANDOM_FILEPATH, context->fd_random);
275                 return 1;
276         }
277
278         return 0;
279 }
280
281 static void sigpipe_handler(int x)
282 {
283 }
284
285
286 LWS_VISIBLE int
287 lws_plat_context_early_init(void)
288 {
289         sigset_t mask;
290
291         signal(SIGUSR2, lws_sigusr2);
292         sigemptyset(&mask);
293         sigaddset(&mask, SIGUSR2);
294
295         sigprocmask(SIG_BLOCK, &mask, NULL);
296
297         signal(SIGPIPE, sigpipe_handler);
298
299         return 0;
300 }
301
302 LWS_VISIBLE void
303 lws_plat_context_early_destroy(struct libwebsocket_context *context)
304 {
305 }
306
307 LWS_VISIBLE void
308 lws_plat_context_late_destroy(struct libwebsocket_context *context)
309 {
310         close(context->dummy_pipe_fds[0]);
311         close(context->dummy_pipe_fds[1]);
312         close(context->fd_random);
313 }
314
315 /* cast a struct sockaddr_in6 * into addr for ipv6 */
316
317 LWS_VISIBLE int
318 interface_to_sa(struct libwebsocket_context *context,
319                 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
320 {
321         int rc = -1;
322
323         struct ifaddrs *ifr;
324         struct ifaddrs *ifc;
325 #ifdef LWS_USE_IPV6
326         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
327 #endif
328
329         getifaddrs(&ifr);
330         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
331                 if (!ifc->ifa_addr)
332                         continue;
333
334                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
335
336                 if (strcmp(ifc->ifa_name, ifname))
337                         continue;
338
339                 switch (ifc->ifa_addr->sa_family) {
340                 case AF_INET:
341 #ifdef LWS_USE_IPV6
342                         if (LWS_IPV6_ENABLED(context)) {
343                                 /* map IPv4 to IPv6 */
344                                 bzero((char *)&addr6->sin6_addr,
345                                                 sizeof(struct in6_addr));
346                                 addr6->sin6_addr.s6_addr[10] = 0xff;
347                                 addr6->sin6_addr.s6_addr[11] = 0xff;
348                                 memcpy(&addr6->sin6_addr.s6_addr[12],
349                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
350                                                         sizeof(struct in_addr));
351                         } else
352 #endif
353                                 memcpy(addr,
354                                         (struct sockaddr_in *)ifc->ifa_addr,
355                                                     sizeof(struct sockaddr_in));
356                         break;
357 #ifdef LWS_USE_IPV6
358                 case AF_INET6:
359                         if (rc >= 0)
360                                 break;
361                         memcpy(&addr6->sin6_addr,
362                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
363                                                        sizeof(struct in6_addr));
364                         break;
365 #endif
366                 default:
367                         continue;
368                 }
369                 rc = 0;
370         }
371
372         freeifaddrs(ifr);
373
374         if (rc == -1) {
375                 /* check if bind to IP adddress */
376 #ifdef LWS_USE_IPV6
377                 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
378                         rc = 0;
379                 else
380 #endif
381                 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
382                         rc = 0;
383         }
384
385         return rc;
386 }
387
388 LWS_VISIBLE void
389 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
390                                                        struct libwebsocket *wsi)
391 {
392         lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
393         context->fds[context->fds_count++].revents = 0;
394 }
395
396 LWS_VISIBLE void
397 lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
398                                                 struct libwebsocket *wsi, int m)
399 {
400 }
401
402 LWS_VISIBLE void
403 lws_plat_service_periodic(struct libwebsocket_context *context)
404 {
405         /* if our parent went down, don't linger around */
406         if (context->started_with_parent &&
407                               kill(context->started_with_parent, 0) < 0)
408                 kill(getpid(), SIGTERM);
409 }
410
411 LWS_VISIBLE int
412 lws_plat_change_pollfd(struct libwebsocket_context *context,
413                       struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
414 {
415         return 0;
416 }
417
418 LWS_VISIBLE int
419 lws_plat_open_file(const char* filename, unsigned long* filelen)
420 {
421         struct stat stat_buf;
422         int ret = open(filename, O_RDONLY);
423
424         if (ret < 0)
425                 return LWS_INVALID_FILE;
426
427         fstat(ret, &stat_buf);
428         *filelen = stat_buf.st_size;
429         return ret;
430 }
431
432 #ifdef LWS_USE_IPV6
433 LWS_VISIBLE const char *
434 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
435 {
436         return inet_ntop(af, src, dst, cnt);
437 }
438 #endif