aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/src/Socket.cpp
blob: a9759b76b5103d6cc1be9c453434df8598e3424e (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
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com)          */
/* Copyright (c) 2008-2017 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>  */
/* Copyright (c) 2014-2022 Ulrich Sibiller <uli42@gmx.de>                 */
/* Copyright (c) 2014-2019 Mihai Moldovan <ionic@ionic.de>                */
/* Copyright (c) 2011-2022 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com)           */
/*                                                                        */
/* NXCOMP, NX protocol compression and NX extensions to this software     */
/* are copyright of the aforementioned persons and companies.             */
/*                                                                        */
/* Redistribution and use of the present software is allowed according    */
/* to terms specified in the file LICENSE.nxcomp which comes in the       */
/* source distribution.                                                   */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/* NOTE: This software has received contributions from various other      */
/* contributors, only the core maintainers and supporters are listed as   */
/* copyright holders. Please contact us, if you feel you should be listed */
/* as copyright holder, as well.                                          */
/*                                                                        */
/**************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/utsname.h>

#if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
#include <netinet/in_systm.h>
#endif

#ifdef __sun
#include <unistd.h>
#include <sys/termios.h>
#endif

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#include <netdb.h>
#include <fcntl.h>

//
// System specific defines.
//

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
#define SOL_IP  IPPROTO_IP
#endif

#ifdef __sun
#define INADDR_NONE  ((unsigned int) -1)
#endif

//
// The TIOCOUTQ ioctl is not implemented on Cygwin.
// Note also that TIOCOUTQ and IPTOS_LOWDELAY are
// disabled when running on MacOS/X.
//

#if defined(__CYGWIN__) || defined(__CYGWIN32__)
#define TIOCOUTQ  ((unsigned int) -1)
#endif

//
// NX includes.
//

#include "Misc.h"
#include "Socket.h"

//
// Set verbosity level.
//

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG

//
// Set this only once by querying OS details.
//

static int _kernelStep = -1;

int GetKernelStep()
{
  if (_kernelStep < 0)
  {
    //
    // At the moment only NX clients run on Win32
    // and MacOS/X so we are not really interested
    // in the relevant OS dependent functions.
    //

    #if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__APPLE__)

    _kernelStep = 0;

    #else

    struct utsname buffer;

    if (uname(&buffer) < 0)
    {
      #ifdef WARNING
      *logofs << "Socket: WARNING! Failed to get system info. Error is "
              << EGET() << " '" << ESTR() << "'.\n" << logofs_flush;

      *logofs << "Socket: WARNING! Assuming lowest system support.\n"
              << logofs_flush;
      #endif

      cerr << "Warning" << ": Failed to get system info. Error is "
           << EGET() << " '" << ESTR() << "'.\n";

      cerr << "Warning" << ": Assuming lowest system support.\n";

      _kernelStep = 0;
    }
    else
    {
      #ifdef TEST
      *logofs << "Socket: System is '" << buffer.sysname
              << "' nodename '" << buffer.nodename << "' release '"
              << buffer.release << "'.\n" << logofs_flush;

      *logofs << "Socket: Version is '" << buffer.version << "' machine '"
              << buffer.machine << "'.\n" << logofs_flush;
      #endif

      //
      // Should test support on other operating systems.
      //

      if (strcmp(buffer.sysname, "Linux") == 0)
      {
        if (strncmp(buffer.release, "2.0.", 4) == 0 ||
                strncmp(buffer.release, "2.2.", 4) == 0)
        {
          #ifdef TEST
          *logofs << "Socket: Assuming level 2 system support.\n"
                  << logofs_flush;
          #endif

          _kernelStep = 2;
        }
        else
        {
          #ifdef TEST
          *logofs << "Socket: Assuming level 3 system support.\n"
                  << logofs_flush;
          #endif

          _kernelStep = 3;
        }
      }
      else if (strcmp(buffer.sysname, "SunOS") == 0)
      {
        #ifdef TEST
        *logofs << "Socket: Assuming level 1 system support.\n"
                << logofs_flush;
        #endif

        _kernelStep = 1;
      }
      else
      {
        #ifdef TEST
        *logofs << "Socket: Assuming level 0 system support.\n"
                << logofs_flush;
        #endif

        _kernelStep = 0;
      }
    }

    #endif /* #if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__APPLE__) */
  }

  return _kernelStep;
}

