1 #include "private-libwebsockets.h"
4 * included from libwebsockets.c for unix builds
7 unsigned long long time_in_microseconds(void)
10 gettimeofday(&tv, NULL);
11 return (tv.tv_sec * 1000000) + tv.tv_usec;
14 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
17 return read(context->fd_random, (char *)buf, len);
20 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
22 struct libwebsocket_pollfd fds;
24 /* treat the fact we got a truncated send pending as if we're choked */
25 if (wsi->truncated_send_len)
32 if (poll(&fds, 1, 0) != 1)
35 if ((fds.revents & POLLOUT) == 0)
38 /* okay to send another packet without blocking */
44 lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
46 return poll(fd, 1, 0);
50 * This is just used to interrupt poll waiting
51 * we don't have to do anything with it.
53 static void lws_sigusr2(int sig)
58 * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
59 * @context: Websocket context
61 * This function let a call to libwebsocket_service() waiting for a timeout
65 libwebsocket_cancel_service(struct libwebsocket_context *context)
69 if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
70 lwsl_err("Cannot write to dummy pipe");
73 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
75 int syslog_level = LOG_DEBUG;
79 syslog_level = LOG_ERR;
82 syslog_level = LOG_WARNING;
85 syslog_level = LOG_NOTICE;
88 syslog_level = LOG_INFO;
91 syslog(syslog_level, "%s", line);
95 lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
101 /* stay dead once we are dead */
106 lws_libev_run(context);
108 context->service_tid = context->protocols[0].callback(context, NULL,
109 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
111 #ifdef LWS_OPENSSL_SUPPORT
112 /* if we know we have non-network pending data, do not wait in poll */
113 if (context->ssl_flag_buffered_reads)
116 n = poll(context->fds, context->fds_count, timeout_ms);
117 context->service_tid = 0;
119 #ifdef LWS_OPENSSL_SUPPORT
120 if (!context->ssl_flag_buffered_reads && n == 0) {
122 if (n == 0) /* poll timeout */ {
124 libwebsocket_service_fd(context, NULL);
128 #ifdef LWS_OPENSSL_SUPPORT
129 /* any more will have to set it fresh this time around */
130 context->ssl_flag_buffered_reads = 0;
134 if (LWS_ERRNO != LWS_EINTR)
139 /* any socket with events to service? */
141 for (n = 0; n < context->fds_count; n++) {
142 #ifdef LWS_OPENSSL_SUPPORT
143 struct libwebsocket *wsi;
145 wsi = context->lws_lookup[context->fds[n].fd];
149 * if he's not flowcontrolled, make sure we service ssl
152 if (wsi->ssl && wsi->buffered_reads_pending) {
153 lwsl_debug("wsi %p: forcing POLLIN\n", wsi);
154 context->fds[n].revents |= context->fds[n].events & POLLIN;
155 if (context->fds[n].revents & POLLIN)
156 wsi->buffered_reads_pending = 0;
158 /* somebody left with pending SSL read data */
159 context->ssl_flag_buffered_reads = 1;
162 if (!context->fds[n].revents)
165 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
166 if (read(context->fds[n].fd, &buf, 1) != 1)
167 lwsl_err("Cannot read from dummy pipe.");
171 m = libwebsocket_service_fd(context, &context->fds[n]);
174 /* if something closed, retry this slot */
183 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
186 socklen_t optlen = sizeof(optval);
188 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
190 struct protoent *tcp_proto;
193 if (context->ka_time) {
194 /* enable keepalive on this socket */
196 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
197 (const void *)&optval, optlen) < 0)
200 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
201 defined(__CYGWIN__) || defined(__OpenBSD__)
204 * didn't find a way to set these per-socket, need to
205 * tune kernel systemwide values
208 /* set the keepalive conditions we want on it too */
209 optval = context->ka_time;
210 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
211 (const void *)&optval, optlen) < 0)
214 optval = context->ka_interval;
215 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
216 (const void *)&optval, optlen) < 0)
219 optval = context->ka_probes;
220 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
221 (const void *)&optval, optlen) < 0)
228 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
229 !defined(__OpenBSD__)
230 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
232 tcp_proto = getprotobyname("TCP");
233 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
236 /* We are nonblocking... */
237 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
244 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
247 if (setgid(info->gid))
248 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
250 if (setuid(info->uid))
251 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
255 lws_plat_init_fd_tables(struct libwebsocket_context *context)
257 if (lws_libev_init_fd_table(context))
258 /* libev handled it instead */
261 if (pipe(context->dummy_pipe_fds)) {
262 lwsl_err("Unable to create pipe\n");
266 /* use the read end of pipe as first item */
267 context->fds[0].fd = context->dummy_pipe_fds[0];
268 context->fds[0].events = LWS_POLLIN;
269 context->fds[0].revents = 0;
270 context->fds_count = 1;
272 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
273 if (context->fd_random < 0) {
274 lwsl_err("Unable to open random device %s %d\n",
275 SYSTEM_RANDOM_FILEPATH, context->fd_random);
282 static void sigpipe_handler(int x)
288 lws_plat_context_early_init(void)
292 signal(SIGUSR2, lws_sigusr2);
294 sigaddset(&mask, SIGUSR2);
296 sigprocmask(SIG_BLOCK, &mask, NULL);
298 signal(SIGPIPE, sigpipe_handler);
304 lws_plat_context_early_destroy(struct libwebsocket_context *context)
309 lws_plat_context_late_destroy(struct libwebsocket_context *context)
311 close(context->dummy_pipe_fds[0]);
312 close(context->dummy_pipe_fds[1]);
313 close(context->fd_random);
316 /* cast a struct sockaddr_in6 * into addr for ipv6 */
319 interface_to_sa(struct libwebsocket_context *context,
320 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
327 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
331 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
335 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
337 if (strcmp(ifc->ifa_name, ifname))
340 switch (ifc->ifa_addr->sa_family) {
343 if (LWS_IPV6_ENABLED(context)) {
344 /* map IPv4 to IPv6 */
345 bzero((char *)&addr6->sin6_addr,
346 sizeof(struct in6_addr));
347 addr6->sin6_addr.s6_addr[10] = 0xff;
348 addr6->sin6_addr.s6_addr[11] = 0xff;
349 memcpy(&addr6->sin6_addr.s6_addr[12],
350 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
351 sizeof(struct in_addr));
355 (struct sockaddr_in *)ifc->ifa_addr,
356 sizeof(struct sockaddr_in));
360 memcpy(&addr6->sin6_addr,
361 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
362 sizeof(struct in6_addr));
374 /* check if bind to IP adddress */
376 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
380 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
388 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
389 struct libwebsocket *wsi)
391 lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
392 context->fds[context->fds_count++].revents = 0;
396 lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
397 struct libwebsocket *wsi, int m)
402 lws_plat_service_periodic(struct libwebsocket_context *context)
404 /* if our parent went down, don't linger around */
405 if (context->started_with_parent &&
406 kill(context->started_with_parent, 0) < 0)
407 kill(getpid(), SIGTERM);
411 lws_plat_change_pollfd(struct libwebsocket_context *context,
412 struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
418 lws_plat_open_file(const char* filename, unsigned long* filelen)
420 struct stat stat_buf;
421 int ret = open(filename, O_RDONLY);
424 return LWS_INVALID_FILE;
426 if (fstat(ret, &stat_buf) < 0) {
428 return LWS_INVALID_FILE;
430 *filelen = stat_buf.st_size;
435 LWS_VISIBLE const char *
436 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
438 return inet_ntop(af, src, dst, cnt);