File: main.C

package info (click to toggle)
dxpc 3.9.2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,140 kB
  • ctags: 1,253
  • sloc: cpp: 12,036; sh: 2,823; makefile: 78
file content (1359 lines) | stat: -rw-r--r-- 40,114 bytes parent folder | download | duplicates (3)
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
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
#include "dxpcconf.h"

#include <sys/time.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#if !defined(__MINGW32__)
# include <sys/wait.h>
# include <sys/socket.h>
# include <sys/resource.h>
# include <sys/utsname.h>
# include <netdb.h>
# include <netinet/in.h>
# include <pwd.h>
# include <arpa/inet.h>
#endif
#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
# include <netinet/tcp.h>
# include <sys/un.h>
#endif

#include "X-headers.H"

#ifdef _AIX
# include <strings.h>
#endif /* _AIX */

#include "constants.H"
#include "util.H"
#include "ClientMultiplexer.H"
#include "ServerMultiplexer.H"

#include "Compresser.H"

#if defined(__EMX__ ) || defined(__CYGWIN32__) || defined(__MINGW32__)

struct sockaddr_un
{
    u_short sun_family;         /* socket family: AF_UNIX */
    char sun_path[108];         /* path name (not used) */
};

#endif

#ifdef _AIX
# include <sys/select.h>
#endif /* _AIX */

#if defined(hpux) && !defined(RLIM_INFINITY)
/* HP-UX hides this define */
# define    RLIM_INFINITY   0x7fffffff
#endif

static void Usage(const char **argv);
static void Cleanup();
static void HandleSignal(int);
static int AwaitConnection(int portNum);
static int ConnectToRemote(char *remoteHost, int portNum);
static void DaemonInit(unsigned int);
static void KillDaemon(unsigned int);
static void MakeLockFileName(char *, unsigned int);

static int VersionNumbersMatch(int sockfd);
static int ReadDataCh(int fd, char *buf, int maxlen, char stop);
static int parseRemoteOptions(char *opts);

// This variable tells whether or not the client should initiate the
// connection.  (see -w option)
static int ClientInitiateConnection = 0;

// Maximum number of open file descriptors for this process
static unsigned int maxNumFDs = 0;

// Variable to tell output routines whether they should be quiet or not
// Added for -f option
// This variable is used by other files
int silent = 0;

// Variable to indicate whether the user wants backing store turned on
// to help reduce network traffic at some cost in server memory.
// This variable is used by other files.
int wantBackingStore = 0;

// These two variables are needed for the forking
// We don't fork by default since old versions didn't
static int dofork = 0;
static char lockfilename[512] = { 0 };

// And the default lockfilename - this goes in the user's $HOME dir
static const char *LOCK_FILE_NAME = ".dxpc.pid";

// Now statistics can go to a file
char logfilename[1024] = { 0 };
OSTREAM *logofs;

// Controls miniLZO PutImage compression: used by other files.
int compressImages = -1;

// Info about sockets used by the client proxy (global so that
// the cleanup signal handler can access it)
static char udomSocketPathname[100];
static int useUnixDomainSocket = 1;
sockaddr_in serverAddr;

// dxpc runs in client mode or server mode
enum ProxyMode
{
    PROXY_CLIENT, PROXY_SERVER
}
proxyMode = PROXY_SERVER;

// Macro is TRUE if we should initiate the connection.
#define WE_INITIATE_CONNECTION \
    ((proxyMode == PROXY_SERVER && !ClientInitiateConnection) \
  || (proxyMode == PROXY_CLIENT && ClientInitiateConnection))

// #define COREDUMPS_ENABLED

#ifdef COREDUMPS_ENABLED
int enableCoredumps(void)
{
    rlimit rlim;

    if (getrlimit(RLIMIT_CORE, &rlim))
    {
        CERR << "Cannot read RLIMIT_CORE: " << strerror(errno) << ENDL;
        return -1;
    }

    if (rlim.rlim_cur < rlim.rlim_max)
    {
        rlim.rlim_cur = rlim.rlim_max;
        if (setrlimit(RLIMIT_CORE, &rlim))
        {
            CERR << "Cannot set RLIMIT_CORE: " << strerror(errno) << ENDL;
            return -2;
        }
    }
    CERR << "Set RLIMIT_CORE to " << rlim.rlim_max << ENDL;
    return 0;
}
#endif

