aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/src/ServerProxy.cpp
blob: c3fcc300d5470431f4f701921df34758f108b03d (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
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com)          */
/* Copyright (c) 2008-2014 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>  */
/* Copyright (c) 2014-2016 Ulrich Sibiller <uli42@gmx.de>                 */
/* Copyright (c) 2014-2016 Mihai Moldovan <ionic@ionic.de>                */
/* Copyright (c) 2011-2016 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 <unistd.h>

#include "NXalert.h"

#include "Socket.h"

#include "ServerProxy.h"

#include "ServerChannel.h"
#include "GenericChannel.h"

//
// Set the verbosity level.
//

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

//
// Log the operations related to sending
// and receiving the control tokens.
//

#undef  TOKEN

ServerProxy::ServerProxy(int proxyFd) : Proxy(proxyFd)

{
  xServerAddrFamily_ = -1;
  xServerAddrLength_ = 0;

  xServerAddr_    = NULL;
  xServerDisplay_ = NULL;

  cupsServerPort_  = NULL;
  smbServerPort_   = NULL;
  mediaServerPort_ = NULL;
  httpServerPort_  = NULL;

  fontServerPort_ = NULL;

  #ifdef DEBUG
  *logofs << "ServerProxy: Created new object at " << this
          << ".\n" << logofs_flush;
  #endif
}

ServerProxy::~ServerProxy()
{
  delete xServerAddr_;

  delete [] xServerDisplay_;

  delete [] fontServerPort_;

  #ifdef DEBUG
  *logofs << "ServerProxy: Deleted object at " << this
          << ".\n" << logofs_flush;
  #endif
}

void ServerProxy::handleDisplayConfiguration(const char *xServerDisplay, int xServerAddrFamily,
                                                 sockaddr *xServerAddr, unsigned int xServerAddrLength)
{
  delete xServerAddr_;

  xServerAddr_ = xServerAddr;

  xServerAddrFamily_ = xServerAddrFamily;
  xServerAddrLength_ = xServerAddrLength;

  delete [] xServerDisplay_;

  xServerDisplay_ = new char[strlen(xServerDisplay) + 1];

  strcpy(xServerDisplay_, xServerDisplay);

  #ifdef DEBUG
  *logofs << "ServerProxy: Set display configuration to display '"
          << xServerDisplay_ << "'.\n"
          << logofs_flush;
  #endif
}

void ServerProxy::handlePortConfiguration(ChannelEndPoint &cupsServerPort,
                                          ChannelEndPoint &smbServerPort,
                                          ChannelEndPoint &mediaServerPort,
                                          ChannelEndPoint &httpServerPort,
                                          const char *fontServerPort)
{
  cupsServerPort_  = cupsServerPort;
  smbServerPort_   = smbServerPort;
  mediaServerPort_ = mediaServerPort;
  httpServerPort_  = httpServerPort;

  delete [] fontServerPort_;

  fontServerPort_ = new char[strlen(fontServerPort) + 1];

  strcpy(fontServerPort_, fontServerPort);

  #ifdef DEBUG
  *logofs << "ServerProxy: Set port configuration to CUPS "
          << cupsServerPort_ << ", SMB " << smbServerPort_
          << ", media " << mediaServerPort_ << ", HTTP "
          << httpServerPort_ << ".\n"
          << logofs_flush;
  #endif
}

int ServerProxy::handleNewConnection(T_channel_type type, int clientFd)
{
  switch (type)
  {
    case channel_font:
    {
      return handleNewGenericConnection(clientFd, channel_font, "font");
    }
    case channel_slave:
    {
      return handleNewSlaveConnection(clientFd);
    }
    default:
    {
      #ifdef PANIC
      *logofs << "ServerProxy: PANIC! Unsupported channel with type '"
              << getTypeName(type) << "'.\n" << logofs_flush;
      #endif

      cerr << "Error" << ": Unsupported channel with type '"
           << getTypeName(type) << "'.\n";

      return -1;
    }
  }
}

int ServerProxy::handleNewConnectionFromProxy(T_channel_type type, int channelId)
{
  switch (type)
  {
    case channel_x11:
    {
      return handleNewXConnectionFromProxy(channelId);
    }
    case channel_cups:
    {
      return handleNewGenericConnectionFromProxy(channelId, channel_cups,
                                                     cupsServerPort_, "CUPS");
    }
    case channel_smb:
    {
      smbServerPort_.setDefaultTCPInterface(1);
      return handleNewGenericConnectionFromProxy(channelId, channel_smb,
                                                     smbServerPort_, "SMB");
    }
    case channel_media:
    {
      return handleNewGenericConnectionFromProxy(channelId, channel_media,
                                                     mediaServerPort_, "media");
    }
    case channel_http:
    {
      return handleNewGenericConnectionFromProxy(channelId, channel_http,
                                                     httpServerPort_, "HTTP");
    }
    case channel_slave:
    {
      return handleNewSlaveConnectionFromProxy(channelId);
    }
    default:
    {
      #ifdef PANIC
      *logofs << "ServerProxy: PANIC! Unsupported channel with type '"
              << getTypeName(type) << "'.\n" << logofs_flush;
      #endif

      cerr << "Error" << ": Unsupported channel with type '"
           << getTypeName(type) << "'.\n";

      return -1;
    }
  }
}

int ServerProxy::handleNewAgentConnection(Agent *agent)
{
  #ifdef PANIC
  *logofs << "ServerProxy: PANIC! Can't create an agent "
          << "connection at this side.\n"
          << logofs_flush;
  #endif

  cerr << "Error" << ": Can't create an agent "
       << "connection at this side.\n";

  return -1;
}

int ServerProxy::handleNewXConnection(int clientFd)
{
  #ifdef PANIC
  *logofs << "ServerProxy: PANIC! Can't create a new X channel "
          << "with FD#" << clientFd << " at this side.\n"
          << logofs_flush;
  #endif

  cerr << "Error" << ": Can't create a new X channel "
       << "with FD#" << clientFd << " at this side.\n";

  return -1;
}

int ServerProxy::handleNewXConnectionFromProxy(int channelId)
{
  //
  // Connect to the real X server.
  //

  int retryConnect = control -> OptionServerRetryConnect;

  int xServerFd;

  for (;;)
  {
    xServerFd = socket(xServerAddrFamily_, SOCK_STREAM, PF_UNSPEC);

    if (xServerFd < 0)
    {
      #ifdef PANIC
      *logofs << "ServerProxy: PANIC! Call to socket failed. "
              << "Error is " << EGET() << " '" << ESTR()
              << "'.\n" << logofs_flush;
      #endif

      cerr << "Error" << ": Call to socket failed. "
           << "Error is " << EGET() << " '" << ESTR()
           << "'.\n";

      return -1;
    }

    #ifdef TEST
    *logofs << "ServerProxy: Trying to connect to X server '"
            << xServerDisplay_ << "'.\n" << logofs_flush;
    #endif

    int result = connect(xServerFd, xServerAddr_, xServerAddrLength_);

    getNewTimestamp();

    if (result < 0)
    {
      #ifdef WARNING
      *logofs << "ServerProxy: WARNING! Connection to '"
              << xServerDisplay_ << "' failed with error '"
              << ESTR() << "'. Retrying.\n" << logofs_flush;
      #endif

      close(xServerFd);

      if (--retryConnect == 0)
      {
        #ifdef PANIC
        *logofs << "ServerProxy: PANIC! Connection to '"
                << xServerDisplay_ << "' for channel ID#"
                << channelId << " failed. Error is "
                << EGET() << " '" << ESTR() << "'.\n"
                << logofs_flush;
        #endif

        cerr << "Error" << ": Connection to '"
             << xServerDisplay_ << "' failed. Error is "
             << EGET() << " '" << ESTR() << "'.\n";

        close(xServerFd);

        return -1;
      }

      if (activeChannels_.getSize() == 0)
      {
        sleep(2);
      }
      else
      {
        sleep(1);
      }
    }
    else
    {
      break;
    }
  }

  assignChannelMap(channelId, xServerFd);

  #ifdef TEST
  *logofs << "ServerProxy: X server descriptor FD#" << xServerFd
          << " mapped to channel ID#" << channelId << ".\n"
          << logofs_flush;
  #endif

  //
  // Turn queuing off for path proxy-to-X-server.
  //

  if (control -> OptionServerNoDelay == 1)
  {
    SetNoDelay(xServerFd, control -> OptionServerNoDelay);
  }

  //
  // If requested, set the size of the TCP send
  // and receive buffers.
  //

  if (control -> OptionServerSendBuffer != -1)
  {
    SetSendBuffer(xServerFd, control -> OptionServerSendBuffer);
  }

  if (control -> OptionServerReceiveBuffer != -1)
  {
    SetReceiveBuffer(xServerFd, control -> OptionServerReceiveBuffer);
  }

  if (allocateTransport(xServerFd, channelId) < 0)
  {
    return -1;
  }

  //
  // Starting from protocol level 3 client and server
  // caches are created in proxy and shared between all
  // channels. If remote proxy has older protocol level
  // pointers are NULL and channels must create their
  // own instances.
  //

  channels_[channelId] = new ServerChannel(transports_[channelId], compressor_);

  if (channels_[channelId] == NULL)
  {
    deallocateTransport(channelId);

    return -1;
  }

  increaseChannels(channelId);

  //
  // Propagate channel stores and caches to the new
  // channel.
  //

  channels_[channelId] -> setOpcodes(opcodeStore_);

  channels_[channelId] -> setStores(clientStore_, serverStore_);

  channels_[channelId] -> setCaches(clientCache_, serverCache_);

  int port = atoi(fontServerPort_);

  if (port > 0)
  {
    channels_[channelId] -> setPorts(port);
  }

  //
  // Let channel configure itself according
  // to control parameters.
  //

  channels_[channelId] -> handleConfiguration();

  //
  // Check if we have successfully loaded the
  // selected cache and, if not, remove it
  // from disk.
  //

  handleCheckLoad();

  return 1;
}

//
// Check if we still need to drop a channel. We need
// to check this explicitly at the time we receive a
// request to load or save the cache because we could
// receive the control message before having entered
// the function handling the channel events.
//

int ServerProxy::handleCheckDrop()
{
  T_list channelList = activeChannels_.copyList();

  for (T_list::iterator j = channelList.begin();
           j != channelList.end(); j++)
  {
    int channelId = *j;

    if (channels_[channelId] != NULL &&
            (channels_[channelId] -> getDrop() == 1 ||
                channels_[channelId] -> getClosing() == 1))
    {
      #ifdef TEST
      *logofs << "ServerProxy: Dropping the descriptor FD#"
              << getFd(channelId) << " channel ID#"
              << channelId << ".\n" << logofs_flush;
      #endif

      handleDrop(channelId);
    }
  }

  return 1;
}

int ServerProxy::handleCheckLoad()
{
  //
  // Check if we just created the first X channel
  // but the client side didn't tell us to load
  // the cache selected at the session negotiation.
  // This is very likely because the load operation
  // failed at the remote side, for example because
  // the cache was invalid or corrupted.
  //

  int channelCount = getChannels(channel_x11);

  if (channelCount != 1)
  {
    return 0;
  }

  if (control -> PersistentCacheEnableLoad == 1 &&
          control -> PersistentCachePath != NULL &&
              control -> PersistentCacheName != NULL &&
                  isTimestamp(timeouts_.loadTs) == 0)
  {
    #ifdef WARNING
    *logofs << "ServerProxy: WARNING! Cache file '" << control -> PersistentCachePath
            << "/" << control -> PersistentCacheName << "' not loaded.\n"
            << logofs_flush;
    #endif

    //
    // Remove the cache file.
    //

    #ifdef WARNING
    *logofs << "ServerProxy: WARNING! Removing supposedly "
            << "incompatible cache '" << control -> PersistentCachePath
            << "/" << control -> PersistentCacheName
            << "'.\n" << logofs_flush;
    #endif

    handleResetPersistentCache();
  }

  return 1;
}

int ServerProxy::handleLoadFromProxy()
{
  //
  // Be sure we drop any confirmed channel.
  //

  handleCheckDrop();

  //
  // Check that either no X channel is
  // remaining or we are inside a reset.
  //

  int channelCount = getChannels(channel_x11);

  if (channelCount > 0)
  {
    #ifdef PANIC
    *logofs << "ServerProxy: PANIC! Protocol violation "
            << "in command load with " << channelCount
            << " channels.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Protocol violation "
         << "in command load from proxy.\n";

    return -1;
  }
  else if (handleLoadStores() < 0)
  {
    #ifdef WARNING
    *logofs << "ServerProxy: WARNING! Failed to load content "
            << "of persistent cache.\n" << logofs_flush;
    #endif

    return -1;
  }

  return 1;
}

int ServerProxy::handleSaveFromProxy()
{
  //
  // Be sure we drop any confirmed channel.
  //

  handleCheckDrop();

  //
  // Now verify that all channels are gone.
  //

  int channelCount = getChannels(channel_x11);

  if (channelCount > 0)
  {
    #ifdef PANIC
    *logofs << "ServerProxy: PANIC! Protocol violation "
            << "in command save with " << channelCount
            << " channels.\n" << logofs_flush;
    #endif

    cerr << "Error" << ": Protocol violation "
         << "in command save from proxy.\n";

    return -1;
  }
  else if (handleSaveStores() < 0)
  {
    #ifdef PANIC
    *logofs << "ServerProxy: PANIC! Failed to save stores "
            << "to persistent cache.\n" << logofs_flush;
    #endif

    return -1;
  }

  return 1;
}

int ServerProxy::handleSaveAllStores(ostream *cachefs, md5_state_t *md5StateStream,
                                         md5_state_t *md5StateClient) const
{
  if (clientStore_ -> saveRequestStores(cachefs, md5StateStream, md5StateClient,
                                            discard_checksum, use_data) < 0)
  {
    return -1;
  }
  else if (serverStore_ -> saveReplyStores(cachefs, md5StateStream, md5StateClient,
                                               use_checksum, discard_data) < 0)
  {
    return -1;
  }
  else if (serverStore_ -> saveEventStores(cachefs, md5StateStream, md5StateClient,
                                               use_checksum, discard_data) < 0)
  {
    return -1;
  }

  return 1;
}

int ServerProxy::handleLoadAllStores(istream *cachefs, md5_state_t *md5StateStream) const
{
  if (clientStore_ -> loadRequestStores(cachefs, md5StateStream,
                                            discard_checksum, use_data) < 0)
  {
    return -1;
  }
  else if (serverStore_ -> loadReplyStores(cachefs, md5StateStream,
                                               use_checksum, discard_data) < 0)
  {
    return -1;
  }
  else if (serverStore_ -> loadEventStores(cachefs, md5StateStream,
                                               use_checksum, discard_data) < 0)
  {
    return -1;
  }

  return 1;
}