summaryrefslogtreecommitdiff
path: root/lib/extension-x-google-mux.c
blob: 95253b44348187d0c1a4b8740a55130a1382e88f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
#include "private-libwebsockets.h"
#include "extension-x-google-mux.h"

#define MUX_REAL_CHILD_INDEX_OFFSET 2

static int ongoing_subchannel;
static struct libwebsocket * tag_with_parent = NULL;
static int client_handshake_generation_is_for_mux_child;

static int lws_addheader_mux_opcode(unsigned char *pb, int len)
{
	unsigned char *start = pb;

	*pb++ = LWS_WS_OPCODE_07__NOSPEC__MUX | 0x80;
	if (len < 126)
		*pb++ = len;
	else {
		if (len > 65535) {
			*pb++ = 127;
			*pb++ = 0;
			*pb++ = 0;
			*pb++ = 0;
			*pb++ = 0;
			*pb++ = (len ) >> 24;
			*pb++ = (len) >> 16;
			*pb++ = (len) >> 8;
			*pb++ = (len) >> 0;
		} else {
			*pb++ = 126;
			*pb++ = (len) >> 8;
			*pb++ = (len) >> 0;
		}
	}

	return pb - start;
}

static int
lws_mux_subcommand_header(int cmd, int channel, unsigned char *pb, int len)
{
	unsigned char *start = pb;

	if (channel == 0) {
		fprintf(stderr, "lws_mux_subcommand_header: given ch 0\n");
		*((int *)0) = 0;
	}

	if (channel < 31)
		*pb++ = (channel << 3) | cmd;
	else {
		*pb++ = (31 << 3) | cmd;
		*pb++ = channel >> 8;
		*pb++ = channel;
	}

	if (len <= 253)
		*pb++ = len;
	else {
		if (len <= 65535) {
			*pb++ = 254;
			*pb++ = len >> 8;
			*pb++ = len;
		} else {
			*pb++ = 255;
			*pb++ = len >> 24;
			*pb++ = len >> 16;
			*pb++ = len >> 8;
			*pb++ = len;
		}
	}

	return pb - start;
}

static int lws_ext_x_google_mux__send_addchannel(
	struct libwebsocket_context *context,
	struct libwebsocket *wsi,
	struct lws_ext_x_google_mux_conn *parent_conn,
	struct libwebsocket *wsi_child,
	int channel,
	const char *url
) {

	unsigned char send_buf[LWS_SEND_BUFFER_PRE_PADDING + 2048 +
						  LWS_SEND_BUFFER_POST_PADDING];
	unsigned char *pb = &send_buf[LWS_SEND_BUFFER_PRE_PADDING];
	char *p;
	char delta_headers[1536];
	int delta_headers_len;
	int subcommand_length;
	int n;

	if (channel == 0) {
		fprintf(stderr, "lws_ext_x_google_mux__send_addchannel: given ch 0\n");
		*((int *)0) = 0;
	}

	wsi_child->ietf_spec_revision = wsi->ietf_spec_revision;

	client_handshake_generation_is_for_mux_child = 1;
	p = libwebsockets_generate_client_handshake(context, wsi_child,
								 delta_headers);
	client_handshake_generation_is_for_mux_child = 0;
	delta_headers_len = p - delta_headers;

	subcommand_length = lws_mux_subcommand_header(
		   LWS_EXT_XGM_OPC__ADDCHANNEL, channel, pb, delta_headers_len);

	pb += lws_addheader_mux_opcode(pb, subcommand_length + delta_headers_len);
	pb += lws_mux_subcommand_header(LWS_EXT_XGM_OPC__ADDCHANNEL, channel,
							 pb, delta_headers_len);

//	n = sprintf((char *)pb, "%s\x0d\x0a", url);
//	pb += n;

	if (delta_headers_len)
		memcpy(pb, delta_headers, delta_headers_len);

	pb += delta_headers_len;

	muxdebug("add channel sends %ld\n",
				   pb - &send_buf[LWS_SEND_BUFFER_PRE_PADDING]);

	parent_conn->defeat_mux_opcode_wrapping = 1;

	/* send the request to the server */

	n = lws_issue_raw_ext_access(wsi, &send_buf[LWS_SEND_BUFFER_PRE_PADDING],
				   pb - &send_buf[LWS_SEND_BUFFER_PRE_PADDING]);

	parent_conn->defeat_mux_opcode_wrapping = 0;

	return n;
}