int main(int argc, char **argv)
{
    udomSocketPathname[0] = 0;

    unsigned int proxyPort = DEFAULT_PROXY_PORT;
    unsigned int displayNum = DEFAULT_DISPLAY_NUM;
    unsigned int statisticsLevel = 0;
    int useTCPSocket = 1;
    char *remoteHost = NULL;
    const char *displaySrc = 0;

#ifdef COREDUMPS_ENABLED
    enableCoredumps();
#endif

    // used to use getopt here, but it caused too many
    // portability problems
    for (int argi = 1; argi < argc; argi++)
    {
        char *nextArg = argv[argi];

        if (*nextArg == '-')
        {
            switch (nextArg[1])
            {
            case 'b':
                {
                    const char *arg =
                        GetArg(argi, argc, (const char **) argv);
                    if (!arg)
                        Usage((const char **) argv);
                    if (*arg == 'a')
                        wantBackingStore = 2 /* Always */ ;
                    else if (*arg == 'm' || *arg == 'w')
                        wantBackingStore = 1 /* WhenMapped */ ;
                    else if (*arg == 'n')
                        wantBackingStore = 0;
                    else
                        Usage((const char **) argv);
                }
                break;

            case 'd':
                {
                    const char *arg =
                        GetArg(argi, argc, (const char **) argv);
                    if (arg == NULL)
                        Usage((const char **) argv);
                    else
                        displayNum = atoi(arg);
                }
                break;
            case 'D':
                displaySrc = GetArg(argi, argc, (const char **) argv);
                if (!displaySrc)
                {
                    Usage((const char **) argv);
                }
                break;
            case 'f':
                dofork = 1;
                break;
            case 'k':
                KillDaemon(proxyPort);  // This function doesn't return

                break;
            case 'l':
                {
                    const char *arg =
                        GetArg(argi, argc, (const char **) argv);
                    if (!arg)
                        Usage((const char **) argv);
                    else
                        strcpy(logfilename, arg);
                }
                break;
            case 'p':
                {
                    const char *arg =
                        GetArg(argi, argc, (const char **) argv);
                    if (arg == NULL)
                        Usage((const char **) argv);
                    else
                        proxyPort = atoi(arg);
                }
                break;
            case 's':
                {
                    const char *arg =
                        GetArg(argi, argc, (const char **) argv);
                    if (arg == NULL)
                        Usage((const char **) argv);
                    else
                        statisticsLevel = atoi(arg);
                }
                break;
            case 't':
                useTCPSocket = 0;
                break;
            case 'u':
                useUnixDomainSocket = 0;
                break;
            case 'v':
                PrintVersionInfo();
                exit(0);
            case 'w':
                ClientInitiateConnection = 1;
                break;

            case 'i':
                {
                    const char *arg =
                        GetArg(argi, argc, (const char **) argv);
                    if (arg == NULL)
                        Usage((const char **) argv);
                    else
                        compressImages = atoi(arg);
                }
                break;
            default:
                Usage((const char **) argv);
            }
        }
        else
        {
            if (remoteHost != NULL)
                Usage((const char **) argv);
            else
                remoteHost = nextArg;
        }
    }

    if (logfilename[0] != '\0')
    {
        logofs = new OFSTREAM(logfilename, IOS_OUT);
    }
    else
    {
        logofs = &COUT;
    }

    int xServerAddrFamily = AF_INET;
    sockaddr *xServerAddr = NULL;
    unsigned int xServerAddrLength = 0;

    if ((!remoteHost && !ClientInitiateConnection) ||
        (remoteHost && ClientInitiateConnection))
    {
        COUT << "dxpc proxy running in CLIENT mode" << ENDL;
        proxyMode = PROXY_CLIENT;
    }
    else
    {
        COUT << "dxpc proxy running in SERVER mode" << ENDL;
        proxyMode = PROXY_SERVER;

        // Keep Cleanup() from deleting the real X server's pipe
        // when we exit...
        useUnixDomainSocket = 0;

        // $DISPLAY is the X server for which we'll act as a proxy
        if (!displaySrc)
        {
            displaySrc = getenv("DISPLAY");
        }
        if ((displaySrc == NULL) || (*displaySrc == 0))
        {
            CERR << "$DISPLAY is not set" << ENDL;
            Cleanup();
        }
        char *display = new char[strlen(displaySrc) + 1];

        if (!display)
        {
            CERR << "Out of memory duping DISPLAY" << ENDL;
            Cleanup();
        }
        strcpy(display, displaySrc);
        char *separator = strchr(display, ':');

        if ((separator == NULL) || !isdigit(*(separator + 1)))
        {
            CERR << "invalid DISPLAY '" << display << "'" << ENDL;
            Cleanup();
        }
        *separator = 0;
        int displayNum = atoi(separator + 1);

        if ((separator == display) || !strcmp(display, "unix"))
        {
            // UNIX domain port
            xServerAddrFamily = AF_UNIX;
            sockaddr_un *xServerAddrUNIX = new sockaddr_un;

            xServerAddrUNIX->sun_family = AF_UNIX;
            sprintf(udomSocketPathname, "/tmp/.X11-unix/X%d", displayNum);
            struct stat statInfo;

            if (stat(udomSocketPathname, &statInfo) == -1)
            {
#if (defined(__hpux) || defined(hpux)) && !defined(PGID_USE_PID)
                sprintf(udomSocketPathname, "/usr/spool/sockets/X11/%d",
                        displayNum);
                if (stat(udomSocketPathname, &statInfo) == -1)
                {
#endif
                    CERR << "cannot open UNIX domain connection to X server"
                        << ENDL;
                    Cleanup();
#if (defined(__hpux) || defined(hpux)) && !defined(PGID_USE_PID)
                }
#endif
            }
            strcpy(xServerAddrUNIX->sun_path, udomSocketPathname);
            xServerAddr = (sockaddr *) xServerAddrUNIX;
            //      xServerAddrLength = strlen(udomSocketPathname) + 2;
            xServerAddrLength = sizeof(sockaddr_un);
        }
        else
        {
            // TCP port
            xServerAddrFamily = AF_INET;
            int ipAddr;
            hostent *hostAddr = gethostbyname(display);

            if (hostAddr == NULL)
            {
                // on some UNIXes, gethostbyname doesn't accept IP addresses,
                // so try inet_addr:
                ipAddr = (int) inet_addr(display);
                if (ipAddr == -1)
                {
                    CERR << "Unknown host '" << display << "'" << ENDL;
                    Cleanup();
                }
            }
            else
                ipAddr = *(int *) hostAddr->h_addr_list[0];
            sockaddr_in *xServerAddrTCP = new sockaddr_in;

            xServerAddrTCP->sin_family = AF_INET;
            xServerAddrTCP->sin_port = htons(X_TCP_PORT + displayNum);
            xServerAddrTCP->sin_addr.s_addr = ipAddr;
            xServerAddr = (sockaddr *) xServerAddrTCP;
            xServerAddrLength = sizeof(sockaddr_in);
        }
        if (display)
        {
            delete[]display;
            display = 0;
        }
    }
    // Sanity check compressImages.
    if (WE_INITIATE_CONNECTION)
    {
        if (compressImages != -1)
        {
            CERR << "warning: image compression option (-i) ignored "
                << "when initiating connection" << ENDL;
            compressImages = -1;
        }
    }
    else
    {
        // We will accept connections; we control image compression.
        if (compressImages == -1)
        {
            // Default image compression level.
            compressImages = DEFAULT_IMAGE_COMPRESSION_LEVEL;
        }
        else if (compressImages &&
                 !Compresser::isValidCompressionLevel(compressImages))
        {
            CERR << "warning: invalid image compression level "
                << compressImages << ": image compression disabled" << ENDL;
            compressImages = 0;
        }
        if (!silent)
        {
            COUT << "using image compression level "
                << compressImages << ENDL;
        }
    }

    // Increase the max # of open file descriptors for this process

    maxNumFDs = 0;
#if defined(RLIMIT_NOFILE)
    rlimit limits;

    if (getrlimit(RLIMIT_NOFILE, &limits) == 0)
    {
        if (limits.rlim_max == RLIM_INFINITY)
            maxNumFDs = 0;
        else
            maxNumFDs = (unsigned int) limits.rlim_max;
    }
#endif /* RLIMIT_NOFILE */

#if defined(_SC_OPEN_MAX)
    if (maxNumFDs == 0)
        maxNumFDs = sysconf(_SC_OPEN_MAX);
#endif

#if defined(FD_SETSIZE)
    if (maxNumFDs > FD_SETSIZE)
        maxNumFDs = FD_SETSIZE;
#endif /* FD_SETSIZE */

#if defined(RLIMIT_NOFILE)
    if (limits.rlim_cur < maxNumFDs)
    {
        limits.rlim_cur = maxNumFDs;
        setrlimit(RLIMIT_NOFILE, &limits);
    }
#endif /* RLIMIT_NOFILE */

#ifndef __MINGW32__
    if (maxNumFDs == 0)
    {
        CERR <<
            "cannot determine number of available file descriptors, exiting!"
            << ENDL;
        return 1;
    }

    // Install some signal handlers for graceful shutdown
    signal(SIGHUP, HandleSignal);
    signal(SIGINT, HandleSignal);
    signal(SIGTERM, HandleSignal);

    signal(SIGPIPE, (void (*)(int)) SIG_IGN);
#else // __MINGW32__
    WSADATA wsaData;

    if (WSAStartup(0x20, &wsaData))
    {
        CERR << "WSAStartup failed" << ENDL;
        return 1;
    }
#endif // __MINGW32__

    // If running as client proxy, open sockets that mimic an
    // X display to which X clients can connect (e.g., unix:8
    // and <hostname>:8)
    int tcpFD = -1;
    int unixFD = -1;

    if (proxyMode == PROXY_CLIENT)
    {
        if (useTCPSocket)
        {
            // Open TCP socket for display
            tcpFD = socket(AF_INET, SOCK_STREAM, PF_UNSPEC);
            if (tcpFD == -1)
            {
                CERR << "socket() failed for TCP socket, errno=" <<
                    errno << ENDL;
                Cleanup();
            }
            int flag = 1;

            if (setsockopt(tcpFD, SOL_SOCKET, SO_REUSEADDR, (char *) &flag,
                           sizeof(flag)) < 0)
            {
                CERR <<
                    "setsockopt(SO_REUSEADDR) failed for TCP socket, errno = "
                    << errno << ENDL;
            }

            SETNODELAY(tcpFD);

            sockaddr_in tcpAddr;

            tcpAddr.sin_family = AF_INET;
            unsigned int xPortTCP = X_TCP_PORT + displayNum;

            tcpAddr.sin_port = htons(xPortTCP);
            tcpAddr.sin_addr.s_addr = htonl(INADDR_ANY);
            if (bind(tcpFD, (sockaddr *) & tcpAddr, sizeof(tcpAddr)) == -1)
            {
                CERR << "bind() failed for TCP port " << xPortTCP <<
                    ", errno=" << errno << ENDL;
                Cleanup();
            }
            if (listen(tcpFD, 5) == -1)
            {
                CERR << "listen() failed for TCP port " << xPortTCP <<
                    ", errno=" << errno << ENDL;
                Cleanup();
            }
        }
        if (useUnixDomainSocket)
        {
            // Open UNIX domain socket for display
            unixFD = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
            if (unixFD == -1)
            {
                CERR << "socket() failed for UNIX domain socket, errno=" <<
                    errno << ENDL;
                Cleanup();
            }
            sockaddr_un unixAddr;

            unixAddr.sun_family = AF_UNIX;
            struct stat dirStat;

            if ((stat("/tmp/.X11-unix", &dirStat) == -1) && (errno == ENOENT))
            {
                mkdir("/tmp/.X11-unix"
#ifndef __MINGW32__
                      , 0777
#endif
                    );
                chmod("/tmp/.X11-unix", 0777);
            }
            sprintf(udomSocketPathname, "/tmp/.X11-unix/X%d", displayNum);
            strcpy(unixAddr.sun_path, udomSocketPathname);
            if (bind(unixFD, (sockaddr *) & unixAddr,
                     strlen(udomSocketPathname) + 2) == -1)
            {
                CERR << "bind() failed for UNIX domain socket " <<
                    udomSocketPathname << ", errno=" << errno << ENDL;
                CERR <<
                    "This probably means you do not have sufficient rights to "
                    << "write to /tmp/.X11-unix/." << ENDL <<
                    "Either use the -u option or obtain the necessary rights."
                    << ENDL;
                Cleanup();
            }
            if (listen(unixFD, 5) == -1)
            {
                CERR << "listen() failed for UNIX domain socket " <<
                    udomSocketPathname << ", errno=" << errno << ENDL;
                Cleanup();
            }
        }
    }
    if (dofork)
    {
        DaemonInit(proxyPort);
    }
    // Set up low-bandwidth connection between the proxies
    int proxyFD = -1;

    if (WE_INITIATE_CONNECTION)
    {
        proxyFD = ConnectToRemote(remoteHost, proxyPort);
        // Compare the version number sent by the host to our version. If we don't
        // get back an identical string we exit.
        if (!VersionNumbersMatch(proxyFD))
        {
            Cleanup();
        }
        if (!silent)
        {
            *logofs << "connected to "
                << ((proxyMode == PROXY_CLIENT) ? "server" : "client")
                << " proxy\nready" << ENDL;
        }
    }
    else
    {
        proxyFD = AwaitConnection(proxyPort);

        if (!silent)
        {
            *logofs << "connected to "
                << ((proxyMode == PROXY_CLIENT) ? "server" : "client")
                << " proxy\nready" << ENDL;
        }
    }

    // Create multiplexer
    Multiplexer *multiplexer;

    if (proxyMode == PROXY_SERVER)
        multiplexer = new ServerMultiplexer(proxyFD, xServerAddrFamily,
                                            xServerAddr, xServerAddrLength,
                                            statisticsLevel);
    else
        multiplexer = new ClientMultiplexer(proxyFD, statisticsLevel);

    if (compressImages)
    {
        int err = lzo_init();

        if (err != LZO_E_OK)
        {
            CERR << "warning: cannot initialize image compression library"
                << " (error " << err << ")" << ENDL;
            compressImages = 0;
        }
    }

    // Loop ENDLessly, reading from all of the open file descriptors
    for (;;)
    {
        fd_set readSet;

        FD_ZERO(&readSet);
        FD_SET(proxyFD, &readSet);
        unsigned int numFDsToSelect = proxyFD + 1;

        if (proxyMode == PROXY_CLIENT)
        {
            if (useTCPSocket)
            {
                FD_SET(tcpFD, &readSet);
                if (tcpFD >= (int) numFDsToSelect)
                    numFDsToSelect = tcpFD + 1;
            }
            if (useUnixDomainSocket)
            {
                FD_SET(unixFD, &readSet);
                if (unixFD >= (int) numFDsToSelect)
                    numFDsToSelect = unixFD + 1;
            }
        }
        multiplexer->setSelectFDs(&readSet, numFDsToSelect);

        timeval delay;

        delay.tv_sec = 600;
        delay.tv_usec = 0;

        // PGID_USE_PID, defined in <sys/types.h>, is specific to HP-UX 10.x
#if (defined(__hpux) || defined(hpux)) && !defined(PGID_USE_PID)
        int result =
            select(numFDsToSelect, (int *) &readSet, NULL, NULL, &delay);
#else
        int result = select(numFDsToSelect, &readSet, NULL, NULL, &delay);
#endif
        if (result == -1)
        {
            if (errno == EINTR)
                continue;
            CERR << "select() failed, errno=" << errno << ENDL;
            delete multiplexer;
            Cleanup();
        }
        for (unsigned int j = 0; j < numFDsToSelect; j++)
        {
            if (!(FD_ISSET(j, &readSet)))
                continue;

            if (proxyMode == PROXY_CLIENT)
            {
                if ((((int) j == tcpFD) && useTCPSocket)
                 || (((int) j == unixFD) && useUnixDomainSocket))
                {
                    int newFD = -1;

                    if (((int) j == tcpFD) && useTCPSocket)
                    {
                        sockaddr_in newAddr;
                        ACCEPT_SOCKLEN_T addrLen = sizeof(sockaddr_in);
                        newFD = accept(tcpFD, (sockaddr *) & newAddr, &addrLen);
                    }
                    else
                    {
                        sockaddr_un newAddr;
                        ACCEPT_SOCKLEN_T addrLen = sizeof(sockaddr_un);
                        newFD = accept(unixFD, (sockaddr *) & newAddr, &addrLen);
                    }

                    if (newFD == -1)
                    {
                        CERR << "accept() failed, errno=" << errno << ENDL;
                        delete multiplexer;
                        Cleanup();
                    }

                    SETNODELAY(newFD);

                    multiplexer->createNewConnection(newFD);
                    continue;
                }
            }
            if (!multiplexer->handleSelect(j))
            {
                delete multiplexer;
                Cleanup();
            }
        }
    }

    return 0;
}

