2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5 * Copyright (c) 2015 Leon Dang
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #ifndef WITHOUT_CAPSICUM
35 #include <sys/capsicum.h>
37 #include <sys/endian.h>
38 #include <sys/socket.h>
39 #include <sys/select.h>
41 #include <arpa/inet.h>
42 #include <stdatomic.h>
43 #include <machine/cpufunc.h>
44 #include <machine/specialreg.h>
45 #include <netinet/in.h>
49 #ifndef WITHOUT_CAPSICUM
50 #include <capsicum_helpers.h>
55 #include <pthread_np.h>
70 #include "sockstream.h"
73 #include <openssl/des.h>
76 /* Delays in microseconds */
77 #define CFD_SEL_DELAY 10000
78 #define SCREEN_REFRESH_DELAY 33300 /* 30Hz */
79 #define SCREEN_POLL_DELAY (SCREEN_REFRESH_DELAY / 2)
81 static int rfb_debug = 0;
82 #define DPRINTF(params) if (rfb_debug) PRINTLN params
83 #define WPRINTF(params) PRINTLN params
85 #define VERSION_LENGTH 12
86 #define AUTH_LENGTH 16
87 #define PASSWD_LENGTH 8
89 /* Protocol versions */
94 /* Client-to-server msg types */
95 #define CS_SET_PIXEL_FORMAT 0
96 #define CS_SET_ENCODINGS 2
97 #define CS_UPDATE_MSG 3
98 #define CS_KEY_EVENT 4
99 #define CS_POINTER_EVENT 5
100 #define CS_CUT_TEXT 6
102 #define SECURITY_TYPE_NONE 1
103 #define SECURITY_TYPE_VNC_AUTH 2
105 #define AUTH_FAILED_UNAUTH 1
106 #define AUTH_FAILED_ERROR 2
131 atomic_bool update_all;
132 atomic_bool input_detected;
138 uint32_t *crc; /* WxH crc cells */
139 uint32_t *crc_tmp; /* buffer to store single crc row */
140 int crc_width, crc_height;
157 struct rfb_srvr_info {
160 struct rfb_pixfmt pixfmt;
164 struct rfb_pixfmt_msg {
167 struct rfb_pixfmt pixfmt;
170 #define RFB_ENCODING_RAW 0
171 #define RFB_ENCODING_ZLIB 6
172 #define RFB_ENCODING_RESIZE -223
174 #define RFB_MAX_WIDTH 2000
175 #define RFB_MAX_HEIGHT 1200
176 #define RFB_ZLIB_BUFSZ RFB_MAX_WIDTH*RFB_MAX_HEIGHT*4
178 /* percentage changes to screen before sending the entire screen */
179 #define RFB_SEND_ALL_THRESH 25
187 struct rfb_updt_msg {
210 struct rfb_srvr_updt_msg {
216 struct rfb_srvr_rect_hdr {
224 struct rfb_cuttext_msg {
231 rfb_send_server_init_msg(int cfd)
233 struct bhyvegc_image *gc_image;
234 struct rfb_srvr_info sinfo;
236 gc_image = console_get_image();
238 sinfo.width = htons(gc_image->width);
239 sinfo.height = htons(gc_image->height);
240 sinfo.pixfmt.bpp = 32;
241 sinfo.pixfmt.depth = 32;
242 sinfo.pixfmt.bigendian = 0;
243 sinfo.pixfmt.truecolor = 1;
244 sinfo.pixfmt.red_max = htons(255);
245 sinfo.pixfmt.green_max = htons(255);
246 sinfo.pixfmt.blue_max = htons(255);
247 sinfo.pixfmt.red_shift = 16;
248 sinfo.pixfmt.green_shift = 8;
249 sinfo.pixfmt.blue_shift = 0;
250 sinfo.pixfmt.pad[0] = 0;
251 sinfo.pixfmt.pad[1] = 0;
252 sinfo.pixfmt.pad[2] = 0;
253 sinfo.namelen = htonl(strlen("bhyve"));
254 (void)stream_write(cfd, &sinfo, sizeof(sinfo));
255 (void)stream_write(cfd, "bhyve", strlen("bhyve"));
259 rfb_send_resize_update_msg(struct rfb_softc *rc, int cfd)
261 struct rfb_srvr_updt_msg supdt_msg;
262 struct rfb_srvr_rect_hdr srect_hdr;
264 /* Number of rectangles: 1 */
267 supdt_msg.numrects = htons(1);
268 stream_write(cfd, &supdt_msg, sizeof(struct rfb_srvr_updt_msg));
270 /* Rectangle header */
271 srect_hdr.x = htons(0);
272 srect_hdr.y = htons(0);
273 srect_hdr.width = htons(rc->width);
274 srect_hdr.height = htons(rc->height);
275 srect_hdr.encoding = htonl(RFB_ENCODING_RESIZE);
276 stream_write(cfd, &srect_hdr, sizeof(struct rfb_srvr_rect_hdr));
280 rfb_recv_set_pixfmt_msg(struct rfb_softc *rc, int cfd)
282 struct rfb_pixfmt_msg pixfmt_msg;
284 (void)stream_read(cfd, ((void *)&pixfmt_msg)+1, sizeof(pixfmt_msg)-1);
289 rfb_recv_set_encodings_msg(struct rfb_softc *rc, int cfd)
291 struct rfb_enc_msg enc_msg;
295 (void)stream_read(cfd, ((void *)&enc_msg)+1, sizeof(enc_msg)-1);
297 for (i = 0; i < htons(enc_msg.numencs); i++) {
298 (void)stream_read(cfd, &encoding, sizeof(encoding));
299 switch (htonl(encoding)) {
300 case RFB_ENCODING_RAW:
301 rc->enc_raw_ok = true;
303 case RFB_ENCODING_ZLIB:
304 if (!rc->enc_zlib_ok) {
305 deflateInit(&rc->zstream, Z_BEST_SPEED);
306 rc->enc_zlib_ok = true;
309 case RFB_ENCODING_RESIZE:
310 rc->enc_resize_ok = true;
317 * Calculate CRC32 using SSE4.2; Intel or AMD Bulldozer+ CPUs only
319 static __inline uint32_t
320 fast_crc32(void *buf, int len, uint32_t crcval)
322 uint32_t q = len / sizeof(uint32_t);
323 uint32_t *p = (uint32_t *)buf;
327 ".byte 0xf2, 0xf, 0x38, 0xf1, 0xf1;"
329 :"0" (crcval), "c" (*p)
338 rfb_send_update_header(struct rfb_softc *rc, int cfd, int numrects)
340 struct rfb_srvr_updt_msg supdt_msg;
344 supdt_msg.numrects = htons(numrects);
346 return stream_write(cfd, &supdt_msg,
347 sizeof(struct rfb_srvr_updt_msg));
351 rfb_send_rect(struct rfb_softc *rc, int cfd, struct bhyvegc_image *gc,
352 int x, int y, int w, int h)
354 struct rfb_srvr_rect_hdr srect_hdr;
356 ssize_t nwrite, total;
362 * Send a single rectangle of the given x, y, w h dimensions.
365 /* Rectangle header */
366 srect_hdr.x = htons(x);
367 srect_hdr.y = htons(y);
368 srect_hdr.width = htons(w);
369 srect_hdr.height = htons(h);
372 w *= sizeof(uint32_t);
373 if (rc->enc_zlib_ok) {
375 rc->zstream.total_in = 0;
376 rc->zstream.total_out = 0;
377 for (p = &gc->data[y * gc->width + x]; y < h; y++) {
378 rc->zstream.next_in = (Bytef *)p;
379 rc->zstream.avail_in = w;
380 rc->zstream.next_out = (Bytef *)zbufp;
381 rc->zstream.avail_out = RFB_ZLIB_BUFSZ + 16 -
382 rc->zstream.total_out;
383 rc->zstream.data_type = Z_BINARY;
385 /* Compress with zlib */
386 err = deflate(&rc->zstream, Z_SYNC_FLUSH);
388 WPRINTF(("zlib[rect] deflate err: %d", err));
389 rc->enc_zlib_ok = false;
390 deflateEnd(&rc->zstream);
393 zbufp = rc->zbuf + rc->zstream.total_out;
396 srect_hdr.encoding = htonl(RFB_ENCODING_ZLIB);
397 nwrite = stream_write(cfd, &srect_hdr,
398 sizeof(struct rfb_srvr_rect_hdr));
402 zlen = htonl(rc->zstream.total_out);
403 nwrite = stream_write(cfd, &zlen, sizeof(uint32_t));
406 return (stream_write(cfd, rc->zbuf, rc->zstream.total_out));
413 for (p = &gc->data[y * gc->width + x]; y < h; y++) {
420 srect_hdr.encoding = htonl(RFB_ENCODING_RAW);
421 nwrite = stream_write(cfd, &srect_hdr,
422 sizeof(struct rfb_srvr_rect_hdr));
426 total = stream_write(cfd, rc->zbuf, total);
432 rfb_send_all(struct rfb_softc *rc, int cfd, struct bhyvegc_image *gc)
434 struct rfb_srvr_updt_msg supdt_msg;
435 struct rfb_srvr_rect_hdr srect_hdr;
441 * Send the whole thing
444 /* Number of rectangles: 1 */
447 supdt_msg.numrects = htons(1);
448 nwrite = stream_write(cfd, &supdt_msg,
449 sizeof(struct rfb_srvr_updt_msg));
453 /* Rectangle header */
456 srect_hdr.width = htons(gc->width);
457 srect_hdr.height = htons(gc->height);
458 if (rc->enc_zlib_ok) {
459 rc->zstream.next_in = (Bytef *)gc->data;
460 rc->zstream.avail_in = gc->width * gc->height *
462 rc->zstream.next_out = (Bytef *)rc->zbuf;
463 rc->zstream.avail_out = RFB_ZLIB_BUFSZ + 16;
464 rc->zstream.data_type = Z_BINARY;
466 rc->zstream.total_in = 0;
467 rc->zstream.total_out = 0;
469 /* Compress with zlib */
470 err = deflate(&rc->zstream, Z_SYNC_FLUSH);
472 WPRINTF(("zlib deflate err: %d", err));
473 rc->enc_zlib_ok = false;
474 deflateEnd(&rc->zstream);
478 srect_hdr.encoding = htonl(RFB_ENCODING_ZLIB);
479 nwrite = stream_write(cfd, &srect_hdr,
480 sizeof(struct rfb_srvr_rect_hdr));
484 zlen = htonl(rc->zstream.total_out);
485 nwrite = stream_write(cfd, &zlen, sizeof(uint32_t));
488 return (stream_write(cfd, rc->zbuf, rc->zstream.total_out));
492 srect_hdr.encoding = htonl(RFB_ENCODING_RAW);
493 nwrite = stream_write(cfd, &srect_hdr,
494 sizeof(struct rfb_srvr_rect_hdr));
498 nwrite = stream_write(cfd, gc->data,
499 gc->width * gc->height * sizeof(uint32_t));
504 #define PIX_PER_CELL 32
505 #define PIXCELL_SHIFT 5
506 #define PIXCELL_MASK 0x1F
509 rfb_send_screen(struct rfb_softc *rc, int cfd)
511 struct bhyvegc_image *gc_image;
514 int celly, cellwidth;
518 int rem_x, rem_y; /* remainder for resolutions not x32 pixels ratio */
520 uint32_t *crc_p, *orig_crc;
524 /* Return if another thread sending */
526 if (atomic_compare_exchange_strong(&rc->sending, &expected, true) == false)
531 /* Updates require a preceding update request */
532 if (atomic_exchange(&rc->pending, false) == false)
536 gc_image = console_get_image();
538 /* Clear old CRC values when the size changes */
539 if (rc->crc_width != gc_image->width ||
540 rc->crc_height != gc_image->height) {
541 memset(rc->crc, 0, sizeof(uint32_t) *
542 howmany(RFB_MAX_WIDTH, PIX_PER_CELL) *
543 howmany(RFB_MAX_HEIGHT, PIX_PER_CELL));
544 rc->crc_width = gc_image->width;
545 rc->crc_height = gc_image->height;
548 /* A size update counts as an update in itself */
549 if (rc->width != gc_image->width ||
550 rc->height != gc_image->height) {
551 rc->width = gc_image->width;
552 rc->height = gc_image->height;
553 if (rc->enc_resize_ok) {
554 rfb_send_resize_update_msg(rc, cfd);
555 rc->update_all = true;
560 if (atomic_exchange(&rc->update_all, false) == true) {
561 retval = rfb_send_all(rc, cfd, gc_image);
566 * Calculate the checksum for each 32x32 cell. Send each that
567 * has changed since the last scan.
572 xcells = howmany(rc->crc_width, PIX_PER_CELL);
573 ycells = howmany(rc->crc_height, PIX_PER_CELL);
575 rem_x = w & PIXCELL_MASK;
577 rem_y = h & PIXCELL_MASK;
579 rem_y = PIX_PER_CELL;
584 * Go through all cells and calculate crc. If significant number
585 * of changes, then send entire screen.
586 * crc_tmp is dual purpose: to store the new crc and to flag as
587 * a cell that has changed.
589 crc_p = rc->crc_tmp - xcells;
590 orig_crc = rc->crc - xcells;
592 memset(rc->crc_tmp, 0, sizeof(uint32_t) * xcells * ycells);
593 for (y = 0; y < h; y++) {
594 if ((y & PIXCELL_MASK) == 0) {
599 for (x = 0; x < xcells; x++) {
600 if (x == (xcells - 1) && rem_x > 0)
603 cellwidth = PIX_PER_CELL;
606 crc_p[x] = fast_crc32(p,
607 cellwidth * sizeof(uint32_t),
610 crc_p[x] = (uint32_t)crc32(crc_p[x],
612 cellwidth * sizeof(uint32_t));
616 /* check for crc delta if last row in cell */
617 if ((y & PIXCELL_MASK) == PIXCELL_MASK || y == (h-1)) {
618 if (orig_crc[x] != crc_p[x]) {
619 orig_crc[x] = crc_p[x];
630 * We only send the update if there are changes.
631 * Restore the pending flag since it was unconditionally cleared
639 /* If number of changes is > THRESH percent, send the whole screen */
640 if (((changes * 100) / (xcells * ycells)) >= RFB_SEND_ALL_THRESH) {
641 retval = rfb_send_all(rc, cfd, gc_image);
645 rfb_send_update_header(rc, cfd, changes);
647 /* Go through all cells, and send only changed ones */
649 for (y = 0; y < h; y += PIX_PER_CELL) {
650 /* previous cell's row */
651 celly = (y >> PIXCELL_SHIFT);
653 /* Delta check crc to previous set */
654 for (x = 0; x < xcells; x++) {
658 if (x == (xcells - 1) && rem_x > 0)
661 cellwidth = PIX_PER_CELL;
662 nwrite = rfb_send_rect(rc, cfd,
665 celly * PIX_PER_CELL,
667 y + PIX_PER_CELL >= h ? rem_y : PIX_PER_CELL);
683 rfb_recv_update_msg(struct rfb_softc *rc, int cfd)
685 struct rfb_updt_msg updt_msg;
687 (void)stream_read(cfd, ((void *)&updt_msg) + 1 , sizeof(updt_msg) - 1);
690 if (!updt_msg.incremental)
691 rc->update_all = true;
695 rfb_recv_key_msg(struct rfb_softc *rc, int cfd)
697 struct rfb_key_msg key_msg;
699 (void)stream_read(cfd, ((void *)&key_msg) + 1, sizeof(key_msg) - 1);
701 console_key_event(key_msg.down, htonl(key_msg.code));
702 rc->input_detected = true;
706 rfb_recv_ptr_msg(struct rfb_softc *rc, int cfd)
708 struct rfb_ptr_msg ptr_msg;
710 (void)stream_read(cfd, ((void *)&ptr_msg) + 1, sizeof(ptr_msg) - 1);
712 console_ptr_event(ptr_msg.button, htons(ptr_msg.x), htons(ptr_msg.y));
713 rc->input_detected = true;
717 rfb_recv_cuttext_msg(struct rfb_softc *rc, int cfd)
719 struct rfb_cuttext_msg ct_msg;
720 unsigned char buf[32];
723 len = stream_read(cfd, ((void *)&ct_msg) + 1, sizeof(ct_msg) - 1);
724 ct_msg.length = htonl(ct_msg.length);
725 while (ct_msg.length > 0) {
726 len = stream_read(cfd, buf, ct_msg.length > sizeof(buf) ?
727 sizeof(buf) : ct_msg.length);
728 ct_msg.length -= len;
733 timeval_delta(struct timeval *prev, struct timeval *now)
736 n1 = now->tv_sec * 1000000 + now->tv_usec;
737 n2 = prev->tv_sec * 1000000 + prev->tv_usec;
742 rfb_wr_thr(void *arg)
744 struct rfb_softc *rc;
747 struct timeval prev_tv;
757 while (rc->cfd >= 0) {
761 tv.tv_usec = CFD_SEL_DELAY;
763 err = select(cfd+1, &rfds, NULL, NULL, &tv);
767 /* Determine if its time to push screen; ~24hz */
768 gettimeofday(&tv, NULL);
769 tdiff = timeval_delta(&prev_tv, &tv);
770 if (tdiff >= SCREEN_POLL_DELAY) {
772 prev_tv.tv_sec = tv.tv_sec;
773 prev_tv.tv_usec = tv.tv_usec;
774 input = atomic_exchange(&rc->input_detected, false);
776 * Refresh the screen on every second trip through the loop,
777 * or if keyboard/mouse input has been detected.
779 if ((++rc->wrcount & 1) || input) {
780 if (rfb_send_screen(rc, cfd) <= 0) {
786 usleep(SCREEN_POLL_DELAY - tdiff);
794 rfb_handle(struct rfb_softc *rc, int cfd)
796 const char *vbuf = "RFB 003.008\n";
797 unsigned char buf[80];
798 unsigned char *message = NULL;
801 unsigned char challenge[AUTH_LENGTH];
802 unsigned char keystr[PASSWD_LENGTH];
803 unsigned char crypt_expected[AUTH_LENGTH];
817 /* 1a. Send server version */
818 stream_write(cfd, vbuf, strlen(vbuf));
820 /* 1b. Read client version */
821 len = stream_read(cfd, buf, VERSION_LENGTH);
822 if (len == VERSION_LENGTH && !strncmp(vbuf, buf, VERSION_LENGTH - 2)) {
823 client_ver = buf[VERSION_LENGTH - 2];
825 if (client_ver != CVERS_3_8 && client_ver != CVERS_3_7) {
826 /* only recognize 3.3, 3.7 & 3.8. Others dflt to 3.3 */
827 client_ver = CVERS_3_3;
830 /* 2a. Send security type */
833 /* In versions 3.7 & 3.8, it's 2-way handshake */
834 /* For version 3.3, server says what the authentication type must be */
837 auth_type = SECURITY_TYPE_VNC_AUTH;
839 auth_type = SECURITY_TYPE_NONE;
842 auth_type = SECURITY_TYPE_NONE;
845 switch (client_ver) {
850 stream_write(cfd, buf, 2);
852 /* 2b. Read agreed security type */
853 len = stream_read(cfd, buf, 1);
854 if (buf[0] != auth_type) {
857 message = "Auth failed: authentication type mismatch";
858 goto report_and_done;
863 be32enc(buf, auth_type);
864 stream_write(cfd, buf, 4);
868 /* 2c. Do VNC authentication */
870 case SECURITY_TYPE_NONE:
872 case SECURITY_TYPE_VNC_AUTH:
874 * The client encrypts the challenge with DES, using a password
875 * supplied by the user as the key.
876 * To form the key, the password is truncated to
877 * eight characters, or padded with null bytes on the right.
878 * The client then sends the resulting 16-bytes response.
881 strncpy(keystr, rc->password, PASSWD_LENGTH);
883 /* VNC clients encrypts the challenge with all the bit fields
884 * in each byte of the password mirrored.
885 * Here we flip each byte of the keystr.
887 for (i = 0; i < PASSWD_LENGTH; i++) {
888 keystr[i] = (keystr[i] & 0xF0) >> 4
889 | (keystr[i] & 0x0F) << 4;
890 keystr[i] = (keystr[i] & 0xCC) >> 2
891 | (keystr[i] & 0x33) << 2;
892 keystr[i] = (keystr[i] & 0xAA) >> 1
893 | (keystr[i] & 0x55) << 1;
896 /* Initialize a 16-byte random challenge */
897 arc4random_buf(challenge, sizeof(challenge));
898 stream_write(cfd, challenge, AUTH_LENGTH);
900 /* Receive the 16-byte challenge response */
901 stream_read(cfd, buf, AUTH_LENGTH);
903 memcpy(crypt_expected, challenge, AUTH_LENGTH);
905 /* Encrypt the Challenge with DES */
906 DES_set_key((const_DES_cblock *)keystr, &ks);
907 DES_ecb_encrypt((const_DES_cblock *)challenge,
908 (const_DES_cblock *)crypt_expected,
910 DES_ecb_encrypt((const_DES_cblock *)(challenge + PASSWD_LENGTH),
911 (const_DES_cblock *)(crypt_expected +
915 if (memcmp(crypt_expected, buf, AUTH_LENGTH) != 0) {
916 message = "Auth Failed: Invalid Password.";
923 WPRINTF(("Auth not supported, no OpenSSL in your system"));
929 switch (client_ver) {
933 /* 2d. Write back a status */
934 stream_write(cfd, &sres, 4);
937 /* 3.7 does not want string explaining cause */
938 if (client_ver == CVERS_3_8) {
939 be32enc(buf, strlen(message));
940 stream_write(cfd, buf, 4);
941 stream_write(cfd, message, strlen(message));
948 /* for VNC auth case send status */
949 if (auth_type == SECURITY_TYPE_VNC_AUTH) {
950 /* 2d. Write back a status */
951 stream_write(cfd, &sres, 4);
958 /* 3a. Read client shared-flag byte */
959 len = stream_read(cfd, buf, 1);
961 /* 4a. Write server-init info */
962 rfb_send_server_init_msg(cfd);
965 rc->zbuf = malloc(RFB_ZLIB_BUFSZ + 16);
966 assert(rc->zbuf != NULL);
969 perror = pthread_create(&tid, NULL, rfb_wr_thr, rc);
971 pthread_set_name_np(tid, "rfbout");
973 /* Now read in client requests. 1st byte identifies type */
975 len = read(cfd, buf, 1);
977 DPRINTF(("rfb client exiting"));
982 case CS_SET_PIXEL_FORMAT:
983 rfb_recv_set_pixfmt_msg(rc, cfd);
985 case CS_SET_ENCODINGS:
986 rfb_recv_set_encodings_msg(rc, cfd);
989 rfb_recv_update_msg(rc, cfd);
992 rfb_recv_key_msg(rc, cfd);
994 case CS_POINTER_EVENT:
995 rfb_recv_ptr_msg(rc, cfd);
998 rfb_recv_cuttext_msg(rc, cfd);
1001 WPRINTF(("rfb unknown cli-code %d!", buf[0] & 0xff));
1008 pthread_join(tid, NULL);
1009 if (rc->enc_zlib_ok)
1010 deflateEnd(&rc->zstream);
1016 struct rfb_softc *rc;
1024 sigaddset(&set, SIGPIPE);
1025 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
1026 perror("pthread_sigmask");
1031 rc->enc_raw_ok = false;
1032 rc->enc_zlib_ok = false;
1033 rc->enc_resize_ok = false;
1035 cfd = accept(rc->sfd, NULL, NULL);
1036 if (rc->conn_wait) {
1037 pthread_mutex_lock(&rc->mtx);
1038 pthread_cond_signal(&rc->cond);
1039 pthread_mutex_unlock(&rc->mtx);
1042 rfb_handle(rc, cfd);
1051 sse42_supported(void)
1053 u_int cpu_registers[4], ecx;
1055 do_cpuid(1, cpu_registers);
1057 ecx = cpu_registers[2];
1059 return ((ecx & CPUID2_SSE42) != 0);
1063 rfb_init(char *hostname, int port, int wait, char *password)
1067 struct rfb_softc *rc;
1068 struct addrinfo *ai = NULL;
1069 struct addrinfo hints;
1072 #ifndef WITHOUT_CAPSICUM
1073 cap_rights_t rights;
1076 rc = calloc(1, sizeof(struct rfb_softc));
1078 cnt = howmany(RFB_MAX_WIDTH, PIX_PER_CELL) *
1079 howmany(RFB_MAX_HEIGHT, PIX_PER_CELL);
1080 rc->crc = calloc(cnt, sizeof(uint32_t));
1081 rc->crc_tmp = calloc(cnt, sizeof(uint32_t));
1082 rc->crc_width = RFB_MAX_WIDTH;
1083 rc->crc_height = RFB_MAX_HEIGHT;
1086 rc->password = password;
1088 snprintf(servname, sizeof(servname), "%d", port ? port : 5900);
1090 if (!hostname || strlen(hostname) == 0)
1092 hostname = "127.0.0.1";
1093 #elif defined(INET6)
1097 memset(&hints, 0, sizeof(hints));
1098 hints.ai_family = AF_UNSPEC;
1099 hints.ai_socktype = SOCK_STREAM;
1100 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_PASSIVE;
1102 if ((e = getaddrinfo(hostname, servname, &hints, &ai)) != 0) {
1103 EPRINTLN("getaddrinfo: %s", gai_strerror(e));
1107 rc->sfd = socket(ai->ai_family, ai->ai_socktype, 0);
1113 setsockopt(rc->sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1115 if (bind(rc->sfd, ai->ai_addr, ai->ai_addrlen) < 0) {
1120 if (listen(rc->sfd, 1) < 0) {
1125 #ifndef WITHOUT_CAPSICUM
1126 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
1127 if (caph_rights_limit(rc->sfd, &rights) == -1)
1128 errx(EX_OSERR, "Unable to apply rights for sandbox");
1131 rc->hw_crc = sse42_supported();
1133 rc->conn_wait = wait;
1135 pthread_mutex_init(&rc->mtx, NULL);
1136 pthread_cond_init(&rc->cond, NULL);
1139 pthread_create(&rc->tid, NULL, rfb_thr, rc);
1140 pthread_set_name_np(rc->tid, "rfb");
1143 DPRINTF(("Waiting for rfb client..."));
1144 pthread_mutex_lock(&rc->mtx);
1145 pthread_cond_wait(&rc->cond, &rc->mtx);
1146 pthread_mutex_unlock(&rc->mtx);
1147 DPRINTF(("rfb client connected"));