aboutsummaryrefslogtreecommitdiff
path: root/doc/nx-X11_vs_XOrg69_patches/Xtranssock.c.NX.patch
blob: fc81419d716f01ec52439f4bea4b66d36211ec85 (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
--- ./nx-X11/lib/xtrans/Xtranssock.c.X.original	2015-02-13 14:03:44.672442927 +0100
+++ ./nx-X11/lib/xtrans/Xtranssock.c	2015-02-13 14:03:44.672442927 +0100
@@ -53,6 +53,35 @@
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+/**************************************************************************/
+/*                                                                        */
+/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
+/*                                                                        */
+/* NX-X11, NX protocol compression and NX extensions to this software     */
+/* are copyright of NoMachine. Redistribution and use of the present      */
+/* software is allowed according to terms specified in the file LICENSE   */
+/* which comes in the source distribution.                                */
+/*                                                                        */
+/* Check http://www.nomachine.com/licensing.html for applicability.       */
+/*                                                                        */
+/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
+/*                                                                        */
+/* All rights reserved.                                                   */
+/*                                                                        */
+/**************************************************************************/
+
+#ifdef NX_TRANS_SOCKET
+
+#ifdef NX_TRANS_DEBUG
+#define XTRANSDEBUG 5
+#endif
+
+#ifndef PF_LOCAL
+#define PF_LOCAL PF_UNIX
+#endif
+
+#endif
+
 #include <ctype.h>
 #ifdef XTHREADS
 #include <X11/Xthreads.h>
@@ -294,6 +323,560 @@
 static int haveIPv6 = 1;
 #endif
 
+#ifndef X11_t
+
+/*
+ * No NX changes if this is not
+ * compiled as a X11 transport.
+ */
+
+#undef NX_TRANS_SOCKET
+
+#endif
+
+#ifdef NX_TRANS_SOCKET
+
+#ifdef TRANS_CLIENT
+
+#include "NX.h"
+
+typedef struct
+{
+    XtransConnInfo info;
+    int            local;
+    int            remote;
+    int            congestion;
+
+} _NXProxyConnInfo;
+
+#define NX_PROXY_CONN_LIMIT     256
+
+static _NXProxyConnInfo *_NXProxyConnInfoTab[NX_PROXY_CONN_LIMIT];
+
+#endif /* #ifdef TRANS_CLIENT */
+
+/*
+ * Override the UNIX_DIR and UNIX_PATH settings and
+ * make them configurable, based on the NX_TEMP or
+ * the TEMP environment.
+ *
+ * We must be careful as the same defines are used
+ * for different directories, based on the subsystem
+ * that is compiling this, while we want to override
+ * only the '/tmp/.X11-unix' and '/tmp/.X11-unix/X'
+ * settings.
+ */
+
+static char _NXUnixDir[1024];
+static char _NXUnixPath[1024];
+
+static char *_NXGetUnixDir(char *dir)
+{
+    const char *tempDir;
+
+    PRMSG (3, "_NXGetUnixDir(%s)\n", dir, 0, 0);
+
+    if (strcmp(dir, UNIX_DIR) != 0)
+    {
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_NXGetUnixDir: Returning other Unix directory [%s].\n", dir);
+#endif
+        return dir;
+    }
+
+    /*
+     * Check the environment only once.
+     */
+
+    if (*_NXUnixDir != '\0')
+    {
+        return _NXUnixDir;
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_NXGetUnixDir: Trying with the NX_TEMP environment.\n");
+#endif
+
+    tempDir = getenv("NX_TEMP");
+
+    if (tempDir == NULL || *tempDir == '\0')
+    {
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_NXGetUnixDir: Trying with the TEMP environment.\n");
+#endif
+
+        tempDir = getenv("TEMP");
+    }
+
+    if (tempDir != NULL && *tempDir != '\0')
+    {
+        if (strlen(tempDir) + strlen("/.X11-unix") + 1 > 1024)
+        {
+#ifdef NX_TRANS_TEST
+            fprintf(stderr, "_NXGetUnixDir: WARNING! Maximum length of X11 Unix directory exceeded.\n");
+#endif
+            goto _NXGetUnixDirError;
+        }
+
+        strcpy(_NXUnixDir, tempDir);
+        strcat(_NXUnixDir, "/.X11-unix");
+
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_NXGetUnixDir: Using X11 Unix directory [%s].\n", _NXUnixDir);
+#endif
+
+        return _NXUnixDir;
+    }
+
+_NXGetUnixDirError:
+
+    strcpy(_NXUnixDir, dir);
+ 
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_NXGetUnixDir: Returning default X11 Unix directory [%s].\n", _NXUnixDir);
+#endif
+
+    return _NXUnixDir;
+}
+
+static char *_NXGetUnixPath(char *path)
+{
+    const char *unixDir;
+
+    PRMSG (3, "_NXGetUnixPath(%s)\n", path, 0, 0);
+
+    if (strcmp(path, UNIX_PATH) != 0)
+    {
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_NXGetUnixPath: Returning other X11 Unix path [%s].\n", path);
+#endif
+        return path;
+    }
+
+    /*
+     * Check the environment only once.
+     */
+
+    if (*_NXUnixPath != '\0')
+    {
+        return _NXUnixPath;
+    }
+
+    unixDir = _NXGetUnixDir(UNIX_DIR);
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_NXGetUnixPath: Got X11 Unix directory [%s].\n", unixDir);
+#endif
+
+    if (strlen(unixDir) + strlen("/X") + 1 > 1024)
+    {
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_NXGetUnixPath: WARNING! Maximum length of X11 Unix path exceeded.\n");
+#endif
+
+        goto _NXGetUnixPathError;
+    }
+
+    strcpy(_NXUnixPath, unixDir);
+    strcat(_NXUnixPath, "/X");
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_NXGetUnixPath: Returning X11 Unix path [%s].\n", _NXUnixPath);
+#endif
+
+    return _NXUnixPath;
+
+_NXGetUnixPathError:
+
+    strcpy(_NXUnixPath, path);
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_NXGetUnixPath: Returning default X11 Unix path [%s].\n", _NXUnixPath);
+#endif
+
+    return _NXUnixPath;
+}
+
+#ifdef hpux
+
+static char *_NXGetOldUnixPath(char *path)
+{
+    PRMSG (3, "_NXGetOldUnixPath(%s)\n", path, 0, 0);
+
+    if (strcmp(path, OLD_UNIX_PATH) == 0)
+    {
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_NXGetOldUnixPath: Returning X11 Unix path [%s].\n",
+                    _NXGetUnixPath(path));
+#endif
+
+        return _NXGetUnixPath(path);
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_NXGetOldUnixPath: Returning other old X11 Unix path [%s].\n", path);
+#endif
+
+    return path;
+}
+
+#endif /* #ifdef hpux */
+
+/*
+ * Forcibly close any connection attempt on the
+ * listening socket. Need this to avoid loopback
+ * connections to the agent server.
+ */
+
+#ifdef TRANS_SERVER
+
+void TRANS(SocketRejectConnection) (XtransConnInfo ciptr)
+{
+    size_t sa_l = sizeof(struct sockaddr);
+    struct sockaddr sa;
+    fd_set fs;
+    struct timeval t;
+    int f;
+
+    PRMSG (3, "SocketRejectConnection(%x)\n", ciptr, 0, 0);
+
+    FD_ZERO(&fs);
+    FD_SET(ciptr -> fd, &fs);
+
+    t.tv_sec  = 0;
+    t.tv_usec = 0;
+
+    /*
+     * Check if there is an awaiting connection.
+     */
+
+    if (select(ciptr -> fd + 1, &fs, NULL, NULL, &t) == 1)
+    {
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "SocketRejectConnection: Accepting connection attempt on fd [%d].\n",
+                    ciptr -> fd);
+#endif
+        /*
+         * If there is one, close it.
+         */
+ 
+        if ((f = accept(ciptr -> fd, &sa, &sa_l)) >= 0)
+        {
+#ifdef NX_TRANS_TEST
+            fprintf(stderr, "SocketRejectConnection: Closing connection attempt on fd [%d].\n",
+                        ciptr -> fd);
+#endif
+            close(f);
+        }
+    }
+}
+
+#endif /* #ifdef TRANS_SERVER */
+
+#ifdef TRANS_CLIENT
+
+void *TRANS(SocketProxyConnInfo) (XtransConnInfo ciptr)
+{
+    if (_NXProxyConnInfoTab[ciptr->fd] != NULL)
+    {
+      return ciptr->priv;
+    }
+
+    return NULL;
+}
+
+static XtransConnInfo TRANS(SocketCreateConnInfo) ()
+{
+    XtransConnInfo ciptr;
+
+    int fds[2];
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCreateConnInfo: Going to create the NX connection info.\n");
+#endif
+
+    if ((ciptr = (XtransConnInfo) xcalloc (1, sizeof(struct _XtransConnInfo))) == NULL)
+    {
+        PRMSG (1, "SocketCreateConnInfo: malloc failed\n", 0, 0, 0);
+        return NULL;
+    }
+
+    /*
+     * Create a pair of sockets. We'll communicate with
+     * the NX proxy by reading and writing to our end.
+     */
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCreateConnInfo: Going to create the NX socketpair.\n");
+#endif
+
+    if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0)
+    {
+        PRMSG (1, "SocketCreateConnInfo: socketpair() failed.\n", 0, 0, 0);
+        xfree ((char *) ciptr);
+        return NULL;
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCreateConnInfo: X socket end is [%d] NX proxy end is [%d].\n",
+                fds[0], fds[1]);
+#endif
+
+    /*
+     * Save in _NXProxyConnInfoTab the local and remote end of
+     * the socketpair. The remote end will be used by the proxy.
+     * When the memory-to-memory transport is activated, the
+     * agent and the proxy don't read or write to the real des-
+     * criptors but the communication takes place by reading
+     * and writing to the proxy's buffers.
+     */
+
+    ciptr->fd = fds[0];
+
+    if (ciptr->fd >= NX_PROXY_CONN_LIMIT)
+    {
+        PRMSG (1, "SocketCreateConnInfo: No space for a new _NXProxyConnInfo for [%d].\n",
+                  ciptr->fd, 0, 0);
+        xfree ((char *) ciptr);
+        return NULL;
+    }
+    else if (_NXProxyConnInfoTab[ciptr->fd] != NULL)
+    {
+        PRMSG (1, "SocketCreateConnInfo: _NXProxyConnInfo for [%d] is not NULL. Exiting.\n",
+                   ciptr->fd, 0, 0);
+        exit(1);
+    }
+
+    _NXProxyConnInfoTab[ciptr->fd] = (_NXProxyConnInfo *) xcalloc(1, sizeof(_NXProxyConnInfo));
+
+    if (_NXProxyConnInfoTab[ciptr->fd] == NULL)
+    {
+        PRMSG (1, "SocketCreateConnInfo: Alloc of _NXProxyConnInfo failed.\n", 0, 0, 0);
+        xfree ((char *) ciptr);
+        return NULL;
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCreateConnInfo: Allocated new _NXProxyConnInfo for [%d].\n",
+                ciptr->fd);
+#endif
+
+    _NXProxyConnInfoTab[ciptr->fd]->info       = ciptr;
+    _NXProxyConnInfoTab[ciptr->fd]->local      = fds[0];
+    _NXProxyConnInfoTab[ciptr->fd]->remote     = fds[1];
+    _NXProxyConnInfoTab[ciptr->fd]->congestion = 0;
+
+    ciptr->priv = (char *) _NXProxyConnInfoTab[ciptr->fd];
+
+    return ciptr;
+}
+
+static int TRANS(SocketConnectConnInfo) (XtransConnInfo ciptr, char *host, char *port)
+{
+    int fds[2];
+    char display[1024];
+
+    _NXProxyConnInfo *proxy_conn;
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketConnectConnInfo: Going to connect NX fd [%d] to host [%s] port [%s].\n",
+                ciptr->fd, host, port);
+#endif
+
+    /*
+     * We should have already created the socket pair.
+     */
+
+    proxy_conn = (_NXProxyConnInfo *) ciptr->priv;
+
+    if (proxy_conn == NULL)
+    {
+        PRMSG (1, "SocketConnectConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n", 0, 0, 0);
+
+        exit(1);
+    }
+    else if (_NXProxyConnInfoTab[ciptr->fd] != (_NXProxyConnInfo *) ciptr->priv)
+    {
+        PRMSG (1, "SocketConnectConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n",
+                   0, 0, 0);
+
+        exit(1);
+    }
+
+    if (strlen(host) + strlen(port) + 1 >= 1023)
+    {
+        PRMSG (1, "SocketConnectConnInfo: Length of NX display string '%s:%s' would exceed %d characters.\n",
+                   host, port, 1023);
+
+        return TRANS_CONNECT_FAILED;
+    }
+
+    sprintf(display, "%s:%s", host, port);
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketConnectConnInfo: Setting close-on-exec flag on local NX descriptor [%d].\n",
+                proxy_conn -> local);
+#endif
+
+#ifdef F_SETFD
+#ifdef FD_CLOEXEC
+    if (fcntl(proxy_conn -> local, F_SETFD, FD_CLOEXEC) != 0)
+#else
+    if (fcntl(proxy_conn -> local, F_SETFD, 1) != 0)
+#endif
+#endif
+    {
+        PRMSG (1, "SocketConnectConnInfo: Cannot set close-on-exec on local NX descriptor [%d].\n",
+                   proxy_conn -> local, 0, 0);
+
+        return TRANS_CONNECT_FAILED;
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketConnectConnInfo: Creating the NX transport with display [%s].\n",
+                display);
+#endif
+
+    if (NXTransCreate(NX_FD_ANY, NX_MODE_CLIENT, display) < 0)
+    {
+        PRMSG (1, "SocketConnectConnInfo: Cannot create the NX transport.\n",
+                   0, 0, 0);
+
+        return TRANS_CONNECT_FAILED;
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketConnectConnInfo: Starting the NX agent with descriptor [%d].\n",
+                proxy_conn -> remote);
+#endif
+
+    fds[0] = proxy_conn -> local;
+    fds[1] = proxy_conn -> remote;
+
+    NXTransAgent(fds);
+
+    return 0;
+}
+
+static void TRANS(SocketCloseConnInfo) (XtransConnInfo ciptr)
+{
+    _NXProxyConnInfo *proxy_conn;
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCloseConnInfo: Going to close the NX fd [%d].\n", ciptr->fd);
+#endif
+
+    proxy_conn = (_NXProxyConnInfo *) ciptr->priv;
+
+    if (proxy_conn == NULL)
+    {
+        PRMSG (1, "SocketCloseConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n", 0, 0, 0);
+
+        exit(1);
+    }
+    else if (ciptr->fd >= NX_PROXY_CONN_LIMIT ||
+                 _NXProxyConnInfoTab[ciptr->fd] != (_NXProxyConnInfo *) ciptr->priv)
+    {
+        PRMSG (1, "SocketCloseConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n",
+                   0, 0, 0);
+        exit(1);
+    }
+    else if (_NXProxyConnInfoTab[ciptr->fd] -> info != ciptr ||
+                 _NXProxyConnInfoTab[ciptr->fd] -> local != ciptr->fd)
+    {
+        PRMSG (1, "SocketCloseConnInfo: Invalid _NXProxyConnInfo structure for [%d]. Exiting.\n",
+                   ciptr->fd, 0, 0);
+        exit(1);
+    }
+    else if (proxy_conn->local < 0 || proxy_conn->remote < 0)
+    {
+        PRMSG (1, "SocketCloseConnInfo: Invalid socket pair in NX connection for [%d]. Exiting.\n",
+                   ciptr->fd, 0, 0);
+        exit(1);
+    }
+
+    NXTransClose(ciptr->fd);
+
+    /*
+     *  Get rid of the _NXProxyConnInfo structure.
+     */
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCloseConnInfo: Freeing _NXProxyConnInfo structure for [%d].\n",
+                ciptr->fd);
+#endif
+
+    xfree((char *) _NXProxyConnInfoTab[ciptr->fd]);
+
+    _NXProxyConnInfoTab[ciptr->fd] = NULL;
+
+    ciptr->priv = NULL;
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCloseConnInfo: Should now close the local descriptor [%d].\n",
+                ciptr->fd);
+#endif
+}
+
+#endif /* #ifdef TRANS_CLIENT */
+
+#if defined(TRANS_CLIENT) && defined(NX_TRANS_CHANGE)
+
+/*
+ * Check the congestion state of the NX transport
+ * and return 1 if there has been a change. This
+ * can be extended by adding a few counters track-
+ * ing the bandwidth usage of the X11 connection.
+ */
+
+int TRANS(SocketCongestionChange) (XtransConnInfo ciptr, int *state)
+{
+    int congestion;
+
+    _NXProxyConnInfo *proxy_conn;
+
+    PRMSG (3, "SocketCongestionChange(%x)\n", ciptr, 0, 0);
+
+    proxy_conn = (_NXProxyConnInfo *) ciptr->priv;
+
+    if (proxy_conn == NULL)
+    {
+#ifdef NX_TRANS_DEBUG
+        fprintf(stderr, "SocketCongestionChange: Descriptor [%d] doesn't appear to be a NX connection.\n",
+                    ciptr->fd);
+#endif
+        return 0;
+    }
+
+#ifdef NX_TRANS_DEBUG
+    fprintf(stderr, "SocketCongestionChange: Checking congestion on fd [%d] with old state [%d].\n",
+                ciptr->fd, proxy_conn->congestion);
+#endif
+
+    congestion = NXTransCongestion(ciptr->fd);
+
+    if (congestion != proxy_conn->congestion)
+    {
+        proxy_conn->congestion = congestion;
+
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "SocketCongestionChange: Change detected on fd [%d] with new state [%d].\n",
+                    ciptr->fd, proxy_conn->congestion);
+#endif
+        return 1;
+    }
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketCongestionChange: No change on fd [%d] with current state [%d].\n",
+                ciptr->fd, congestion);
+#endif
+    return 0;
+}
+
+#endif /* #if defined(TRANS_CLIENT) && defined(NX_TRANS_CHANGE) */
+
+#endif /* #ifdef NX_TRANS_SOCKET */
+
 /*
  * These are some utility function used by the real interface function below.
  */