static void Usage(const char **argv)
{
    CERR << "Usage: " << argv[0] <<
        " [common options] [client options | server options] [connect options]"
        << ENDL << ENDL;
    CERR << "[common options]" << ENDL <<
        "    -p port_num         Specifies the TCP port on which the LISTENING process"
        << ENDL <<
        "                        listens, or to which the CONNECTING process connects"
        << ENDL <<
        "                        (default is " << DEFAULT_PROXY_PORT << ")." 
	<< ENDL <<
        "    -f                  fork (starts process in background)."
        << ENDL
        <<
        "    -k                  kill (kills backgrounded process started via -f)."
        <<
        ENDL
        <<
        "    -v                  Print version/license info and exit."
        <<
        ENDL
        <<
        "    -s (1|2)            Print compression stats; -s 1 prints a summary on"
        <<
        ENDL
        <<
        "                        exit, -s 2 prints details for each X application."
        <<
        ENDL
        <<
        "    -l log_file         write output to a logfile instead of stdout."
        << ENDL << ENDL;
    CERR << "[client options] (only meaningful to the CLIENT process)" 
        << ENDL << 
        "    -i compression_lvl  Specify image bitmap compression level (0-9,99,"
        << ENDL <<
        "                        999). 0 disables bitmap compression; 999 is maximal;"
        << ENDL <<
        "                        other values are tradeoffs of speed vs. compression."
        << ENDL <<
        "                        Default is " << DEFAULT_IMAGE_COMPRESSION_LEVEL << "."
        << ENDL <<
        "    -d display_num      Specify display number to emulate. Default is 8."
        << ENDL <<
        "    -u                  Do not attempt to open UNIX domain socket for"
        << ENDL <<
        "                        proxied server."
        << ENDL <<
        "    -t                  Do not attempt to open TCP socket for proxied server."
        << ENDL << ENDL;
    CERR << "[server options] (only meaningful to the SERVER process)" 
	<< ENDL <<
        "    -D                  Specify X host on which to display proxied"
        << ENDL <<
        "                        applications. Defaults to value of the DISPLAY"
        << ENDL <<
        "                        environment variable."
        << ENDL <<
        "    -b (a|w)            force backing store (-ba = always, -bw = when mapped)"
        << ENDL << ENDL;
    CERR << "[connect options]" 
	<< ENDL <<
        "     hostname           The name of the machine on which the LISTENING"
        << ENDL <<
        "                        process is running. The presence of the hostname"
        << ENDL <<
        "                        argument specifies that this is the CONNECTING"
        << ENDL <<
        "                        process. Its absence specifies that this is the"
        << ENDL <<
        "                        LISTENING process."
        << ENDL <<
        "     -w                 If this is the CONNECTING process, specifies that it"
        << ENDL <<
        "                        is the CLIENT process (by default, the CONNECTING"
        << ENDL <<
        "                        process is the SERVER process). If this is the"
        << ENDL <<
        "                        LISTENING process, specifies that it is the SERVER"
        << ENDL <<
        "                        process (by default, the LISTENING process is the"
        << ENDL << 
	"                        CLIENT process)." 
	<< ENDL << ENDL;
    CERR << "Notes on modes:" << ENDL << ENDL;
    CERR <<
        "dxpc has two modes; the connection mode, which is either LISTENING or"
        << ENDL <<
        "CONNECTING; and the X mode, which is either CLIENT or SERVER."
        << ENDL << ENDL;
    CERR <<
        "The LISTENING process waits for a CONNECTING process to initiate the TCP"
        << ENDL <<
        "connection between the two processes. The LISTENING process must always be"
        << ENDL <<
        "started first. The CONNECTING process initiates the connection to the"
        << ENDL <<
        "LISTENING process. dxpc will run as the CONNECTING process if a hostname"
        << ENDL <<
        "argument is supplied (see connect options, above). Otherwise it will run as"
        << ENDL << 
	"the LISTENING process." 
	<< ENDL << ENDL;
    CERR <<
        "The SERVER process is typically located on the same machine as the real X"
        << ENDL <<
        "server, and is responsible for displaying the output of applications. The"
        << ENDL <<
        "CLIENT process is typically located on the same machine as the X"
        << ENDL <<
        "applications, and is responsible for forwarding the output of those"
        << ENDL <<
        "applications to the SERVER process. By default, dxpc runs as the CLIENT"
        << ENDL <<
        "process if it is the LISTENING process (due to the lack of a hostname"
        << ENDL <<
        "argument) and the SERVER process if it is the CONNECTING process, but the -w"
        << ENDL << "switch reverses this (see connect options, above)."
        << ENDL << ENDL;
    CERR <<
        "For example, the command 'dxpc myhost.work.com' starts dxpc as the"
        << ENDL <<
        "CONNECTING process (because a host name is supplied) and the SERVER process"
        << ENDL <<
        "(because it is the CONNECTING process and -w is not supplied). The command"
        << ENDL <<
        "'dxpc -w' starts dxpc as the LISTENING process (because no hostname is"
        << ENDL <<
        "supplied) and the SERVER process (because it is the LISTENING process, and"
        << ENDL << "-w reverses the usual logic)." << ENDL;

    exit(1);
}

