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)
104 #ifdef LWS_OPENSSL_SUPPORT
105 struct lws *wsi, *wsi_next;
108 /* stay dead once we are dead */
113 lws_libev_run(context);
115 if (!context->service_tid_detected) {
118 memset(&_lws, 0, sizeof(_lws));
119 _lws.context = context;
121 context->service_tid_detected = context->protocols[0].callback(
122 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
124 context->service_tid = context->service_tid_detected;
126 #ifdef LWS_OPENSSL_SUPPORT
127 /* if we know we have non-network pending data, do not wait in poll */
128 if (lws_ssl_anybody_has_buffered_read(context))
131 n = poll(context->fds, context->fds_count, timeout_ms);
132 context->service_tid = 0;
134 #ifdef LWS_OPENSSL_SUPPORT
135 if (!lws_ssl_anybody_has_buffered_read(context) && n == 0) {
137 if (n == 0) /* poll timeout */ {
139 lws_service_fd(context, NULL);
144 if (LWS_ERRNO != LWS_EINTR)
149 #ifdef LWS_OPENSSL_SUPPORT
151 * For all guys with buffered SSL read data already saved up, if they
152 * are not flowcontrolled, fake their POLLIN status so they'll get
153 * service to use up the buffered incoming data, even though their
154 * network socket may have nothing
157 wsi = context->pending_read_list;
159 wsi_next = wsi->pending_read_list_next;
160 context->fds[wsi->position_in_fds_table].revents |=
161 context->fds[wsi->position_in_fds_table].events & POLLIN;
162 if (context->fds[wsi->position_in_fds_table].revents & POLLIN)
164 * he's going to get serviced now, take him off the
165 * list of guys with buffered SSL. If he still has some
166 * at the end of the service, he'll get put back on the
169 lws_ssl_remove_wsi_from_buffered_list(wsi);
175 /* any socket with events to service? */
177 for (n = 0; n < context->fds_count; n++) {
178 if (!context->fds[n].revents)
181 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
182 if (read(context->fds[n].fd, &buf, 1) != 1)
183 lwsl_err("Cannot read from dummy pipe.");
187 m = lws_service_fd(context, &context->fds[n]);
190 /* if something closed, retry this slot */
199 lws_plat_set_socket_options(struct lws_context *context, int fd)
202 socklen_t optlen = sizeof(optval);
204 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
206 struct protoent *tcp_proto;
209 if (context->ka_time) {
210 /* enable keepalive on this socket */
212 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
213 (const void *)&optval, optlen) < 0)
216 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
217 defined(__CYGWIN__) || defined(__OpenBSD__)
220 * didn't find a way to set these per-socket, need to
221 * tune kernel systemwide values
224 /* set the keepalive conditions we want on it too */
225 optval = context->ka_time;
226 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
227 (const void *)&optval, optlen) < 0)
230 optval = context->ka_interval;
231 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
232 (const void *)&optval, optlen) < 0)
235 optval = context->ka_probes;
236 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
237 (const void *)&optval, optlen) < 0)
244 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
245 !defined(__OpenBSD__)
246 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
249 tcp_proto = getprotobyname("TCP");
250 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
254 /* We are nonblocking... */
255 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
262 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
264 if (info->uid != -1) {
265 struct passwd *p = getpwuid(info->uid);
268 initgroups(p->pw_name, info->gid);
269 if (setuid(info->uid))
270 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
272 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
274 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
277 if (setgid(info->gid))
278 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
282 static void sigpipe_handler(int x)
287 lws_plat_context_early_init(void)
291 signal(SIGUSR2, lws_sigusr2);
293 sigaddset(&mask, SIGUSR2);
295 sigprocmask(SIG_BLOCK, &mask, NULL);
297 signal(SIGPIPE, sigpipe_handler);
303 lws_plat_context_early_destroy(struct lws_context *context)
308 lws_plat_context_late_destroy(struct lws_context *context)
310 if (context->lws_lookup)
311 lws_free(context->lws_lookup);
313 close(context->dummy_pipe_fds[0]);
314 close(context->dummy_pipe_fds[1]);
315 close(context->fd_random);
318 /* cast a struct sockaddr_in6 * into addr for ipv6 */
321 interface_to_sa(struct lws_context *context,
322 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
329 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
333 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
337 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
339 if (strcmp(ifc->ifa_name, ifname))
342 switch (ifc->ifa_addr->sa_family) {
345 if (LWS_IPV6_ENABLED(context)) {
346 /* map IPv4 to IPv6 */
347 bzero((char *)&addr6->sin6_addr,
348 sizeof(struct in6_addr));
349 addr6->sin6_addr.s6_addr[10] = 0xff;
350 addr6->sin6_addr.s6_addr[11] = 0xff;
351 memcpy(&addr6->sin6_addr.s6_addr[12],
352 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
353 sizeof(struct in_addr));
357 (struct sockaddr_in *)ifc->ifa_addr,
358 sizeof(struct sockaddr_in));
362 memcpy(&addr6->sin6_addr,
363 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
364 sizeof(struct in6_addr));
376 /* check if bind to IP adddress */
378 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
382 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
390 lws_plat_insert_socket_into_fds(struct lws_context *context,
393 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
394 context->fds[context->fds_count++].revents = 0;
398 lws_plat_delete_socket_from_fds(struct lws_context *context,
399 struct lws *wsi, int m)
404 lws_plat_service_periodic(struct lws_context *context)
406 /* if our parent went down, don't linger around */
407 if (context->started_with_parent &&
408 kill(context->started_with_parent, 0) < 0)
409 kill(getpid(), SIGTERM);
413 lws_plat_change_pollfd(struct lws_context *context,
414 struct lws *wsi, struct lws_pollfd *pfd)
419 LWS_VISIBLE const char *
420 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
422 return inet_ntop(af, src, dst, cnt);
425 static lws_filefd_type
426 _lws_plat_file_open(struct lws *wsi, const char *filename,
427 unsigned long *filelen, int flags)
429 struct stat stat_buf;
430 int ret = open(filename, flags, 0664);
433 return LWS_INVALID_FILE;
435 if (fstat(ret, &stat_buf) < 0) {
437 return LWS_INVALID_FILE;
439 *filelen = stat_buf.st_size;
444 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
450 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
452 return lseek(fd, offset, SEEK_CUR);
456 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
457 unsigned char *buf, unsigned long len)
461 n = read((int)fd, buf, len);
473 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
474 unsigned char *buf, unsigned long len)
478 n = write((int)fd, buf, len);
490 lws_plat_init(struct lws_context *context,
491 struct lws_context_creation_info *info)
493 context->lws_lookup = lws_zalloc(sizeof(struct lws *) * context->max_fds);
494 if (context->lws_lookup == NULL) {
496 "Unable to allocate lws_lookup array for %d connections\n",
501 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
502 if (context->fd_random < 0) {
503 lwsl_err("Unable to open random device %s %d\n",
504 SYSTEM_RANDOM_FILEPATH, context->fd_random);
508 if (!lws_libev_init_fd_table(context)) {
509 /* otherwise libev handled it instead */
511 if (pipe(context->dummy_pipe_fds)) {
512 lwsl_err("Unable to create pipe\n");
517 /* use the read end of pipe as first item */
518 context->fds[0].fd = context->dummy_pipe_fds[0];
519 context->fds[0].events = LWS_POLLIN;
520 context->fds[0].revents = 0;
521 context->fds_count = 1;
523 context->fops.open = _lws_plat_file_open;
524 context->fops.close = _lws_plat_file_close;
525 context->fops.seek_cur = _lws_plat_file_seek_cur;
526 context->fops.read = _lws_plat_file_read;
527 context->fops.write = _lws_plat_file_write;