@@ -562,6 +1145,29 @@
     SocketInitOnce();
 
     while ((i = TRANS(SocketSelectFamily) (i, transname)) >= 0) {
+
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+        if ((!strcmp(protocol, "local") || !strcmp(protocol, "nx")) &&
+                (!strcasecmp(host, "nx") || !strncasecmp(host, "nx,", 3)))
+        {
+            ciptr = TRANS(SocketCreateConnInfo) ();
+
+            if (ciptr == NULL)
+            {
+                PRMSG (1, "SocketOpenCOTSClient: Unable to create the NX connection info for %s.\n",
+                           transname, 0, 0);
+
+                return NULL;
+            }
+
+            ciptr->index = i;
+
+            return ciptr;
+        }
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
 	if ((ciptr = TRANS(SocketOpen) (
 		 i, Sockettrans2devtab[i].devcotsname)) != NULL)
 	    break;
@@ -576,6 +1182,12 @@
 	return NULL;
     }
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    ciptr->priv = NULL;
+
+#endif
+
     /* Save the index for later use */
 
     ciptr->index = i;
@@ -677,6 +1289,29 @@
     SocketInitOnce();
 
     while ((i = TRANS(SocketSelectFamily) (i, thistrans->TransName)) >= 0) {
+
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+        if ((!strcmp(protocol, "local") || !strcmp(protocol, "nx")) &&
+                (!strcasecmp(host, "nx") || !strncasecmp(host, "nx,", 3)))
+        {
+            ciptr = TRANS(SocketCreateConnInfo) ();
+
+            if (ciptr == NULL)
+            {
+                PRMSG (1, "SocketOpenCLTSClient: Unable to create the NX connection info for %s.\n",
+                           thistrans->TransName, 0, 0);
+
+                return NULL;
+            }
+
+            ciptr->index = i;
+
+            return ciptr;
+        }
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
 	if ((ciptr = TRANS(SocketOpen) (
 		 i, Sockettrans2devtab[i].devcotsname)) != NULL)
 	    break;
@@ -691,6 +1326,12 @@
 	return NULL;
     }
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    ciptr->priv = NULL;
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
     /* Save the index for later use */
 
     ciptr->index = i;