static void Cleanup()
{
    if (!silent)
        *logofs << "Closing all file descriptors and shutting down..." <<
            ENDL;

    if (dofork)
        if (remove(lockfilename))
            perror("Unable to remove lockfile");

    if (useUnixDomainSocket)
        unlink(udomSocketPathname);

    for (unsigned int i = 0; i < maxNumFDs; i++)
        (void) close(i);

    exit(1);
}

static void HandleSignal(int)
{
    Cleanup();
}

// Open TCP socket to listen for server proxy; block until server
// proxy connects, then close listener socket and return FD of socket
// on which server proxy is connected
//
static int AwaitConnection(int portNum)
{
    int proxyFD = socket(AF_INET, SOCK_STREAM, 0);

    if (proxyFD == -1)
    {
        CERR << "socket() failed for TCP socket, errno=" << errno << ENDL;
        Cleanup();
    }
    int flag = 1;

    if (setsockopt(proxyFD, SOL_SOCKET, SO_REUSEADDR, (char *) &flag,
                   sizeof(flag)) < 0)
    {
        CERR << "setsockopt(SO_REUSEADDR) failed for proxy port, errno=" <<
            errno << ENDL;
    }
    sockaddr_in tcpAddr;

    tcpAddr.sin_family = AF_INET;
    tcpAddr.sin_port = htons(portNum);
    tcpAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(proxyFD, (sockaddr *) & tcpAddr, sizeof(tcpAddr)) == -1)
    {
        CERR << "bind() failed for TCP port " << portNum <<
            ", errno=" << errno << ENDL;
        Cleanup();
    }
    if (listen(proxyFD, 1) == -1)
    {
        CERR << "listen() failed for TCP port " << portNum <<
            ", errno=" << errno << ENDL;
        Cleanup();
    }
    for (;;)
    {
        fd_set readSet;

        FD_ZERO(&readSet);
        FD_SET(proxyFD, &readSet);
#if (defined(__hpux) || defined(hpux)) && !defined(PGID_USE_PID)
        int result = select(proxyFD + 1, (int *) &readSet, NULL, NULL, NULL);
#else
        int result = select(proxyFD + 1, &readSet, NULL, NULL, NULL);
#endif
        if (result == -1)
        {
            if (errno == EINTR)
                continue;
            CERR << "select() failed, errno=" << errno << ENDL;
            Cleanup();
        }
        if (FD_ISSET(proxyFD, &readSet))
        {
            sockaddr_in newAddr;
            ACCEPT_SOCKLEN_T addrLen = sizeof(sockaddr_in);
            int newFD = accept(proxyFD, (sockaddr *) & newAddr, &addrLen);

            if (newFD == -1)
            {
                CERR << "accept() failed, errno=" << errno << ENDL;
                Cleanup();
            }
            // Now we send our version number.
            char version[40];

            if (!WE_INITIATE_CONNECTION)
            {
                sprintf(version, "DXPC %i.%i/ci=%d",
                        DXPC_VERSION_MAJOR, DXPC_VERSION_MINOR,
                        compressImages);
            }
            else
            {
                sprintf(version, "DXPC %i.%i", DXPC_VERSION_MAJOR,
                        DXPC_VERSION_MINOR);
            }
            SOCKWRITE(newFD, version, strlen(version) + 1);

            // If the client doesn't like our version it will simply close the
            // socket.

            SOCKCLOSE(proxyFD);
            return newFD;
        }
    }
}

