refactor and introduce without server configure option
Move server-only stuff into their own files and make building
that depend on not having --without-server on the configure
Make fragments in other places conditional as well
Remove client-related members from struct libwebscket when
building LWS_NO_CLIENT
Apps:
normal: build test server, client, fraggle, ping
--without-client: build test server
--without-server: build test client, ping
Signed-off-by: Andy Green <andy.green@linaro.org>
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 99ce9e8..000861c 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -128,49 +128,6 @@
return 0;
}
-#ifdef LWS_OPENSSL_SUPPORT
-static void
-libwebsockets_decode_ssl_error(void)
-{
- char buf[256];
- u_long err;
-
- while ((err = ERR_get_error()) != 0) {
- ERR_error_string_n(err, buf, sizeof(buf));
- lwsl_err("*** %s\n", buf);
- }
-}
-#endif
-
-
-static int
-interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
-{
- int rc = -1;
-#ifdef WIN32
- /* TODO */
-#else
- struct ifaddrs *ifr;
- struct ifaddrs *ifc;
- struct sockaddr_in *sin;
-
- getifaddrs(&ifr);
- for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
- if (strcmp(ifc->ifa_name, ifname))
- continue;
- if (ifc->ifa_addr == NULL)
- continue;
- sin = (struct sockaddr_in *)ifc->ifa_addr;
- if (sin->sin_family != AF_INET)
- continue;
- memcpy(addr, sin, addrlen);
- rc = 0;
- }
-
- freeifaddrs(ifr);
-#endif
- return rc;
-}
void
libwebsocket_close_and_free_session(struct libwebsocket_context *context,
@@ -367,10 +324,10 @@
for (n = 0; n < WSI_TOKEN_COUNT; n++)
if (wsi->utf8_token[n].token)
free(wsi->utf8_token[n].token);
-
+#ifndef LWS_NO_CLIENT
if (wsi->c_address)
free(wsi->c_address);
-
+#endif
if (wsi->rxflow_buffer)
free(wsi->rxflow_buffer);
@@ -703,54 +660,6 @@
}
}
-struct libwebsocket *
-libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
-{
- struct libwebsocket *new_wsi;
- int n;
-
- new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
- if (new_wsi == NULL) {
- lwsl_err("Out of memory for new connection\n");
- return NULL;
- }
-
- memset(new_wsi, 0, sizeof(struct libwebsocket));
- new_wsi->count_active_extensions = 0;
- new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
-
- /* intialize the instance struct */
-
- new_wsi->state = WSI_STATE_HTTP;
- new_wsi->name_buffer_pos = 0;
- new_wsi->mode = LWS_CONNMODE_HTTP_SERVING;
-
- for (n = 0; n < WSI_TOKEN_COUNT; n++) {
- new_wsi->utf8_token[n].token = NULL;
- new_wsi->utf8_token[n].token_len = 0;
- }
-
- /*
- * these can only be set once the protocol is known
- * we set an unestablished connection's protocol pointer
- * to the start of the supported list, so it can look
- * for matching ones during the handshake
- */
- new_wsi->protocol = context->protocols;
- new_wsi->user_space = NULL;
-
- /*
- * Default protocol is 76 / 00
- * After 76, there's a header specified to inform which
- * draft the client wants, when that's seen we modify
- * the individual connection's spec revision accordingly
- */
- new_wsi->ietf_spec_revision = 0;
-
- return new_wsi;
-}
-
-
/**
* libwebsocket_service_fd() - Service polled socket with something waiting
* @context: Websocket context
@@ -766,23 +675,20 @@
libwebsocket_service_fd(struct libwebsocket_context *context,
struct pollfd *pollfd)
{
+ struct libwebsocket *wsi;
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
- struct libwebsocket *wsi;
- struct libwebsocket *new_wsi;
int n;
int m;
- ssize_t len;
- int accept_fd;
- unsigned int clilen;
- struct sockaddr_in cli_addr;
struct timeval tv;
int more = 1;
struct lws_tokens eff_buf;
- int opt = 1;
#ifndef LWS_NO_CLIENT
extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
#endif
+#ifndef LWS_NO_SERVER
+ extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
+#endif
/*
* you can call us with pollfd = NULL to just allow the once-per-second
* global timeout checks; if less than a second since the last check
@@ -856,278 +762,13 @@
switch (wsi->mode) {
+#ifndef LWS_NO_SERVER
case LWS_CONNMODE_HTTP_SERVING:
-
- /* handle http headers coming in */
-
- /* any incoming data ready? */
-
- if (pollfd->revents & POLLIN) {
-
- #ifdef LWS_OPENSSL_SUPPORT
- if (wsi->ssl)
- len = SSL_read(wsi->ssl, buf, sizeof buf);
- else
- #endif
- len = recv(pollfd->fd, buf, sizeof buf, 0);
-
- if (len < 0) {
- lwsl_debug("Socket read returned %d\n", len);
- if (errno != EINTR && errno != EAGAIN)
- libwebsocket_close_and_free_session(context,
- wsi, LWS_CLOSE_STATUS_NOSTATUS);
- return 0;
- }
- if (!len) {
- libwebsocket_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NOSTATUS);
- return 0;
- }
-
- n = libwebsocket_read(context, wsi, buf, len);
- if (n < 0)
- /* we closed wsi */
- return 0;
- }
-
- /* this handles POLLOUT for http serving fragments */
-
- if (!(pollfd->revents & POLLOUT))
- break;
-
- /* one shot */
- pollfd->events &= ~POLLOUT;
-
- if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE)
- break;
-
- if (libwebsockets_serve_http_file_fragment(context, wsi) < 0)
- libwebsocket_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NOSTATUS);
- else
- if (wsi->state == WSI_STATE_HTTP && wsi->protocol->callback)
- if (user_callback_handle_rxflow(wsi->protocol->callback, context, wsi, LWS_CALLBACK_HTTP_FILE_COMPLETION, wsi->user_space,
- wsi->filepath, wsi->filepos))
- libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
- break;
-
case LWS_CONNMODE_SERVER_LISTENER:
-
- /* pollin means a client has connected to us then */
-
- if (!(pollfd->revents & POLLIN))
- break;
-
- /* listen socket got an unencrypted connection... */
-
- clilen = sizeof(cli_addr);
- accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
- &clilen);
- if (accept_fd < 0) {
- lwsl_warn("ERROR on accept: %s\n", strerror(errno));
- break;
- }
-
- /* Disable Nagle */
- opt = 1;
- setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY,
- (const void *)&opt, sizeof(opt));
-
- /*
- * look at who we connected to and give user code a chance
- * to reject based on client IP. There's no protocol selected
- * yet so we issue this to protocols[0]
- */
-
- if ((context->protocols[0].callback)(context, wsi,
- LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
- (void *)(long)accept_fd, NULL, 0)) {
- lwsl_debug("Callback denied network connection\n");
- compatible_close(accept_fd);
- break;
- }
-
- new_wsi = libwebsocket_create_new_server_wsi(context);
- if (new_wsi == NULL) {
- compatible_close(accept_fd);
- break;
- }
-
- new_wsi->sock = accept_fd;
-
-
-#ifdef LWS_OPENSSL_SUPPORT
- new_wsi->ssl = NULL;
-
- if (context->use_ssl) {
-
- new_wsi->ssl = SSL_new(context->ssl_ctx);
- if (new_wsi->ssl == NULL) {
- lwsl_err("SSL_new failed: %s\n",
- ERR_error_string(SSL_get_error(
- new_wsi->ssl, 0), NULL));
- libwebsockets_decode_ssl_error();
- free(new_wsi);
- compatible_close(accept_fd);
- break;
- }
-
- SSL_set_ex_data(new_wsi->ssl,
- openssl_websocket_private_data_index, context);
-
- SSL_set_fd(new_wsi->ssl, accept_fd);
-
- n = SSL_accept(new_wsi->ssl);
- if (n != 1) {
- /*
- * browsers seem to probe with various
- * ssl params which fail then retry
- * and succeed
- */
- lwsl_debug("SSL_accept failed skt %u: %s\n",
- pollfd->fd,
- ERR_error_string(SSL_get_error(
- new_wsi->ssl, n), NULL));
- SSL_free(
- new_wsi->ssl);
- free(new_wsi);
- compatible_close(accept_fd);
- break;
- }
-
- lwsl_debug("accepted new SSL conn "
- "port %u on fd=%d SSL ver %s\n",
- ntohs(cli_addr.sin_port), accept_fd,
- SSL_get_version(new_wsi->ssl));
-
- } else
-#endif
- lwsl_debug("accepted new conn port %u on fd=%d\n",
- ntohs(cli_addr.sin_port), accept_fd);
-
- insert_wsi_socket_into_fds(context, new_wsi);
- break;
-
case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
-
- /* as we are listening, POLLIN means accept() is needed */
-
- if (!(pollfd->revents & POLLIN))
- break;
-
- /* listen socket got an unencrypted connection... */
-
- clilen = sizeof(cli_addr);
- accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
- &clilen);
- if (accept_fd < 0) {
- lwsl_warn("ERROR on accept %d\n", accept_fd);
- return 0;
- }
-
- /* create a dummy wsi for the connection and add it */
-
- new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
- if (new_wsi == NULL) {
- lwsl_err("Out of mem\n");
- goto bail_prox_listener;
- }
- memset(new_wsi, 0, sizeof (struct libwebsocket));
- new_wsi->sock = accept_fd;
- new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY;
- new_wsi->state = WSI_STATE_ESTABLISHED;
- new_wsi->count_active_extensions = 0;
- /* note which protocol we are proxying */
- new_wsi->protocol_index_for_broadcast_proxy =
- wsi->protocol_index_for_broadcast_proxy;
-
- insert_wsi_socket_into_fds(context, new_wsi);
- break;
-
-bail_prox_listener:
- compatible_close(accept_fd);
- break;
-
case LWS_CONNMODE_BROADCAST_PROXY:
-
- /* handle session socket closed */
-
- if (pollfd->revents & (POLLERR | POLLHUP)) {
-
- lwsl_debug("Session Socket %p (fd=%d) dead\n",
- (void *)wsi, pollfd->fd);
-
- libwebsocket_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NORMAL);
- return 0;
- }
-
- /*
- * either extension code with stuff to spill, or the user code,
- * requested a callback when it was OK to write
- */
-
- if (pollfd->revents & POLLOUT)
- if (lws_handle_POLLOUT_event(context, wsi,
- pollfd) < 0) {
- libwebsocket_close_and_free_session(
- context, wsi, LWS_CLOSE_STATUS_NORMAL);
- return 0;
- }
-
- /* any incoming data ready? */
-
- if (!(pollfd->revents & POLLIN))
- break;
-
- /* get the issued broadcast payload from the socket */
-
- len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING,
- MAX_BROADCAST_PAYLOAD);
- if (len < 0) {
- lwsl_err("Error reading broadcast payload\n");
- break;
- }
-
- /* broadcast it to all guys with this protocol index */
-
- for (n = 0; n < context->fds_count; n++) {
-
- new_wsi = context->lws_lookup[context->fds[n].fd];
- if (new_wsi == NULL)
- continue;
-
- /* only to clients we are serving to */
-
- if (new_wsi->mode != LWS_CONNMODE_WS_SERVING)
- continue;
-
- /*
- * never broadcast to non-established
- * connection
- */
-
- if (new_wsi->state != WSI_STATE_ESTABLISHED)
- continue;
-
- /*
- * only broadcast to connections using
- * the requested protocol
- */
-
- if (new_wsi->protocol->protocol_index !=
- wsi->protocol_index_for_broadcast_proxy)
- continue;
-
- /* broadcast it to this connection */
-
- user_callback_handle_rxflow(new_wsi->protocol->callback, context, new_wsi,
- LWS_CALLBACK_BROADCAST,
- new_wsi->user_space,
- buf + LWS_SEND_BUFFER_PRE_PADDING, len);
- }
- break;
-
+ return lws_server_socket_service(context, wsi, pollfd);
+#endif
case LWS_CONNMODE_WS_SERVING:
case LWS_CONNMODE_WS_CLIENT:
@@ -1535,7 +1176,13 @@
return wsi->sock;
}
-
+#ifdef LWS_NO_SERVER
+int
+_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
+{
+ return 0;
+}
+#else
int
_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
{
@@ -1579,6 +1226,7 @@
return 1;
}
+#endif
/**
* libwebsocket_rx_flow_control() - Enable and disable socket servicing for
@@ -1736,17 +1384,13 @@
{
int n;
int m;
- int sockfd = 0;
int fd;
struct sockaddr_in serv_addr, cli_addr;
int opt = 1;
struct libwebsocket_context *context = NULL;
unsigned int slen;
char *p;
- char hostname[1024] = "";
-// struct hostent *he;
struct libwebsocket *wsi;
- struct sockaddr sa;
#ifdef LWS_OPENSSL_SUPPORT
SSL_METHOD *method;
@@ -1848,11 +1492,12 @@
openssl_websocket_private_data_index = 0;
#endif
- if (options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME) {
+ strcpy(context->canonical_hostname, "unknown");
- strcpy(context->canonical_hostname, "unknown");
-
- } else {
+#ifndef LWS_NO_SERVER
+ if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
+ struct sockaddr sa;
+ char hostname[1024] = "";
/* find canonical hostname */
@@ -1880,9 +1525,9 @@
strncpy(context->canonical_hostname, hostname,
sizeof context->canonical_hostname - 1);
- // lwsl_debug("context->canonical_hostname = %s\n",
- // context->canonical_hostname);
+ lwsl_info(" canonical_hostname = %s\n", context->canonical_hostname);
}
+#endif
/* split the proxy ads:port if given */
@@ -1901,11 +1546,12 @@
*p = '\0';
context->http_proxy_port = atoi(p + 1);
- lwsl_debug("Using proxy %s:%u\n",
+ lwsl_info(" Proxy %s:%u\n",
context->http_proxy_address,
context->http_proxy_port);
}
+#ifndef LWS_NO_SERVER
if (port) {
#ifdef LWS_OPENSSL_SUPPORT
@@ -1926,6 +1572,7 @@
"serving unencrypted\n");
#endif
}
+#endif
/* ignore SIGPIPE */
#ifdef WIN32
@@ -1970,6 +1617,8 @@
SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
+#ifndef LWS_NO_CLIENT
+
/* client context */
if (port == CONTEXT_PORT_NO_LISTEN) {
@@ -2021,6 +1670,7 @@
LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
context->ssl_client_ctx, NULL, 0);
}
+#endif
/* as a server, are we requiring clients to identify themselves? */
@@ -2078,9 +1728,12 @@
if (lws_b64_selftest())
return NULL;
+#ifndef LWS_NO_SERVER
/* set up our external listening socket we serve on */
if (port) {
+ extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
+ int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
@@ -2135,6 +1788,7 @@
listen(sockfd, LWS_SOMAXCONN);
lwsl_info(" Listening on port %d\n", port);
}
+#endif
/*
* drop any root privs for this process