int SetReuseAddress(int fd)
{
  int flag = 1;

  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
                     (char *) &flag, sizeof(flag)) < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set SO_REUSEADDR flag on FD#"
            << fd << ". Error is " << EGET() << " '" << ESTR() << "'.\n"
            << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set SO_REUSEADDR flag on FD#"
            << fd << ". Error is " << EGET() << " '" << ESTR()
            << "'.\n";

    return -1;
  }
  #ifdef TEST
  else
  {
    *logofs << "Socket: Set SO_REUSEADDR flag on FD#"
            << fd << ".\n" << logofs_flush;
  }
  #endif

  return 1;
}

int SetNonBlocking(int fd, int value)
{
  int flags = fcntl(fd, F_GETFL);

  if (flags >= 0)
  {
    if (value == 0)
    {
      flags &= ~O_NONBLOCK;
    }
    else
    {
      flags |= O_NONBLOCK;
    }
  }

  if (flags < 0 || fcntl(fd, F_SETFL, flags) < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set O_NONBLOCK flag on FD#"
            << fd << " to " << value << ". Error is " << EGET()
            << " '" << ESTR() << "'.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set O_NONBLOCK flag on FD#"
         << fd << " to " << value << ". Error is " << EGET()
         << " '" << ESTR() << "'.\n";

    return -1;
  }
  #ifdef TEST
  else
  {
    *logofs << "Socket: Set O_NONBLOCK flag on FD#"
            << fd << " to " << value << ".\n"
            << logofs_flush;
  }
  #endif

  return 1;
}

int SetLingerTimeout(int fd, int timeout)
{
  struct linger linger_value;

  if (timeout > 0)
  {
    linger_value.l_onoff  = 1;
    linger_value.l_linger = timeout;
  }
  else
  {
    linger_value.l_onoff  = 0;
    linger_value.l_linger = 0;
  }

  if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger_value, sizeof(linger_value)) < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set SO_LINGER values to "
            << linger_value.l_onoff << " and " << linger_value.l_linger
            << " on FD#" << fd << ". Error is " << EGET() << " '"
            << ESTR() << "'.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set SO_LINGER values to "
         << linger_value.l_onoff << " and " << linger_value.l_linger
         << " on FD#" << fd << ". Error is " << EGET() << " '"
         << ESTR() << "'.\n";

    return -1;
  }
  #ifdef TEST
  else
  {
    *logofs << "Socket: Set SO_LINGER values to "
            << linger_value.l_onoff << " and " << linger_value.l_linger
            << " on FD#" << fd << ".\n" << logofs_flush;
  }
  #endif

  return 1;
}

int SetSendBuffer(int fd, int size)
{
  if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set SO_SNDBUF size to "
            << size << " on FD#" << fd << ". Error is "
            << EGET() << " '" << ESTR() << "'.\n"
            << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set SO_SNDBUF size to "
         << size << " on FD#" << fd << ". Error is "
         << EGET() << " '" << ESTR() << "'.\n";

    return -1;
  }
  #ifdef TEST
  else
  {
    *logofs << "Socket: Set SO_SNDBUF on FD#" << fd
            << " to " << size << " bytes.\n"
            << logofs_flush;
  }
  #endif

  return 1;
}

int SetReceiveBuffer(int fd, int size)
{
  if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set SO_RCVBUF size to "
            << size << " on FD#" << fd << ". Error is "
            << EGET() << " '" << ESTR() << "'.\n"
            << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set SO_RCVBUF size to "
         << size << " on FD#" << fd << ". Error is "
         << EGET() << " '" << ESTR() << "'.\n";

    return -1;
  }
  #ifdef TEST
  else
  {
    *logofs << "Socket: Set SO_RCVBUF on FD#" << fd
            << " to " << size << " bytes.\n"
            << logofs_flush;
  }
  #endif

  return 1;
}