/**
 * lws_extension_x_google_mux_parser():  Parse mux buffer headers coming in
 * 					 from a muxed connection into subchannel
 * 					 specific actions
 * @wsi:	muxed websocket instance
 * @conn:	x-google-mux private data bound to that @wsi
 * @c:		next character in muxed stream
 */

static int
lws_extension_x_google_mux_parser(struct libwebsocket_context *context,
			struct libwebsocket *wsi,
			struct libwebsocket_extension *this_ext,
			struct lws_ext_x_google_mux_conn *conn, unsigned char c)
{
	struct libwebsocket *wsi_child = NULL;
	struct libwebsocket_extension *ext;
	struct lws_ext_x_google_mux_conn *child_conn = NULL;
	int n;
	void *v;

//	fprintf(stderr, "XRX: %02X %d %d\n", c, conn->state, conn->length);

	/*
	 * [ <Channel ID b12.. b8> <Mux Opcode b2..b0> ]
	 * [ <Channel ID b7.. b0> ]
	 */
	
	switch (conn->state) {

	case LWS_EXT_XGM_STATE__MUX_BLOCK_1:
//		fprintf(stderr, "LWS_EXT_XGM_STATE__MUX_BLOCK_1: opc=%d channel=%d\n", c & 7, c >> 3);
		conn->block_subopcode = c & 7;
		conn->block_subchannel = (c >> 3) & 0x1f;
		conn->ignore_cmd = 0;

		if (conn->block_subchannel != 31)
			goto interpret;
		else
			conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_2;
		break;

	case LWS_EXT_XGM_STATE__MUX_BLOCK_2:
		conn->block_subchannel = c << 8;
		conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_3;
		break;

	case LWS_EXT_XGM_STATE__MUX_BLOCK_3:
		conn->block_subchannel |= c;

interpret:
//		fprintf(stderr, "LWS_EXT_XGM_STATE__MUX_BLOCK_3: subchannel=%d\n", conn->block_subchannel);
		ongoing_subchannel = conn->block_subchannel;

		/*
		 * convert the subchannel index to a child wsi
		 */

		/* act on the muxing opcode */
		
		switch (conn->block_subopcode) {
		case LWS_EXT_XGM_OPC__DATA:
			conn->state = LWS_EXT_XGM_STATE__DATA;
			break;
		case LWS_EXT_XGM_OPC__ADDCHANNEL:
			conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN;
			switch (wsi->mode) {

			/* client: parse accepted headers returned by server */

			case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
			case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
			case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
			case LWS_CONNMODE_WS_CLIENT:
				wsi_child = conn->wsi_children[conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET];
				wsi_child->state = WSI_STATE_HTTP_HEADERS;
				wsi_child->parser_state = WSI_TOKEN_NAME_PART;
				break;
			default:
				wsi_child = libwebsocket_create_new_server_wsi(context);
				conn->wsi_children[conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET] = wsi_child;
				wsi_child->state = WSI_STATE_HTTP_HEADERS;
				wsi_child->parser_state = WSI_TOKEN_NAME_PART;
				wsi_child->extension_handles = wsi;
				muxdebug("MUX LWS_EXT_XGM_OPC__ADDCHANNEL... "
					"created child subchannel %d\n", conn->block_subchannel);
				break;
			}
			break;
		case LWS_EXT_XGM_OPC__DROPCHANNEL:
			conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
			break;
		case LWS_EXT_XGM_OPC__FLOWCONTROL:
			conn->state = LWS_EXT_XGM_STATE__FLOWCONTROL_1;
			break;
		default:
			fprintf(stderr, "xgm: unknown subopcode\n");
			return -1;
		}
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN:
		switch (c) {
		case 254:
			conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN16_1;
			break;
		case 255:
			conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_1;
			break;
		default:
			conn->length = c;
			conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS;
			break;
		}
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN16_1:
		conn->length = c << 8;
		conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN16_2;
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN16_2:
		conn->length |= c;
		conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS;
		muxdebug("conn->length in mux block is %d\n", conn->length);
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_1:
		conn->length = c << 24;
		conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_2;
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_2:
		conn->length |= c << 16;
		conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_3;
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_3:
		conn->length |= c << 8;
		conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_4;
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_4:
		conn->length |= c;
		conn->state = LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS;
		break;

	case LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS:

		if (conn->block_subchannel == 0 || conn->block_subchannel >=
				(sizeof(conn->wsi_children) /
					       sizeof(conn->wsi_children[0]))) {
			fprintf(stderr, "Illegal subchannel %d in "
			   "LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS, ignoring",
							conn->block_subchannel);
			conn->ignore_cmd = 1;
		}

		if (conn->block_subchannel == 1 && !conn->original_ch1_closed) {
			fprintf(stderr, "illegal request to add ch1 when it's still live, ignoring\n");
			conn->ignore_cmd = 1;
		}

		if (conn->ignore_cmd) {
			if (--conn->length)
				return 0;
			conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
			return 0;
		}

		switch (wsi->mode) {

		/* client: parse accepted headers returned by server */
			
		case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
		case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
		case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
		case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
		case LWS_CONNMODE_WS_CLIENT:

			muxdebug("Client LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS in %c\n", c);
			if (conn->block_subchannel == 1) {
				fprintf(stderr, "adding ch1\n");
				wsi_child = wsi;
				child_conn = conn;
			} else
				wsi_child = conn->wsi_children[
				            conn->block_subchannel -
			  	  	  	   MUX_REAL_CHILD_INDEX_OFFSET];

			libwebsocket_parse(wsi_child, c);

			if (--conn->length)
				return 0;

			/* it's here we create the actual ext conn via callback */
			tag_with_parent = wsi;
			lws_client_interpret_server_handshake(context, wsi_child);
			tag_with_parent = NULL;

			//if (wsi->parser_state != WSI_PARSING_COMPLETE)
//				break;

			/* client: we received all server's ADD ack */

			if (conn->block_subchannel != 1) {
				child_conn = lws_get_extension_user_matching_ext(
								   wsi_child, this_ext);
				muxdebug("Received server's ADD Channel ACK for "
					 "subchannel %d child_conn=%p!\n",
					    conn->block_subchannel, (void *)child_conn);

				wsi_child->xor_mask = xor_no_mask;
				wsi_child->ietf_spec_revision = wsi->ietf_spec_revision;

				wsi_child->mode = LWS_CONNMODE_WS_CLIENT;
				wsi_child->state = WSI_STATE_ESTABLISHED;
				child_conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
			}

			conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
			child_conn->subchannel = conn->block_subchannel;

			/* allocate the per-connection user memory (if any) */

			if (wsi_child->protocol->per_session_data_size) {
				wsi_child->user_space = malloc(
				    wsi_child->protocol->per_session_data_size);
				if (wsi_child->user_space  == NULL) {
					fprintf(stderr, "Out of memory for "
							   "conn user space\n");
					goto bail2;
				}
			} else
				wsi_child->user_space = NULL;

			/* clear his proxy connection timeout */

			libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);

			/* mark him as being alive */

			wsi_child->state = WSI_STATE_ESTABLISHED;
			wsi_child->mode = LWS_CONNMODE_WS_CLIENT;

			if (wsi_child->protocol)
				fprintf(stderr, "mux handshake OK for protocol %s\n",
					wsi_child->protocol->name);
			else
				fprintf(stderr, "mux child handshake ends up with no protocol!\n");

			/*
			 * inform all extensions, not just active ones since they
			 * already know
			 */

			ext = context->extensions;

			while (ext && ext->callback) {
				v = NULL;
				for (n = 0; n < wsi_child->count_active_extensions; n++)
					if (wsi_child->active_extensions[n] == ext) {
						v = wsi_child->active_extensions_user[n];
					}

				ext->callback(context, ext, wsi_child,
				      LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
				ext++;
			}

			/* call him back to inform him he is up */

			wsi->protocol->callback(context, wsi_child,
					 LWS_CALLBACK_CLIENT_ESTABLISHED,
					 wsi_child->user_space,
					 NULL, 0);

			return 0;

bail2:
		exit(1);

		/* server: parse proposed changed headers from client */

		default:
			break;
		}

		/*
		 * SERVER
		 */

		wsi_child = conn->wsi_children[conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET];

		muxdebug("Server LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS in %d\n", conn->length);

		libwebsocket_read(context, wsi_child, &c, 1);

		if (--conn->length > 0)
			break;

		muxdebug("Server LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS done\n");

		/*
		 * server: header diffs are all seen, we must process
		 * the add action
		 */

		/* reply with ADDCHANNEL to ack it */

		wsi->xor_mask = xor_no_mask;
		child_conn = lws_get_extension_user_matching_ext(wsi_child,
								      this_ext);
		if (!child_conn) {
			fprintf(stderr, "wsi_child %p has no child conn!", (void *)wsi_child);
			break;
		}
		child_conn->wsi_parent = wsi;
		conn->sticky_mux_used = 1;
		child_conn->subchannel = conn->block_subchannel;


		muxdebug("Setting child conn parent to %p\n", (void *)wsi);

		wsi_child->mode = LWS_CONNMODE_WS_SERVING;
		wsi_child->state = WSI_STATE_ESTABLISHED;
		wsi_child->lws_rx_parse_state = LWS_RXPS_NEW;
		wsi_child->rx_packet_length = 0;

		/* allocate the per-connection user memory (if any) */

		if (wsi_child->protocol->per_session_data_size) {
			wsi_child->user_space = malloc(
				    wsi_child->protocol->per_session_data_size);
			if (wsi_child->user_space  == NULL) {
				fprintf(stderr, "Out of memory for "
							   "conn user space\n");
				break;
			}
		} else
			wsi_child->user_space = NULL;


		conn->wsi_children[conn->block_subchannel -
		                       MUX_REAL_CHILD_INDEX_OFFSET] = wsi_child;
		if (conn->highest_child_subchannel <=
			   conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET)
			conn->highest_child_subchannel =
					conn->block_subchannel -
						MUX_REAL_CHILD_INDEX_OFFSET + 1;


		/* notify user code that we're ready to roll */

		if (wsi_child->protocol->callback)
			wsi_child->protocol->callback(
					wsi_child->protocol->owning_server,
					wsi_child, LWS_CALLBACK_ESTABLISHED,
						wsi_child->user_space, NULL, 0);

		muxdebug("setting conn state LWS_EXT_XGM_STATE__MUX_BLOCK_1\n");
		conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
		break;

	case LWS_EXT_XGM_STATE__FLOWCONTROL_1:
		conn->length = c << 24;
		conn->state = LWS_EXT_XGM_STATE__FLOWCONTROL_2;
		break;

	case LWS_EXT_XGM_STATE__FLOWCONTROL_2:
		conn->length |= c << 16;
		conn->state = LWS_EXT_XGM_STATE__FLOWCONTROL_3;
		break;

	case LWS_EXT_XGM_STATE__FLOWCONTROL_3:
		conn->length |= c << 8;
		conn->state = LWS_EXT_XGM_STATE__FLOWCONTROL_4;
		break;

	case LWS_EXT_XGM_STATE__FLOWCONTROL_4:
		conn->length |= c;
		conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
		break;

	case LWS_EXT_XGM_STATE__DATA:

//		fprintf(stderr, "LWS_EXT_XGM_STATE__DATA in\n");

		/*
		 * we have cooked websocket frame content following just like
		 * it went on the wire without mux, including masking and any
		 * other extensions (including this guy can himself be another
		 * level of channel mux, there's no restriction).
		 *
		 * We deal with it by just feeding it to the child wsi's rx
		 * state machine.  The only issue is, we need that state machine
		 * to tell us when it ate a full frame, so we watch its state
		 * afterwards
		 */
		if (conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET >= conn->highest_child_subchannel) {
			fprintf(stderr, "Illegal subchannel %d\n", conn->block_subchannel);
			return -1;
		}

		// fprintf(stderr, "LWS_EXT_XGM_STATE__DATA: ch %d\n", conn->block_subchannel);

		if (conn->block_subchannel == 1) {
			if (conn->original_ch1_closed) {
				fprintf(stderr, "data sent to closed ch1\n");
				return -1;
			}
			wsi_child = wsi;
		} else
			wsi_child = conn->wsi_children[conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET];

		if (!wsi_child) {
			fprintf(stderr, "Bad subchannel %d\n", conn->block_subchannel);
			return -1;
		}

		switch (wsi_child->mode) {

		/* client receives something */
			
		case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
		case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
		case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
		case LWS_CONNMODE_WS_CLIENT:
//			fprintf(stderr, "  client\n");
			if (libwebsocket_client_rx_sm(wsi_child, c) < 0) {
				libwebsocket_close_and_free_session(
					context,
					wsi_child,
					LWS_CLOSE_STATUS_GOINGAWAY);
			}

			return 0;

		/* server is receiving from client */

		default:
//			fprintf(stderr, "  server\n");
			if (libwebsocket_rx_sm(wsi_child, c) < 0) {
				fprintf(stderr, "probs\n");
				libwebsocket_close_and_free_session(
					context,
					wsi_child,
					LWS_CLOSE_STATUS_GOINGAWAY);
			}
			break;
		}
		break;
	}