@@ -826,6 +1467,11 @@
 {
     PRMSG (2,"SocketSetOption(%d,%d,%d)\n", ciptr->fd, option, arg);
 
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "SocketSetOption: WARNING! Not setting option [%d] with value [%d] on descriptor [%d].\n",
+                option, arg, ciptr -> fd);
+#endif
+
     return -1;
 }
 
@@ -875,6 +1521,11 @@
     else
 	retry = 0;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
+    fprintf(stderr, "SocketCreateListener: Creating listener for ciptr at [%p] on path [%s].\n",
+                (void *) ciptr, ((struct sockaddr_un *) sockname)->sun_family == AF_UNIX ?
+                    ((struct sockaddr_un *) sockname)->sun_path : "TCP");
+#endif
     while (bind (fd, (struct sockaddr *) sockname, namelen) < 0)
     {
 	if (errno == EADDRINUSE) {
@@ -926,6 +1577,11 @@
 
     ciptr->flags = 1 | (ciptr->flags & TRANS_KEEPFLAGS);
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
+    fprintf(stderr, "SocketCreateListener: Set flags to [%d] for ciptr [%p].\n",
+                ciptr->flags, (void *) ciptr);
+#endif
+
     return 0;
 }
 
@@ -1084,9 +1740,15 @@
 #else
     mode = 0777;
 #endif
+#ifdef NX_TRANS_SOCKET
+    if (trans_mkdir(_NXGetUnixDir(UNIX_DIR), mode) == -1) {
+        PRMSG (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n",
+               _NXGetUnixDir(UNIX_DIR), errno, 0);
+#else
     if (trans_mkdir(UNIX_DIR, mode) == -1) {
 	PRMSG (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n",
 	       UNIX_DIR, errno, 0);
+#endif
 	(void) umask (oldUmask);
 	return TRANS_CREATE_LISTENER_FAILED;
     }
@@ -1095,12 +1757,20 @@
     sockname.sun_family = AF_UNIX;
 
     if (port && *port) {
+#ifdef NX_TRANS_SOCKET
+        if (set_sun_path(port, _NXGetUnixPath(UNIX_PATH), sockname.sun_path) != 0) {
+#else
 	if (set_sun_path(port, UNIX_PATH, sockname.sun_path) != 0) {
+#endif
 	    PRMSG (1, "SocketUNIXCreateListener: path too long\n", 0, 0, 0);
 	    return TRANS_CREATE_LISTENER_FAILED;
 	}
     } else {
+#ifdef NX_TRANS_SOCKET
+	sprintf (sockname.sun_path, "%s%ld", _NXGetUnixPath(UNIX_PATH), (long)getpid());
+#else
 	sprintf (sockname.sun_path, "%s%ld", UNIX_PATH, (long)getpid());
+#endif
     }
 
 #if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) && !defined(Lynx)
@@ -1110,6 +1780,10 @@
     namelen = strlen(sockname.sun_path) + sizeof(sockname.sun_family);
 #endif
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
+    fprintf(stderr, "SocketUNIXCreateListener: Unlinking path [%s] for ciptr at [%p].\n",
+                sockname.sun_path, (void *) ciptr);
+#endif
     unlink (sockname.sun_path);
 
     if ((status = TRANS(SocketCreateListener) (ciptr,
@@ -1181,9 +1855,15 @@
 #else
 	mode = 0777;
 #endif
+#ifdef NX_TRANS_SOCKET
+        if (trans_mkdir(_NXGetUnixDir(UNIX_DIR), mode) == -1) {
+            PRMSG (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n",
+            _NXGetUnixDir(UNIX_DIR), errno, 0);
+#else
         if (trans_mkdir(UNIX_DIR, mode) == -1) {
             PRMSG (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n",
 	    UNIX_DIR, errno, 0);
+#endif
 	    (void) umask (oldUmask);
 	    return TRANS_RESET_FAILURE;
         }
@@ -1962,7 +2642,12 @@
      * we know for sure it will fail.
      */
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+    if (strcmp(host, "unix") != 0 && strcasecmp(host, "nx") != 0 &&
+            strncasecmp(host, "nx,", 3) != 0 && !UnixHostReallyLocal (host))
+#else
     if (strcmp (host, "unix") != 0 && !UnixHostReallyLocal (host))
+#endif
     {
 	PRMSG (1,
 	   "SocketUNIXConnect: Cannot connect to non-local host %s\n",
@@ -1988,7 +2673,11 @@
     
     sockname.sun_family = AF_UNIX;
 
+#ifdef NX_TRANS_SOCKET
+    if (set_sun_path(port, _NXGetUnixPath(UNIX_PATH), sockname.sun_path) != 0) {
+#else
     if (set_sun_path(port, UNIX_PATH, sockname.sun_path) != 0) {
+#endif
 	PRMSG (1, "SocketUNIXConnect: path too long\n", 0, 0, 0);
 	return TRANS_CONNECT_FAILED;
     }
@@ -2006,7 +2695,11 @@
      * This is gross, but it was in Xlib
      */
     old_sockname.sun_family = AF_UNIX;
+#ifdef NX_TRANS_SOCKET
+    if (set_sun_path(port, _NXGetOldUnixPath(OLD_UNIX_PATH), old_sockname.sun_path) != 0) {
+#else
     if (set_sun_path(port, OLD_UNIX_PATH, old_sockname.sun_path) != 0) {
+#endif
 	PRMSG (1, "SocketUNIXConnect: path too long\n", 0, 0, 0);
 	return TRANS_CONNECT_FAILED;
     }
@@ -2014,6 +2707,19 @@
 	sizeof (old_sockname.sun_family);
 #endif
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    if (ciptr->priv != NULL)
+    {
+        if (TRANS(SocketConnectConnInfo) (ciptr, host, port) != 0)
+        {
+            return TRANS_CONNECT_FAILED;
+        }
+
+        goto SocketUNIXConnectPost;
+    }
+
+#endif
 
     /*
      * Do the connect()
@@ -2065,6 +2771,12 @@
 	}
     }
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+SocketUNIXConnectPost:
+
+#endif
+
     /*
      * Get the socket name and the peer name from the connect socket,
      * since this is unix domain.
@@ -2099,6 +2811,58 @@
 {
     PRMSG (2,"SocketBytesReadable(%p,%d,%p)\n",
 	ciptr, ciptr->fd, pend);
+
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    if (ciptr->priv)
+    {
+        if (NXTransRunning(ciptr->fd) == 0)
+        {
+            /*
+             * Force the application to shut down the
+             * socket if the NX transport is gone. We
+             * may probably save this additional call.
+             */
+
+#ifdef NX_TRANS_DEBUG
+            fprintf(stderr, "SocketBytesReadable: NX transport not running for descriptor [%d].\n",
+                        ciptr->fd);
+#endif
+            ESET(EPIPE);
+
+            return -1;
+        }
+        else
+        {
+            /*
+             * Emulate BytesReadable. Some X applications may use the system
+             * select() in their main loop, instead of the _XSelect() that is
+             * replaced by NX. Still these applications use _XEventsQueued to
+             * poll events from the X connection, and _XEventsQueued uses the
+             * NX _XSelect(), so it is generally possible to let the client
+             * yield the control to NX and let it handle the I/O on the proxy
+             * descriptors even if the application is not explicitly designed
+             * to work as a NX agent.         
+             */
+
+#ifdef NX_TRANS_DEBUG
+
+            int result;
+
+            result = NXTransReadable(ciptr->fd, (int *) pend);
+
+            fprintf(stderr, "SocketBytesReadable: Descriptor [%d] result [%d] readable [%ld].\n",
+                        ciptr->fd, result, *pend);
+
+            return result;
+#else
+            return NXTransReadable(ciptr->fd, (int *) pend);
+#endif
+        }
+    }
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
 #if defined(QNX4)
     *pend = 0L; /* FIONREAD only returns a short. Zero out upper bits */
 #endif
@@ -2128,6 +2892,41 @@
 {
     PRMSG (2,"SocketRead(%d,%p,%d)\n", ciptr->fd, buf, size);
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    /*
+     * If we have a valid priv pointer then this
+     * is an internal connection to the proxy.
+     * In this case we should emulate the read.
+     */
+
+    if (ciptr->priv)
+    {
+        int result;
+
+        result = NXTransRead(ciptr->fd, buf, size);
+
+#ifdef NX_TRANS_DEBUG
+        if (result < 0 && EGET() == EAGAIN)
+        {
+            fprintf(stderr, "SocketRead: Read from descriptor [%d] would block.\n",
+                     ciptr->fd);
+        }
+        else
+        {
+            fprintf(stderr, "SocketRead: Read [%d] bytes from descriptor [%d].\n",
+                     result, ciptr->fd);
+        }
+#endif
+        return result;
+    }
+    else
+    {
+        return read (ciptr->fd, buf, size);
+    }
+
+#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
 #if defined(WIN32) || defined(__UNIXOS2__)
     {
 	int ret = recv ((SOCKET)ciptr->fd, buf, size, 0);
@@ -2139,6 +2938,8 @@
 #else
     return read (ciptr->fd, buf, size);
 #endif /* WIN32 */
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
 }
 
 
@@ -2148,6 +2949,41 @@
 {
     PRMSG (2,"SocketWrite(%d,%p,%d)\n", ciptr->fd, buf, size);
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    /*
+     * If we have a valid priv pointer then this
+     * is an internal connection to the proxy.
+     * In this case we should emulate the write.
+     */
+
+    if (ciptr->priv)
+    {
+        int result;
+
+        result = NXTransWrite(ciptr->fd, buf, size);
+
+#ifdef NX_TRANS_DEBUG
+        if (result < 0 && EGET() == EAGAIN)
+        {
+            fprintf(stderr, "SocketWrite: Write on descriptor [%d] would block.\n",
+                     ciptr->fd);
+        }
+        else
+        {
+            fprintf(stderr, "SocketWrite: Written [%d] bytes on descriptor [%d].\n",
+                     result, ciptr->fd);
+        }
+#endif
+        return result;
+    }
+    else
+    {
+        return write (ciptr->fd, buf, size);
+    }
+
+#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
 #if defined(WIN32) || defined(__UNIXOS2__)
     {
 	int ret = send ((SOCKET)ciptr->fd, buf, size, 0);
@@ -2159,6 +2995,8 @@
 #else
     return write (ciptr->fd, buf, size);
 #endif /* WIN32 */
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
 }
 
 
@@ -2168,7 +3006,28 @@
 {
     PRMSG (2,"SocketReadv(%d,%p,%d)\n", ciptr->fd, buf, size);
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    /*
+     * If we have a valid priv pointer then this
+     * is an internal connection to the proxy.
+     * In this case we should emulate the readv.
+     */
+
+    if (ciptr->priv)
+    {
+        return NXTransReadVector(ciptr->fd, buf, size);
+    }
+    else
+    {
+        return READV (ciptr, buf, size);
+    }
+
+#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
     return READV (ciptr, buf, size);
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
 }
 
 
@@ -2178,7 +3037,28 @@
 {
     PRMSG (2,"SocketWritev(%d,%p,%d)\n", ciptr->fd, buf, size);
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    /*
+     * If we have a valid priv pointer then this
+     * is an internal connection to the proxy.
+     * In this case we should emulate the writev.
+     */
+
+    if (ciptr->priv)
+    {
+        return NXTransWriteVector(ciptr->fd, buf, size);
+    }
+    else
+    {
+        return WRITEV (ciptr, buf, size);
+    }
+
+#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
+
     return WRITEV (ciptr, buf, size);
+
+#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */
 }
 
 
@@ -2234,17 +3114,41 @@
     struct sockaddr_un	*sockname = (struct sockaddr_un *) ciptr->addr;
     int ret;
 
-    PRMSG (2,"SocketUNIXClose(%p,%d)\n", ciptr, ciptr->fd, 0);
+    PRMSG (2,"SocketUNIXClose(%x,%d)\n", ciptr, ciptr->fd, 0);
+
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    if (ciptr->priv)
+    {
+        TRANS(SocketCloseConnInfo) (ciptr);
+    }
+
+#endif
 
     ret = close(ciptr->fd);
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
+    fprintf(stderr, "SocketUNIXClose: Flags are [%d] for ciptr at [%p] check is [%d].\n",
+                ciptr->flags, (void *) ciptr, (ciptr->flags && sockname
+                    && sockname->sun_family == AF_UNIX && sockname->sun_path[0]));
+#endif
+
     if (ciptr->flags
        && sockname
        && sockname->sun_family == AF_UNIX
        && sockname->sun_path[0])
     {
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
+        if (!(ciptr->flags & TRANS_NOUNLINK))
+        {
+                fprintf(stderr, "SocketUNIXClose: Unlinking path [%s] for ciptr at [%p].\n",
+                            sockname->sun_path, (void *) ciptr);
+                unlink (sockname->sun_path);
+        }
+#else
 	if (!(ciptr->flags & TRANS_NOUNLINK))
 		unlink (sockname->sun_path);
+#endif
     }
 
     return ret;
@@ -2263,6 +3167,15 @@
     PRMSG (2,"SocketUNIXCloseForCloning(%p,%d)\n",
 	ciptr, ciptr->fd, 0);
 
+#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT)
+
+    if (ciptr->priv)
+    {
+        TRANS(SocketCloseConnInfo) (ciptr);
+    }
+
+#endif
+
     ret = close(ciptr->fd);
 
     return ret;