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.
63 * lws_cancel_service_pt() - Cancel servicing of pending socket activity
65 * @wsi: Cancel service on the thread this wsi is serviced by
67 * This function let a call to lws_service() waiting for a timeout
71 lws_cancel_service_pt(struct lws *wsi)
73 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
76 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
77 lwsl_err("Cannot write to dummy pipe");
81 * lws_cancel_service() - Cancel ALL servicing of pending socket activity
82 * @context: Websocket context
84 * This function let a call to lws_service() waiting for a timeout
88 lws_cancel_service(struct lws_context *context)
90 struct lws_context_per_thread *pt = &context->pt[0];
91 char buf = 0, m = context->count_threads;
94 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
95 lwsl_err("Cannot write to dummy pipe");
100 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
102 int syslog_level = LOG_DEBUG;
106 syslog_level = LOG_ERR;
109 syslog_level = LOG_WARNING;
112 syslog_level = LOG_NOTICE;
115 syslog_level = LOG_INFO;
118 syslog(syslog_level, "%s", line);
122 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
124 struct lws_context_per_thread *pt = &context->pt[tsi];
128 #ifdef LWS_OPENSSL_SUPPORT
129 struct lws *wsi_next;
132 /* stay dead once we are dead */
137 lws_libev_run(context);
139 if (!context->service_tid_detected) {
142 memset(&_lws, 0, sizeof(_lws));
143 _lws.context = context;
145 context->service_tid_detected = context->protocols[0].callback(
146 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
148 context->service_tid = context->service_tid_detected;
150 /* if we know we are draining rx ext, do not wait in poll */
151 if (pt->rx_draining_ext_list)
154 #ifdef LWS_OPENSSL_SUPPORT
155 /* if we know we have non-network pending data, do not wait in poll */
156 if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
158 lwsl_err("ssl buffered read\n");
162 n = poll(pt->fds, pt->fds_count, timeout_ms);
164 #ifdef LWS_OPENSSL_SUPPORT
165 if (!pt->rx_draining_ext_list &&
166 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
168 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
170 lws_service_fd_tsi(context, NULL, tsi);
175 if (LWS_ERRNO != LWS_EINTR)
181 * For all guys with already-available ext data to drain, if they are
182 * not flowcontrolled, fake their POLLIN status
184 wsi = pt->rx_draining_ext_list;
186 pt->fds[wsi->position_in_fds_table].revents |=
187 pt->fds[wsi->position_in_fds_table].events & POLLIN;
188 wsi = wsi->u.ws.rx_draining_ext_list;
191 #ifdef LWS_OPENSSL_SUPPORT
193 * For all guys with buffered SSL read data already saved up, if they
194 * are not flowcontrolled, fake their POLLIN status so they'll get
195 * service to use up the buffered incoming data, even though their
196 * network socket may have nothing
199 wsi = pt->pending_read_list;
201 wsi_next = wsi->pending_read_list_next;
202 pt->fds[wsi->position_in_fds_table].revents |=
203 pt->fds[wsi->position_in_fds_table].events & POLLIN;
204 if (pt->fds[wsi->position_in_fds_table].revents & POLLIN)
206 * he's going to get serviced now, take him off the
207 * list of guys with buffered SSL. If he still has some
208 * at the end of the service, he'll get put back on the
211 lws_ssl_remove_wsi_from_buffered_list(wsi);
217 /* any socket with events to service? */
220 for (n = 0; n < pt->fds_count && c; n++) {
221 if (!pt->fds[n].revents)
226 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
227 if (read(pt->fds[n].fd, &buf, 1) != 1)
228 lwsl_err("Cannot read from dummy pipe.");
232 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
235 /* if something closed, retry this slot */
244 lws_plat_service(struct lws_context *context, int timeout_ms)
246 return lws_plat_service_tsi(context, timeout_ms, 0);
250 lws_plat_set_socket_options(struct lws_context *context, int fd)
253 socklen_t optlen = sizeof(optval);
255 #if defined(__APPLE__) || \
256 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
257 defined(__NetBSD__) || \
259 struct protoent *tcp_proto;
262 if (context->ka_time) {
263 /* enable keepalive on this socket */
265 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
266 (const void *)&optval, optlen) < 0)
269 #if defined(__APPLE__) || \
270 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
271 defined(__NetBSD__) || \
272 defined(__CYGWIN__) || defined(__OpenBSD__)
275 * didn't find a way to set these per-socket, need to
276 * tune kernel systemwide values
279 /* set the keepalive conditions we want on it too */
280 optval = context->ka_time;
281 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
282 (const void *)&optval, optlen) < 0)
285 optval = context->ka_interval;
286 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
287 (const void *)&optval, optlen) < 0)
290 optval = context->ka_probes;
291 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
292 (const void *)&optval, optlen) < 0)
299 #if !defined(__APPLE__) && \
300 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
301 !defined(__NetBSD__) && \
302 !defined(__OpenBSD__)
303 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
306 tcp_proto = getprotobyname("TCP");
307 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
311 /* We are nonblocking... */
312 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
319 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
321 if (info->uid != -1) {
322 struct passwd *p = getpwuid(info->uid);
325 initgroups(p->pw_name, info->gid);
326 if (setuid(info->uid))
327 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
329 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
331 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
334 if (setgid(info->gid))
335 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
340 sigpipe_handler(int x)
345 lws_plat_context_early_init(void)
349 signal(SIGUSR2, lws_sigusr2);
351 sigaddset(&mask, SIGUSR2);
353 sigprocmask(SIG_BLOCK, &mask, NULL);
355 signal(SIGPIPE, sigpipe_handler);
361 lws_plat_context_early_destroy(struct lws_context *context)
366 lws_plat_context_late_destroy(struct lws_context *context)
368 struct lws_context_per_thread *pt = &context->pt[0];
369 int m = context->count_threads;
371 if (context->lws_lookup)
372 lws_free(context->lws_lookup);
375 close(pt->dummy_pipe_fds[0]);
376 close(pt->dummy_pipe_fds[1]);
379 close(context->fd_random);
382 /* cast a struct sockaddr_in6 * into addr for ipv6 */
385 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
392 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
396 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
400 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
402 if (strcmp(ifc->ifa_name, ifname))
405 switch (ifc->ifa_addr->sa_family) {
409 /* map IPv4 to IPv6 */
410 bzero((char *)&addr6->sin6_addr,
411 sizeof(struct in6_addr));
412 addr6->sin6_addr.s6_addr[10] = 0xff;
413 addr6->sin6_addr.s6_addr[11] = 0xff;
414 memcpy(&addr6->sin6_addr.s6_addr[12],
415 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
416 sizeof(struct in_addr));
420 (struct sockaddr_in *)ifc->ifa_addr,
421 sizeof(struct sockaddr_in));
425 memcpy(&addr6->sin6_addr,
426 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
427 sizeof(struct in6_addr));
439 /* check if bind to IP adddress */
441 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
445 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
453 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
455 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
457 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
458 pt->fds[pt->fds_count++].revents = 0;
462 lws_plat_delete_socket_from_fds(struct lws_context *context,
463 struct lws *wsi, int m)
465 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
470 lws_plat_service_periodic(struct lws_context *context)
472 /* if our parent went down, don't linger around */
473 if (context->started_with_parent &&
474 kill(context->started_with_parent, 0) < 0)
475 kill(getpid(), SIGTERM);
479 lws_plat_change_pollfd(struct lws_context *context,
480 struct lws *wsi, struct lws_pollfd *pfd)
485 LWS_VISIBLE const char *
486 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
488 return inet_ntop(af, src, dst, cnt);
491 static lws_filefd_type
492 _lws_plat_file_open(struct lws *wsi, const char *filename,
493 unsigned long *filelen, int flags)
495 struct stat stat_buf;
496 int ret = open(filename, flags, 0664);
499 return LWS_INVALID_FILE;
501 if (fstat(ret, &stat_buf) < 0) {
503 return LWS_INVALID_FILE;
505 *filelen = stat_buf.st_size;
510 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
516 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
518 return lseek(fd, offset, SEEK_CUR);
522 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
523 unsigned char *buf, unsigned long len)
527 n = read((int)fd, buf, len);
539 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
540 unsigned char *buf, unsigned long len)
544 n = write((int)fd, buf, len);
556 lws_plat_init(struct lws_context *context,
557 struct lws_context_creation_info *info)
559 struct lws_context_per_thread *pt = &context->pt[0];
560 int n = context->count_threads, fd;
562 /* master context has the global fd lookup array */
563 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
565 if (context->lws_lookup == NULL) {
566 lwsl_err("OOM on lws_lookup array for %d connections\n",
571 lwsl_notice(" mem: platform fd map: %5u bytes\n",
572 sizeof(struct lws *) * context->max_fds);
573 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
575 context->fd_random = fd;
576 if (context->fd_random < 0) {
577 lwsl_err("Unable to open random device %s %d\n",
578 SYSTEM_RANDOM_FILEPATH, context->fd_random);
582 if (!lws_libev_init_fd_table(context)) {
583 /* otherwise libev handled it instead */
586 if (pipe(pt->dummy_pipe_fds)) {
587 lwsl_err("Unable to create pipe\n");
591 /* use the read end of pipe as first item */
592 pt->fds[0].fd = pt->dummy_pipe_fds[0];
593 pt->fds[0].events = LWS_POLLIN;
594 pt->fds[0].revents = 0;
600 context->fops.open = _lws_plat_file_open;
601 context->fops.close = _lws_plat_file_close;
602 context->fops.seek_cur = _lws_plat_file_seek_cur;
603 context->fops.read = _lws_plat_file_read;
604 context->fops.write = _lws_plat_file_write;