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;
17 LWS_VISIBLE int lws_get_random(struct lws_context *context,
20 return read(context->fd_random, (char *)buf, len);
23 LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
25 struct lws_pollfd fds;
27 /* treat the fact we got a truncated send pending as if we're choked */
28 if (wsi->truncated_send_len)
35 if (poll(&fds, 1, 0) != 1)
38 if ((fds.revents & POLLOUT) == 0)
41 /* okay to send another packet without blocking */
47 lws_poll_listen_fd(struct lws_pollfd *fd)
49 return poll(fd, 1, 0);
53 * This is just used to interrupt poll waiting
54 * we don't have to do anything with it.
56 static void lws_sigusr2(int sig)
61 * lws_cancel_service() - Cancel servicing of pending websocket activity
62 * @context: Websocket context
64 * This function let a call to lws_service() waiting for a timeout
68 lws_cancel_service(struct lws_context *context)
72 if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
73 lwsl_err("Cannot write to dummy pipe");
76 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
78 int syslog_level = LOG_DEBUG;
82 syslog_level = LOG_ERR;
85 syslog_level = LOG_WARNING;
88 syslog_level = LOG_NOTICE;
91 syslog_level = LOG_INFO;
94 syslog(syslog_level, "%s", line);
98 lws_plat_service(struct lws_context *context, int timeout_ms)
103 #ifdef LWS_OPENSSL_SUPPORT
104 struct lws *wsi, *wsi_next;
107 /* stay dead once we are dead */
112 lws_libev_run(context);
114 context->service_tid = context->protocols[0].callback(context, NULL,
115 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
117 #ifdef LWS_OPENSSL_SUPPORT
118 /* if we know we have non-network pending data, do not wait in poll */
119 if (lws_ssl_anybody_has_buffered_read(context))
122 n = poll(context->fds, context->fds_count, timeout_ms);
123 context->service_tid = 0;
125 #ifdef LWS_OPENSSL_SUPPORT
126 if (!lws_ssl_anybody_has_buffered_read(context) && n == 0) {
128 if (n == 0) /* poll timeout */ {
130 lws_service_fd(context, NULL);
135 if (LWS_ERRNO != LWS_EINTR)
140 #ifdef LWS_OPENSSL_SUPPORT
142 * For all guys with buffered SSL read data already saved up, if they
143 * are not flowcontrolled, fake their POLLIN status so they'll get
144 * service to use up the buffered incoming data, even though their
145 * network socket may have nothing
148 wsi = context->pending_read_list;
150 wsi_next = wsi->pending_read_list_next;
151 context->fds[wsi->position_in_fds_table].revents |=
152 context->fds[wsi->position_in_fds_table].events & POLLIN;
153 if (context->fds[wsi->position_in_fds_table].revents & POLLIN) {
155 * he's going to get serviced now, take him off the
156 * list of guys with buffered SSL. If he still has some
157 * at the end of the service, he'll get put back on the
160 lws_ssl_remove_wsi_from_buffered_list(wsi);
166 /* any socket with events to service? */
168 for (n = 0; n < context->fds_count; n++) {
170 if (!context->fds[n].revents)
173 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
174 if (read(context->fds[n].fd, &buf, 1) != 1)
175 lwsl_err("Cannot read from dummy pipe.");
179 m = lws_service_fd(context, &context->fds[n]);
182 /* if something closed, retry this slot */
191 lws_plat_set_socket_options(struct lws_context *context, int fd)
194 socklen_t optlen = sizeof(optval);
196 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
198 struct protoent *tcp_proto;
201 if (context->ka_time) {
202 /* enable keepalive on this socket */
204 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
205 (const void *)&optval, optlen) < 0)
208 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
209 defined(__CYGWIN__) || defined(__OpenBSD__)
212 * didn't find a way to set these per-socket, need to
213 * tune kernel systemwide values
216 /* set the keepalive conditions we want on it too */
217 optval = context->ka_time;
218 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
219 (const void *)&optval, optlen) < 0)
222 optval = context->ka_interval;
223 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
224 (const void *)&optval, optlen) < 0)
227 optval = context->ka_probes;
228 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
229 (const void *)&optval, optlen) < 0)
236 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
237 !defined(__OpenBSD__)
238 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
241 tcp_proto = getprotobyname("TCP");
242 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
246 /* We are nonblocking... */
247 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
254 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
256 if (info->uid != -1) {
257 struct passwd *p = getpwuid(info->uid);
260 initgroups(p->pw_name, info->gid);
261 if (setuid(info->uid))
262 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
264 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
266 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
269 if (setgid(info->gid))
270 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
274 static void sigpipe_handler(int x)
280 lws_plat_context_early_init(void)
284 signal(SIGUSR2, lws_sigusr2);
286 sigaddset(&mask, SIGUSR2);
288 sigprocmask(SIG_BLOCK, &mask, NULL);
290 signal(SIGPIPE, sigpipe_handler);
296 lws_plat_context_early_destroy(struct lws_context *context)
301 lws_plat_context_late_destroy(struct lws_context *context)
303 if (context->lws_lookup)
304 lws_free(context->lws_lookup);
306 close(context->dummy_pipe_fds[0]);
307 close(context->dummy_pipe_fds[1]);
308 close(context->fd_random);
311 /* cast a struct sockaddr_in6 * into addr for ipv6 */
314 interface_to_sa(struct lws_context *context,
315 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
322 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
326 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
330 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
332 if (strcmp(ifc->ifa_name, ifname))
335 switch (ifc->ifa_addr->sa_family) {
338 if (LWS_IPV6_ENABLED(context)) {
339 /* map IPv4 to IPv6 */
340 bzero((char *)&addr6->sin6_addr,
341 sizeof(struct in6_addr));
342 addr6->sin6_addr.s6_addr[10] = 0xff;
343 addr6->sin6_addr.s6_addr[11] = 0xff;
344 memcpy(&addr6->sin6_addr.s6_addr[12],
345 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
346 sizeof(struct in_addr));
350 (struct sockaddr_in *)ifc->ifa_addr,
351 sizeof(struct sockaddr_in));
355 memcpy(&addr6->sin6_addr,
356 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
357 sizeof(struct in6_addr));
369 /* check if bind to IP adddress */
371 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
375 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
383 lws_plat_insert_socket_into_fds(struct lws_context *context,
386 lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
387 context->fds[context->fds_count++].revents = 0;
391 lws_plat_delete_socket_from_fds(struct lws_context *context,
392 struct lws *wsi, int m)
397 lws_plat_service_periodic(struct lws_context *context)
399 /* if our parent went down, don't linger around */
400 if (context->started_with_parent &&
401 kill(context->started_with_parent, 0) < 0)
402 kill(getpid(), SIGTERM);
406 lws_plat_change_pollfd(struct lws_context *context,
407 struct lws *wsi, struct lws_pollfd *pfd)
412 LWS_VISIBLE const char *
413 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
415 return inet_ntop(af, src, dst, cnt);
418 static lws_filefd_type
419 _lws_plat_file_open(struct lws *wsi, const char *filename,
420 unsigned long *filelen, int flags)
422 struct stat stat_buf;
423 int ret = open(filename, flags, 0664);
426 return LWS_INVALID_FILE;
428 if (fstat(ret, &stat_buf) < 0) {
430 return LWS_INVALID_FILE;
432 *filelen = stat_buf.st_size;
437 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
443 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
445 return lseek(fd, offset, SEEK_CUR);
449 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
450 unsigned char *buf, unsigned long len)
454 n = read((int)fd, buf, len);
466 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
467 unsigned char *buf, unsigned long len)
471 n = write((int)fd, buf, len);
483 lws_plat_init(struct lws_context *context,
484 struct lws_context_creation_info *info)
486 context->lws_lookup = lws_zalloc(sizeof(struct lws *) * context->max_fds);
487 if (context->lws_lookup == NULL) {
489 "Unable to allocate lws_lookup array for %d connections\n",
494 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
495 if (context->fd_random < 0) {
496 lwsl_err("Unable to open random device %s %d\n",
497 SYSTEM_RANDOM_FILEPATH, context->fd_random);
501 if (lws_libev_init_fd_table(context))
502 /* libev handled it instead */
505 if (pipe(context->dummy_pipe_fds)) {
506 lwsl_err("Unable to create pipe\n");
510 /* use the read end of pipe as first item */
511 context->fds[0].fd = context->dummy_pipe_fds[0];
512 context->fds[0].events = LWS_POLLIN;
513 context->fds[0].revents = 0;
514 context->fds_count = 1;
516 context->fops.open = _lws_plat_file_open;
517 context->fops.close = _lws_plat_file_close;
518 context->fops.seek_cur = _lws_plat_file_seek_cur;
519 context->fops.read = _lws_plat_file_read;
520 context->fops.write = _lws_plat_file_write;