// Connect to remote proxy.  If successful, return FD of connection;
// if unsuccessful, return -1
//
static int ConnectToRemote(char *remoteHost, int portNum)
{
    int remoteIPAddr;
    hostent *hostAddr = gethostbyname(remoteHost);

    if (hostAddr == NULL)
    {
        // on some UNIXes, gethostbyname doesn't accept IP addresses,
        // so try inet_addr:
        remoteIPAddr = (int) inet_addr(remoteHost);
        if (remoteIPAddr == -1)
        {
            CERR << "Unknown host '" << remoteHost << "'" << ENDL;
            Cleanup();
        }
    }
    else
        remoteIPAddr = *(int *) hostAddr->h_addr_list[0];

    int remoteProxyFD = socket(AF_INET, SOCK_STREAM, PF_UNSPEC);

    if (remoteProxyFD == -1)
    {
        CERR << "socket() failed, errno=" << errno << ENDL;
        Cleanup();
    }
    int flag = 1;

    if (setsockopt(remoteProxyFD, SOL_SOCKET, SO_REUSEADDR, (char *) &flag,
                   sizeof(flag)) < 0)
    {
        CERR << "setsockopt(SO_REUSEADDR) failed for proxy port, errno=" <<
            errno << ENDL;
    }
    if (!silent)
    {
        *logofs << "trying to connect to remote proxy..." << ENDL;
    }
    sockaddr_in addr;

    addr.sin_family = AF_INET;
    addr.sin_port = htons(portNum);
    addr.sin_addr.s_addr = remoteIPAddr;
    if (connect(remoteProxyFD, (sockaddr *) & addr, sizeof(sockaddr_in)) ==
        -1)
    {
        CERR << "connect() failed, errno=" << errno << ENDL;
        SOCKCLOSE(remoteProxyFD);
        Cleanup();
    }
    CERR << "connect succeeded." << ENDL;

    SETNODELAY(remoteProxyFD);

    return remoteProxyFD;
}

