FreeBSD Bugzilla – Attachment 219467 Details for
Bug 250795
support RFB 3.3 for VNC client interoperability
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
svn diff output of the relevant changes; ver3
rfb.diff4 (text/plain), 10.87 KB, created by
marko
on 2020-11-08 17:22:44 UTC
(
hide
)
Description:
svn diff output of the relevant changes; ver3
Filename:
MIME Type:
Creator:
marko
Created:
2020-11-08 17:22:44 UTC
Size:
10.87 KB
patch
obsolete
>Index: rfb.c >=================================================================== >--- rfb.c (revision 367488) >+++ rfb.c (working copy) >@@ -105,9 +105,11 @@ > int zbuflen; > > int conn_wait; >- int sending; >+ int pending; >+ int update_all; > pthread_mutex_t mtx; > pthread_cond_t cond; >+ pthread_cond_t writer_cond; > > int hw_crc; > uint32_t *crc; /* WxH crc cells */ >@@ -202,7 +204,6 @@ > uint32_t length; > }; > >- > static void > rfb_send_server_init_msg(int cfd) > { >@@ -223,6 +224,9 @@ > sinfo.pixfmt.red_shift = 16; > sinfo.pixfmt.green_shift = 8; > sinfo.pixfmt.blue_shift = 0; >+ sinfo.pixfmt.pad[0] = 0; >+ sinfo.pixfmt.pad[1] = 0; >+ sinfo.pixfmt.pad[2] = 0; > sinfo.namelen = htonl(strlen("bhyve")); > (void)stream_write(cfd, &sinfo, sizeof(sinfo)); > (void)stream_write(cfd, "bhyve", strlen("bhyve")); >@@ -265,7 +269,6 @@ > int i; > uint32_t encoding; > >- assert((sizeof(enc_msg) - 1) == 3); > (void)stream_read(cfd, ((void *)&enc_msg)+1, sizeof(enc_msg)-1); > > for (i = 0; i < htons(enc_msg.numencs); i++) { >@@ -308,12 +311,23 @@ > return (crcval); > } > >+static int >+rfb_send_update_header(struct rfb_softc *rc, int cfd, int numrects) >+{ >+ struct rfb_srvr_updt_msg supdt_msg; > >+ supdt_msg.type = 0; >+ supdt_msg.pad = 0; >+ supdt_msg.numrects = htons(numrects); >+ >+ return stream_write(cfd, &supdt_msg, >+ sizeof(struct rfb_srvr_updt_msg)); >+} >+ > static int > rfb_send_rect(struct rfb_softc *rc, int cfd, struct bhyvegc_image *gc, > int x, int y, int w, int h) > { >- struct rfb_srvr_updt_msg supdt_msg; > struct rfb_srvr_rect_hdr srect_hdr; > unsigned long zlen; > ssize_t nwrite, total; >@@ -325,16 +339,6 @@ > * Send a single rectangle of the given x, y, w h dimensions. > */ > >- /* Number of rectangles: 1 */ >- supdt_msg.type = 0; >- supdt_msg.pad = 0; >- supdt_msg.numrects = htons(1); >- nwrite = stream_write(cfd, &supdt_msg, >- sizeof(struct rfb_srvr_updt_msg)); >- if (nwrite <= 0) >- return (nwrite); >- >- > /* Rectangle header */ > srect_hdr.x = htons(x); > srect_hdr.y = htons(y); >@@ -479,7 +483,7 @@ > #define PIXCELL_MASK 0x1F > > static int >-rfb_send_screen(struct rfb_softc *rc, int cfd, int all) >+rfb_send_screen(struct rfb_softc *rc, int cfd) > { > struct bhyvegc_image *gc_image; > ssize_t nwrite; >@@ -493,21 +497,36 @@ > uint32_t *crc_p, *orig_crc; > int changes; > >+ retval = 1; >+ > console_refresh(); > gc_image = console_get_image(); > >- pthread_mutex_lock(&rc->mtx); >- if (rc->sending) { >- pthread_mutex_unlock(&rc->mtx); >- return (1); >+ /* Clear old CRC values when the size changes */ >+ if (rc->crc_width != gc_image->width || >+ rc->crc_height != gc_image->height) { >+ memset(rc->crc, 0, sizeof(uint32_t) * >+ howmany(RFB_MAX_WIDTH, PIX_PER_CELL) * >+ howmany(RFB_MAX_HEIGHT, PIX_PER_CELL)); >+ rc->crc_width = gc_image->width; >+ rc->crc_height = gc_image->height; > } >- rc->sending = 1; >- pthread_mutex_unlock(&rc->mtx); > >- retval = 0; >+ /* A size update counts as an update in itself */ >+ if (rc->width != gc_image->width || >+ rc->height != gc_image->height) { >+ rc->width = gc_image->width; >+ rc->height = gc_image->height; >+ if (rc->enc_resize_ok) { >+ rfb_send_resize_update_msg(rc, cfd); >+ rc->update_all = 1; >+ goto done; >+ } >+ } > >- if (all) { >+ if (rc->update_all) { > retval = rfb_send_all(rc, cfd, gc_image); >+ rc->update_all = 0; > goto done; > } > >@@ -516,11 +535,6 @@ > * has changed since the last scan. > */ > >- /* Resolution changed */ >- >- rc->crc_width = gc_image->width; >- rc->crc_height = gc_image->height; >- > w = rc->crc_width; > h = rc->crc_height; > xcells = howmany(rc->crc_width, PIX_PER_CELL); >@@ -580,12 +594,20 @@ > } > } > >+ /* We only send the update if there are changes */ >+ if (!changes) { >+ goto done; >+ } >+ rc->pending = 0; >+ > /* If number of changes is > THRESH percent, send the whole screen */ > if (((changes * 100) / (xcells * ycells)) >= RFB_SEND_ALL_THRESH) { > retval = rfb_send_all(rc, cfd, gc_image); > goto done; > } >- >+ >+ rfb_send_update_header(rc, cfd, changes); >+ > /* Go through all cells, and send only changed ones */ > crc_p = rc->crc_tmp; > for (y = 0; y < h; y += PIX_PER_CELL) { >@@ -613,45 +635,28 @@ > } > } > } >- retval = 1; > > done: >- pthread_mutex_lock(&rc->mtx); >- rc->sending = 0; >- pthread_mutex_unlock(&rc->mtx); >- >+ > return (retval); > } > > > static void >-rfb_recv_update_msg(struct rfb_softc *rc, int cfd, int discardonly) >+rfb_recv_update_msg(struct rfb_softc *rc, int cfd) > { > struct rfb_updt_msg updt_msg; >- struct bhyvegc_image *gc_image; > > (void)stream_read(cfd, ((void *)&updt_msg) + 1 , sizeof(updt_msg) - 1); > >- console_refresh(); >- gc_image = console_get_image(); >- >- updt_msg.x = htons(updt_msg.x); >- updt_msg.y = htons(updt_msg.y); >- updt_msg.width = htons(updt_msg.width); >- updt_msg.height = htons(updt_msg.height); >- >- if (updt_msg.width != gc_image->width || >- updt_msg.height != gc_image->height) { >- rc->width = gc_image->width; >- rc->height = gc_image->height; >- if (rc->enc_resize_ok) >- rfb_send_resize_update_msg(rc, cfd); >+ rc->pending = 1; >+ if (!updt_msg.incremental) { >+ rc->update_all = 1; > } > >- if (discardonly) >- return; >- >- rfb_send_screen(rc, cfd, 1); >+ pthread_mutex_lock(&rc->mtx); >+ pthread_cond_signal(&rc->writer_cond); >+ pthread_mutex_unlock(&rc->mtx); > } > > static void >@@ -703,40 +708,28 @@ > rfb_wr_thr(void *arg) > { > struct rfb_softc *rc; >- fd_set rfds; >- struct timeval tv; >- struct timeval prev_tv; >- int64_t tdiff; >+ struct timespec ts; > int cfd; >- int err; > > rc = arg; > cfd = rc->cfd; > >- prev_tv.tv_sec = 0; >- prev_tv.tv_usec = 0; > while (rc->cfd >= 0) { >- FD_ZERO(&rfds); >- FD_SET(cfd, &rfds); >- tv.tv_sec = 0; >- tv.tv_usec = 10000; >+ clock_gettime(CLOCK_MONOTONIC, &ts); >+ ts.tv_nsec += 10000000; >+ if (ts.tv_nsec >= 1000000000) { >+ ts.tv_sec++; >+ ts.tv_nsec -= 1000000000; >+ } >+ pthread_mutex_lock(&rc->mtx); >+ pthread_cond_timedwait(&rc->writer_cond, &rc->mtx, &ts); >+ pthread_mutex_unlock(&rc->mtx); > >- err = select(cfd+1, &rfds, NULL, NULL, &tv); >- if (err < 0) >- return (NULL); >- >- /* Determine if its time to push screen; ~24hz */ >- gettimeofday(&tv, NULL); >- tdiff = timeval_delta(&prev_tv, &tv); >- if (tdiff > 40000) { >- prev_tv.tv_sec = tv.tv_sec; >- prev_tv.tv_usec = tv.tv_usec; >- if (rfb_send_screen(rc, cfd, 0) <= 0) { >+ /* Updates require a preceding update request */ >+ if (rc->pending) { >+ if (rfb_send_screen(rc, cfd) <= 0) { > return (NULL); > } >- } else { >- /* sleep */ >- usleep(40000 - tdiff); > } > } > >@@ -758,7 +751,8 @@ > DES_key_schedule ks; > int i; > #endif >- >+ uint8_t client_ver; >+ uint8_t auth_type; > pthread_t tid; > uint32_t sres = 0; > int len; >@@ -771,27 +765,55 @@ > > /* 1b. Read client version */ > len = stream_read(cfd, buf, VERSION_LENGTH); >+ if (len == VERSION_LENGTH && !strncmp(vbuf, buf, VERSION_LENGTH - 2)) { >+ client_ver = buf[VERSION_LENGTH - 2]; >+ } >+ if (client_ver != '8' && client_ver != '7') { >+ /* only recognize 3.3, 3.7 & 3.8. Others dflt to 3.3 */ >+ client_ver = '3'; >+ } > > /* 2a. Send security type */ > buf[0] = 1; >+ >+ /* In versions 3.7 & 3.8, it's 2-way handshake */ >+ /* For version 3.3, server says what the authentication type must be */ > #ifndef NO_OPENSSL >- if (rc->password) >- buf[1] = SECURITY_TYPE_VNC_AUTH; >- else >- buf[1] = SECURITY_TYPE_NONE; >+ if (rc->password) { >+ auth_type = SECURITY_TYPE_VNC_AUTH; >+ } else { >+ auth_type = SECURITY_TYPE_NONE; >+ } > #else >- buf[1] = SECURITY_TYPE_NONE; >+ auth_type = SECURITY_TYPE_NONE; > #endif > >- stream_write(cfd, buf, 2); >+ switch (client_ver) { >+ case '7': >+ case '8': >+ buf[0] = 1; >+ buf[1] = auth_type; >+ stream_write(cfd, buf, 2); > >- /* 2b. Read agreed security type */ >- len = stream_read(cfd, buf, 1); >+ /* 2b. Read agreed security type */ >+ len = stream_read(cfd, buf, 1); >+ if (buf[0] != auth_type) { >+ /* deny */ >+ sres = htonl(1); >+ message = "Auth failed: authentication type mismatch"; >+ goto report_and_done; >+ } >+ break; >+ case '3': >+ default: >+ be32enc(buf, auth_type); >+ stream_write(cfd, buf, 4); >+ break; >+ } > > /* 2c. Do VNC authentication */ >- switch (buf[0]) { >+ switch (auth_type) { > case SECURITY_TYPE_NONE: >- sres = 0; > break; > case SECURITY_TYPE_VNC_AUTH: > /* >@@ -839,10 +861,11 @@ > if (memcmp(crypt_expected, buf, AUTH_LENGTH) != 0) { > message = "Auth Failed: Invalid Password."; > sres = htonl(1); >- } else >+ } else { > sres = 0; >+ } > #else >- sres = 0; >+ sres = htonl(1); > WPRINTF(("Auth not supported, no OpenSSL in your system")); > #endif > >@@ -849,14 +872,34 @@ > break; > } > >- /* 2d. Write back a status */ >- stream_write(cfd, &sres, 4); >+ switch (client_ver) { >+ case '7': >+ case '8': >+report_and_done: >+ /* 2d. Write back a status */ >+ stream_write(cfd, &sres, 4); > >- if (sres) { >- be32enc(buf, strlen(message)); >- stream_write(cfd, buf, 4); >- stream_write(cfd, message, strlen(message)); >- goto done; >+ if (sres) { >+ /* 3.7 does not want string explaining cause */ >+ if (client_ver == '8') { >+ be32enc(buf, strlen(message)); >+ stream_write(cfd, buf, 4); >+ stream_write(cfd, message, strlen(message)); >+ } >+ goto done; >+ } >+ break; >+ case '3': >+ default: >+ /* for VNC auth case send status */ >+ if (auth_type == SECURITY_TYPE_VNC_AUTH) { >+ /* 2d. Write back a status */ >+ stream_write(cfd, &sres, 4); >+ } >+ if (sres) { >+ goto done; >+ } >+ break; > } > > /* 3a. Read client shared-flag byte */ >@@ -870,8 +913,6 @@ > assert(rc->zbuf != NULL); > } > >- rfb_send_screen(rc, cfd, 1); >- > perror = pthread_create(&tid, NULL, rfb_wr_thr, rc); > if (perror == 0) > pthread_set_name_np(tid, "rfbout"); >@@ -892,7 +933,7 @@ > rfb_recv_set_encodings_msg(rc, cfd); > break; > case 3: >- rfb_recv_update_msg(rc, cfd, 1); >+ rfb_recv_update_msg(rc, cfd); > break; > case 4: > rfb_recv_key_msg(rc, cfd); >@@ -974,6 +1015,8 @@ > struct addrinfo *ai = NULL; > struct addrinfo hints; > int on = 1; >+ int cnt; >+ pthread_condattr_t condattr; > #ifndef WITHOUT_CAPSICUM > cap_rights_t rights; > #endif >@@ -980,10 +1023,10 @@ > > rc = calloc(1, sizeof(struct rfb_softc)); > >- rc->crc = calloc(howmany(RFB_MAX_WIDTH * RFB_MAX_HEIGHT, 32), >- sizeof(uint32_t)); >- rc->crc_tmp = calloc(howmany(RFB_MAX_WIDTH * RFB_MAX_HEIGHT, 32), >- sizeof(uint32_t)); >+ cnt = howmany(RFB_MAX_WIDTH, PIX_PER_CELL) * >+ howmany(RFB_MAX_HEIGHT, PIX_PER_CELL); >+ rc->crc = calloc(cnt, sizeof(uint32_t)); >+ rc->crc_tmp = calloc(cnt, sizeof(uint32_t)); > rc->crc_width = RFB_MAX_WIDTH; > rc->crc_height = RFB_MAX_HEIGHT; > rc->sfd = -1; >@@ -1037,10 +1080,15 @@ > > rc->conn_wait = wait; > if (wait) { >- pthread_mutex_init(&rc->mtx, NULL); > pthread_cond_init(&rc->cond, NULL); > } > >+ pthread_mutex_init(&rc->mtx, NULL); >+ >+ pthread_condattr_init(&condattr); >+ pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC); >+ pthread_cond_init(&rc->writer_cond, &condattr); >+ > pthread_create(&rc->tid, NULL, rfb_thr, rc); > pthread_set_name_np(rc->tid, "rfb"); >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 250795
:
219296
|
219393
| 219467 |
220542