summaryrefslogtreecommitdiffstats
path: root/libxrdp/xrdp_mcs.c
blob: c1b0b9081e72039abff4b208286e8c683e6a4e16 (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
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2004-2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * mcs layer
 */

#include "libxrdp.h"
#include "log.h"

/*****************************************************************************/
struct xrdp_mcs *APP_CC
xrdp_mcs_create(struct xrdp_sec *owner, struct trans *trans,
                struct stream *client_mcs_data,
                struct stream *server_mcs_data)
{
    struct xrdp_mcs *self;

    DEBUG(("  in xrdp_mcs_create"));
    self = (struct xrdp_mcs *)g_malloc(sizeof(struct xrdp_mcs), 1);
    self->sec_layer = owner;
    self->userid = 1;
    self->chanid = 1001;
    self->client_mcs_data = client_mcs_data;
    self->server_mcs_data = server_mcs_data;
    self->iso_layer = xrdp_iso_create(self, trans);
    self->channel_list = list_create();
    DEBUG(("  out xrdp_mcs_create"));
    return self;
}

/*****************************************************************************/
void APP_CC
xrdp_mcs_delete(struct xrdp_mcs *self)
{
    struct mcs_channel_item *channel_item;
    int index;
    int count;

    if (self == 0)
    {
        return;
    }

    /* here we have to free the channel items and anything in them */
    count = self->channel_list->count;

    for (index = count - 1; index >= 0; index--)
    {
        channel_item = (struct mcs_channel_item *)
                       list_get_item(self->channel_list, index);
        g_free(channel_item);
    }

    list_delete(self->channel_list);

    xrdp_iso_delete(self->iso_layer);
    /* make sure we get null pointer exception if struct is used again. */
    DEBUG(("xrdp_mcs_delete processed"))
    g_memset(self, 0, sizeof(struct xrdp_mcs)) ;
    g_free(self);
}

/*****************************************************************************/
/* This function sends channel join confirm */
/* returns error = 1 ok = 0 */
static int APP_CC
xrdp_mcs_send_cjcf(struct xrdp_mcs *self, int userid, int chanid)
{
    struct stream *s;

    DEBUG(("  in xrdp_mcs_send_cjcf"));
    make_stream(s);
    init_stream(s, 8192);

    if (xrdp_iso_init(self->iso_layer, s) != 0)
    {
        free_stream(s);
        DEBUG(("  out xrdp_mcs_send_cjcf error"));
        return 1;
    }

    out_uint8(s, (MCS_CJCF << 2) | 2);
    out_uint8(s, 0);
    out_uint16_be(s, userid);
    out_uint16_be(s, chanid); /* TODO Explain why we send this two times */
    out_uint16_be(s, chanid);
    s_mark_end(s);

    if (xrdp_iso_send(self->iso_layer, s) != 0)
    {
        free_stream(s);
        DEBUG(("  out xrdp_mcs_send_cjcf error"));
        return 1;
    }

    free_stream(s);
    DEBUG(("  out xrdp_mcs_send_cjcf"));
    return 0;
}

/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_recv(struct xrdp_mcs *self, struct stream *s, int *chan)
{
    int appid;
    int opcode;
    int len;
    int userid;
    int chanid;
    DEBUG(("  in xrdp_mcs_recv"));

    while (1)
    {
        if (xrdp_iso_recv(self->iso_layer, s) != 0)
        {
            DEBUG(("   out xrdp_mcs_recv, xrdp_iso_recv return non zero"));
            g_writeln("xrdp_mcs_recv: xrdp_iso_recv failed");
            return 1;
        }

        if (!s_check_rem(s, 1))
        {
            return 1;
        }

        in_uint8(s, opcode);
        appid = opcode >> 2;

        if (appid == MCS_DPUM) /* Disconnect Provider Ultimatum */
        {
            g_writeln("received Disconnect Provider Ultimatum");
            DEBUG(("  out xrdp_mcs_recv appid != MCS_DPUM"));
            return 1;
        }

        /* this is channels getting added from the client */
        if (appid == MCS_CJRQ)
        {

            if (!s_check_rem(s, 4))
            {
                return 1;
            }

            in_uint16_be(s, userid);
            in_uint16_be(s, chanid);
            log_message(LOG_LEVEL_DEBUG,"MCS_CJRQ - channel join request received");
            DEBUG(("xrdp_mcs_recv  adding channel %4.4x", chanid));

            if (xrdp_mcs_send_cjcf(self, userid, chanid) != 0)
            {
                log_message(LOG_LEVEL_ERROR,"Non handled error from xrdp_mcs_send_cjcf") ;
            }

            s = libxrdp_force_read(self->iso_layer->trans);
            if (s == 0)
            {
                g_writeln("xrdp_mcs_recv: libxrdp_force_read failed");
                return 1;
            }

            continue;
        }

        if (appid == MCS_SDRQ || appid == MCS_SDIN)
        {
            break;
        }
        else
        {
            log_message(LOG_LEVEL_DEBUG,"Recieved an unhandled appid:%d",appid);
        }

        break;
    }

    if (appid != MCS_SDRQ)
    {
        DEBUG(("  out xrdp_mcs_recv err got 0x%x need MCS_SDRQ", appid));
        return 1;
    }

    if (!s_check_rem(s, 6))
    {
        return 1;
    }

    in_uint8s(s, 2);
    in_uint16_be(s, *chan);
    in_uint8s(s, 1);
    in_uint8(s, len);

    if (len & 0x80)
    {
        if (!s_check_rem(s, 1))
        {
            return 1;
        }
        in_uint8s(s, 1);
    }

    DEBUG(("  out xrdp_mcs_recv"));
    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_parse_header(struct xrdp_mcs *self, struct stream *s,
                          int tag_val, int *len)
{
    int tag;
    int l;
    int i;

    if (tag_val > 0xff)
    {
        if (!s_check_rem(s, 2))
        {
            return 1;
        }
        in_uint16_be(s, tag);
    }
    else
    {
        if (!s_check_rem(s, 1))
        {
            return 1;
        }
        in_uint8(s, tag);
    }

    if (tag != tag_val)
    {
        return 1;
    }

    if (!s_check_rem(s, 1))
    {
        return 1;
    }

    in_uint8(s, l);

    if (l & 0x80)
    {
        l = l & ~0x80;
        *len = 0;

        while (l > 0)
        {
            if (!s_check_rem(s, 1))
            {
                return 1;
            }
            in_uint8(s, i);
            *len = (*len << 8) | i;
            l--;
        }
    }
    else
    {
        *len = l;
    }

    if (s_check(s))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_parse_domain_params(struct xrdp_mcs *self, struct stream *s)
{
    int len;

    if (xrdp_mcs_ber_parse_header(self, s, MCS_TAG_DOMAIN_PARAMS, &len) != 0)
    {
        return 1;
    }

    if ((len < 0) || !s_check_rem(s, len))
    {
        return 1;
    }

    in_uint8s(s, len);

    if (s_check(s))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_connect_initial(struct xrdp_mcs *self)
{
    int len;
    struct stream *s;

    s = libxrdp_force_read(self->iso_layer->trans);
    if (s == 0)
    {
        return 1;
    }

    if (xrdp_iso_recv(self->iso_layer, s) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_ber_parse_header(self, s, MCS_CONNECT_INITIAL, &len) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_OCTET_STRING, &len) != 0)
    {
        return 1;
    }

    if ((len < 0) || !s_check_rem(s, len))
    {
        return 1;
    }

    in_uint8s(s, len);

    if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_OCTET_STRING, &len) != 0)
    {
        return 1;
    }

    if ((len < 0) || !s_check_rem(s, len))
    {
        return 1;
    }

    in_uint8s(s, len);

    if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_BOOLEAN, &len) != 0)
    {
        return 1;
    }

    if ((len < 0) || !s_check_rem(s, len))
    {
        return 1;
    }

    in_uint8s(s, len);

    if (xrdp_mcs_parse_domain_params(self, s) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_parse_domain_params(self, s) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_parse_domain_params(self, s) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_OCTET_STRING, &len) != 0)
    {
        return 1;
    }

    /* mcs data can not be zero length */
    if ((len <= 0) || (len > 16 * 1024))
    {
        return 1;
    }

    if (!s_check_rem(s, len))
    {
        return 1;
    }

    /* make a copy of client mcs data */
    init_stream(self->client_mcs_data, len);
    out_uint8a(self->client_mcs_data, s->p, len);
    in_uint8s(s, len);
    s_mark_end(self->client_mcs_data);

    if (s_check_end(s))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_edrq(struct xrdp_mcs *self)
{
    int opcode;
    struct stream *s;

    DEBUG(("    in xrdp_mcs_recv_edrq"));

    s = libxrdp_force_read(self->iso_layer->trans);
    if (s == 0)
    {
        return 1;
    }

    if (xrdp_iso_recv(self->iso_layer, s) != 0)
    {
        return 1;
    }

    if (!s_check_rem(s, 1))
    {
        return 1;
    }

    in_uint8(s, opcode);

    if ((opcode >> 2) != MCS_EDRQ)
    {
        return 1;
    }

    if (!s_check_rem(s, 4))
    {
        return 1;
    }

    in_uint8s(s, 2);
    in_uint8s(s, 2);

    if (opcode & 2)
    {
        if (!s_check_rem(s, 2))
        {
            return 1;
        }
        in_uint16_be(s, self->userid);
    }

    if (!(s_check_end(s)))
    {
        return 1;
    }

    DEBUG(("    out xrdp_mcs_recv_edrq"));
    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_aurq(struct xrdp_mcs *self)
{
    int opcode;
    struct stream *s;

    DEBUG(("    in xrdp_mcs_recv_aurq"));

    s = libxrdp_force_read(self->iso_layer->trans);
    if (s == 0)
    {
        return 1;
    }

    if (xrdp_iso_recv(self->iso_layer, s) != 0)
    {
        return 1;
    }

    if (!s_check_rem(s, 1))
    {
        return 1;
    }

    in_uint8(s, opcode);

    if ((opcode >> 2) != MCS_AURQ)
    {
        return 1;
    }

    if (opcode & 2)
    {
        if (!s_check_rem(s, 2))
        {
            return 1;
        }
        in_uint16_be(s, self->userid);
    }

    if (!(s_check_end(s)))
    {
        return 1;
    }

    DEBUG(("    out xrdp_mcs_recv_aurq"));
    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_send_aucf(struct xrdp_mcs *self)
{
    struct stream *s;

    DEBUG(("  in xrdp_mcs_send_aucf"));
    make_stream(s);
    init_stream(s, 8192);

    if (xrdp_iso_init(self->iso_layer, s) != 0)
    {
        free_stream(s);
        DEBUG(("  out xrdp_mcs_send_aucf error"));
        return 1;
    }

    out_uint8(s, ((MCS_AUCF << 2) | 2));
    out_uint8s(s, 1);
    out_uint16_be(s, self->userid);
    s_mark_end(s);

    if (xrdp_iso_send(self->iso_layer, s) != 0)
    {
        free_stream(s);
        DEBUG(("  out xrdp_mcs_send_aucf error"));
        return 1;
    }

    free_stream(s);
    DEBUG(("  out xrdp_mcs_send_aucf"));
    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_cjrq(struct xrdp_mcs *self)
{
    int opcode;
    struct stream *s;

    s = libxrdp_force_read(self->iso_layer->trans);
    if (s == 0)
    {
        return 1;
    }

    if (xrdp_iso_recv(self->iso_layer, s) != 0)
    {
        return 1;
    }

    if (!s_check_rem(s, 1))
    {
        return 1;
    }

    in_uint8(s, opcode);

    if ((opcode >> 2) != MCS_CJRQ)
    {
        return 1;
    }

    if (!s_check_rem(s, 4))
    {
        return 1;
    }

    in_uint8s(s, 4);

    if (opcode & 2)
    {
        if (!s_check_rem(s, 2))
        {
            return 1;
        }
        in_uint8s(s, 2);
    }

    if (!(s_check_end(s)))
    {
        return 1;
    }

    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_header(struct xrdp_mcs *self, struct stream *s,
                        int tag_val, int len)
{
    if (tag_val > 0xff)
    {
        out_uint16_be(s, tag_val);
    }
    else
    {
        out_uint8(s, tag_val);
    }

    if (len >= 0x80)
    {
        out_uint8(s, 0x82);
        out_uint16_be(s, len);
    }
    else
    {
        out_uint8(s, len);
    }

    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_int8(struct xrdp_mcs *self, struct stream *s, int value)
{
    xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 1);
    out_uint8(s, value);
    return 0;
}

#if 0 /* not used */
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_int16(struct xrdp_mcs *self, struct stream *s, int value)
{
    xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 2);
    out_uint8(s, (value >> 8));
    out_uint8(s, value);
    return 0;
}
#endif

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_int24(struct xrdp_mcs *self, struct stream *s, int value)
{
    xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 3);
    out_uint8(s, (value >> 16));
    out_uint8(s, (value >> 8));
    out_uint8(s, value);
    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_out_domain_params(struct xrdp_mcs *self, struct stream *s,
                           int max_channels,
                           int max_users, int max_tokens,
                           int max_pdu_size)
{
    xrdp_mcs_ber_out_header(self, s, MCS_TAG_DOMAIN_PARAMS, 26);
    xrdp_mcs_ber_out_int8(self, s, max_channels);
    xrdp_mcs_ber_out_int8(self, s, max_users);
    xrdp_mcs_ber_out_int8(self, s, max_tokens);
    xrdp_mcs_ber_out_int8(self, s, 1);
    xrdp_mcs_ber_out_int8(self, s, 0);
    xrdp_mcs_ber_out_int8(self, s, 1);
    xrdp_mcs_ber_out_int24(self, s, max_pdu_size);
    xrdp_mcs_ber_out_int8(self, s, 2);
    return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_send_connect_response(struct xrdp_mcs *self)
{
    int data_len;
    struct stream *s;

    DEBUG(("  in xrdp_mcs_send_connect_response"));
    make_stream(s);
    init_stream(s, 8192);
    data_len = (int) (self->server_mcs_data->end - self->server_mcs_data->data);
    xrdp_iso_init(self->iso_layer, s);
    xrdp_mcs_ber_out_header(self, s, MCS_CONNECT_RESPONSE, data_len + 38);
    xrdp_mcs_ber_out_header(self, s, BER_TAG_RESULT, 1);
    out_uint8(s, 0);
    xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 1);
    out_uint8(s, 0);
    xrdp_mcs_out_domain_params(self, s, 22, 3, 0, 0xfff8);
    xrdp_mcs_ber_out_header(self, s, BER_TAG_OCTET_STRING, data_len);
    /* mcs data */
    out_uint8a(s, self->server_mcs_data->data, data_len);
    s_mark_end(s);

    if (xrdp_iso_send(self->iso_layer, s) != 0)
    {
        free_stream(s);
        DEBUG(("  out xrdp_mcs_send_connect_response error"));
        return 1;
    }

    free_stream(s);
    DEBUG(("  out xrdp_mcs_send_connect_response"));
    return 0;
}

/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_incoming(struct xrdp_mcs *self)
{
    DEBUG(("  in xrdp_mcs_incoming"));

    if (xrdp_iso_incoming(self->iso_layer) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_recv_connect_initial(self) != 0)
    {
        return 1;
    }

    /* in xrdp_sec.c */
    if (xrdp_sec_process_mcs_data(self->sec_layer) != 0)
    {
        return 1;
    }

    /* in xrdp_sec.c */
    if (xrdp_sec_out_mcs_data(self->sec_layer) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_send_connect_response(self) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_recv_edrq(self) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_recv_aurq(self) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_send_aucf(self) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_recv_cjrq(self) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_send_cjcf(self, self->userid,
                           self->userid + MCS_USERCHANNEL_BASE) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_recv_cjrq(self) != 0)
    {
        return 1;
    }

    if (xrdp_mcs_send_cjcf(self, self->userid, MCS_GLOBAL_CHANNEL) != 0)
    {
        return 1;
    }

    DEBUG(("  out xrdp_mcs_incoming"));
    return 0;
}

/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_init(struct xrdp_mcs *self, struct stream *s)
{
    xrdp_iso_init(self->iso_layer, s);
    s_push_layer(s, mcs_hdr, 8);
    return 0;
}

/*****************************************************************************/
/* returns error */
/* Inform the callback that an mcs packet has been sent.  This is needed so
   the module can send any high priority mcs packets like audio. */
static int APP_CC
xrdp_mcs_call_callback(struct xrdp_mcs *self)
{
    int rv;
    struct xrdp_session *session;

    rv = 0;
    /* if there is a callback, call it here */
    session = self->sec_layer->rdp_layer->session;

    if (session != 0)
    {
        if (session->callback != 0)
        {
            /* in xrdp_wm.c */
            rv = session->callback(session->id, 0x5556, 0, 0, 0, 0);
        }
        else
        {
            g_writeln("in xrdp_mcs_send, session->callback is nil");
        }
    }
    else
    {
        g_writeln("in xrdp_mcs_send, session is nil");
    }

    return rv;
}

/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_send(struct xrdp_mcs *self, struct stream *s, int chan)
{
    int len;
    char *lp;
    //static int max_len = 0;

    DEBUG(("  in xrdp_mcs_send"));
    s_pop_layer(s, mcs_hdr);
    len = (s->end - s->p) - 8;

    if (len > 8192 * 2)
    {
        g_writeln("error in xrdp_mcs_send, size too big, its %d", len);
    }

    //if (len > max_len)
    //{
    //  max_len = len;
    //  g_printf("mcs max length is %d\r\n", max_len);
    //}
    //g_printf("mcs length %d max length is %d\r\n", len, max_len);
    //g_printf("mcs length %d\r\n", len);
    out_uint8(s, MCS_SDIN << 2);
    out_uint16_be(s, self->userid);
    out_uint16_be(s, chan);
    out_uint8(s, 0x70);

    if (len >= 128)
    {
        len = len | 0x8000;
        out_uint16_be(s, len);
    }
    else
    {
        out_uint8(s, len);
        /* move everything up one byte */
        lp = s->p;

        while (lp < s->end)
        {
            lp[0] = lp[1];
            lp++;
        }

        s->end--;
    }

    if (xrdp_iso_send(self->iso_layer, s) != 0)
    {
        DEBUG(("  out xrdp_mcs_send error"));
        return 1;
    }

    /* todo, do we need to call this for every mcs packet,
       maybe every 5 or so */
    if (chan == MCS_GLOBAL_CHANNEL)
    {
        xrdp_mcs_call_callback(self);
    }

    DEBUG(("  out xrdp_mcs_send"));
    return 0;
}

/**
 * Internal help function to close the socket
 * @param self
 */
void APP_CC
close_rdp_socket(struct xrdp_mcs *self)
{
    if (self->iso_layer != 0)
    {
        if (self->iso_layer->trans != 0)
        {
            g_tcp_close(self->iso_layer->trans->sck);
            self->iso_layer->trans->sck = 0 ;
            g_writeln("xrdp_mcs_disconnect - socket closed");
            return;
        }
    }
    g_writeln("Failed to close socket");
}

/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_disconnect(struct xrdp_mcs *self)
{
    struct stream *s;

    DEBUG(("  in xrdp_mcs_disconnect"));
    make_stream(s);
    init_stream(s, 8192);

    if (xrdp_iso_init(self->iso_layer, s) != 0)
    {
        free_stream(s);
        close_rdp_socket(self);
        DEBUG(("  out xrdp_mcs_disconnect error - 1"));
        return 1;
    }

    out_uint8(s, (MCS_DPUM << 2) | 1);
    out_uint8(s, 0x80);
    s_mark_end(s);

    if (xrdp_iso_send(self->iso_layer, s) != 0)
    {
        free_stream(s);
        close_rdp_socket(self);
        DEBUG(("  out xrdp_mcs_disconnect error - 2"));
        return 1;
    }

    free_stream(s);
    close_rdp_socket(self);
    DEBUG(("xrdp_mcs_disconnect - close sent"));
    return 0;
}