update ping test client and stop exposing payload to extensions

Ping and Pong payload in control messages need to be
above the fray of extension payload munging

Signed-off-by: Andy Green <andy.green@linaro.org>
diff --git a/lib/client-parser.c b/lib/client-parser.c
index 72e91b2..ee87d71 100644
--- a/lib/client-parser.c
+++ b/lib/client-parser.c
@@ -34,7 +34,7 @@
 	struct lws_tokens eff_buf;
 	int m;
 
-	lwsl_parser(" CRX: %02X %d\n", c, wsi->lws_rx_parse_state);
+//	lwsl_parser(" CRX: %02X %d\n", c, wsi->lws_rx_parse_state);
 
 	switch (wsi->lws_rx_parse_state) {
 	case LWS_RXPS_NEW:
@@ -401,6 +401,7 @@
 			return -1;
 
 		case LWS_WS_OPCODE_07__PING:
+			lwsl_info("client received ping, doing pong\n");
 			/* parrot the ping packet payload back as a pong*/
 			n = libwebsocket_write(wsi, (unsigned char *)
 			    &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
@@ -409,6 +410,9 @@
 			break;
 
 		case LWS_WS_OPCODE_07__PONG:
+			lwsl_info("client receied pong\n");
+			lwsl_hexdump(&wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
+				    wsi->rx_user_buffer_head);
 			/* keep the statistics... */
 			wsi->pings_vs_pongs--;
 
@@ -486,7 +490,9 @@
 		if (eff_buf.token_len > 0) {
 			eff_buf.token[eff_buf.token_len] = '\0';
 
-			if (wsi->protocol->callback)
+			if (wsi->protocol->callback) {
+				if (callback_action == LWS_CALLBACK_CLIENT_RECEIVE_PONG)
+					lwsl_info("Client doing pong callback\n");
 				wsi->protocol->callback(
 						wsi->protocol->owning_server,
 						wsi,
@@ -494,6 +500,7 @@
 						wsi->user_space,
 						eff_buf.token,
 						eff_buf.token_len);
+			}
 		}
 already_done:
 		wsi->rx_user_buffer_head = 0;
diff --git a/lib/handshake.c b/lib/handshake.c
index 634ccc8..d3ce3c8 100644
--- a/lib/handshake.c
+++ b/lib/handshake.c
@@ -562,6 +562,9 @@
 #endif
 
 #ifndef LWS_NO_CLIENT
+
+//		lwsl_info("mode=%d\n", wsi->mode);
+
 		switch (wsi->mode) {
 		case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
 		case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index df90289..68c14e6 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -306,9 +306,11 @@
 
 	/* delete it from the internal poll list if still present */
 
+	m = 0;
 	for (n = 0; n < context->fds_count; n++) {
 		if (context->fds[n].fd != wsi->sock)
 			continue;
+		m = 1;
 		while (n < context->fds_count - 1) {
 			context->fds[n] = context->fds[n + 1];
 			n++;
@@ -318,6 +320,9 @@
 		n = context->fds_count;
 	}
 
+	if (!m)
+		lwsl_err("Failed to remove fd %d from fds array\n", wsi->sock);
+
 	/* remove also from external POLL support via protocol 0 */
 	if (wsi->sock)
 		context->protocols[0].callback(context, wsi,
diff --git a/lib/output.c b/lib/output.c
index 8e72b7d..671e78c 100644
--- a/lib/output.c
+++ b/lib/output.c
@@ -312,14 +312,17 @@
 	eff_buf.token = (char *)buf;
 	eff_buf.token_len = len;
 
-	for (n = 0; n < wsi->count_active_extensions; n++) {
-		m = wsi->active_extensions[n]->callback(
-			wsi->protocol->owning_server,
-			wsi->active_extensions[n], wsi,
-			LWS_EXT_CALLBACK_PAYLOAD_TX,
-			wsi->active_extensions_user[n], &eff_buf, 0);
-		if (m < 0)
-			return -1;
+	if (protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) {
+
+		for (n = 0; n < wsi->count_active_extensions; n++) {
+			m = wsi->active_extensions[n]->callback(
+				wsi->protocol->owning_server,
+				wsi->active_extensions[n], wsi,
+				LWS_EXT_CALLBACK_PAYLOAD_TX,
+				wsi->active_extensions_user[n], &eff_buf, 0);
+			if (m < 0)
+				return -1;
+		}
 	}
 
 	buf = (unsigned char *)eff_buf.token;
@@ -586,7 +589,7 @@
 	lwsl_debug("\n");
 #endif
 
-	if (protocol == LWS_WRITE_HTTP) {
+	if (protocol == LWS_WRITE_HTTP || protocol == LWS_WRITE_PONG || protocol == LWS_WRITE_PING) {
 		if (lws_issue_raw(wsi, (unsigned char *)buf - pre,
 							      len + pre + post))
 			return -1;
diff --git a/lib/parsers.c b/lib/parsers.c
index 98d7124..6ef26dd 100644
--- a/lib/parsers.c
+++ b/lib/parsers.c
@@ -779,10 +779,11 @@
 			return -1;
 
 		case LWS_WS_OPCODE_07__PING:
+			lwsl_info("received %d byte ping, sending pong\n", wsi->rx_user_buffer_head);
+			lwsl_hexdump(&wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head);
 			/* parrot the ping packet payload back as a pong */
 			n = libwebsocket_write(wsi, (unsigned char *)
-			    &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
-				    wsi->rx_user_buffer_head, LWS_WRITE_PONG);
+			    &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head, LWS_WRITE_PONG);
 			/* ... then just drop it */
 			wsi->rx_user_buffer_head = 0;
 			return 0;
diff --git a/test-server/test-ping.c b/test-server/test-ping.c
index bc03100..d768df1 100644
--- a/test-server/test-ping.c
+++ b/test-server/test-ping.c
@@ -226,6 +226,9 @@
 			shift -= 8;
 		}
 
+		while (p - &pingbuf[LWS_SEND_BUFFER_PRE_PADDING] < size)
+			*p++ = 0;
+
 		gettimeofday(&tv, NULL);
 
 		psd->ringbuffer[psd->ringbuffer_head].issue_timestamp =