1
/**************************************************************************/
3
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/. */
5
/* NXCOMP, NX protocol compression and NX extensions to this software */
6
/* are copyright of NoMachine. Redistribution and use of the present */
7
/* software is allowed according to terms specified in the file LICENSE */
8
/* which comes in the source distribution. */
10
/* Check http://www.nomachine.com/licensing.html for applicability. */
12
/* NX and NoMachine are trademarks of NoMachine S.r.l. */
14
/* All rights reserved. */
16
/**************************************************************************/
28
// Flush immediately on prioritized messages.
31
#define FLUSH_PRIORITY 0
34
// Maximum number of bytes sent for each token.
37
#define TOKEN_SIZE 1536
40
// Maximum number of tokens that can be spent
41
// by the client proxy before having to block
42
// waiting for a token reply.
45
#define TOKEN_LIMIT 24
48
// By default assume the proxy is running as a
49
// standalone program.
52
#define LINK_ENCRYPTED 0
55
// Maximum number of pids the proxy will record
56
// and kill at shutdown.
62
// Allocate on the NX client side channels whose
63
// ids are a multiple of 8 (starting from 0). All
64
// the other ids can be used to allocate channels
65
// at the NX server side (X client side).
68
#define CHANNEL_MASK 0x07
71
// Kill session if control parameters cannot be
72
// negotiated before this timeout.
75
#define INIT_TIMEOUT 60000
78
// Enter the congestion state if the remote does
79
// not reply to a ping within the given amount
83
#define PING_TIMEOUT 5000
86
// Only send one motion event any N milliseconds.
89
#define MOTION_TIMEOUT 0
93
// Force an update of the congestion counter if
94
// the proxy is idle for this time.
97
#define IDLE_TIMEOUT 50
100
// Close X connection if can't write before this
104
#define CHANNEL_TIMEOUT 10000
107
// Warn user (or close proxy connection) if don't
108
// receive any data before this timeout.
111
#define PROXY_TIMEOUT 120000
114
// How many milliseconds to wait for the shared
115
// memory completion event to become available.
118
#define SHMEM_TIMEOUT 200
120
// Before closing down the proxy, wait for the
121
// given amount of miliseconds to let all the
122
// running applications to close down their
125
// A null timeout will cause the proxy to wait
126
// indefinitely, until the watchdog process is
127
// killed. This is usually the way the proxy is
128
// started by the NX server. If on the other
129
// hand a timeout is given and there no channel
130
// is remaining, the proxy will be closed down
131
// using a small timeout, presently of 500 ms.
134
#define CLEANUP_TIMEOUT 3000
137
// Wait this amount of milliseconds after any
138
// iteration of the house-keeping process.
141
#define KEEPER_TIMEOUT 60000
144
// In case of timeout, select can return control
145
// to program earlier or later of this amount of
146
// ms. Consider this when calculating if timeout
150
#define LATENCY_TIMEOUT 1
153
// Control memory allocation in transport
154
// and other classes.
157
#define TRANSPORT_X_BUFFER_SIZE 131072
158
#define TRANSPORT_PROXY_BUFFER_SIZE 65536
159
#define TRANSPORT_GENERIC_BUFFER_SIZE 16384
161
#define TRANSPORT_X_BUFFER_THRESHOLD 262144
162
#define TRANSPORT_PROXY_BUFFER_THRESHOLD 131072
163
#define TRANSPORT_GENERIC_BUFFER_THRESHOLD 32768
166
// Never allow buffers to exceed this limit.
169
#define TRANSPORT_MAXIMUM_BUFFER_SIZE 393216
172
// Immediately flush the accumulated data to
173
// the X server if the write buffer exceeds
177
#define TRANSPORT_FLUSH_BUFFER_SIZE 16384
180
// Defaults used for socket options.
183
#define OPTION_PROXY_KEEP_ALIVE 0
184
#define OPTION_PROXY_LOW_DELAY 1
185
#define OPTION_PROXY_CLIENT_NO_DELAY 1
186
#define OPTION_PROXY_SERVER_NO_DELAY 1
187
#define OPTION_CLIENT_NO_DELAY 1
188
#define OPTION_SERVER_NO_DELAY 1
190
#define OPTION_PROXY_RECEIVE_BUFFER -1
191
#define OPTION_CLIENT_RECEIVE_BUFFER -1
192
#define OPTION_SERVER_RECEIVE_BUFFER -1
194
#define OPTION_PROXY_SEND_BUFFER -1
195
#define OPTION_CLIENT_SEND_BUFFER -1
196
#define OPTION_SERVER_SEND_BUFFER -1
198
#define OPTION_PROXY_RETRY_CONNECT 30
199
#define OPTION_PROXY_RETRY_ACCEPT 3
200
#define OPTION_SERVER_RETRY_CONNECT 3
203
// Defaults used for cache persistence.
206
#define PERSISTENT_CACHE_THRESHOLD 102400
208
#define PERSISTENT_CACHE_ENABLE_LOAD 1
209
#define PERSISTENT_CACHE_ENABLE_SAVE 1
211
#define PERSISTENT_CACHE_CHECK_ON_SHUTDOWN 0
213
#define PERSISTENT_CACHE_LOAD_PACKED 1
214
#define PERSISTENT_CACHE_LOAD_RENDER 1
216
#define PERSISTENT_CACHE_DISK_LIMIT 33554432
219
// Defaults used for image cache.
222
#define IMAGE_CACHE_ENABLE_LOAD 0
223
#define IMAGE_CACHE_ENABLE_SAVE 0
225
#define IMAGE_CACHE_DISK_LIMIT 33554432
228
// Suggested defaults for read length parameters
229
// used by read buffer classes.
232
#define CLIENT_INITIAL_READ_SIZE 8192
233
#define CLIENT_MAXIMUM_BUFFER_SIZE 262144
235
#define SERVER_INITIAL_READ_SIZE 8192
236
#define SERVER_MAXIMUM_BUFFER_SIZE 65536
238
#define PROXY_INITIAL_READ_SIZE 65536
239
#define PROXY_MAXIMUM_BUFFER_SIZE 262144 + 1024
241
#define GENERIC_INITIAL_READ_SIZE 8192
242
#define GENERIC_MAXIMUM_BUFFER_SIZE 8192
245
// Calculate bitrate in given time frames.
246
// Values are in milliseconds.
249
#define SHORT_BITRATE_TIME_FRAME 5000
250
#define LONG_BITRATE_TIME_FRAME 30000
253
// Bandwidth control. A value of 0 means no
254
// limit. Values are stored internally in
258
#define CLIENT_BITRATE_LIMIT 0
259
#define SERVER_BITRATE_LIMIT 0
262
// Default values for cache control. We limit
263
// the maximum size of a request to 262144 but
264
// we need to consider the replies, whose size
268
#define MINIMUM_MESSAGE_SIZE 4
269
#define MAXIMUM_MESSAGE_SIZE 4194304
270
#define MAXIMUM_REQUEST_SIZE 262144
272
#define CLIENT_TOTAL_STORAGE_SIZE 8388608
273
#define SERVER_TOTAL_STORAGE_SIZE 8388608
275
#define STORE_TIME_LIMIT 3600
277
#define STORE_HITS_LOAD_BONUS 10
278
#define STORE_HITS_ADD_BONUS 20
279
#define STORE_HITS_LIMIT 100
281
#define STORE_HITS_TOUCH 1
282
#define STORE_HITS_UNTOUCH 2
285
// Default parameters for message splitting.
289
#define SPLIT_TIMEOUT 50
290
#define SPLIT_TOTAL_SIZE 128
291
#define SPLIT_TOTAL_STORAGE_SIZE 1048576
292
#define SPLIT_DATA_THRESHOLD 65536
293
#define SPLIT_DATA_PACKET_LIMIT 24576
296
// Agent related parameters.
299
#define PACK_METHOD 63
300
#define PACK_QUALITY 9
301
#define HIDE_RENDER 0
302
#define TAINT_REPLIES 1
303
#define TAINT_THRESHOLD 8
306
// In current version only X server support is
307
// implemented. Note that use of shared memory
308
// is negotiated according to options provided
312
#define SHMEM_CLIENT 0
313
#define SHMEM_SERVER 1
316
// Default size of shared memory segments used
317
// in MIT-SHM support.
320
#define SHMEM_CLIENT_SIZE 0
321
#define SHMEM_SERVER_SIZE 2097152
324
// What do we do at the end of session? If this
325
// flag is set, we launch a new client letting
326
// the user run a new NX session.
329
#define ENABLE_RESTART_ON_SHUTDOWN 0
332
// Do we produce a core dump on fatal errors?
335
#define ENABLE_CORE_DUMP_ON_ABORT 0
338
// Reopen the log file if it exceeds this size.
341
#define FILE_SIZE_LIMIT 60000000
344
// Check periodically if we need to truncate the
345
// log file. By default check every minute.
348
#define FILE_SIZE_CHECK_TIMEOUT 60000
351
// Set defaults for control. They should be what
352
// you get in case of 'local' connection.
357
ProxyMode = proxy_undefined;
358
ProxyStage = stage_undefined;
359
SessionMode = session_undefined;
360
FlushPolicy = policy_undefined;
361
LinkMode = link_undefined;
363
LinkEncrypted = LINK_ENCRYPTED;
364
FlushPriority = FLUSH_PRIORITY;
366
TokenSize = TOKEN_SIZE;
367
TokenLimit = TOKEN_LIMIT;
369
ChannelMask = CHANNEL_MASK;
371
InitTimeout = INIT_TIMEOUT;
372
PingTimeout = PING_TIMEOUT;
373
MotionTimeout = MOTION_TIMEOUT;
374
IdleTimeout = IDLE_TIMEOUT;
376
ChannelTimeout = CHANNEL_TIMEOUT;
377
ProxyTimeout = PROXY_TIMEOUT;
378
ShmemTimeout = SHMEM_TIMEOUT;
380
CleanupTimeout = CLEANUP_TIMEOUT;
381
KeeperTimeout = KEEPER_TIMEOUT;
382
LatencyTimeout = LATENCY_TIMEOUT;
384
FileSizeLimit = FILE_SIZE_LIMIT;
385
FileSizeCheckTimeout = FILE_SIZE_CHECK_TIMEOUT;
387
EnableRestartOnShutdown = ENABLE_RESTART_ON_SHUTDOWN;
389
KillDaemonOnShutdownLimit = KILL_LIMIT;
391
KillDaemonOnShutdown = new int[KillDaemonOnShutdownLimit];
393
for (int i = 0; i < KILL_LIMIT; i++)
395
KillDaemonOnShutdown[i] = -1;
398
KillDaemonOnShutdownNumber = 0;
400
EnableCoreDumpOnAbort = ENABLE_CORE_DUMP_ON_ABORT;
403
// Collect statistics by default.
406
EnableStatistics = 1;
409
// Memory restrictions if any.
412
LocalMemoryLevel = -1;
415
// Compression must be negotiated between proxies.
418
LocalDeltaCompression = -1;
419
RemoteDeltaCompression = -1;
421
LocalDataCompression = -1;
422
LocalStreamCompression = -1;
424
RemoteDataCompression = -1;
425
RemoteStreamCompression = -1;
427
LocalDataCompressionLevel = -1;
428
LocalDataCompressionThreshold = -1;
429
LocalStreamCompressionLevel = -1;
431
RemoteDataCompressionLevel = -1;
432
RemoteStreamCompressionLevel = -1;
435
// Transport buffers' allocation parameters.
438
TransportXBufferSize = TRANSPORT_X_BUFFER_SIZE;
439
TransportProxyBufferSize = TRANSPORT_PROXY_BUFFER_SIZE;
440
TransportGenericBufferSize = TRANSPORT_GENERIC_BUFFER_SIZE;
442
TransportXBufferThreshold = TRANSPORT_X_BUFFER_THRESHOLD;
443
TransportProxyBufferThreshold = TRANSPORT_PROXY_BUFFER_THRESHOLD;
444
TransportGenericBufferThreshold = TRANSPORT_GENERIC_BUFFER_THRESHOLD;
446
TransportMaximumBufferSize = TRANSPORT_MAXIMUM_BUFFER_SIZE;
449
// Flush the write buffer if it exceeds
453
TransportFlushBufferSize = TRANSPORT_FLUSH_BUFFER_SIZE;
459
OptionProxyKeepAlive = OPTION_PROXY_KEEP_ALIVE;
460
OptionProxyLowDelay = OPTION_PROXY_LOW_DELAY;
461
OptionProxyClientNoDelay = OPTION_PROXY_CLIENT_NO_DELAY;
462
OptionProxyServerNoDelay = OPTION_PROXY_SERVER_NO_DELAY;
463
OptionClientNoDelay = OPTION_CLIENT_NO_DELAY;
464
OptionServerNoDelay = OPTION_SERVER_NO_DELAY;
466
OptionProxyReceiveBuffer = OPTION_PROXY_RECEIVE_BUFFER;
467
OptionClientReceiveBuffer = OPTION_CLIENT_RECEIVE_BUFFER;
468
OptionServerReceiveBuffer = OPTION_SERVER_RECEIVE_BUFFER;
470
OptionProxySendBuffer = OPTION_PROXY_SEND_BUFFER;
471
OptionClientSendBuffer = OPTION_CLIENT_SEND_BUFFER;
472
OptionServerSendBuffer = OPTION_SERVER_SEND_BUFFER;
474
OptionProxyRetryAccept = OPTION_PROXY_RETRY_ACCEPT;
475
OptionProxyRetryConnect = OPTION_PROXY_RETRY_CONNECT;
476
OptionServerRetryConnect = OPTION_SERVER_RETRY_CONNECT;
479
// Base NX directories.
489
// Set defaults for handling persistent cache.
492
PersistentCachePath = NULL;
493
PersistentCacheName = NULL;
495
PersistentCacheThreshold = PERSISTENT_CACHE_THRESHOLD;
497
PersistentCacheEnableLoad = PERSISTENT_CACHE_ENABLE_LOAD;
498
PersistentCacheEnableSave = PERSISTENT_CACHE_ENABLE_SAVE;
500
PersistentCacheCheckOnShutdown = PERSISTENT_CACHE_CHECK_ON_SHUTDOWN;
502
PersistentCacheLoadPacked = PERSISTENT_CACHE_LOAD_PACKED;
503
PersistentCacheLoadRender = PERSISTENT_CACHE_LOAD_RENDER;
505
PersistentCacheDiskLimit = PERSISTENT_CACHE_DISK_LIMIT;
508
// Set defaults for image cache.
511
ImageCachePath = NULL;
513
ImageCacheEnableLoad = IMAGE_CACHE_ENABLE_LOAD;
514
ImageCacheEnableSave = IMAGE_CACHE_ENABLE_SAVE;
516
ImageCacheDiskLimit = IMAGE_CACHE_DISK_LIMIT;
519
// Set defaults for the read buffers.
522
ClientInitialReadSize = CLIENT_INITIAL_READ_SIZE;
523
ClientMaximumBufferSize = CLIENT_MAXIMUM_BUFFER_SIZE;
525
ServerInitialReadSize = SERVER_INITIAL_READ_SIZE;
526
ServerMaximumBufferSize = SERVER_MAXIMUM_BUFFER_SIZE;
528
ProxyInitialReadSize = PROXY_INITIAL_READ_SIZE;
529
ProxyMaximumBufferSize = PROXY_MAXIMUM_BUFFER_SIZE;
531
GenericInitialReadSize = GENERIC_INITIAL_READ_SIZE;
532
GenericMaximumBufferSize = GENERIC_MAXIMUM_BUFFER_SIZE;
534
ShortBitrateTimeFrame = SHORT_BITRATE_TIME_FRAME;
535
LongBitrateTimeFrame = LONG_BITRATE_TIME_FRAME;
538
// Bandwidth control.
541
LocalBitrateLimit = -1;
543
ClientBitrateLimit = CLIENT_BITRATE_LIMIT;
544
ServerBitrateLimit = SERVER_BITRATE_LIMIT;
547
// Default parameters for message handling.
550
ClientTotalStorageSize = CLIENT_TOTAL_STORAGE_SIZE;
551
ServerTotalStorageSize = SERVER_TOTAL_STORAGE_SIZE;
553
LocalTotalStorageSize = -1;
554
RemoteTotalStorageSize = -1;
556
StoreTimeLimit = STORE_TIME_LIMIT;
558
StoreHitsLoadBonus = STORE_HITS_LOAD_BONUS;
559
StoreHitsAddBonus = STORE_HITS_ADD_BONUS;
560
StoreHitsLimit = STORE_HITS_LIMIT;
562
StoreHitsTouch = STORE_HITS_TOUCH;
563
StoreHitsUntouch = STORE_HITS_UNTOUCH;
565
MinimumMessageSize = MINIMUM_MESSAGE_SIZE;
566
MaximumMessageSize = MAXIMUM_MESSAGE_SIZE;
567
MaximumRequestSize = MAXIMUM_REQUEST_SIZE;
569
SplitMode = SPLIT_MODE;
570
SplitTimeout = SPLIT_TIMEOUT;
571
SplitTotalSize = SPLIT_TOTAL_SIZE;
572
SplitTotalStorageSize = SPLIT_TOTAL_STORAGE_SIZE;
573
SplitDataThreshold = SPLIT_DATA_THRESHOLD;
574
SplitDataPacketLimit = SPLIT_DATA_PACKET_LIMIT;
576
PackMethod = PACK_METHOD;
577
PackQuality = PACK_QUALITY;
578
HideRender = HIDE_RENDER;
579
TaintReplies = TAINT_REPLIES;
580
TaintThreshold = TAINT_THRESHOLD;
582
ShmemClient = SHMEM_CLIENT;
583
ShmemServer = SHMEM_SERVER;
585
ShmemClientSize = SHMEM_CLIENT_SIZE;
586
ShmemServerSize = SHMEM_SERVER_SIZE;
589
// Get local version number from compile time
590
// settings. Version of remote proxy will be
591
// checked at connection time.
594
RemoteVersionMajor = -1;
595
RemoteVersionMinor = -1;
596
RemoteVersionPatch = -1;
598
CompatVersionMajor = -1;
599
CompatVersionMinor = -1;
600
CompatVersionPatch = -1;
604
strcpy(version, VERSION);
608
value = strtok(version, ".");
610
for (int i = 0; value != NULL && i < 3; i++)
616
LocalVersionMajor = atoi(value);
622
LocalVersionMinor = atoi(value);
628
LocalVersionPatch = atoi(value);
633
value = strtok(NULL, ".");
637
*logofs << "Control: Major version is " << LocalVersionMajor
638
<< " minor is " << LocalVersionMinor << " patch is "
639
<< LocalVersionPatch << ".\n" << logofs_flush;
643
// Initialize local implemented methods later
644
// and negotiate remote methods at connection
648
LocalUnpackMethods = NULL;
649
RemoteUnpackMethods = NULL;
652
// Set to 1 those methods which are implemented.
655
setLocalUnpackMethods();
658
// Set the protocol version at the
659
// time the session is negotiated.
671
if (KillDaemonOnShutdown != NULL)
673
delete [] KillDaemonOnShutdown;
676
if (HomePath != NULL)
681
if (RootPath != NULL)
686
if (SystemPath != NULL)
688
delete [] SystemPath;
691
if (TempPath != NULL)
696
if (ClientPath != NULL)
698
delete [] ClientPath;
701
if (PersistentCachePath != NULL)
703
delete [] PersistentCachePath;
706
if (PersistentCacheName != NULL)
708
delete [] PersistentCacheName;
711
if (LocalUnpackMethods != NULL)
713
delete [] LocalUnpackMethods;
716
if (RemoteUnpackMethods != NULL)
718
delete [] RemoteUnpackMethods;
721
if (ImageCachePath != NULL)
723
delete [] ImageCachePath;
728
// Set the protocol step based on the
732
void Control::setProtoStep(int step)
789
*logofs << "Control: PANIC! Invalid protocol step "
790
<< "with value " << step << ".\n"
799
int Control::getProtoStep()
801
if (protoStep10_ == 1)
805
else if (protoStep9_ == 1)
809
else if (protoStep8_ == 1)
813
else if (protoStep7_ == 1)
817
else if (protoStep6_ == 1)
824
*logofs << "Control: PANIC! Can't identify the "
825
<< "protocol step.\n" << logofs_flush;
833
// Set here the pack/unpack methods that are
834
// implemented by this NX proxy.
837
void Control::setLocalUnpackMethods()
839
LocalUnpackMethods = new unsigned char[PACK_METHOD_LIMIT];
840
RemoteUnpackMethods = new unsigned char[PACK_METHOD_LIMIT];
842
for (int i = 0; i < PACK_METHOD_LIMIT; i++)
844
LocalUnpackMethods[i] = 0;
845
RemoteUnpackMethods[i] = 0;
848
LocalUnpackMethods[NO_PACK] = 1;
850
LocalUnpackMethods[PACK_MASKED_8_COLORS] = 1;
851
LocalUnpackMethods[PACK_MASKED_64_COLORS] = 1;
852
LocalUnpackMethods[PACK_MASKED_256_COLORS] = 1;
853
LocalUnpackMethods[PACK_MASKED_512_COLORS] = 1;
854
LocalUnpackMethods[PACK_MASKED_4K_COLORS] = 1;
855
LocalUnpackMethods[PACK_MASKED_32K_COLORS] = 1;
856
LocalUnpackMethods[PACK_MASKED_64K_COLORS] = 1;
857
LocalUnpackMethods[PACK_MASKED_256K_COLORS] = 1;
858
LocalUnpackMethods[PACK_MASKED_2M_COLORS] = 1;
859
LocalUnpackMethods[PACK_MASKED_16M_COLORS] = 1;
861
LocalUnpackMethods[PACK_RAW_8_BITS] = 1;
862
LocalUnpackMethods[PACK_RAW_16_BITS] = 1;
863
LocalUnpackMethods[PACK_RAW_24_BITS] = 1;
865
LocalUnpackMethods[PACK_COLORMAP_256_COLORS] = 1;
867
LocalUnpackMethods[PACK_JPEG_8_COLORS] = 1;
868
LocalUnpackMethods[PACK_JPEG_64_COLORS] = 1;
869
LocalUnpackMethods[PACK_JPEG_256_COLORS] = 1;
870
LocalUnpackMethods[PACK_JPEG_512_COLORS] = 1;
871
LocalUnpackMethods[PACK_JPEG_4K_COLORS] = 1;
872
LocalUnpackMethods[PACK_JPEG_32K_COLORS] = 1;
873
LocalUnpackMethods[PACK_JPEG_64K_COLORS] = 1;
874
LocalUnpackMethods[PACK_JPEG_256K_COLORS] = 1;
875
LocalUnpackMethods[PACK_JPEG_2M_COLORS] = 1;
876
LocalUnpackMethods[PACK_JPEG_16M_COLORS] = 1;
878
LocalUnpackMethods[PACK_PNG_8_COLORS] = 1;
879
LocalUnpackMethods[PACK_PNG_64_COLORS] = 1;
880
LocalUnpackMethods[PACK_PNG_256_COLORS] = 1;
881
LocalUnpackMethods[PACK_PNG_512_COLORS] = 1;
882
LocalUnpackMethods[PACK_PNG_4K_COLORS] = 1;
883
LocalUnpackMethods[PACK_PNG_32K_COLORS] = 1;
884
LocalUnpackMethods[PACK_PNG_64K_COLORS] = 1;
885
LocalUnpackMethods[PACK_PNG_256K_COLORS] = 1;
886
LocalUnpackMethods[PACK_PNG_2M_COLORS] = 1;
887
LocalUnpackMethods[PACK_PNG_16M_COLORS] = 1;
889
LocalUnpackMethods[PACK_RGB_16M_COLORS] = 1;
890
LocalUnpackMethods[PACK_RLE_16M_COLORS] = 1;
892
LocalUnpackMethods[PACK_ALPHA] = 1;
893
LocalUnpackMethods[PACK_COLORMAP] = 1;
895
LocalUnpackMethods[PACK_BITMAP_16M_COLORS] = 1;