introduce-k-switch-defeat-masking.patch

Signed-off-by: Andy Green <andy@warmcat.com>
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 403a7f6..c88715e 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -635,7 +635,7 @@
 			       struct libwebsocket_protocols *protocols,
 			       const char *ssl_cert_filepath,
 			       const char *ssl_private_key_filepath,
-			       int gid, int uid)
+			       int gid, int uid, unsigned int options)
 {
 	int n;
 	int sockfd = 0;
@@ -662,6 +662,7 @@
 	this->listen_port = port;
 	this->http_proxy_port = 0;
 	this->http_proxy_address[0] = '\0';
+	this->options = options;
 
 	/* find canonical hostname */
 
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index 5700a04..e579303 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -24,6 +24,8 @@
 
 #define CONTEXT_PORT_NO_LISTEN 0
 
+#define LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK 1
+
 enum libwebsocket_callback_reasons {
 	LWS_CALLBACK_ESTABLISHED,
 	LWS_CALLBACK_CLIENT_ESTABLISHED,
@@ -174,7 +176,8 @@
 libwebsocket_create_context(int port,
 		  struct libwebsocket_protocols *protocols,
 		  const char *ssl_cert_filepath,
-		  const char *ssl_private_key_filepath, int gid, int uid);
+		  const char *ssl_private_key_filepath, int gid, int uid,
+		  unsigned int options);
 
 extern void
 libwebsocket_context_destroy(struct libwebsocket_context *this);
diff --git a/lib/parsers.c b/lib/parsers.c
index a97e75a..b1c45a0 100644
--- a/lib/parsers.c
+++ b/lib/parsers.c
@@ -223,6 +223,10 @@
 static inline unsigned char
 xor_mask(struct libwebsocket *wsi, unsigned char c)
 {
+	if (wsi->protocol->owning_server->options &
+					   LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK)
+		return c;
+	
 	c ^= wsi->masking_key_04[wsi->frame_mask_index++];
 	if (wsi->frame_mask_index == 20)
 		wsi->frame_mask_index = 0;
@@ -267,6 +271,10 @@
 	case LWS_RXPS_04_MASK_NONCE_3:
 		wsi->frame_masking_nonce_04[3] = c;
 
+		if (wsi->protocol->owning_server->options &
+					   LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK)
+			goto post_mask;
+
 		/*
 		 * we are able to compute the frame key now
 		 * it's a SHA1 of ( frame nonce we were just sent, concatenated
@@ -296,6 +304,7 @@
 
 		wsi->frame_mask_index = 0;
 
+post_mask:
 		wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
 		break;
 
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index 6a5df59..c2010f9 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -172,6 +172,7 @@
 	char http_proxy_address[256];
 	char canonical_hostname[1024];
 	unsigned int http_proxy_port;
+	unsigned int options;
 #ifdef LWS_OPENSSL_SUPPORT
 	int use_ssl;
 	SSL_CTX *ssl_ctx;