clean move lws_plat externs
[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 #ifdef LWS_OPENSSL_SUPPORT
54 static void lws_sigusr2(int sig)
55 {
56 }
57 #endif
58
59 #ifdef LWS_USE_LIBEV
60 LWS_VISIBLE void 
61 libwebsocket_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
62 {
63         struct libwebsocket_pollfd eventfd;
64         struct lws_io_watcher *lws_io = (struct lws_io_watcher*)watcher;
65         struct libwebsocket_context *context = lws_io->context;
66
67         if (revents & EV_ERROR)
68                 return;
69
70         eventfd.fd = watcher->fd;
71         eventfd.revents = EV_NONE;
72         if (revents & EV_READ)
73                 eventfd.revents |= LWS_POLLIN;
74
75         if (revents & EV_WRITE)
76                 eventfd.revents |= LWS_POLLOUT;
77
78         libwebsocket_service_fd(context,&eventfd);
79 }
80
81 LWS_VISIBLE void
82 libwebsocket_sigint_cb(
83     struct ev_loop *loop, struct ev_signal* watcher, int revents)
84 {
85     ev_break(loop, EVBREAK_ALL);
86 }
87
88 LWS_VISIBLE int
89 libwebsocket_initloop(
90         struct libwebsocket_context *context,
91         struct ev_loop *loop)
92 {
93         int status = 0;
94         int backend;
95         const char * backend_name;
96         struct ev_io *w_accept = (ev_io *)&context->w_accept;
97         struct ev_signal *w_sigint = (ev_signal *)&context->w_sigint;
98
99         if (!loop)
100                 loop = ev_default_loop(0);
101
102         context->io_loop = loop;
103    
104         /*
105          * Initialize the accept w_accept with the listening socket
106          * and register a callback for read operations:
107          */
108         ev_io_init(w_accept, libwebsocket_accept_cb,
109                                         context->listen_service_fd, EV_READ);
110         ev_io_start(context->io_loop,w_accept);
111         ev_signal_init(w_sigint, libwebsocket_sigint_cb, SIGINT);
112         ev_signal_start(context->io_loop,w_sigint);
113         backend = ev_backend(loop);
114
115         switch (backend) {
116         case EVBACKEND_SELECT:
117                 backend_name = "select";
118                 break;
119         case EVBACKEND_POLL:
120                 backend_name = "poll";
121                 break;
122         case EVBACKEND_EPOLL:
123                 backend_name = "epoll";
124                 break;
125         case EVBACKEND_KQUEUE:
126                 backend_name = "kqueue";
127                 break;
128         case EVBACKEND_DEVPOLL:
129                 backend_name = "/dev/poll";
130                 break;
131         case EVBACKEND_PORT:
132                 backend_name = "Solaris 10 \"port\"";
133                 break;
134         default:
135                 backend_name = "Unknown libev backend";
136                 break;
137         };
138
139         lwsl_notice(" libev backend: %s\n", backend_name);
140
141         return status;
142 }
143
144 #endif /* LWS_USE_LIBEV */
145
146 /**
147  * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
148  * @context:    Websocket context
149  *
150  *      This function let a call to libwebsocket_service() waiting for a timeout
151  *      immediately return.
152  */
153 LWS_VISIBLE void
154 libwebsocket_cancel_service(struct libwebsocket_context *context)
155 {
156         char buf = 0;
157
158         if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
159                 lwsl_err("Cannot write to dummy pipe");
160 }
161
162 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
163 {
164         int syslog_level = LOG_DEBUG;
165
166         switch (level) {
167         case LLL_ERR:
168                 syslog_level = LOG_ERR;
169                 break;
170         case LLL_WARN:
171                 syslog_level = LOG_WARNING;
172                 break;
173         case LLL_NOTICE:
174                 syslog_level = LOG_NOTICE;
175                 break;
176         case LLL_INFO:
177                 syslog_level = LOG_INFO;
178                 break;
179         }
180         syslog(syslog_level, "%s", line);
181 }
182
183 LWS_VISIBLE int
184 lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
185 {
186         int n;
187         int m;
188         char buf;
189
190         /* stay dead once we are dead */
191
192         if (context == NULL)
193                 return 1;
194
195 #ifdef LWS_USE_LIBEV
196         if (context->io_loop && LWS_LIBEV_ENABLED(context))
197                 ev_run(context->io_loop, 0);
198 #endif /* LWS_USE_LIBEV */
199         context->service_tid = context->protocols[0].callback(context, NULL,
200                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
201
202         n = poll(context->fds, context->fds_count, timeout_ms);
203         context->service_tid = 0;
204
205         if (n == 0) /* poll timeout */ {
206                 libwebsocket_service_fd(context, NULL);
207                 return 0;
208         }
209
210         if (n < 0) {
211                 if (LWS_ERRNO != LWS_EINTR)
212                         return -1;
213                 return 0;
214         }
215
216         /* any socket with events to service? */
217
218         for (n = 0; n < context->fds_count; n++) {
219                 if (!context->fds[n].revents)
220                         continue;
221
222                 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
223                         if (read(context->fds[n].fd, &buf, 1) != 1)
224                                 lwsl_err("Cannot read from dummy pipe.");
225                         continue;
226                 }
227
228                 m = libwebsocket_service_fd(context, &context->fds[n]);
229                 if (m < 0)
230                         return -1;
231                 /* if something closed, retry this slot */
232                 if (m)
233                         n--;
234         }
235
236         return 0;
237 }
238
239 LWS_VISIBLE int
240 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
241 {
242         int optval = 1;
243         socklen_t optlen = sizeof(optval);
244
245 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
246         struct protoent *tcp_proto;
247 #endif
248
249         if (context->ka_time) {
250                 /* enable keepalive on this socket */
251                 optval = 1;
252                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
253                                              (const void *)&optval, optlen) < 0)
254                         return 1;
255
256 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
257
258                 /*
259                  * didn't find a way to set these per-socket, need to
260                  * tune kernel systemwide values
261                  */
262 #else
263                 /* set the keepalive conditions we want on it too */
264                 optval = context->ka_time;
265                 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
266                                              (const void *)&optval, optlen) < 0)
267                         return 1;
268
269                 optval = context->ka_interval;
270                 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
271                                              (const void *)&optval, optlen) < 0)
272                         return 1;
273
274                 optval = context->ka_probes;
275                 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
276                                              (const void *)&optval, optlen) < 0)
277                         return 1;
278 #endif
279         }
280
281         /* Disable Nagle */
282         optval = 1;
283 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
284         setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
285 #else
286         tcp_proto = getprotobyname("TCP");
287         setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
288 #endif
289
290         /* We are nonblocking... */
291         fcntl(fd, F_SETFL, O_NONBLOCK);
292
293         return 0;
294 }
295
296 LWS_VISIBLE void
297 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
298 {
299         if (info->gid != -1)
300                 if (setgid(info->gid))
301                         lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
302         if (info->uid != -1)
303                 if (setuid(info->uid))
304                         lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO)); 
305 }
306
307 LWS_VISIBLE int
308 lws_plat_init_fd_tables(struct libwebsocket_context *context)
309 {
310 #ifdef LWS_USE_LIBEV
311         if (LWS_LIBEV_ENABLED(context)) {
312                 context->w_accept.context = context;
313                 context->w_sigint.context = context;
314                 return 0;
315         }
316 #endif
317         if (pipe(context->dummy_pipe_fds)) {
318                 lwsl_err("Unable to create pipe\n");
319                 return 1;
320         }
321
322         /* use the read end of pipe as first item */
323         context->fds[0].fd = context->dummy_pipe_fds[0];
324         context->fds[0].events = LWS_POLLIN;
325         context->fds[0].revents = 0;
326         context->fds_count = 1;
327         
328         context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
329         if (context->fd_random < 0) {
330                 lwsl_err("Unable to open random device %s %d\n",
331                                     SYSTEM_RANDOM_FILEPATH, context->fd_random);
332                 return 1;
333         }
334
335         return 0;
336 }
337
338 static void sigpipe_handler(int x)
339 {
340 }
341
342
343 LWS_VISIBLE int
344 lws_plat_context_early_init(void)
345 {
346         sigset_t mask;
347
348         signal(SIGUSR2, lws_sigusr2);
349         sigemptyset(&mask);
350         sigaddset(&mask, SIGUSR2);
351
352         sigprocmask(SIG_BLOCK, &mask, NULL);
353         
354         signal(SIGPIPE, sigpipe_handler);
355
356         return 0;
357 }
358
359 LWS_VISIBLE void
360 lws_plat_context_early_destroy(struct libwebsocket_context *context)
361 {
362 }
363
364 LWS_VISIBLE void
365 lws_plat_context_late_destroy(struct libwebsocket_context *context)
366 {
367         close(context->dummy_pipe_fds[0]);
368         close(context->dummy_pipe_fds[1]);
369         close(context->fd_random);
370 }
371
372 /* cast a struct sockaddr_in6 * into addr for ipv6 */
373
374 LWS_VISIBLE int
375 interface_to_sa(struct libwebsocket_context *context,
376                 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
377 {
378         int rc = -1;
379
380         struct ifaddrs *ifr;
381         struct ifaddrs *ifc;
382 #ifdef LWS_USE_IPV6
383         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
384 #endif
385
386         getifaddrs(&ifr);
387         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
388                 if (!ifc->ifa_addr)
389                         continue;
390
391                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
392
393                 if (strcmp(ifc->ifa_name, ifname))
394                         continue;
395
396                 switch (ifc->ifa_addr->sa_family) {
397                 case AF_INET:
398 #ifdef LWS_USE_IPV6
399                         if (LWS_IPV6_ENABLED(context)) {
400                                 /* map IPv4 to IPv6 */
401                                 bzero((char *)&addr6->sin6_addr,
402                                                 sizeof(struct in6_addr));
403                                 addr6->sin6_addr.s6_addr[10] = 0xff;
404                                 addr6->sin6_addr.s6_addr[11] = 0xff;
405                                 memcpy(&addr6->sin6_addr.s6_addr[12],
406                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
407                                                         sizeof(struct in_addr));
408                         } else
409 #endif
410                                 memcpy(addr,
411                                         (struct sockaddr_in *)ifc->ifa_addr,
412                                                     sizeof(struct sockaddr_in));
413                         break;
414 #ifdef LWS_USE_IPV6
415                 case AF_INET6:
416                         if (rc >= 0)
417                                 break;
418                         memcpy(&addr6->sin6_addr,
419                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
420                                                        sizeof(struct in6_addr));
421                         break;
422 #endif
423                 default:
424                         continue;
425                 }
426                 rc = 0;
427         }
428
429         freeifaddrs(ifr);
430         
431         return rc;
432 }
433
434 LWS_VISIBLE void
435 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
436                                                        struct libwebsocket *wsi)
437 {
438 #ifdef LWS_USE_LIBEV
439         if (context && context->io_loop && LWS_LIBEV_ENABLED(context))
440                 ev_io_start(context->io_loop, (struct ev_io *)&wsi->w_read);
441 #endif /* LWS_USE_LIBEV */
442         context->fds[context->fds_count++].revents = 0;
443 }
444
445 LWS_VISIBLE void
446 lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
447                                                 struct libwebsocket *wsi, int m)
448 {
449 }
450
451 LWS_VISIBLE void
452 lws_plat_service_periodic(struct libwebsocket_context *context)
453 {
454         /* if our parent went down, don't linger around */
455         if (context->started_with_parent &&
456                               kill(context->started_with_parent, 0) < 0)
457                 kill(getpid(), SIGTERM);
458 }
459
460 LWS_VISIBLE int
461 lws_plat_change_pollfd(struct libwebsocket_context *context,
462                       struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
463 {
464         return 0;
465 }
466
467 LWS_VISIBLE int
468 lws_plat_open_file(const char* filename, unsigned long* filelen)
469 {
470         struct stat stat_buf;
471         int ret = open(filename, O_RDONLY);
472
473         if (ret < 0)
474                 return LWS_INVALID_FILE;
475
476         fstat(ret, &stat_buf);
477         *filelen = stat_buf.st_size;
478         return ret;
479 }
480
481 #ifdef LWS_USE_IPV6
482 LWS_VISIBLE const char *
483 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
484
485         return inet_ntop(af, src, dst, cnt);
486 }
487 #endif