static int VersionNumbersMatch(int sockfd)
{
    // We consider the version numbers to match if the major and minor
    // numbers match.  Different patch levels should by definition be
    // compatible with each other.

    char version[20];
    char recvmsg[40];
    char *opts;

    sprintf(version, "DXPC %i.%i", DXPC_VERSION_MAJOR, DXPC_VERSION_MINOR);

    if (ReadDataCh(sockfd, recvmsg, sizeof(recvmsg), '\0') < 0)
    {
        CERR << "No version number from far end" << ENDL;
        return 0;
    }

    // Split any available options off from version number.
    if ((opts = strchr(recvmsg, '/')) != NULL)
    {
        *opts++ = 0;
    }

    if (strncmp(recvmsg, version, strlen(version)))
    {
        // Make sure recvmsg will be printable
        unsigned int ctr;

        for (ctr = 0;
             ctr < strlen(recvmsg) &&
             (isgraph(recvmsg[ctr]) || isspace(recvmsg[ctr])); ctr++);

        recvmsg[ctr] = 0;

        CERR << "Error version numbers don't match!" << ENDL
            << "Local version: " << version << ENDL
            << "Remote version: " << recvmsg << ENDL;

        return 0;
    }

    if (parseRemoteOptions(opts))
    {
        // Not strictly a version number mismatch, but fail anyway.
        return 0;
    }

    // We initiated the connection; if we didn't learn a compression level,
    // we're screwed.
    if (compressImages == -1)
    {
        CERR << "error: host didn't specify image compression level" << ENDL;
        return 0;
    }
    return 1;
}