	return 0;
}



int lws_extension_callback_x_google_mux(
	struct libwebsocket_context *context,
	struct libwebsocket_extension *ext,
	struct libwebsocket *wsi,
	enum libwebsocket_extension_callback_reasons reason,
					       void *user, void *in, size_t len)
{
	unsigned char send_buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
						  LWS_SEND_BUFFER_POST_PADDING];
	struct lws_ext_x_google_mux_conn *conn =
				       (struct lws_ext_x_google_mux_conn *)user;
	struct lws_ext_x_google_mux_conn *parent_conn;
	struct lws_ext_x_google_mux_conn *child_conn;
	int n;
	struct lws_tokens *eff_buf = (struct lws_tokens *)in;
	unsigned char *p = NULL;
	struct lws_ext_x_google_mux_context *mux_ctx =
						  ext->per_context_private_data;
	struct libwebsocket *wsi_parent;
	struct libwebsocket *wsi_child;
	struct libwebsocket *wsi_temp;
	unsigned char *pin = (unsigned char *)in;
	unsigned char *basepin;
	int m;
	int done = 0;
	unsigned char *pb = &send_buf[LWS_SEND_BUFFER_PRE_PADDING];
	int subcommand_length;

	if (eff_buf)
		p = (unsigned char *)eff_buf->token;

	switch (reason) {

	/* these guys are once per context */

	case LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT:
	case LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT:

		ext->per_context_private_data = malloc(
				  sizeof (struct lws_ext_x_google_mux_context));
		mux_ctx = (struct lws_ext_x_google_mux_context *)
						  ext->per_context_private_data;
		mux_ctx->active_conns = 0;
		break;

	case LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT:
	case LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT:

		if (!mux_ctx)
			break;
		for (n = 0; n < mux_ctx->active_conns; n++)
			if (mux_ctx->wsi_muxconns[n]) {
				libwebsocket_close_and_free_session(
					context, mux_ctx->wsi_muxconns[n],
					LWS_CLOSE_STATUS_GOINGAWAY);
				mux_ctx->wsi_muxconns[n] = NULL;
			}

		free(mux_ctx);
		break;

	/*
	 * channel management
	 */

	case LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION:

		muxdebug("LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION %s:%u\n",
						 (char *)in, (unsigned int)len);

		/*
		 * Does a physcial connection to the same server:port already
		 * exist so we can piggyback on it?
		 */

		for (n = 0; n < mux_ctx->active_conns && !done; n++) {

			wsi_parent = mux_ctx->wsi_muxconns[n];
			if (!wsi_parent)
				continue;

			muxdebug("  %s / %s\n", wsi_parent->c_address, (char *)in);
			if (strcmp(wsi_parent->c_address, in))
				continue;
			muxdebug("  %u / %u\n", wsi_parent->c_port, (unsigned int)len);

			if (wsi_parent->c_port != (unsigned int)len)
				continue;

			/*
			 * does this potential parent already have an
			 * x-google-mux conn associated with him?
			 */

			parent_conn = NULL;
			for (m = 0; m < wsi_parent->count_active_extensions; m++)
				if (ext == wsi_parent->active_extensions[m])
					parent_conn = (struct lws_ext_x_google_mux_conn *)
						wsi_parent->active_extensions_user[m];

			if (parent_conn == NULL) {

				/*
				 * he doesn't -- see if that's just because it
				 * is early in his connection sequence or if we
				 * should give up on him
				 */

				switch (wsi_parent->mode) {
				case LWS_CONNMODE_WS_SERVING:
				case LWS_CONNMODE_WS_CLIENT:
					continue;
				default:
					break;
				}

				/*
				 * our putative parent is still connecting
				 * himself, we have to become a candidate child
				 * and find out our final fate when the parent
				 * completes connection
				 */

				 wsi->candidate_children_list = wsi_parent->candidate_children_list;
				 wsi_parent->candidate_children_list = wsi;
				 wsi->mode = LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD;

				 done = 1;
				 continue;
			}

			if (parent_conn->highest_child_subchannel >=
					(sizeof(parent_conn->wsi_children) /
					   sizeof(parent_conn->wsi_children[0]))) {
				fprintf(stderr, "Can't add any more children\n");
				continue;
			}
			/*
			 * this established connection will do, bind them
			 * from now on child will only operate through parent
			 * connection
			 */

			wsi->candidate_children_list = wsi_parent->candidate_children_list;
			wsi_parent->candidate_children_list = wsi;
			wsi->mode = LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD;

			fprintf(stderr, "attaching to existing mux\n");

			conn = parent_conn;
			wsi = wsi_parent;

			goto handle_additions;

		}

		/*
		 * either way, note the existence of this connection in case
		 * he will become a possible mux parent later
		 */

		mux_ctx->wsi_muxconns[mux_ctx->active_conns++] = wsi;
		if (done)
			return 1;

		fprintf(stderr, "x-google-mux: unable to mux connection\n");

		break;

	/* these guys are once per connection */

	case LWS_EXT_CALLBACK_CLIENT_CONSTRUCT:
		muxdebug("LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: setting parent = %p\n", (void *)tag_with_parent);
		conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
		if (conn->block_subchannel != 1)
			conn->wsi_parent = tag_with_parent;
		break;

	case LWS_EXT_CALLBACK_CONSTRUCT:
		muxdebug("LWS_EXT_CALLBACK_CONSTRUCT\n");
		conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;
		break;

	case LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE:
		muxdebug("LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE\n");

		if (conn->subchannel == 1) {

			/*
			 * special case of original mux parent channel closing
			 */

			conn->original_ch1_closed = 1;

			fprintf(stderr, "original mux parent channel closing\n");

			parent_conn = conn;
		} else {

			parent_conn = lws_get_extension_user_matching_ext(conn->wsi_parent, ext);
			if (parent_conn == 0) {
				fprintf(stderr, "failed to get parent conn\n");
				break;
			}
		}

		/* see if that was the end of the mux entirely */

		if (!parent_conn->original_ch1_closed)
			break;

		done = 0;
		for (n = 0; n < !done && parent_conn->highest_child_subchannel; n++)
			if (parent_conn->wsi_children[n])
				done = 1;

		/* if he has children, don't let him close for real! */

		if (done) {
			fprintf(stderr, "VETO closure\n");
			return 1;
		}

		/* no children, ch1 is closed, let him destroy himself */

		if (conn->subchannel == 1)
			fprintf(stderr, "ALLOW closure of mux parent\n");

		break;

	case LWS_EXT_CALLBACK_DESTROY:
		muxdebug("LWS_EXT_CALLBACK_DESTROY\n");

		/*
		 * remove us from parent if noted in parent
		 */

		if (conn->subchannel == 1) {

			/*
			 * special case of original mux parent channel closing
			 */

			conn->original_ch1_closed = 1;

			fprintf(stderr, "original mux parent channel closing\n");

			parent_conn = conn;
			wsi_parent = wsi;
		} else {

			wsi_parent = conn->wsi_parent;
			parent_conn = lws_get_extension_user_matching_ext(conn->wsi_parent, ext);
			if (parent_conn == 0) {
				fprintf(stderr, "failed to get parent conn\n");
				break;
			}
			for (n = 0; n < parent_conn->highest_child_subchannel; n++)
				if (parent_conn->wsi_children[n] == wsi)
					parent_conn->wsi_children[n] = NULL;
		}

		/* see if that was the end of the mux entirely */

		if (!parent_conn->original_ch1_closed)
			break;

		done = 0;
		for (n = 0; n < !done && parent_conn->highest_child_subchannel; n++)
			if (parent_conn->wsi_children[n])
				done = 1;

		if (done == 0)
			if (parent_conn != conn)

				/*
				 * parent closed last and no children left
				 * ... and we are not parent already ourselves
				 */

				libwebsocket_close_and_free_session(context, wsi_parent, LWS_CLOSE_STATUS_NORMAL);

		break;

	case LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING:
		muxdebug("LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING\n");

		for (n = 0; n < mux_ctx->active_conns; n++)
			if (mux_ctx->wsi_muxconns[n] == wsi) {
				while (n++ < mux_ctx->active_conns)
					mux_ctx->wsi_muxconns[n - 1] =
						       mux_ctx->wsi_muxconns[n];
				mux_ctx->active_conns--;
				return 0;
			}

		/*
		 * liberate any candidate children otherwise imprisoned
		 */

		wsi_parent = wsi->candidate_children_list;
		while (wsi_parent) {
			wsi_temp = wsi_parent->candidate_children_list;
			/* let them each connect privately then */
			__libwebsocket_client_connect_2(context, wsi_parent);
			wsi_parent = wsi_temp;
		}

		break;

	case LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED:
		muxdebug("LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED\n");

handle_additions:
		/*
		 * did this putative parent get x-google-mux authorized in the
		 * end?
		 */

		if (!conn) {

			muxdebug("  Putative parent didn't get mux extension, let them go it alone\n");

			/*
			 * no, we can't be a parent for mux children.  Let
			 * them all go it alone
			 */

			wsi_child = wsi->candidate_children_list;
			while (wsi_child) {
				wsi_temp = wsi_child->candidate_children_list;
				/* let them each connect privately then */
				__libwebsocket_client_connect_2(context, wsi_child);
				wsi_child = wsi_temp;
			}

			return 1;
		}

		/*
		 * we did get mux extension authorized by server, in that case
		 * if we have any candidate children let's try to attach them
		 * as mux subchannel real children
		 */
		
		wsi_child = wsi->candidate_children_list;
		n = 0;
		while (wsi_child) {

			wsi_temp = wsi_child->candidate_children_list;

			/* find an empty subchannel */

			while ((n < (sizeof(conn->wsi_children) / sizeof(conn->wsi_children[0]))) &&
					conn->wsi_children[n] != NULL)
				n++;

			if (n >= (sizeof(conn->wsi_children) / sizeof(conn->wsi_children[0]))) {
				/* no room at the inn */

				/* let them each connect privately then */
				__libwebsocket_client_connect_2(context, wsi_child);
				wsi_child = wsi_temp;
				continue;
			}

			muxdebug("  using mux addchannel action for candidate child\n");
			
			/* pile the children on the parent */
			lws_ext_x_google_mux__send_addchannel(context, wsi,
					conn, wsi_child,
					n + MUX_REAL_CHILD_INDEX_OFFSET, wsi->c_path);

			conn->sticky_mux_used = 1;

			conn->wsi_children[n] = wsi_child;
			if ((n + 1) > conn->highest_child_subchannel)
				conn->highest_child_subchannel = n + 1;

			muxdebug("Setting CHILD LIST entry %d to %p\n",
				  n + MUX_REAL_CHILD_INDEX_OFFSET, (void *)wsi_parent);
			wsi_child = wsi_temp;
		}
		wsi->candidate_children_list = NULL;
		return 1;

	/*
	 * whenever we receive something on a muxed link
	 */

	case LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX:

		muxdebug("LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX\n");

		if (wsi->opcode != LWS_WS_OPCODE_07__NOSPEC__MUX)
			return 0; /* unhandled */

		conn->state = LWS_EXT_XGM_STATE__MUX_BLOCK_1;

		n = eff_buf->token_len;
		while (n--)
			if (lws_extension_x_google_mux_parser(context, wsi, ext,
							      conn, *p++) < 0) {
				return -1;
			}
		return 1; /* handled */

	/*
	 * when something might need sending on our transport
	 */

	case LWS_EXT_CALLBACK_PACKET_TX_DO_SEND:

		muxdebug("LWS_EXT_CALLBACK_PACKET_TX_DO_SEND: %p, "
			 "my subchannel=%d\n",
			 (void *)conn->wsi_parent, conn->subchannel);

		pin = *((unsigned char **)in);
		basepin = pin;

		wsi_parent = conn->wsi_parent;

		if (conn->subchannel == 1) {

			/*
			 * if we weren't 'closed', then we were the original
			 * connection that established this link, ie, it's
			 * the parent wsi
			 */

			if (conn->original_ch1_closed) {
				fprintf(stderr, "Trying to send on dead original ch1\n");
				return 0;
			}

			/* send on ourselves */

			wsi_parent = wsi;

		} else {

			/*
			 * he's not a child connection of a mux
			 */

			if (!conn->wsi_parent) {
	//			fprintf(stderr, "conn %p has no parent\n", (void *)conn);
				return 0;
			}

			/*
			 * get parent / transport mux context
			 */

			parent_conn = lws_get_extension_user_matching_ext(conn->wsi_parent, ext);
			if (parent_conn == 0) {
				fprintf(stderr, "failed to get parent conn\n");
				return 0;
			}

			/*
			 * mux transport is in singular mode, let the caller send it
			 * no more muxified than it already is
			 */

			if (!parent_conn->sticky_mux_used) {
	//			fprintf(stderr, "parent in singular mode\n");
				return 0;
			}
		}

		if (!conn->defeat_mux_opcode_wrapping) {

			n = 1;
			if (conn->subchannel >= 31)
				n = 3;

			/*
			 * otherwise we need to take care of the sending action using
			 * mux protocol.  Prepend the channel + opcode
			 */

			pin -= lws_addheader_mux_opcode(send_buf, len + n) + n;
			basepin = pin;
			pin += lws_addheader_mux_opcode(pin, len + n);

			if (conn->subchannel >= 31) {
				*pin++ = (31 << 3) | LWS_EXT_XGM_OPC__DATA;
				*pin++ = conn->subchannel >> 8;
				*pin++ = conn->subchannel;
			} else
				*pin++ = (conn->subchannel << 3) | LWS_EXT_XGM_OPC__DATA;
		}

		/*
		 * recurse to allow nesting
		 */

		lws_issue_raw_ext_access(wsi_parent, basepin, (pin - basepin) + len);

		return 1; /* handled */

	case LWS_EXT_CALLBACK_1HZ:
		/*
		 * if we have children, service their timeouts using the same
		 * handler as toplevel guys to allow recursion
		 */
		for (n = 0; n < conn->highest_child_subchannel; n++)
			if (conn->wsi_children[n])
				libwebsocket_service_timeout_check(context,
						    conn->wsi_children[n], len);
		break;

	case LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE:
		/*
		 * if a mux child is asking for callback on writable, we have
		 * to pass it up to his parent
		 */

		muxdebug("LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE %s\n", wsi->protocol->name);

		if (conn->wsi_parent == NULL) {
			muxdebug("  no parent\n");
			break;
		}

		if (!conn->awaiting_POLLOUT) {

			muxdebug("  !conn->awaiting_POLLOUT\n");

			conn->awaiting_POLLOUT = 1;
			parent_conn = NULL;
			for (m = 0; m < conn->wsi_parent->count_active_extensions; m++)
				if (ext == conn->wsi_parent->active_extensions[m])
					parent_conn = (struct lws_ext_x_google_mux_conn *)
						conn->wsi_parent->active_extensions_user[m];

			if (parent_conn != NULL) {
				parent_conn->count_children_needing_POLLOUT++;
				muxdebug("  count_children_needing_POLLOUT bumped\n");
			} else
				fprintf(stderr, "unable to identify parent conn\n");
		}
		muxdebug("  requesting on parent %p\n", (void *)conn->wsi_parent);
		libwebsocket_callback_on_writable(context, conn->wsi_parent);

		return 1;

	case LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX:

		fprintf(stderr, "LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX %p\n", (void *)wsi->extension_handles);

		/* send raw if we're not a child */

		if (!wsi->extension_handles)
			return 0;

		subcommand_length = lws_mux_subcommand_header(LWS_EXT_XGM_OPC__ADDCHANNEL, ongoing_subchannel, pb, len);

		pb += lws_addheader_mux_opcode(pb, subcommand_length + len);
		pb += lws_mux_subcommand_header(LWS_EXT_XGM_OPC__ADDCHANNEL, ongoing_subchannel, pb, len);
		memcpy(pb, in, len);
		pb += len;

		lws_issue_raw_ext_access(wsi->extension_handles, &send_buf[LWS_SEND_BUFFER_PRE_PADDING],
					   pb - &send_buf[LWS_SEND_BUFFER_PRE_PADDING]);


		return 1; /* handled */

	case LWS_EXT_CALLBACK_IS_WRITEABLE:
		/*
		 * we are writable, inform children if any care
		 */
		muxdebug("LWS_EXT_CALLBACK_IS_WRITEABLE: %s\n", wsi->protocol->name);

		if (!conn->count_children_needing_POLLOUT) {
			muxdebug("  no children need POLLOUT\n");
			return 0;
		}

		for (n = 0; n < conn->highest_child_subchannel; n++) {

			if (!conn->wsi_children[n])
				continue;

			child_conn = NULL;
			for (m = 0; m < conn->wsi_children[n]->count_active_extensions; m++)
				if (ext == conn->wsi_children[n]->active_extensions[m])
					child_conn = (struct lws_ext_x_google_mux_conn *)
							conn->wsi_children[n]->active_extensions_user[m];

			if (!child_conn) {
				fprintf(stderr, "unable to identify child conn\n");
				continue;
			}

			if (!child_conn->awaiting_POLLOUT)
				continue;

			child_conn->awaiting_POLLOUT = 0;
			conn->count_children_needing_POLLOUT--;
			lws_handle_POLLOUT_event(context, conn->wsi_children[n], NULL);
			if (!conn->count_children_needing_POLLOUT)
				return 2; /* all handled */
			else
				return 1; /* handled but need more */
		}
		break;

	case LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION:

		/* disallow deflate-stream if we are a mux child connection */

		if (strcmp(in, "deflate-stream") == 0 &&
				 client_handshake_generation_is_for_mux_child) {

			fprintf(stderr, "mux banned deflate-stream on child connection\n");
			return 1; /* disallow */
		}
		break;

	default:
		break;
	}

	return 0;
}