1 #include "private-libwebsockets.h"
7 * included from libwebsockets.c for unix builds
10 unsigned long long time_in_microseconds(void)
13 gettimeofday(&tv, NULL);
14 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
18 lws_get_random(struct lws_context *context, void *buf, int len)
20 return read(context->fd_random, (char *)buf, len);
24 lws_send_pipe_choked(struct lws *wsi)
26 struct lws_pollfd fds;
28 /* treat the fact we got a truncated send pending as if we're choked */
36 if (poll(&fds, 1, 0) != 1)
39 if ((fds.revents & POLLOUT) == 0)
42 /* okay to send another packet without blocking */
48 lws_poll_listen_fd(struct lws_pollfd *fd)
50 return poll(fd, 1, 0);
54 * This is just used to interrupt poll waiting
55 * we don't have to do anything with it.
57 static void lws_sigusr2(int sig)
62 * lws_cancel_service_pt() - Cancel servicing of pending socket activity
64 * @wsi: Cancel service on the thread this wsi is serviced by
66 * This function let a call to lws_service() waiting for a timeout
70 lws_cancel_service_pt(struct lws *wsi)
72 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
75 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
76 lwsl_err("Cannot write to dummy pipe");
80 * lws_cancel_service() - Cancel ALL servicing of pending socket activity
81 * @context: Websocket context
83 * This function let a call to lws_service() waiting for a timeout
87 lws_cancel_service(struct lws_context *context)
89 struct lws_context_per_thread *pt = &context->pt[0];
90 char buf = 0, m = context->count_threads;
93 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
94 lwsl_err("Cannot write to dummy pipe");
99 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
101 int syslog_level = LOG_DEBUG;
105 syslog_level = LOG_ERR;
108 syslog_level = LOG_WARNING;
111 syslog_level = LOG_NOTICE;
114 syslog_level = LOG_INFO;
117 syslog(syslog_level, "%s", line);
121 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
123 struct lws_context_per_thread *pt = &context->pt[tsi];
127 #ifdef LWS_OPENSSL_SUPPORT
128 struct lws *wsi_next;
131 /* stay dead once we are dead */
136 lws_libev_run(context);
138 if (!context->service_tid_detected) {
141 memset(&_lws, 0, sizeof(_lws));
142 _lws.context = context;
144 context->service_tid_detected = context->protocols[0].callback(
145 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
147 context->service_tid = context->service_tid_detected;
149 /* if we know we are draining rx ext, do not wait in poll */
150 if (pt->rx_draining_ext_list)
153 #ifdef LWS_OPENSSL_SUPPORT
154 /* if we know we have non-network pending data, do not wait in poll */
155 if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
157 lwsl_err("ssl buffered read\n");
161 n = poll(pt->fds, pt->fds_count, timeout_ms);
163 #ifdef LWS_OPENSSL_SUPPORT
164 if (!pt->rx_draining_ext_list &&
165 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
167 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
169 lws_service_fd_tsi(context, NULL, tsi);
174 if (LWS_ERRNO != LWS_EINTR)
180 * For all guys with already-available ext data to drain, if they are
181 * not flowcontrolled, fake their POLLIN status
183 wsi = pt->rx_draining_ext_list;
185 pt->fds[wsi->position_in_fds_table].revents |=
186 pt->fds[wsi->position_in_fds_table].events & POLLIN;
187 wsi = wsi->u.ws.rx_draining_ext_list;
190 #ifdef LWS_OPENSSL_SUPPORT
192 * For all guys with buffered SSL read data already saved up, if they
193 * are not flowcontrolled, fake their POLLIN status so they'll get
194 * service to use up the buffered incoming data, even though their
195 * network socket may have nothing
198 wsi = pt->pending_read_list;
200 wsi_next = wsi->pending_read_list_next;
201 pt->fds[wsi->position_in_fds_table].revents |=
202 pt->fds[wsi->position_in_fds_table].events & POLLIN;
203 if (pt->fds[wsi->position_in_fds_table].revents & POLLIN)
205 * he's going to get serviced now, take him off the
206 * list of guys with buffered SSL. If he still has some
207 * at the end of the service, he'll get put back on the
210 lws_ssl_remove_wsi_from_buffered_list(wsi);
216 /* any socket with events to service? */
219 for (n = 0; n < pt->fds_count && c; n++) {
220 if (!pt->fds[n].revents)
225 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
226 if (read(pt->fds[n].fd, &buf, 1) != 1)
227 lwsl_err("Cannot read from dummy pipe.");
231 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
234 /* if something closed, retry this slot */
243 lws_plat_service(struct lws_context *context, int timeout_ms)
245 return lws_plat_service_tsi(context, timeout_ms, 0);
249 lws_plat_set_socket_options(struct lws_context *context, int fd)
252 socklen_t optlen = sizeof(optval);
254 #if defined(__APPLE__) || \
255 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
256 defined(__NetBSD__) || \
258 struct protoent *tcp_proto;
261 if (context->ka_time) {
262 /* enable keepalive on this socket */
264 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
265 (const void *)&optval, optlen) < 0)
268 #if defined(__APPLE__) || \
269 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
270 defined(__NetBSD__) || \
271 defined(__CYGWIN__) || defined(__OpenBSD__)
274 * didn't find a way to set these per-socket, need to
275 * tune kernel systemwide values
278 /* set the keepalive conditions we want on it too */
279 optval = context->ka_time;
280 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
281 (const void *)&optval, optlen) < 0)
284 optval = context->ka_interval;
285 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
286 (const void *)&optval, optlen) < 0)
289 optval = context->ka_probes;
290 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
291 (const void *)&optval, optlen) < 0)
298 #if !defined(__APPLE__) && \
299 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
300 !defined(__NetBSD__) && \
301 !defined(__OpenBSD__)
302 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
305 tcp_proto = getprotobyname("TCP");
306 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
310 /* We are nonblocking... */
311 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
318 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
320 if (info->uid != -1) {
321 struct passwd *p = getpwuid(info->uid);
324 initgroups(p->pw_name, info->gid);
325 if (setuid(info->uid))
326 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
328 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
330 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
333 if (setgid(info->gid))
334 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
338 static void sigpipe_handler(int x)
343 lws_plat_context_early_init(void)
347 signal(SIGUSR2, lws_sigusr2);
349 sigaddset(&mask, SIGUSR2);
351 sigprocmask(SIG_BLOCK, &mask, NULL);
353 signal(SIGPIPE, sigpipe_handler);
359 lws_plat_context_early_destroy(struct lws_context *context)
364 lws_plat_context_late_destroy(struct lws_context *context)
366 struct lws_context_per_thread *pt = &context->pt[0];
367 int m = context->count_threads;
369 if (context->lws_lookup)
370 lws_free(context->lws_lookup);
373 close(pt->dummy_pipe_fds[0]);
374 close(pt->dummy_pipe_fds[1]);
377 close(context->fd_random);
380 /* cast a struct sockaddr_in6 * into addr for ipv6 */
383 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
390 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
394 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
398 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
400 if (strcmp(ifc->ifa_name, ifname))
403 switch (ifc->ifa_addr->sa_family) {
407 /* map IPv4 to IPv6 */
408 bzero((char *)&addr6->sin6_addr,
409 sizeof(struct in6_addr));
410 addr6->sin6_addr.s6_addr[10] = 0xff;
411 addr6->sin6_addr.s6_addr[11] = 0xff;
412 memcpy(&addr6->sin6_addr.s6_addr[12],
413 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
414 sizeof(struct in_addr));
418 (struct sockaddr_in *)ifc->ifa_addr,
419 sizeof(struct sockaddr_in));
423 memcpy(&addr6->sin6_addr,
424 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
425 sizeof(struct in6_addr));
437 /* check if bind to IP adddress */
439 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
443 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
451 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
453 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
455 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
456 pt->fds[pt->fds_count++].revents = 0;
460 lws_plat_delete_socket_from_fds(struct lws_context *context,
461 struct lws *wsi, int m)
463 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
468 lws_plat_service_periodic(struct lws_context *context)
470 /* if our parent went down, don't linger around */
471 if (context->started_with_parent &&
472 kill(context->started_with_parent, 0) < 0)
473 kill(getpid(), SIGTERM);
477 lws_plat_change_pollfd(struct lws_context *context,
478 struct lws *wsi, struct lws_pollfd *pfd)
483 LWS_VISIBLE const char *
484 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
486 return inet_ntop(af, src, dst, cnt);
489 static lws_filefd_type
490 _lws_plat_file_open(struct lws *wsi, const char *filename,
491 unsigned long *filelen, int flags)
493 struct stat stat_buf;
494 int ret = open(filename, flags, 0664);
497 return LWS_INVALID_FILE;
499 if (fstat(ret, &stat_buf) < 0) {
501 return LWS_INVALID_FILE;
503 *filelen = stat_buf.st_size;
508 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
514 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
516 return lseek(fd, offset, SEEK_CUR);
520 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
521 unsigned char *buf, unsigned long len)
525 n = read((int)fd, buf, len);
537 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
538 unsigned char *buf, unsigned long len)
542 n = write((int)fd, buf, len);
554 lws_plat_init(struct lws_context *context,
555 struct lws_context_creation_info *info)
557 struct lws_context_per_thread *pt = &context->pt[0];
558 int n = context->count_threads, fd;
560 /* master context has the global fd lookup array */
561 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
563 if (context->lws_lookup == NULL) {
564 lwsl_err("OOM on lws_lookup array for %d connections\n",
569 lwsl_notice(" mem: platform fd map: %5u bytes\n",
570 sizeof(struct lws *) * context->max_fds);
571 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
573 context->fd_random = fd;
574 if (context->fd_random < 0) {
575 lwsl_err("Unable to open random device %s %d\n",
576 SYSTEM_RANDOM_FILEPATH, context->fd_random);
580 if (!lws_libev_init_fd_table(context)) {
581 /* otherwise libev handled it instead */
584 if (pipe(pt->dummy_pipe_fds)) {
585 lwsl_err("Unable to create pipe\n");
589 /* use the read end of pipe as first item */
590 pt->fds[0].fd = pt->dummy_pipe_fds[0];
591 pt->fds[0].events = LWS_POLLIN;
592 pt->fds[0].revents = 0;
598 context->fops.open = _lws_plat_file_open;
599 context->fops.close = _lws_plat_file_close;
600 context->fops.seek_cur = _lws_plat_file_seek_cur;
601 context->fops.read = _lws_plat_file_read;
602 context->fops.write = _lws_plat_file_write;