static int ReadDataCh(int fd, char *buf, int maxlen, char stop)
{
    int ctr = 0;
    int result;

    while (ctr < maxlen)
    {
        if ((result = SOCKREAD(fd, buf + ctr, 1)) == -1 || !result)
        {
            return -1;
        }
        if (result && *(buf + ctr++) == stop)
        {
            return ctr;
        }
    }

    return 0;
}

static void DaemonInit(unsigned int displayNum)
{
#if defined(__EMX__) || defined(__MINGW32__)
    CERR << "The daemon option is disabled on this platform" << ENDL;
    exit(-1);
#else

    switch (fork())
    {
    case -1:
        perror("dxpc");
        Cleanup();
    case 0:
        break;
    default:
        exit(0);
    }
    pid_t pid;

    if ((pid = setsid()) == -1)
    {
        CERR << "Error setsid() failed." << ENDL;
        Cleanup();
    }
    MakeLockFileName(lockfilename, displayNum);

    // First we try to open the file to see if dxpc is already running
    FILE *pidfile = fopen(lockfilename, "r");

    if (pidfile)
    {
        // The open was successful
        // So we try and read a pid out of it.
	long oldpidval;

	switch (fscanf(pidfile, "%ld", &oldpidval))
        {
        case 0:
	    CERR << "Found invalid old pidfile " << lockfilename
                << ".  Overriding." << ENDL;
            break;
        case EOF:
            CERR << "Error reading from old pidfile " << lockfilename
                << ".  Overriding." << ENDL;
            break;
        default:
            int override = 0;

            switch (kill(oldpidval, 0))
            {
            case ESRCH:
            case EPERM:
                // Either the pid doesn't exist or is owned by someone
                // else.  It's probably safe to override.
                CERR << "Stale pidfile found.  Overriding." << ENDL;
                override = 1;
                break;
            default:
                CERR << "Error.  It appears another dxpc is running at pid "
                    << oldpidval << ENDL
                    << "If this isn't correct, then delete " << lockfilename
                    << ENDL;
            }
            if (override)
                break;
            fclose(pidfile);
            dofork = 0;         // So Cleanup() won't delete the lockfile

            Cleanup();
        }

        fclose(pidfile);
    }
    if (!(pidfile = fopen(lockfilename, "w")))
    {
        perror("dxpc");
        Cleanup();
    }
    fprintf(pidfile, "%lu", (unsigned long) pid);
    fclose(pidfile);

    // Now turn off all non-error output to the console
    silent = 1;

#endif
    return;
}

