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 ((unsigned long long)tv.tv_sec * 1000000LL) + 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)
100 #ifdef LWS_OPENSSL_SUPPORT
101 struct libwebsocket *wsi, *wsi_next;
104 /* stay dead once we are dead */
109 lws_libev_run(context);
111 context->service_tid = context->protocols[0].callback(context, NULL,
112 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
114 #ifdef LWS_OPENSSL_SUPPORT
115 /* if we know we have non-network pending data, do not wait in poll */
116 if (lws_ssl_anybody_has_buffered_read(context))
119 n = poll(context->fds, context->fds_count, timeout_ms);
120 context->service_tid = 0;
122 #ifdef LWS_OPENSSL_SUPPORT
123 if (!lws_ssl_anybody_has_buffered_read(context) && n == 0) {
125 if (n == 0) /* poll timeout */ {
127 libwebsocket_service_fd(context, NULL);
132 if (LWS_ERRNO != LWS_EINTR)
137 #ifdef LWS_OPENSSL_SUPPORT
139 * For all guys with buffered SSL read data already saved up, if they
140 * are not flowcontrolled, fake their POLLIN status so they'll get
141 * service to use up the buffered incoming data, even though their
142 * network socket may have nothing
145 wsi = context->pending_read_list;
147 wsi_next = wsi->pending_read_list_next;
148 context->fds[wsi->sock].revents |=
149 context->fds[wsi->sock].events & POLLIN;
150 if (context->fds[wsi->sock].revents & POLLIN) {
152 * he's going to get serviced now, take him off the
153 * list of guys with buffered SSL. If he still has some
154 * at the end of the service, he'll get put back on the
157 lws_ssl_remove_wsi_from_buffered_list(context, wsi);
163 /* any socket with events to service? */
165 for (n = 0; n < context->fds_count; n++) {
167 if (!context->fds[n].revents)
170 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
171 if (read(context->fds[n].fd, &buf, 1) != 1)
172 lwsl_err("Cannot read from dummy pipe.");
176 m = libwebsocket_service_fd(context, &context->fds[n]);
179 /* if something closed, retry this slot */
188 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
191 socklen_t optlen = sizeof(optval);
193 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
195 struct protoent *tcp_proto;
198 if (context->ka_time) {
199 /* enable keepalive on this socket */
201 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
202 (const void *)&optval, optlen) < 0)
205 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
206 defined(__CYGWIN__) || defined(__OpenBSD__)
209 * didn't find a way to set these per-socket, need to
210 * tune kernel systemwide values
213 /* set the keepalive conditions we want on it too */
214 optval = context->ka_time;
215 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
216 (const void *)&optval, optlen) < 0)
219 optval = context->ka_interval;
220 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
221 (const void *)&optval, optlen) < 0)
224 optval = context->ka_probes;
225 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
226 (const void *)&optval, optlen) < 0)
233 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
234 !defined(__OpenBSD__)
235 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
238 tcp_proto = getprotobyname("TCP");
239 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
243 /* We are nonblocking... */
244 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
251 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
254 if (setgid(info->gid))
255 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
257 if (setuid(info->uid))
258 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
262 lws_plat_init_fd_tables(struct libwebsocket_context *context)
264 if (lws_libev_init_fd_table(context))
265 /* libev handled it instead */
268 if (pipe(context->dummy_pipe_fds)) {
269 lwsl_err("Unable to create pipe\n");
273 /* use the read end of pipe as first item */
274 context->fds[0].fd = context->dummy_pipe_fds[0];
275 context->fds[0].events = LWS_POLLIN;
276 context->fds[0].revents = 0;
277 context->fds_count = 1;
279 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
280 if (context->fd_random < 0) {
281 lwsl_err("Unable to open random device %s %d\n",
282 SYSTEM_RANDOM_FILEPATH, context->fd_random);
289 static void sigpipe_handler(int x)
295 lws_plat_context_early_init(void)
299 signal(SIGUSR2, lws_sigusr2);
301 sigaddset(&mask, SIGUSR2);
303 sigprocmask(SIG_BLOCK, &mask, NULL);
305 signal(SIGPIPE, sigpipe_handler);
311 lws_plat_context_early_destroy(struct libwebsocket_context *context)
316 lws_plat_context_late_destroy(struct libwebsocket_context *context)
318 close(context->dummy_pipe_fds[0]);
319 close(context->dummy_pipe_fds[1]);
320 close(context->fd_random);
323 /* cast a struct sockaddr_in6 * into addr for ipv6 */
326 interface_to_sa(struct libwebsocket_context *context,
327 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
334 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
338 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
342 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
344 if (strcmp(ifc->ifa_name, ifname))
347 switch (ifc->ifa_addr->sa_family) {
350 if (LWS_IPV6_ENABLED(context)) {
351 /* map IPv4 to IPv6 */
352 bzero((char *)&addr6->sin6_addr,
353 sizeof(struct in6_addr));
354 addr6->sin6_addr.s6_addr[10] = 0xff;
355 addr6->sin6_addr.s6_addr[11] = 0xff;
356 memcpy(&addr6->sin6_addr.s6_addr[12],
357 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
358 sizeof(struct in_addr));
362 (struct sockaddr_in *)ifc->ifa_addr,
363 sizeof(struct sockaddr_in));
367 memcpy(&addr6->sin6_addr,
368 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
369 sizeof(struct in6_addr));
381 /* check if bind to IP adddress */
383 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
387 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
395 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
396 struct libwebsocket *wsi)
398 lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
399 context->fds[context->fds_count++].revents = 0;
403 lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
404 struct libwebsocket *wsi, int m)
409 lws_plat_service_periodic(struct libwebsocket_context *context)
411 /* if our parent went down, don't linger around */
412 if (context->started_with_parent &&
413 kill(context->started_with_parent, 0) < 0)
414 kill(getpid(), SIGTERM);
418 lws_plat_change_pollfd(struct libwebsocket_context *context,
419 struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
425 lws_plat_open_file(const char* filename, unsigned long* filelen)
427 struct stat stat_buf;
428 int ret = open(filename, O_RDONLY);
431 return LWS_INVALID_FILE;
433 if (fstat(ret, &stat_buf) < 0) {
435 return LWS_INVALID_FILE;
437 *filelen = stat_buf.st_size;
442 LWS_VISIBLE const char *
443 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
445 return inet_ntop(af, src, dst, cnt);