aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/ServerChannel.h
blob: 00fc51539d76a7e42913b8e681f23dbd8d5396b5 (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
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/.         */
/*                                                                        */
/* NXCOMP, NX protocol compression and NX extensions to this software     */
/* are copyright of NoMachine. Redistribution and use of the present      */
/* software is allowed according to terms specified in the file LICENSE   */
/* which comes in the source distribution.                                */
/*                                                                        */
/* Check http://www.nomachine.com/licensing.html for applicability.       */
/*                                                                        */
/* NX and NoMachine are trademarks of NoMachine S.r.l.                    */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/**************************************************************************/

#ifndef ServerChannel_H
#define ServerChannel_H

#include "List.h"
#include "Channel.h"

#include "SequenceQueue.h"

#include "ServerReadBuffer.h"

#include "Unpack.h"

//
// Set the verbosity level.
//

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

//
// How many sequence numbers of split commit
// requests we are going to save in order to
// mask errors.
//

#define MAX_COMMIT_SEQUENCE_QUEUE         16

//
// Define this to know when a channel
// is created or destroyed.
//

#undef  REFERENCES

//
// This class implements the X server
// side compression of X protocol.
//

class ServerChannel : public Channel
{
  public:

  ServerChannel(Transport *transport, StaticCompressor *compressor);

  virtual ~ServerChannel();

  virtual int handleRead(EncodeBuffer &encodeBuffer, const unsigned char *message,
                             unsigned int length);

  virtual int handleWrite(const unsigned char *message, unsigned int length);

  virtual int handleSplit(EncodeBuffer &encodeBuffer, MessageStore *store,
                              T_store_action action, int position, const unsigned char opcode,
                                  const unsigned char *buffer, const unsigned int size)
  {
    return 0;
  }

  virtual int handleSplit(DecodeBuffer &decodeBuffer, MessageStore *store,
                                T_store_action action, int position, unsigned char &opcode,
                                    unsigned char *&buffer, unsigned int &size);

  virtual int handleSplit(EncodeBuffer &encodeBuffer)
  {
    return 0;
  }

  virtual int handleSplit(DecodeBuffer &decodeBuffer);

  virtual int handleSplitEvent(EncodeBuffer &encodeBuffer, Split *split);

  virtual int handleSplitEvent(DecodeBuffer &decodeBuffer)
  {
    return 0;
  }

  //
  // Send the last motion notify event
  // received from the X server to the
  // remote proxy.
  //

  virtual int handleMotion(EncodeBuffer &encodeBuffer);

  virtual int handleCompletion(EncodeBuffer &encodeBuffer)
  {
    return 0;
  }

  virtual int handleConfiguration();

  virtual int handleFinish();

  virtual int handleAsyncEvents();

  virtual int needSplit() const
  {
    return 0;
  }

  virtual int needMotion() const
  {
    return (lastMotion_[0] != '\0');
  }

  virtual T_channel_type getType() const
  {
    return channel_x11;
  }

  int setBigEndian(int flag);

  //
  // Initialize the static members.
  //

  static int setReferences();

  private:

  int handleFastReadReply(EncodeBuffer &encodeBuffer, const unsigned char &opcode,
                              const unsigned char *&buffer, const unsigned int &size);

  int handleFastReadEvent(EncodeBuffer &encodeBuffer, const unsigned char &opcode,
                              const unsigned char *&buffer, const unsigned int &size);

  int handleFastWriteRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                                 unsigned char *&buffer, unsigned int &size);

  //
  // Handle the fake authorization cookie
  // and the X server's reply.
  //

  int handleAuthorization(unsigned char *buffer);
  int handleAuthorization(const unsigned char *buffer, int size);

  //
  // Set the unpack colormap and the alpha
  // blending data to be used to unpack
  // images.
  //

  int handleGeometry(unsigned char &opcode, unsigned char *&buffer,
                         unsigned int &size);

  int handleColormap(unsigned char &opcode, unsigned char *&buffer,
                         unsigned int &size);

  int handleAlpha(unsigned char &opcode, unsigned char *&buffer,
                      unsigned int &size);

  //
  // Manage the decoded buffer to unpack
  // the image and move the data to the
  // shared memory segment.
  //

  int handleImage(unsigned char &opcode, unsigned char *&buffer,
                      unsigned int &size);

  //
  // Uncompress a packed image in one
  // or more graphic X requests.
  //

  int handleUnpack(unsigned char &opcode, unsigned char *&buffer,
                       unsigned int &size);

  //
  // Move the image to the shared
  // memory buffer.
  //

  int handleShmem(unsigned char &opcode, unsigned char *&buffer,
                      unsigned int &size);

  //
  // Handle suppression of error on
  // commit of image splits.
  //

  void initCommitQueue();

  void updateCommitQueue(unsigned short sequence);

  int checkCommitError(unsigned char error, unsigned short sequence,
                           const unsigned char *buffer);

  void clearCommitQueue()
  {
    if (commitSequenceQueue_[0] != 0)
    {
      initCommitQueue();
    }
  }

  //
  // Check if the user pressed the
  // CTRL+ALT+SHIFT+ESC keystroke.
  //

  int checkKeyboardEvent(unsigned char event, unsigned short sequence,
                             const unsigned char *buffer);

  //
  // Other utilities.
  //

  void handleEncodeCharInfo(const unsigned char *nextSrc, EncodeBuffer &encodeBuffer);

  //
  // Handle the MIT-SHM initialization
  // messages exchanged with the remote
  // proxy.
  //

  int handleShmemRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                             unsigned char *&buffer, unsigned int &size);


  int handleShmemReply(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                           const unsigned int stage, const unsigned char *buffer,
                               const unsigned int size);

  //
  // Try to read more events in the attempt to
  // get the MIT-SHM image completion event
  // from the X server.
  //

  int handleShmemEvent();

  //
  // Handle the MIT-SHM events as they are read
  // from the socket.
  //

  int checkShmemEvent(unsigned char event, unsigned short sequence,
                          const unsigned char *buffer);

  int checkShmemError(unsigned char error, unsigned short sequence,
                          const unsigned char *buffer);

  //
  // Query the port used to tunnel
  // the font server connections.
  //

  int handleFontRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                            unsigned char *&buffer, unsigned int &size);

  int handleFontReply(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                          const unsigned char *buffer, const unsigned int size);

  //
  // Set the cache policy for image
  // requests.
  //

  int handleCacheRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                             unsigned char *&buffer, unsigned int &size);

  //
  // Decode the start and end split
  // requests.
  //

  int handleStartSplitRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                                  unsigned char *&buffer, unsigned int &size);

  int handleEndSplitRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                                unsigned char *&buffer, unsigned int &size);

  //
  // Remove the split store and the
  // incomplete messages from the
  // memory cache.
  //

  int handleAbortSplitRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                                  unsigned char *&buffer, unsigned int &size);

  //
  // Send the split requests to the
  // X server once they have been
  // recomposed.
  //

  int handleCommitSplitRequest(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                                   unsigned char *&buffer, unsigned int &size);

  int handleSplitChecksum(DecodeBuffer &decodeBuffer, T_checksum &checksum);

  void handleSplitEnable()
  {
    if (control -> isProtoStep7() == 0)
    {
      #if defined(TEST) || defined(SPLIT)
      *logofs << "handleSplitEnable: WARNING! Disabling load "
              << "and save with an old proxy version.\n"
              << logofs_flush;
      #endif

      splitState_.save = 0;
      splitState_.load = 0;
    }
  }

  //
  // Allocate and free the shared memory
  // support resources.
  //

  void handleShmemStateAlloc();
  void handleShmemStateRemove();

  //
  // Temporary storage for the image info.
  //

  void handleImageStateAlloc(unsigned char opcode)
  {
    if (imageState_ == NULL)
    {
      imageState_ = new T_image_state();
    }

    imageState_ -> opcode = opcode;
  }

  void handleImageStateRemove()
  {
    if (imageState_ != NULL)
    {
      delete imageState_;

      imageState_ = NULL;
    }
  }

  //
  // Store the information needed to unpack
  // images per each known agent's client.
  //

  void handleUnpackStateInit(int resource);

  void handleUnpackAllocGeometry(int resource);
  void handleUnpackAllocColormap(int resource);
  void handleUnpackAllocAlpha(int resource);

  void handleUnpackStateRemove(int resource);

  typedef struct
  {
    T_geometry *geometry;
    T_colormap *colormap;
    T_alpha    *alpha;

  } T_unpack_state;

  T_unpack_state *unpackState_[256];

  //
  // Own read buffer. It is able to identify
  // full messages read from X descriptor.
  //

  ServerReadBuffer readBuffer_;

  //
  // Sequence number of last request coming
  // from X client or X server.
  //

  unsigned int clientSequence_;
  unsigned int serverSequence_;

  //
  // Used to identify replies based on sequence
  // number of original request.
  //

  SequenceQueue sequenceQueue_;

  //
  // Last motion notify read from the X server.
  //

  unsigned char lastMotion_[32];

  //
  // Sequence numbers of last auto-generated
  // put image requests. Needed to intercept
  // and suppress errors generated by such
  // requests.
  //

  unsigned int commitSequenceQueue_[MAX_COMMIT_SEQUENCE_QUEUE];

  //
  // Let agent select which expose
  // events is going to receive.
  //

  unsigned int enableExpose_;
  unsigned int enableGraphicsExpose_;
  unsigned int enableNoExpose_;

  //
  // Used in initialization and handling
  // of MIT-SHM shared memory put images.
  //

  typedef struct
  {
    int           stage;
    int           present;
    int           enabled;
    int           segment;
    int           id;
    void          *address;
    unsigned int  size;

    unsigned char opcode;
    unsigned char event;
    unsigned char error;

    unsigned int  sequence;
    unsigned int  offset;
    T_timestamp   last;

    unsigned int  checked;

  } T_shmem_state;

  T_shmem_state *shmemState_;

  //
  // Used to pass current image data between
  // the different decompression stages.
  //

  typedef struct
  {
    unsigned char  opcode;

    unsigned int   drawable;
    unsigned int   gcontext;

    unsigned char  method;

    unsigned char  format;
    unsigned char  srcDepth;
    unsigned char  dstDepth;

    unsigned int   srcLength;
    unsigned int   dstLength;
    unsigned int   dstLines;

    short int      srcX;
    short int      srcY;
    unsigned short srcWidth;
    unsigned short srcHeight;

    short int      dstX;
    short int      dstY;
    unsigned short dstWidth;
    unsigned short dstHeight;

    unsigned char  leftPad;

  } T_image_state;

  T_image_state *imageState_;

  //
  // The flags is set according to the
  // split load and save policy set by
  // the encoding side.
  //

  typedef struct
  {
    int resource;
    int current;
    int load;
    int save;
    int commit;

  } T_split_state;

  T_split_state splitState_;

  //
  // List of agent resources.
  //

  List splitResources_;

  //
  // Keep track of object creation and
  // deletion.
  //

  private:

  #ifdef REFERENCES

  static int references_;

  #endif
};

#endif /* ServerChannel_H */