int SetNoDelay(int fd, int value)
{
  int result = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value));

  if (result == 0)
  {
    result = 1;
  }
  else if (result < 0)
  {
    //
    // Is it become a different error on
    // Mac OSX 10.4?
    //

    #if defined(__APPLE__)

    result = 0;

    #endif

    #if defined(__sun)

    if (EGET() == ENOPROTOOPT)
    {
      result = 0;
    }

    #endif

    #if !defined(__APPLE__) && !defined(__sun)

    if (EGET() == EOPNOTSUPP)
    {
      result = 0;
    }

    #endif
  }

  if (result < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set TCP_NODELAY flag on "
            << "FD#" << fd << " to " << value << ". Error is "
            << EGET() << " '" << ESTR() << "'.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set TCP_NODELAY flag on "
         << "FD#" << fd << " to " << value << ". Error is "
         << EGET() << " '" << ESTR() << "'.\n";
  }
  #ifdef TEST
  else if (result == 0)
  {
    #ifdef TEST
    *logofs << "Socket: Option TCP_NODELAY not supported "
            << "on FD#" << fd << ".\n" << logofs_flush;
    #endif
  }
  else
  {
    *logofs << "Socket: Set TCP_NODELAY flag on FD#"
            << fd << " to " << value << ".\n"
            << logofs_flush;
  }
  #endif

  return result;
}

int SetKeepAlive(int fd)
{
  int flag = 1;

  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag)) < 0)
  {
    #ifdef PANIC
    *logofs << "Socket: PANIC! Failed to set SO_KEEPALIVE flag on "
            << "FD#" << fd << ". Error is " << EGET() << " '"
            << ESTR() << "'.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Failed to set SO_KEEPALIVE flag on "
         << "FD#" << fd << ". Error is " << EGET() << " '"
         << ESTR() << "'.\n";

    return -1;
  }
  #ifdef TEST
  else
  {
    *logofs << "Socket: Set SO_KEEPALIVE flag on FD#"
            << fd << ".\n" << logofs_flush;
  }
  #endif

  return 1;
}

int SetLowDelay(int fd)
{
  if (_kernelStep < 0)
  {
    GetKernelStep();
  }

  switch (_kernelStep)
  {
    case 3:
    case 2:
    case 1:
    {
      int flag = IPTOS_LOWDELAY;

      if (setsockopt(fd, SOL_IP, IP_TOS, &flag, sizeof(flag)) < 0)
      {
        if (EGET() == EOPNOTSUPP)
        {
          #ifdef TEST
          *logofs << "Socket: Option IPTOS_LOWDELAY not supported "
                  << "on FD#" << fd << ".\n" << logofs_flush;
          #endif

          return 0;
        }
        else
        {
          #ifdef WARNING
          *logofs << "Socket: WARNING! Failed to set IPTOS_LOWDELAY flag on "
                  << "FD#" << fd << ". Error is " << EGET() << " '" << ESTR()
                  << "'.\n" << logofs_flush;
          #endif

          cerr << "Warning" << ": Failed to set IPTOS_LOWDELAY flag on "
               << "FD#" << fd << ". Error is " << EGET() << " '" << ESTR()
               << "'.\n";

          return -1;
        }
      }
      #ifdef TEST
      else
      {
        *logofs << "Socket: Set IPTOS_LOWDELAY flag on FD#"
                << fd << ".\n" << logofs_flush;
      }
      #endif

      return 1;
    }
    default:
    {
      #ifdef TEST
      *logofs << "Socket: Option IPTOS_LOWDELAY not "
              << "supported on FD#" << fd << ".\n"
              << logofs_flush;
      #endif

      return 0;
    }
  }
}

int SetCloseOnExec(int fd)
{
  if (fcntl(fd, F_SETFD, 1) != 0)
  {
    #ifdef TEST
    *logofs << "NXClient: PANIC! Cannot set close-on-exec "
            << "on FD#" << fd << ". Error is " << EGET()
            << " '" << ESTR() << "'.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Cannot set close-on-exec on FD#"
         << fd << ". Error is " << EGET() << " '" << ESTR()
         << "'.\n";

    return -1;
  }

  return 1;
}

