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() - Cancel servicing of pending websocket activity
63 * @context: Websocket context
65 * This function let a call to lws_service() waiting for a timeout
69 lws_cancel_service(struct lws_context *context)
73 if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
74 lwsl_err("Cannot write to dummy pipe");
77 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
79 int syslog_level = LOG_DEBUG;
83 syslog_level = LOG_ERR;
86 syslog_level = LOG_WARNING;
89 syslog_level = LOG_NOTICE;
92 syslog_level = LOG_INFO;
95 syslog(syslog_level, "%s", line);
99 lws_plat_service(struct lws_context *context, int timeout_ms)
105 #ifdef LWS_OPENSSL_SUPPORT
106 struct lws *wsi_next;
109 /* stay dead once we are dead */
114 lws_libev_run(context);
116 if (!context->service_tid_detected) {
119 memset(&_lws, 0, sizeof(_lws));
120 _lws.context = context;
122 context->service_tid_detected = context->protocols[0].callback(
123 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
125 context->service_tid = context->service_tid_detected;
127 /* if we know we are draining rx ext, do not wait in poll */
128 if (context->rx_draining_ext_list)
131 #ifdef LWS_OPENSSL_SUPPORT
132 /* if we know we have non-network pending data, do not wait in poll */
133 if (lws_ssl_anybody_has_buffered_read(context)) {
135 lwsl_err("ssl buffered read\n");
139 n = poll(context->fds, context->fds_count, timeout_ms);
141 #ifdef LWS_OPENSSL_SUPPORT
142 if (!context->rx_draining_ext_list &&
143 !lws_ssl_anybody_has_buffered_read(context) && n == 0) {
145 if (!context->rx_draining_ext_list && n == 0) /* poll timeout */ {
147 lws_service_fd(context, NULL);
152 if (LWS_ERRNO != LWS_EINTR)
158 * For all guys with already-available ext data to drain, if they are
159 * not flowcontrolled, fake their POLLIN status
161 wsi = context->rx_draining_ext_list;
163 context->fds[wsi->position_in_fds_table].revents |=
164 context->fds[wsi->position_in_fds_table].events & POLLIN;
165 wsi = wsi->u.ws.rx_draining_ext_list;
168 #ifdef LWS_OPENSSL_SUPPORT
170 * For all guys with buffered SSL read data already saved up, if they
171 * are not flowcontrolled, fake their POLLIN status so they'll get
172 * service to use up the buffered incoming data, even though their
173 * network socket may have nothing
176 wsi = context->pending_read_list;
178 wsi_next = wsi->pending_read_list_next;
179 context->fds[wsi->position_in_fds_table].revents |=
180 context->fds[wsi->position_in_fds_table].events & POLLIN;
181 if (context->fds[wsi->position_in_fds_table].revents & POLLIN)
183 * he's going to get serviced now, take him off the
184 * list of guys with buffered SSL. If he still has some
185 * at the end of the service, he'll get put back on the
188 lws_ssl_remove_wsi_from_buffered_list(wsi);
194 /* any socket with events to service? */
196 for (n = 0; n < context->fds_count; n++) {
197 if (!context->fds[n].revents)
200 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
201 if (read(context->fds[n].fd, &buf, 1) != 1)
202 lwsl_err("Cannot read from dummy pipe.");
206 m = lws_service_fd(context, &context->fds[n]);
209 /* if something closed, retry this slot */
218 lws_plat_set_socket_options(struct lws_context *context, int fd)
221 socklen_t optlen = sizeof(optval);
223 #if defined(__APPLE__) || \
224 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
225 defined(__NetBSD__) || \
227 struct protoent *tcp_proto;
230 if (context->ka_time) {
231 /* enable keepalive on this socket */
233 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
234 (const void *)&optval, optlen) < 0)
237 #if defined(__APPLE__) || \
238 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
239 defined(__NetBSD__) || \
240 defined(__CYGWIN__) || defined(__OpenBSD__)
243 * didn't find a way to set these per-socket, need to
244 * tune kernel systemwide values
247 /* set the keepalive conditions we want on it too */
248 optval = context->ka_time;
249 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
250 (const void *)&optval, optlen) < 0)
253 optval = context->ka_interval;
254 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
255 (const void *)&optval, optlen) < 0)
258 optval = context->ka_probes;
259 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
260 (const void *)&optval, optlen) < 0)
267 #if !defined(__APPLE__) && \
268 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
269 !defined(__NetBSD__) && \
270 !defined(__OpenBSD__)
271 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
274 tcp_proto = getprotobyname("TCP");
275 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
279 /* We are nonblocking... */
280 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
287 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
289 if (info->uid != -1) {
290 struct passwd *p = getpwuid(info->uid);
293 initgroups(p->pw_name, info->gid);
294 if (setuid(info->uid))
295 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
297 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
299 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
302 if (setgid(info->gid))
303 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
307 static void sigpipe_handler(int x)
312 lws_plat_context_early_init(void)
316 signal(SIGUSR2, lws_sigusr2);
318 sigaddset(&mask, SIGUSR2);
320 sigprocmask(SIG_BLOCK, &mask, NULL);
322 signal(SIGPIPE, sigpipe_handler);
328 lws_plat_context_early_destroy(struct lws_context *context)
333 lws_plat_context_late_destroy(struct lws_context *context)
335 if (context->lws_lookup)
336 lws_free(context->lws_lookup);
338 close(context->dummy_pipe_fds[0]);
339 close(context->dummy_pipe_fds[1]);
340 close(context->fd_random);
343 /* cast a struct sockaddr_in6 * into addr for ipv6 */
346 interface_to_sa(struct lws_context *context,
347 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
354 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
358 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
362 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
364 if (strcmp(ifc->ifa_name, ifname))
367 switch (ifc->ifa_addr->sa_family) {
370 if (LWS_IPV6_ENABLED(context)) {
371 /* map IPv4 to IPv6 */
372 bzero((char *)&addr6->sin6_addr,
373 sizeof(struct in6_addr));
374 addr6->sin6_addr.s6_addr[10] = 0xff;
375 addr6->sin6_addr.s6_addr[11] = 0xff;
376 memcpy(&addr6->sin6_addr.s6_addr[12],
377 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
378 sizeof(struct in_addr));
382 (struct sockaddr_in *)ifc->ifa_addr,
383 sizeof(struct sockaddr_in));
387 memcpy(&addr6->sin6_addr,
388 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
389 sizeof(struct in6_addr));
401 /* check if bind to IP adddress */
403 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
407 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
415 lws_plat_insert_socket_into_fds(struct lws_context *context,
418 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
419 context->fds[context->fds_count++].revents = 0;
423 lws_plat_delete_socket_from_fds(struct lws_context *context,
424 struct lws *wsi, int m)
429 lws_plat_service_periodic(struct lws_context *context)
431 /* if our parent went down, don't linger around */
432 if (context->started_with_parent &&
433 kill(context->started_with_parent, 0) < 0)
434 kill(getpid(), SIGTERM);
438 lws_plat_change_pollfd(struct lws_context *context,
439 struct lws *wsi, struct lws_pollfd *pfd)
444 LWS_VISIBLE const char *
445 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
447 return inet_ntop(af, src, dst, cnt);
450 static lws_filefd_type
451 _lws_plat_file_open(struct lws *wsi, const char *filename,
452 unsigned long *filelen, int flags)
454 struct stat stat_buf;
455 int ret = open(filename, flags, 0664);
458 return LWS_INVALID_FILE;
460 if (fstat(ret, &stat_buf) < 0) {
462 return LWS_INVALID_FILE;
464 *filelen = stat_buf.st_size;
469 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
475 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
477 return lseek(fd, offset, SEEK_CUR);
481 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
482 unsigned char *buf, unsigned long len)
486 n = read((int)fd, buf, len);
498 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
499 unsigned char *buf, unsigned long len)
503 n = write((int)fd, buf, len);
515 lws_plat_init(struct lws_context *context,
516 struct lws_context_creation_info *info)
518 context->lws_lookup = lws_zalloc(sizeof(struct lws *) * context->max_fds);
519 if (context->lws_lookup == NULL) {
521 "Unable to allocate lws_lookup array for %d connections\n",
526 lwsl_notice(" mem: platform fd map: %5u bytes\n",
527 sizeof(struct lws *) * context->max_fds);
529 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
530 if (context->fd_random < 0) {
531 lwsl_err("Unable to open random device %s %d\n",
532 SYSTEM_RANDOM_FILEPATH, context->fd_random);
536 if (!lws_libev_init_fd_table(context)) {
537 /* otherwise libev handled it instead */
539 if (pipe(context->dummy_pipe_fds)) {
540 lwsl_err("Unable to create pipe\n");
545 /* use the read end of pipe as first item */
546 context->fds[0].fd = context->dummy_pipe_fds[0];
547 context->fds[0].events = LWS_POLLIN;
548 context->fds[0].revents = 0;
549 context->fds_count = 1;
551 context->fops.open = _lws_plat_file_open;
552 context->fops.close = _lws_plat_file_close;
553 context->fops.seek_cur = _lws_plat_file_seek_cur;
554 context->fops.read = _lws_plat_file_read;
555 context->fops.write = _lws_plat_file_write;