-
Notifications
You must be signed in to change notification settings - Fork 22
/
vnc.c
719 lines (624 loc) · 16.3 KB
/
vnc.c
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
/**
* vnc2rdp: proxy for RDP client connect to VNC server
*
* Copyright 2014 Yiwei Li <leeyiw@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <netinet/tcp.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "log.h"
#include "rdp.h"
#include "vnc.h"
#include "vncauth.h"
static int
v2r_vnc_recv(v2r_vnc_t *v)
{
int n = 0;
v2r_packet_reset(v->packet);
n = recv(v->fd, v->packet->current, v->packet->max_len, 0);
if (n == -1 || n == 0) {
goto fail;
}
v->packet->end += n;
return 0;
fail:
return -1;
}
static int
v2r_vnc_recv1(v2r_vnc_t *v, size_t len)
{
int n = 0;
v2r_packet_reset(v->packet);
n = recv(v->fd, v->packet->current, len, MSG_WAITALL);
if (n == -1 || n == 0) {
goto fail;
}
v->packet->end += n;
return 0;
fail:
return -1;
}
static int
v2r_vnc_send(v2r_vnc_t *v)
{
int n = 0;
n = send(v->fd, v->packet->data, V2R_PACKET_LEN(v->packet), 0);
if (n == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_process_vnc_authentication(v2r_vnc_t *v)
{
uint8_t challenge[CHALLENGESIZE];
uint32_t security_result;
/* receive challenge */
if (v2r_vnc_recv1(v, CHALLENGESIZE) == -1) {
goto fail;
}
V2R_PACKET_READ_N(v->packet, challenge, CHALLENGESIZE);
rfbEncryptBytes(challenge, v->session->opt->vnc_password);
/* send response */
v2r_packet_reset(v->packet);
V2R_PACKET_WRITE_N(v->packet, challenge, CHALLENGESIZE);
V2R_PACKET_END(v->packet);
if (v2r_vnc_send(v) == -1) {
goto fail;
}
/* receive SecurityResult */
if (v2r_vnc_recv1(v, sizeof(security_result)) == -1) {
goto fail;
}
V2R_PACKET_READ_UINT32_BE(v->packet, security_result);
if (security_result == RFB_SEC_RESULT_OK) {
v2r_log_info("vnc authentication success");
} else {
v2r_log_error("vnc authentication failed");
goto fail;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_recv_server_init(v2r_vnc_t *v)
{
if (v2r_vnc_recv(v) == -1) {
goto fail;
}
V2R_PACKET_READ_UINT16_BE(v->packet, v->framebuffer_width);
V2R_PACKET_READ_UINT16_BE(v->packet, v->framebuffer_height);
V2R_PACKET_READ_UINT8(v->packet, v->bits_per_pixel);
V2R_PACKET_READ_UINT8(v->packet, v->depth);
V2R_PACKET_READ_UINT8(v->packet, v->big_endian_flag);
V2R_PACKET_READ_UINT8(v->packet, v->true_colour_flag);
V2R_PACKET_READ_UINT16_BE(v->packet, v->red_max);
V2R_PACKET_READ_UINT16_BE(v->packet, v->green_max);
V2R_PACKET_READ_UINT16_BE(v->packet, v->blue_max);
V2R_PACKET_READ_UINT8(v->packet, v->red_shift);
V2R_PACKET_READ_UINT8(v->packet, v->green_shift);
V2R_PACKET_READ_UINT8(v->packet, v->blue_shift);
v2r_log_info("server framebuffer size: %dx%d", v->framebuffer_width,
v->framebuffer_height);
v2r_log_info("server bpp: %d, depth: %d, big_endian: %d, true_colour: %d, "
"red_max: %d, green_max: %d, blue_max: %d, "
"red_shift: %d, green_shift: %d, blue_shift: %d",
v->bits_per_pixel, v->depth, v->big_endian_flag,
v->true_colour_flag, v->red_max, v->green_max, v->blue_max,
v->red_shift, v->green_shift, v->blue_shift);
/* set big_endian_flag to 0 because RDP use little endian */
v->big_endian_flag = 0;
/* set colour shift to RDP order */
switch (v->depth) {
case 24:
v->red_shift = 16;
v->green_shift = 8;
v->blue_shift = 0;
v->bpp = 32;
break;
case 16:
v->red_shift = 11;
v->green_shift = 5;
v->blue_shift = 0;
v->bpp = 16;
break;
case 15:
v->red_shift = 10;
v->green_shift = 5;
v->blue_shift = 0;
v->bpp = 15;
break;
case 8:
v->true_colour_flag = 0;
v->bpp = 8;
break;
}
return 0;
fail:
return -1;
}
int
v2r_vnc_build_conn(v2r_vnc_t *v, int server_fd)
{
int optval = 1;
v->fd = server_fd;
/* disable Nagle algorithm */
if (setsockopt(v->fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval))
== -1) {
goto fail;
}
/* receive ProtocolVersion */
if (v2r_vnc_recv(v) == -1) {
goto fail;
}
/* send ProtocolVersion */
v2r_packet_reset(v->packet);
V2R_PACKET_WRITE_N(v->packet, RFB_PROTOCOL_VERSION,
strlen(RFB_PROTOCOL_VERSION));
V2R_PACKET_END(v->packet);
if (v2r_vnc_send(v) == -1) {
goto fail;
}
/* receive security-type */
if (v2r_vnc_recv1(v, sizeof(v->security_type)) == -1) {
goto fail;
}
V2R_PACKET_READ_UINT32_BE(v->packet, v->security_type);
switch (v->security_type) {
case RFB_SEC_TYPE_NONE:
break;
case RFB_SEC_TYPE_VNC_AUTH:
if (v2r_vnc_process_vnc_authentication(v) == -1) {
goto fail;
}
break;
default:
goto fail;
}
/* send ClientInit message */
v2r_packet_reset(v->packet);
V2R_PACKET_WRITE_UINT8(v->packet, v->session->opt->shared);
v2r_log_info("connect with shared-flag: %d", v->session->opt->shared);
V2R_PACKET_END(v->packet);
if (v2r_vnc_send(v) == -1) {
goto fail;
}
/* recv ServerInit message */
if (v2r_vnc_recv_server_init(v) == -1) {
goto fail;
}
/* send SetPixelFormat message */
v2r_packet_reset(v->packet);
V2R_PACKET_WRITE_UINT8(v->packet, RFB_SET_PIXEL_FORMAT);
V2R_PACKET_WRITE_UINT8(v->packet, 0);
V2R_PACKET_WRITE_UINT8(v->packet, 0);
V2R_PACKET_WRITE_UINT8(v->packet, 0);
/* bits-per-pixel */
V2R_PACKET_WRITE_UINT8(v->packet, v->bits_per_pixel);
/* depth */
V2R_PACKET_WRITE_UINT8(v->packet, v->depth);
/* big-endian-flag */
V2R_PACKET_WRITE_UINT8(v->packet, v->big_endian_flag);
/* true-colour-flag */
V2R_PACKET_WRITE_UINT8(v->packet, v->true_colour_flag);
/* red-max */
V2R_PACKET_WRITE_UINT16_BE(v->packet, v->red_max);
/* green-max */
V2R_PACKET_WRITE_UINT16_BE(v->packet, v->green_max);
/* blue-max */
V2R_PACKET_WRITE_UINT16_BE(v->packet, v->blue_max);
/* red-shift */
V2R_PACKET_WRITE_UINT8(v->packet, v->red_shift);
/* green-shift */
V2R_PACKET_WRITE_UINT8(v->packet, v->green_shift);
/* blue-shift */
V2R_PACKET_WRITE_UINT8(v->packet, v->blue_shift);
/* padding */
V2R_PACKET_WRITE_UINT8(v->packet, 0);
V2R_PACKET_WRITE_UINT8(v->packet, 0);
V2R_PACKET_WRITE_UINT8(v->packet, 0);
V2R_PACKET_END(v->packet);
if (v2r_vnc_send(v) == -1) {
goto fail;
}
/* send SetEncodings message */
v2r_packet_reset(v->packet);
V2R_PACKET_WRITE_UINT8(v->packet, RFB_SET_ENCODINGS);
V2R_PACKET_WRITE_UINT8(v->packet, 0);
V2R_PACKET_WRITE_UINT16_BE(v->packet, 2);
V2R_PACKET_WRITE_UINT32_BE(v->packet, RFB_ENCODING_RAW);
V2R_PACKET_WRITE_UINT32_BE(v->packet, RFB_ENCODING_COPYRECT);
//V2R_PACKET_WRITE_UINT32_BE(v->packet, RFB_ENCODING_CURSOR);
//V2R_PACKET_WRITE_UINT32_BE(v->packet, RFB_ENCODING_DESKTOP_SIZE);
V2R_PACKET_END(v->packet);
if (v2r_vnc_send(v) == -1) {
goto fail;
}
/* send FramebufferUpdateRequest message */
if (v2r_vnc_send_fb_update_req(v, 0, 0, 0, v->framebuffer_width,
v->framebuffer_height) == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
v2r_vnc_t *
v2r_vnc_init(v2r_session_t *s)
{
v2r_vnc_t *v = NULL;
v = (v2r_vnc_t *)malloc(sizeof(v2r_vnc_t));
if (v == NULL) {
goto fail;
}
memset(v, 0, sizeof(v2r_vnc_t));
v->session = s;
v->packet = v2r_packet_init(65535);
if (v->packet == NULL) {
goto fail;
}
v->buffer = NULL;
v->buffer_size = 0;
return v;
fail:
v2r_vnc_destory(v);
return NULL;
}
void
v2r_vnc_destory(v2r_vnc_t *v)
{
if (v == NULL) {
return;
}
if (v->fd != 0) {
close(v->fd);
}
if (v->packet != NULL) {
v2r_packet_destory(v->packet);
}
if (v->buffer != NULL) {
free(v->buffer);
}
free(v);
}
static int
v2r_vnc_process_raw_encoding(v2r_vnc_t *v, uint16_t x, uint16_t y,
uint16_t w, uint16_t h)
{
uint16_t left, top, right, bottom, width, height, i;
uint32_t data_size, line_size, rdp_line_size, rdp_data_size;
uint32_t max_line_per_packet, line_per_packet;
const uint32_t max_byte_per_packet = 8192;
/* VNC bitmap size */
data_size = w * h * v->bits_per_pixel / 8;
/* VNC bitmap row size */
line_size = w * v->bits_per_pixel / 8;
/* RDP bitmap row should contains a multiple of four bytes */
rdp_line_size = (line_size % 4) ? ((line_size / 4) + 1) * 4 : line_size;
/* RDP bitmap size */
rdp_data_size = rdp_line_size * h;
/* send at most max_line_per_packet line to RDP client in a RDP bitmap
* update PDU */
max_line_per_packet = max_byte_per_packet / rdp_line_size;
/* if data size is larger than vnc packet's buffer,
* init a new packet with a larger buffer */
if (data_size > v->packet->max_len) {
v2r_packet_destory(v->packet);
v->packet = v2r_packet_init(data_size);
if (v->packet == NULL) {
goto fail;
}
}
/* and realloc a new buffer for VNC image upside down */
if (rdp_data_size > v->buffer_size) {
v->buffer_size = rdp_data_size;
v->buffer = (uint8_t *)realloc(v->buffer, v->buffer_size);
if (v->buffer == NULL) {
v2r_log_error("failed to allocate memory for swap buffer");
goto fail;
}
}
/* receive VNC bitmap data */
if (v2r_vnc_recv1(v, data_size) == -1) {
goto fail;
}
for (i = 0; i < h; i++) {
memcpy(v->buffer + (h - i - 1) * rdp_line_size,
v->packet->data + i * line_size,
line_size);
}
/*
left = x;
top = y;
right = x + w - 1;
bottom = y + h - 1;
width = w;
height = h;
if (v2r_rdp_send_bitmap_update(v->session->rdp, left, top, right, bottom,
width, height, 32, data_size,
v->packet->data) == -1) {
goto fail;
}
*/
for (i = 0; i < h;) {
if (i + max_line_per_packet > h) {
line_per_packet = h - i;
} else {
line_per_packet = max_line_per_packet;
}
left = x;
top = y + h - i - line_per_packet;
right = x + w - 1;
bottom = y + h - i - 1;
width = w;
height = line_per_packet;
if (v2r_rdp_send_bitmap_update(v->session->rdp,
left, top, right, bottom,
width, height, v->bpp,
rdp_line_size * line_per_packet,
v->buffer + i * rdp_line_size) == -1) {
goto fail;
}
i += line_per_packet;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_process_copy_rect_encoding(v2r_vnc_t *v, uint16_t x, uint16_t y,
uint16_t w, uint16_t h)
{
uint16_t src_x, src_y;
if (v2r_vnc_recv1(v, 4) == -1) {
goto fail;
}
V2R_PACKET_READ_UINT16_BE(v->packet, src_x);
V2R_PACKET_READ_UINT16_BE(v->packet, src_y);
v2r_log_debug("copy rect from src_x: %d src_y: %d", src_x, src_y);
if (v2r_rdp_send_scrblt_order(v->session->rdp, x, y, w, h, src_x, src_y)
== -1) {
goto fail;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_process_framebuffer_update(v2r_vnc_t *v)
{
uint16_t nrects = 0, i = 0, x, y, w, h;
int32_t encoding_type;
if (v2r_vnc_recv1(v, 3) == -1) {
goto fail;
}
V2R_PACKET_SEEK_UINT8(v->packet);
V2R_PACKET_READ_UINT16_BE(v->packet, nrects);
//v2r_log_debug("receive framebuffer update with %d rects", nrects);
for (i = 0; i < nrects; i++) {
if (v2r_vnc_recv1(v, 12) == -1) {
goto fail;
}
V2R_PACKET_READ_UINT16_BE(v->packet, x);
V2R_PACKET_READ_UINT16_BE(v->packet, y);
V2R_PACKET_READ_UINT16_BE(v->packet, w);
V2R_PACKET_READ_UINT16_BE(v->packet, h);
V2R_PACKET_READ_UINT32_BE(v->packet, encoding_type);
//v2r_log_debug("rect %d of %d: pos: %d,%d size: %dx%d encoding: %d",
// i + 1, nrects, x, y, w, h, encoding_type);
switch (encoding_type) {
case RFB_ENCODING_RAW:
if (v2r_vnc_process_raw_encoding(v, x, y, w, h) == -1) {
goto fail;
}
break;
case RFB_ENCODING_COPYRECT:
if (v2r_vnc_process_copy_rect_encoding(v, x, y, w, h) == -1) {
goto fail;
}
break;
default:
v2r_log_warn("unknown encoding type: %d", encoding_type);
break;
}
}
/* send FramebufferUpdateRequest message */
if (v2r_vnc_send_fb_update_req(v, 1, 0, 0, v->framebuffer_width,
v->framebuffer_height) == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_process_set_colour_map_entries(v2r_vnc_t *v)
{
uint16_t i, first_colour, n_colours, red, green, blue;
if (v2r_vnc_recv1(v, 5) == -1) {
goto fail;
}
V2R_PACKET_SEEK(v->packet, 1);
V2R_PACKET_READ_UINT16_BE(v->packet, first_colour);
V2R_PACKET_READ_UINT16_BE(v->packet, n_colours);
v2r_log_info("set colour map entries with first_colour: %d, n_colours: %d",
first_colour, n_colours);
if (v2r_vnc_recv1(v, n_colours * 6) == -1) {
goto fail;
}
for (i = 0; i < n_colours; i++) {
V2R_PACKET_READ_UINT16_LE(v->packet, red);
V2R_PACKET_READ_UINT16_LE(v->packet, green);
V2R_PACKET_READ_UINT16_LE(v->packet, blue);
v->colour_map[first_colour + i][0] = red;
v->colour_map[first_colour + i][1] = green;
v->colour_map[first_colour + i][2] = blue;
}
if (v2r_rdp_send_palette_update(v->session->rdp,
RFB_COLOUR_MAP_ENTRIES_SIZE,
v->colour_map) == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_process_bell(v2r_vnc_t *v)
{
v2r_log_debug("server ring a bell");
/* The value of duration 300 and frequency 200 sounds comfortable for a
* bell ring, it can be changed. ant if you change duration too small it
* will be very diffcult to find out there is a bell ring */
if (v2r_rdp_send_play_sound(v->session->rdp, 300, 200) == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
static int
v2r_vnc_process_server_cut_text(v2r_vnc_t *v)
{
uint32_t length;
if (v2r_vnc_recv1(v, 7) == -1) {
goto fail;
}
V2R_PACKET_SEEK(v->packet, 3);
V2R_PACKET_READ_UINT32_BE(v->packet, length);
if (v2r_vnc_recv1(v, length) == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
int
v2r_vnc_process(v2r_vnc_t *v)
{
uint8_t msg_type;
if (v2r_vnc_recv1(v, 1) == -1) {
goto fail;
}
V2R_PACKET_READ_UINT8(v->packet, msg_type);
switch (msg_type) {
case RFB_FRAMEBUFFER_UPDATE:
if (v2r_vnc_process_framebuffer_update(v) == -1) {
goto fail;
}
break;
case RFB_SET_COLOUR_MAP_ENTRIES:
if (v2r_vnc_process_set_colour_map_entries(v) == -1) {
goto fail;
}
break;
case RFB_BELL:
if (v2r_vnc_process_bell(v) == -1) {
goto fail;
}
break;
case RFB_SERVER_CUT_TEXT:
if (v2r_vnc_process_server_cut_text(v) == -1) {
goto fail;
}
break;
default:
v2r_log_debug("reveive unknown message type %d from vnc server",
msg_type);
goto fail;
break;
}
return 0;
fail:
return -1;
}
int
v2r_vnc_send_fb_update_req(v2r_vnc_t *v, uint8_t incremental,
uint16_t x_pos, uint16_t y_pos,
uint16_t width, uint16_t height)
{
/* if client sent Suppress Output PDU, then don't send framebuffer
* update request */
if (v->session->rdp != NULL &&
v->session->rdp->allow_display_updates == SUPPRESS_DISPLAY_UPDATES) {
return 0;
}
/* send FramebufferUpdateRequest message */
v2r_packet_reset(v->packet);
/* message-type */
V2R_PACKET_WRITE_UINT8(v->packet, RFB_FRAMEBUFFER_UPDATE_REQUEST);
/* incremental */
V2R_PACKET_WRITE_UINT8(v->packet, incremental);
/* x-position */
V2R_PACKET_WRITE_UINT16_BE(v->packet, x_pos);
/* y-position */
V2R_PACKET_WRITE_UINT16_BE(v->packet, y_pos);
/* width */
V2R_PACKET_WRITE_UINT16_BE(v->packet, width);
/* height */
V2R_PACKET_WRITE_UINT16_BE(v->packet, height);
V2R_PACKET_END(v->packet);
if (v2r_vnc_send(v) == -1) {
goto fail;
}
return 0;
fail:
return -1;
}
int
v2r_vnc_send_key_event(v2r_vnc_t *v, uint8_t down_flag, uint32_t key)
{
/* if 'viewonly' is specified, don't send keyboard event to server */
if (v->session->opt->viewonly) {
return 0;
}
/* send KeyEvent message */
v2r_packet_reset(v->packet);
/* message-type */
V2R_PACKET_WRITE_UINT8(v->packet, RFB_KEY_EVENT);
/* down-flag */
V2R_PACKET_WRITE_UINT8(v->packet, down_flag);
/* padding */
V2R_PACKET_WRITE_UINT16_BE(v->packet, 0);
/* key */
V2R_PACKET_WRITE_UINT32_BE(v->packet, key);
V2R_PACKET_END(v->packet);
return v2r_vnc_send(v);
}
int
v2r_vnc_send_pointer_event(v2r_vnc_t *v, uint8_t btn_mask,
uint16_t x_pos, uint16_t y_pos)
{
/* if 'viewonly' is specified, don't send pointer event to server */
if (v->session->opt->viewonly) {
return 0;
}
/* send PointerEvent message */
v2r_packet_reset(v->packet);
/* message-type */
V2R_PACKET_WRITE_UINT8(v->packet, RFB_POINTER_EVENT);
/* button-mask */
V2R_PACKET_WRITE_UINT8(v->packet, btn_mask);
/* x-position */
V2R_PACKET_WRITE_UINT16_BE(v->packet, x_pos);
V2R_PACKET_WRITE_UINT16_BE(v->packet, y_pos);
V2R_PACKET_END(v->packet);
return v2r_vnc_send(v);
}