int GetBytesReadable(int fd)
{
  long readable = 0;

  //
  // It may fail, for example at session
  // shutdown.
  //

  if (ioctl(fd, FIONREAD, &readable) < 0)
  {
    #ifdef TEST
    *logofs << "Socket: PANIC! Failed to get bytes readable "
            << "from FD#" << fd << ". Error is " << EGET()
            << " '" << ESTR() << "'.\n" << logofs_flush;
    #endif

    return -1;
  }

  #ifdef TEST
  *logofs << "Socket: Returning " << (int) readable
          << " bytes readable on FD#" << fd << ".\n"
           << logofs_flush;
  #endif

  return (int) readable;
}

int GetBytesWritable(int fd)
{
  if (_kernelStep < 0)
  {
    GetKernelStep();
  }

  long writable;

  switch (_kernelStep)
  {
    case 3:
    {
      //
      // TODO: Should query the real size
      // of the TCP write buffer.
      //

      writable = 16384 - GetBytesQueued(fd);

      if (writable < 0)
      {
        writable = 0;
      }

      break;
    }
    case 2:
    {
      if (ioctl(fd, TIOCOUTQ, (void *) &writable) < 0)
      {
        #ifdef PANIC
        *logofs << "Socket: PANIC! Failed to get bytes writable "
                << "on FD#" << fd << ". Error is " << EGET()
                << " '" << ESTR() << "'.\n" << logofs_flush;
        #endif

        cerr << "Error" << ": Failed to get bytes writable "
             << "on FD#" << fd << ". Error is " << EGET()
             << " '" << ESTR() << "'.\n";

        return -1;
      }

      break;
    }
    default:
    {
      #ifdef TEST
      *logofs << "Socket: Option TIOCOUTQ not supported "
              << "on FD#" << fd << ",\n" << logofs_flush;
      #endif

      //
      // TODO: Should query the real size
      // of the TCP write buffer.
      //

      writable = 16384;

      break;
    }
  }

  #ifdef TEST
  *logofs << "Socket: Returning " << writable
          << " bytes writable on FD#" << fd
          << ".\n" << logofs_flush;
  #endif

  return (int) writable;
}

int GetBytesQueued(int fd)
{
  //
  // The TIOCOUTQ ioctl is not implemented on Cygwin
  // and returns the space available on Linux Kernels
  // 2.0 and 2.2 (like current MIPS for PS/2).
  //

  if (_kernelStep < 0)
  {
    GetKernelStep();
  }

  long queued;

  switch (_kernelStep)
  {
    case 3:
    {
      if (ioctl(fd, TIOCOUTQ, (void *) &queued) < 0)
      {
        #ifdef PANIC
        *logofs << "Socket: PANIC! Failed to get bytes queued "
                << "on FD#" << fd << ". Error is " << EGET()
                << " '" << ESTR() << "'.\n" << logofs_flush;
        #endif

        cerr << "Error" << ": Failed to get bytes queued "
             << "on FD#" << fd << ". Error is " << EGET()
             << " '" << ESTR() << "'.\n";

        return -1;
      }

      break;
    }
    case 2:
    {
      //
      // TODO: Should query the real size
      // of the TCP write buffer.
      //

      queued = 16384 - GetBytesWritable(fd);

      if (queued < 0)
      {
        queued = 0;
      }

      break;
    }
    default:
    {
      #ifdef TEST
      *logofs << "Socket: Option TIOCOUTQ not supported "
              << "on FD#" << fd << ",\n" << logofs_flush;
      #endif

      queued = 0;

      break;
    }
  }

  #ifdef TEST
  *logofs << "Socket: Returning " << queued
          << " bytes queued on FD#" << fd
          << ".\n" << logofs_flush;
  #endif

  return (int) queued;
}

int GetHostAddress(const char *name)
{
  hostent *host = gethostbyname(name);

  if (host == NULL)
  {
    //
    // On some Unices gethostbyname() doesn't
    // accept IP addresses, so try inet_addr.
    //

    IN_ADDR_T address = inet_addr(name);

    if (address == INADDR_NONE)
    {
      #ifdef PANIC
      *logofs << "Socket: PANIC! Failed to resolve address of '"
              << name << "'.\n" << logofs_flush;
      #endif

      cerr << "Error" << ": Failed to resolve address of '"
           << name << "'.\n";

      return 0;
    }

    return (int) address;
  }
  else
  {
    return (*((int *) host -> h_addr_list[0]));
  }
}