void KillDaemon(unsigned int displayNum)
{
#ifndef __MINGW32__
    char lockfilename[512];

    MakeLockFileName(lockfilename, displayNum);

    FILE *pidfile = fopen(lockfilename, "r");

    if (pidfile)
    {
        // The open was successful
        // So we try and read a pid out of it.
	long pidval;

	switch (fscanf(pidfile, "%ld", &pidval))
        {
        case 0:
        case EOF:
            CERR << "Error reading pid from " << lockfilename << ENDL
                << "You will have to manually kill the daemon." << ENDL;

            break;
        default:
            fclose(pidfile);

            COUT << "Killing dxpc at pid " << pidval << ENDL;

            if (kill(pidval, SIGTERM) == -1)
            {
                perror("dxpc");
                CERR << "Leaving pidfile intact." << ENDL;
                exit(1);
            }
        }
        exit(0);
    }
    else
    {
        CERR << "No daemon is running." << ENDL;
        exit(1);
    }
#else
    // MingW.
    CERR << "Kill not supported on mingw." << ENDL;
    exit(1);
#endif
}

#ifndef __MINGW32__
static void MakeLockFileName(char *lockfilename, unsigned int displayNum)
{
    char *homedir = getenv("HOME");

    if (!homedir)
    {
        CERR << "You have no environment variable HOME!" << ENDL
            << "How did that happen?" << ENDL;
        exit(1);
    }
    strcpy(lockfilename, homedir);
    strcat(lockfilename, "/");
    strcat(lockfilename, LOCK_FILE_NAME);       // constants.h
    struct utsname unameInfo;

    if (uname(&unameInfo) == -1)
    {
        unameInfo.nodename[0] = 0;
    }
    else
    {
        char *dotptr = strchr(unameInfo.nodename, '.');

        if (dotptr)
            *dotptr = 0;
        strcat(lockfilename, "-");
        strcat(lockfilename, unameInfo.nodename);
    }

    struct passwd *passdata;

    if ((passdata = getpwuid(getuid())) != 0)
    {
        strcat(lockfilename, "-");
        strcat(lockfilename, passdata->pw_name);
    }
    else
    {
        strcat(lockfilename, "");
    }
    sprintf(lockfilename + strlen(lockfilename), "-%u", displayNum);
}
#endif // __MINGW32__

// Parse the option string passed from the remote end at startup.
static int parseRemoteOptions(char *opts)
{
    char *name, *value;

    // The options string is intended to be a series of name/value
    // tuples in the form name=value seperated by the '/' character.
    name = strtok(opts, "=");
    while (name)
    {
        value = strtok(NULL, "/");
        if (!value)
        {
            CERR << "error in remote option " << name
                << ": no value found" << ENDL;
            return -1;
        }

        // Currently we only have one known option...
        if (!strcmp(name, "ci"))
        {
            if (WE_INITIATE_CONNECTION)
            {
                // The far end just told us what level of image compression
                // to use.
                compressImages = atoi(value);
                if (!silent)
                {
                    COUT << "using image compression level "
                        << compressImages << ENDL;
                }
            }
            else
            {
                CERR << "image compression option from initiating side"
                    " ignored" << ENDL;
            }
        }
        else
        {
            CERR << "unknown remote option: " << name << " = " << value
                << ENDL;
            return -1;
        }

        name = strtok(NULL, "=");
    }
    return 0;
}