vnc.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272
  1. /*
  2. * QEMU VNC display driver
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2006 Fabrice Bellard
  6. * Copyright (C) 2009 Red Hat, Inc
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "vnc.h"
  27. #include "vnc-jobs.h"
  28. #include "sysemu/sysemu.h"
  29. #include "qemu/sockets.h"
  30. #include "qemu/timer.h"
  31. #include "qemu/acl.h"
  32. #include "qapi/qmp/types.h"
  33. #include "qmp-commands.h"
  34. #include "qemu/osdep.h"
  35. #define VNC_REFRESH_INTERVAL_BASE 30
  36. #define VNC_REFRESH_INTERVAL_INC 50
  37. #define VNC_REFRESH_INTERVAL_MAX 2000
  38. static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
  39. static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
  40. #include "vnc_keysym.h"
  41. #include "d3des.h"
  42. static VncDisplay *vnc_display; /* needed for info vnc */
  43. static DisplayChangeListener *dcl;
  44. static int vnc_cursor_define(VncState *vs);
  45. static void vnc_release_modifiers(VncState *vs);
  46. static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
  47. {
  48. #ifdef _VNC_DEBUG
  49. static const char *mn[] = {
  50. [0] = "undefined",
  51. [VNC_SHARE_MODE_CONNECTING] = "connecting",
  52. [VNC_SHARE_MODE_SHARED] = "shared",
  53. [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive",
  54. [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
  55. };
  56. fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
  57. vs->csock, mn[vs->share_mode], mn[mode]);
  58. #endif
  59. if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
  60. vs->vd->num_exclusive--;
  61. }
  62. vs->share_mode = mode;
  63. if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
  64. vs->vd->num_exclusive++;
  65. }
  66. }
  67. static char *addr_to_string(const char *format,
  68. struct sockaddr_storage *sa,
  69. socklen_t salen) {
  70. char *addr;
  71. char host[NI_MAXHOST];
  72. char serv[NI_MAXSERV];
  73. int err;
  74. size_t addrlen;
  75. if ((err = getnameinfo((struct sockaddr *)sa, salen,
  76. host, sizeof(host),
  77. serv, sizeof(serv),
  78. NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
  79. VNC_DEBUG("Cannot resolve address %d: %s\n",
  80. err, gai_strerror(err));
  81. return NULL;
  82. }
  83. /* Enough for the existing format + the 2 vars we're
  84. * substituting in. */
  85. addrlen = strlen(format) + strlen(host) + strlen(serv);
  86. addr = g_malloc(addrlen + 1);
  87. snprintf(addr, addrlen, format, host, serv);
  88. addr[addrlen] = '\0';
  89. return addr;
  90. }
  91. char *vnc_socket_local_addr(const char *format, int fd) {
  92. struct sockaddr_storage sa;
  93. socklen_t salen;
  94. salen = sizeof(sa);
  95. if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
  96. return NULL;
  97. return addr_to_string(format, &sa, salen);
  98. }
  99. char *vnc_socket_remote_addr(const char *format, int fd) {
  100. struct sockaddr_storage sa;
  101. socklen_t salen;
  102. salen = sizeof(sa);
  103. if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
  104. return NULL;
  105. return addr_to_string(format, &sa, salen);
  106. }
  107. static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa,
  108. socklen_t salen)
  109. {
  110. char host[NI_MAXHOST];
  111. char serv[NI_MAXSERV];
  112. int err;
  113. if ((err = getnameinfo((struct sockaddr *)sa, salen,
  114. host, sizeof(host),
  115. serv, sizeof(serv),
  116. NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
  117. VNC_DEBUG("Cannot resolve address %d: %s\n",
  118. err, gai_strerror(err));
  119. return -1;
  120. }
  121. qdict_put(qdict, "host", qstring_from_str(host));
  122. qdict_put(qdict, "service", qstring_from_str(serv));
  123. qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family)));
  124. return 0;
  125. }
  126. static int vnc_server_addr_put(QDict *qdict, int fd)
  127. {
  128. struct sockaddr_storage sa;
  129. socklen_t salen;
  130. salen = sizeof(sa);
  131. if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
  132. return -1;
  133. }
  134. return put_addr_qdict(qdict, &sa, salen);
  135. }
  136. static int vnc_qdict_remote_addr(QDict *qdict, int fd)
  137. {
  138. struct sockaddr_storage sa;
  139. socklen_t salen;
  140. salen = sizeof(sa);
  141. if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
  142. return -1;
  143. }
  144. return put_addr_qdict(qdict, &sa, salen);
  145. }
  146. static const char *vnc_auth_name(VncDisplay *vd) {
  147. switch (vd->auth) {
  148. case VNC_AUTH_INVALID:
  149. return "invalid";
  150. case VNC_AUTH_NONE:
  151. return "none";
  152. case VNC_AUTH_VNC:
  153. return "vnc";
  154. case VNC_AUTH_RA2:
  155. return "ra2";
  156. case VNC_AUTH_RA2NE:
  157. return "ra2ne";
  158. case VNC_AUTH_TIGHT:
  159. return "tight";
  160. case VNC_AUTH_ULTRA:
  161. return "ultra";
  162. case VNC_AUTH_TLS:
  163. return "tls";
  164. case VNC_AUTH_VENCRYPT:
  165. #ifdef CONFIG_VNC_TLS
  166. switch (vd->subauth) {
  167. case VNC_AUTH_VENCRYPT_PLAIN:
  168. return "vencrypt+plain";
  169. case VNC_AUTH_VENCRYPT_TLSNONE:
  170. return "vencrypt+tls+none";
  171. case VNC_AUTH_VENCRYPT_TLSVNC:
  172. return "vencrypt+tls+vnc";
  173. case VNC_AUTH_VENCRYPT_TLSPLAIN:
  174. return "vencrypt+tls+plain";
  175. case VNC_AUTH_VENCRYPT_X509NONE:
  176. return "vencrypt+x509+none";
  177. case VNC_AUTH_VENCRYPT_X509VNC:
  178. return "vencrypt+x509+vnc";
  179. case VNC_AUTH_VENCRYPT_X509PLAIN:
  180. return "vencrypt+x509+plain";
  181. case VNC_AUTH_VENCRYPT_TLSSASL:
  182. return "vencrypt+tls+sasl";
  183. case VNC_AUTH_VENCRYPT_X509SASL:
  184. return "vencrypt+x509+sasl";
  185. default:
  186. return "vencrypt";
  187. }
  188. #else
  189. return "vencrypt";
  190. #endif
  191. case VNC_AUTH_SASL:
  192. return "sasl";
  193. }
  194. return "unknown";
  195. }
  196. static int vnc_server_info_put(QDict *qdict)
  197. {
  198. if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) {
  199. return -1;
  200. }
  201. qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display)));
  202. return 0;
  203. }
  204. static void vnc_client_cache_auth(VncState *client)
  205. {
  206. #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
  207. QDict *qdict;
  208. #endif
  209. if (!client->info) {
  210. return;
  211. }
  212. #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
  213. qdict = qobject_to_qdict(client->info);
  214. #endif
  215. #ifdef CONFIG_VNC_TLS
  216. if (client->tls.session &&
  217. client->tls.dname) {
  218. qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname));
  219. }
  220. #endif
  221. #ifdef CONFIG_VNC_SASL
  222. if (client->sasl.conn &&
  223. client->sasl.username) {
  224. qdict_put(qdict, "sasl_username",
  225. qstring_from_str(client->sasl.username));
  226. }
  227. #endif
  228. }
  229. static void vnc_client_cache_addr(VncState *client)
  230. {
  231. QDict *qdict;
  232. qdict = qdict_new();
  233. if (vnc_qdict_remote_addr(qdict, client->csock) < 0) {
  234. QDECREF(qdict);
  235. /* XXX: how to report the error? */
  236. return;
  237. }
  238. client->info = QOBJECT(qdict);
  239. }
  240. static void vnc_qmp_event(VncState *vs, MonitorEvent event)
  241. {
  242. QDict *server;
  243. QObject *data;
  244. if (!vs->info) {
  245. return;
  246. }
  247. server = qdict_new();
  248. if (vnc_server_info_put(server) < 0) {
  249. QDECREF(server);
  250. return;
  251. }
  252. data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
  253. vs->info, QOBJECT(server));
  254. monitor_protocol_event(event, data);
  255. qobject_incref(vs->info);
  256. qobject_decref(data);
  257. }
  258. static VncClientInfo *qmp_query_vnc_client(const VncState *client)
  259. {
  260. struct sockaddr_storage sa;
  261. socklen_t salen = sizeof(sa);
  262. char host[NI_MAXHOST];
  263. char serv[NI_MAXSERV];
  264. VncClientInfo *info;
  265. if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
  266. return NULL;
  267. }
  268. if (getnameinfo((struct sockaddr *)&sa, salen,
  269. host, sizeof(host),
  270. serv, sizeof(serv),
  271. NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
  272. return NULL;
  273. }
  274. info = g_malloc0(sizeof(*info));
  275. info->host = g_strdup(host);
  276. info->service = g_strdup(serv);
  277. info->family = g_strdup(inet_strfamily(sa.ss_family));
  278. #ifdef CONFIG_VNC_TLS
  279. if (client->tls.session && client->tls.dname) {
  280. info->has_x509_dname = true;
  281. info->x509_dname = g_strdup(client->tls.dname);
  282. }
  283. #endif
  284. #ifdef CONFIG_VNC_SASL
  285. if (client->sasl.conn && client->sasl.username) {
  286. info->has_sasl_username = true;
  287. info->sasl_username = g_strdup(client->sasl.username);
  288. }
  289. #endif
  290. return info;
  291. }
  292. VncInfo *qmp_query_vnc(Error **errp)
  293. {
  294. VncInfo *info = g_malloc0(sizeof(*info));
  295. if (vnc_display == NULL || vnc_display->display == NULL) {
  296. info->enabled = false;
  297. } else {
  298. VncClientInfoList *cur_item = NULL;
  299. struct sockaddr_storage sa;
  300. socklen_t salen = sizeof(sa);
  301. char host[NI_MAXHOST];
  302. char serv[NI_MAXSERV];
  303. VncState *client;
  304. info->enabled = true;
  305. /* for compatibility with the original command */
  306. info->has_clients = true;
  307. QTAILQ_FOREACH(client, &vnc_display->clients, next) {
  308. VncClientInfoList *cinfo = g_malloc0(sizeof(*info));
  309. cinfo->value = qmp_query_vnc_client(client);
  310. /* XXX: waiting for the qapi to support GSList */
  311. if (!cur_item) {
  312. info->clients = cur_item = cinfo;
  313. } else {
  314. cur_item->next = cinfo;
  315. cur_item = cinfo;
  316. }
  317. }
  318. if (vnc_display->lsock == -1) {
  319. return info;
  320. }
  321. if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa,
  322. &salen) == -1) {
  323. error_set(errp, QERR_UNDEFINED_ERROR);
  324. goto out_error;
  325. }
  326. if (getnameinfo((struct sockaddr *)&sa, salen,
  327. host, sizeof(host),
  328. serv, sizeof(serv),
  329. NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
  330. error_set(errp, QERR_UNDEFINED_ERROR);
  331. goto out_error;
  332. }
  333. info->has_host = true;
  334. info->host = g_strdup(host);
  335. info->has_service = true;
  336. info->service = g_strdup(serv);
  337. info->has_family = true;
  338. info->family = g_strdup(inet_strfamily(sa.ss_family));
  339. info->has_auth = true;
  340. info->auth = g_strdup(vnc_auth_name(vnc_display));
  341. }
  342. return info;
  343. out_error:
  344. qapi_free_VncInfo(info);
  345. return NULL;
  346. }
  347. /* TODO
  348. 1) Get the queue working for IO.
  349. 2) there is some weirdness when using the -S option (the screen is grey
  350. and not totally invalidated
  351. 3) resolutions > 1024
  352. */
  353. static int vnc_update_client(VncState *vs, int has_dirty);
  354. static int vnc_update_client_sync(VncState *vs, int has_dirty);
  355. static void vnc_disconnect_start(VncState *vs);
  356. static void vnc_init_timer(VncDisplay *vd);
  357. static void vnc_remove_timer(VncDisplay *vd);
  358. static void vnc_colordepth(VncState *vs);
  359. static void framebuffer_update_request(VncState *vs, int incremental,
  360. int x_position, int y_position,
  361. int w, int h);
  362. static void vnc_refresh(void *opaque);
  363. static int vnc_refresh_server_surface(VncDisplay *vd);
  364. static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
  365. {
  366. int i;
  367. VncDisplay *vd = ds->opaque;
  368. struct VncSurface *s = &vd->guest;
  369. int width = ds_get_width(ds);
  370. int height = ds_get_height(ds);
  371. h += y;
  372. /* round x down to ensure the loop only spans one 16-pixel block per,
  373. iteration. otherwise, if (x % 16) != 0, the last iteration may span
  374. two 16-pixel blocks but we only mark the first as dirty
  375. */
  376. w += (x % 16);
  377. x -= (x % 16);
  378. x = MIN(x, width);
  379. y = MIN(y, height);
  380. w = MIN(x + w, width) - x;
  381. h = MIN(h, height);
  382. for (; y < h; y++)
  383. for (i = 0; i < w; i += 16)
  384. set_bit((x + i) / 16, s->dirty[y]);
  385. }
  386. void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
  387. int32_t encoding)
  388. {
  389. vnc_write_u16(vs, x);
  390. vnc_write_u16(vs, y);
  391. vnc_write_u16(vs, w);
  392. vnc_write_u16(vs, h);
  393. vnc_write_s32(vs, encoding);
  394. }
  395. void buffer_reserve(Buffer *buffer, size_t len)
  396. {
  397. if ((buffer->capacity - buffer->offset) < len) {
  398. buffer->capacity += (len + 1024);
  399. buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
  400. if (buffer->buffer == NULL) {
  401. fprintf(stderr, "vnc: out of memory\n");
  402. exit(1);
  403. }
  404. }
  405. }
  406. static int buffer_empty(Buffer *buffer)
  407. {
  408. return buffer->offset == 0;
  409. }
  410. uint8_t *buffer_end(Buffer *buffer)
  411. {
  412. return buffer->buffer + buffer->offset;
  413. }
  414. void buffer_reset(Buffer *buffer)
  415. {
  416. buffer->offset = 0;
  417. }
  418. void buffer_free(Buffer *buffer)
  419. {
  420. g_free(buffer->buffer);
  421. buffer->offset = 0;
  422. buffer->capacity = 0;
  423. buffer->buffer = NULL;
  424. }
  425. void buffer_append(Buffer *buffer, const void *data, size_t len)
  426. {
  427. memcpy(buffer->buffer + buffer->offset, data, len);
  428. buffer->offset += len;
  429. }
  430. void buffer_advance(Buffer *buf, size_t len)
  431. {
  432. memmove(buf->buffer, buf->buffer + len,
  433. (buf->offset - len));
  434. buf->offset -= len;
  435. }
  436. static void vnc_desktop_resize(VncState *vs)
  437. {
  438. DisplayState *ds = vs->ds;
  439. if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
  440. return;
  441. }
  442. if (vs->client_width == ds_get_width(ds) &&
  443. vs->client_height == ds_get_height(ds)) {
  444. return;
  445. }
  446. vs->client_width = ds_get_width(ds);
  447. vs->client_height = ds_get_height(ds);
  448. vnc_lock_output(vs);
  449. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  450. vnc_write_u8(vs, 0);
  451. vnc_write_u16(vs, 1); /* number of rects */
  452. vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
  453. VNC_ENCODING_DESKTOPRESIZE);
  454. vnc_unlock_output(vs);
  455. vnc_flush(vs);
  456. }
  457. static void vnc_abort_display_jobs(VncDisplay *vd)
  458. {
  459. VncState *vs;
  460. QTAILQ_FOREACH(vs, &vd->clients, next) {
  461. vnc_lock_output(vs);
  462. vs->abort = true;
  463. vnc_unlock_output(vs);
  464. }
  465. QTAILQ_FOREACH(vs, &vd->clients, next) {
  466. vnc_jobs_join(vs);
  467. }
  468. QTAILQ_FOREACH(vs, &vd->clients, next) {
  469. vnc_lock_output(vs);
  470. vs->abort = false;
  471. vnc_unlock_output(vs);
  472. }
  473. }
  474. int vnc_server_fb_stride(VncDisplay *vd)
  475. {
  476. return pixman_image_get_stride(vd->server);
  477. }
  478. void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
  479. {
  480. uint8_t *ptr;
  481. ptr = (uint8_t *)pixman_image_get_data(vd->server);
  482. ptr += y * vnc_server_fb_stride(vd);
  483. ptr += x * VNC_SERVER_FB_BYTES;
  484. return ptr;
  485. }
  486. static void vnc_dpy_resize(DisplayState *ds)
  487. {
  488. VncDisplay *vd = ds->opaque;
  489. VncState *vs;
  490. vnc_abort_display_jobs(vd);
  491. /* server surface */
  492. qemu_pixman_image_unref(vd->server);
  493. vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
  494. ds_get_width(ds),
  495. ds_get_height(ds),
  496. NULL, 0);
  497. /* guest surface */
  498. #if 0 /* FIXME */
  499. if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
  500. console_color_init(ds);
  501. #endif
  502. qemu_pixman_image_unref(vd->guest.fb);
  503. vd->guest.fb = pixman_image_ref(ds->surface->image);
  504. vd->guest.format = ds->surface->format;
  505. memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty));
  506. QTAILQ_FOREACH(vs, &vd->clients, next) {
  507. vnc_colordepth(vs);
  508. vnc_desktop_resize(vs);
  509. if (vs->vd->cursor) {
  510. vnc_cursor_define(vs);
  511. }
  512. memset(vs->dirty, 0xFF, sizeof(vs->dirty));
  513. }
  514. }
  515. /* fastest code */
  516. static void vnc_write_pixels_copy(VncState *vs,
  517. void *pixels, int size)
  518. {
  519. vnc_write(vs, pixels, size);
  520. }
  521. /* slowest but generic code. */
  522. void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
  523. {
  524. uint8_t r, g, b;
  525. #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
  526. r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
  527. g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8;
  528. b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8;
  529. #else
  530. # error need some bits here if you change VNC_SERVER_FB_FORMAT
  531. #endif
  532. v = (r << vs->client_pf.rshift) |
  533. (g << vs->client_pf.gshift) |
  534. (b << vs->client_pf.bshift);
  535. switch (vs->client_pf.bytes_per_pixel) {
  536. case 1:
  537. buf[0] = v;
  538. break;
  539. case 2:
  540. if (vs->client_be) {
  541. buf[0] = v >> 8;
  542. buf[1] = v;
  543. } else {
  544. buf[1] = v >> 8;
  545. buf[0] = v;
  546. }
  547. break;
  548. default:
  549. case 4:
  550. if (vs->client_be) {
  551. buf[0] = v >> 24;
  552. buf[1] = v >> 16;
  553. buf[2] = v >> 8;
  554. buf[3] = v;
  555. } else {
  556. buf[3] = v >> 24;
  557. buf[2] = v >> 16;
  558. buf[1] = v >> 8;
  559. buf[0] = v;
  560. }
  561. break;
  562. }
  563. }
  564. static void vnc_write_pixels_generic(VncState *vs,
  565. void *pixels1, int size)
  566. {
  567. uint8_t buf[4];
  568. if (VNC_SERVER_FB_BYTES == 4) {
  569. uint32_t *pixels = pixels1;
  570. int n, i;
  571. n = size >> 2;
  572. for (i = 0; i < n; i++) {
  573. vnc_convert_pixel(vs, buf, pixels[i]);
  574. vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
  575. }
  576. }
  577. }
  578. int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  579. {
  580. int i;
  581. uint8_t *row;
  582. VncDisplay *vd = vs->vd;
  583. row = vnc_server_fb_ptr(vd, x, y);
  584. for (i = 0; i < h; i++) {
  585. vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
  586. row += vnc_server_fb_stride(vd);
  587. }
  588. return 1;
  589. }
  590. int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  591. {
  592. int n = 0;
  593. switch(vs->vnc_encoding) {
  594. case VNC_ENCODING_ZLIB:
  595. n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
  596. break;
  597. case VNC_ENCODING_HEXTILE:
  598. vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
  599. n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
  600. break;
  601. case VNC_ENCODING_TIGHT:
  602. n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
  603. break;
  604. case VNC_ENCODING_TIGHT_PNG:
  605. n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
  606. break;
  607. case VNC_ENCODING_ZRLE:
  608. n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
  609. break;
  610. case VNC_ENCODING_ZYWRLE:
  611. n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
  612. break;
  613. default:
  614. vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
  615. n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
  616. break;
  617. }
  618. return n;
  619. }
  620. static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
  621. {
  622. /* send bitblit op to the vnc client */
  623. vnc_lock_output(vs);
  624. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  625. vnc_write_u8(vs, 0);
  626. vnc_write_u16(vs, 1); /* number of rects */
  627. vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
  628. vnc_write_u16(vs, src_x);
  629. vnc_write_u16(vs, src_y);
  630. vnc_unlock_output(vs);
  631. vnc_flush(vs);
  632. }
  633. static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
  634. {
  635. VncDisplay *vd = ds->opaque;
  636. VncState *vs, *vn;
  637. uint8_t *src_row;
  638. uint8_t *dst_row;
  639. int i, x, y, pitch, inc, w_lim, s;
  640. int cmp_bytes;
  641. vnc_refresh_server_surface(vd);
  642. QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
  643. if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
  644. vs->force_update = 1;
  645. vnc_update_client_sync(vs, 1);
  646. /* vs might be free()ed here */
  647. }
  648. }
  649. /* do bitblit op on the local surface too */
  650. pitch = vnc_server_fb_stride(vd);
  651. src_row = vnc_server_fb_ptr(vd, src_x, src_y);
  652. dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
  653. y = dst_y;
  654. inc = 1;
  655. if (dst_y > src_y) {
  656. /* copy backwards */
  657. src_row += pitch * (h-1);
  658. dst_row += pitch * (h-1);
  659. pitch = -pitch;
  660. y = dst_y + h - 1;
  661. inc = -1;
  662. }
  663. w_lim = w - (16 - (dst_x % 16));
  664. if (w_lim < 0)
  665. w_lim = w;
  666. else
  667. w_lim = w - (w_lim % 16);
  668. for (i = 0; i < h; i++) {
  669. for (x = 0; x <= w_lim;
  670. x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
  671. if (x == w_lim) {
  672. if ((s = w - w_lim) == 0)
  673. break;
  674. } else if (!x) {
  675. s = (16 - (dst_x % 16));
  676. s = MIN(s, w_lim);
  677. } else {
  678. s = 16;
  679. }
  680. cmp_bytes = s * VNC_SERVER_FB_BYTES;
  681. if (memcmp(src_row, dst_row, cmp_bytes) == 0)
  682. continue;
  683. memmove(dst_row, src_row, cmp_bytes);
  684. QTAILQ_FOREACH(vs, &vd->clients, next) {
  685. if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
  686. set_bit(((x + dst_x) / 16), vs->dirty[y]);
  687. }
  688. }
  689. }
  690. src_row += pitch - w * VNC_SERVER_FB_BYTES;
  691. dst_row += pitch - w * VNC_SERVER_FB_BYTES;
  692. y += inc;
  693. }
  694. QTAILQ_FOREACH(vs, &vd->clients, next) {
  695. if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
  696. vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
  697. }
  698. }
  699. }
  700. static void vnc_mouse_set(DisplayState *ds, int x, int y, int visible)
  701. {
  702. /* can we ask the client(s) to move the pointer ??? */
  703. }
  704. static int vnc_cursor_define(VncState *vs)
  705. {
  706. QEMUCursor *c = vs->vd->cursor;
  707. int isize;
  708. if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
  709. vnc_lock_output(vs);
  710. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  711. vnc_write_u8(vs, 0); /* padding */
  712. vnc_write_u16(vs, 1); /* # of rects */
  713. vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
  714. VNC_ENCODING_RICH_CURSOR);
  715. isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
  716. vnc_write_pixels_generic(vs, c->data, isize);
  717. vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
  718. vnc_unlock_output(vs);
  719. return 0;
  720. }
  721. return -1;
  722. }
  723. static void vnc_dpy_cursor_define(DisplayState *ds, QEMUCursor *c)
  724. {
  725. VncDisplay *vd = vnc_display;
  726. VncState *vs;
  727. cursor_put(vd->cursor);
  728. g_free(vd->cursor_mask);
  729. vd->cursor = c;
  730. cursor_get(vd->cursor);
  731. vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
  732. vd->cursor_mask = g_malloc0(vd->cursor_msize);
  733. cursor_get_mono_mask(c, 0, vd->cursor_mask);
  734. QTAILQ_FOREACH(vs, &vd->clients, next) {
  735. vnc_cursor_define(vs);
  736. }
  737. }
  738. static int find_and_clear_dirty_height(struct VncState *vs,
  739. int y, int last_x, int x, int height)
  740. {
  741. int h;
  742. for (h = 1; h < (height - y); h++) {
  743. int tmp_x;
  744. if (!test_bit(last_x, vs->dirty[y + h])) {
  745. break;
  746. }
  747. for (tmp_x = last_x; tmp_x < x; tmp_x++) {
  748. clear_bit(tmp_x, vs->dirty[y + h]);
  749. }
  750. }
  751. return h;
  752. }
  753. static int vnc_update_client_sync(VncState *vs, int has_dirty)
  754. {
  755. int ret = vnc_update_client(vs, has_dirty);
  756. vnc_jobs_join(vs);
  757. return ret;
  758. }
  759. static int vnc_update_client(VncState *vs, int has_dirty)
  760. {
  761. if (vs->need_update && vs->csock != -1) {
  762. VncDisplay *vd = vs->vd;
  763. VncJob *job;
  764. int y;
  765. int width, height;
  766. int n = 0;
  767. if (vs->output.offset && !vs->audio_cap && !vs->force_update)
  768. /* kernel send buffers are full -> drop frames to throttle */
  769. return 0;
  770. if (!has_dirty && !vs->audio_cap && !vs->force_update)
  771. return 0;
  772. /*
  773. * Send screen updates to the vnc client using the server
  774. * surface and server dirty map. guest surface updates
  775. * happening in parallel don't disturb us, the next pass will
  776. * send them to the client.
  777. */
  778. job = vnc_job_new(vs);
  779. width = MIN(pixman_image_get_width(vd->server), vs->client_width);
  780. height = MIN(pixman_image_get_height(vd->server), vs->client_height);
  781. for (y = 0; y < height; y++) {
  782. int x;
  783. int last_x = -1;
  784. for (x = 0; x < width / 16; x++) {
  785. if (test_and_clear_bit(x, vs->dirty[y])) {
  786. if (last_x == -1) {
  787. last_x = x;
  788. }
  789. } else {
  790. if (last_x != -1) {
  791. int h = find_and_clear_dirty_height(vs, y, last_x, x,
  792. height);
  793. n += vnc_job_add_rect(job, last_x * 16, y,
  794. (x - last_x) * 16, h);
  795. }
  796. last_x = -1;
  797. }
  798. }
  799. if (last_x != -1) {
  800. int h = find_and_clear_dirty_height(vs, y, last_x, x, height);
  801. n += vnc_job_add_rect(job, last_x * 16, y,
  802. (x - last_x) * 16, h);
  803. }
  804. }
  805. vnc_job_push(job);
  806. vs->force_update = 0;
  807. return n;
  808. }
  809. if (vs->csock == -1)
  810. vnc_disconnect_finish(vs);
  811. return 0;
  812. }
  813. /* audio */
  814. static void audio_capture_notify(void *opaque, audcnotification_e cmd)
  815. {
  816. VncState *vs = opaque;
  817. switch (cmd) {
  818. case AUD_CNOTIFY_DISABLE:
  819. vnc_lock_output(vs);
  820. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
  821. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
  822. vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
  823. vnc_unlock_output(vs);
  824. vnc_flush(vs);
  825. break;
  826. case AUD_CNOTIFY_ENABLE:
  827. vnc_lock_output(vs);
  828. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
  829. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
  830. vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
  831. vnc_unlock_output(vs);
  832. vnc_flush(vs);
  833. break;
  834. }
  835. }
  836. static void audio_capture_destroy(void *opaque)
  837. {
  838. }
  839. static void audio_capture(void *opaque, void *buf, int size)
  840. {
  841. VncState *vs = opaque;
  842. vnc_lock_output(vs);
  843. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
  844. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
  845. vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
  846. vnc_write_u32(vs, size);
  847. vnc_write(vs, buf, size);
  848. vnc_unlock_output(vs);
  849. vnc_flush(vs);
  850. }
  851. static void audio_add(VncState *vs)
  852. {
  853. struct audio_capture_ops ops;
  854. if (vs->audio_cap) {
  855. monitor_printf(default_mon, "audio already running\n");
  856. return;
  857. }
  858. ops.notify = audio_capture_notify;
  859. ops.destroy = audio_capture_destroy;
  860. ops.capture = audio_capture;
  861. vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
  862. if (!vs->audio_cap) {
  863. monitor_printf(default_mon, "Failed to add audio capture\n");
  864. }
  865. }
  866. static void audio_del(VncState *vs)
  867. {
  868. if (vs->audio_cap) {
  869. AUD_del_capture(vs->audio_cap, vs);
  870. vs->audio_cap = NULL;
  871. }
  872. }
  873. static void vnc_disconnect_start(VncState *vs)
  874. {
  875. if (vs->csock == -1)
  876. return;
  877. vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
  878. qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
  879. closesocket(vs->csock);
  880. vs->csock = -1;
  881. }
  882. void vnc_disconnect_finish(VncState *vs)
  883. {
  884. int i;
  885. vnc_jobs_join(vs); /* Wait encoding jobs */
  886. vnc_lock_output(vs);
  887. vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
  888. buffer_free(&vs->input);
  889. buffer_free(&vs->output);
  890. #ifdef CONFIG_VNC_WS
  891. buffer_free(&vs->ws_input);
  892. buffer_free(&vs->ws_output);
  893. #endif /* CONFIG_VNC_WS */
  894. qobject_decref(vs->info);
  895. vnc_zlib_clear(vs);
  896. vnc_tight_clear(vs);
  897. vnc_zrle_clear(vs);
  898. #ifdef CONFIG_VNC_TLS
  899. vnc_tls_client_cleanup(vs);
  900. #endif /* CONFIG_VNC_TLS */
  901. #ifdef CONFIG_VNC_SASL
  902. vnc_sasl_client_cleanup(vs);
  903. #endif /* CONFIG_VNC_SASL */
  904. audio_del(vs);
  905. vnc_release_modifiers(vs);
  906. if (vs->initialized) {
  907. QTAILQ_REMOVE(&vs->vd->clients, vs, next);
  908. qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
  909. }
  910. if (QTAILQ_EMPTY(&vs->vd->clients)) {
  911. dcl->idle = 1;
  912. }
  913. vnc_remove_timer(vs->vd);
  914. if (vs->vd->lock_key_sync)
  915. qemu_remove_led_event_handler(vs->led);
  916. vnc_unlock_output(vs);
  917. qemu_mutex_destroy(&vs->output_mutex);
  918. if (vs->bh != NULL) {
  919. qemu_bh_delete(vs->bh);
  920. }
  921. buffer_free(&vs->jobs_buffer);
  922. for (i = 0; i < VNC_STAT_ROWS; ++i) {
  923. g_free(vs->lossy_rect[i]);
  924. }
  925. g_free(vs->lossy_rect);
  926. g_free(vs);
  927. }
  928. int vnc_client_io_error(VncState *vs, int ret, int last_errno)
  929. {
  930. if (ret == 0 || ret == -1) {
  931. if (ret == -1) {
  932. switch (last_errno) {
  933. case EINTR:
  934. case EAGAIN:
  935. #ifdef _WIN32
  936. case WSAEWOULDBLOCK:
  937. #endif
  938. return 0;
  939. default:
  940. break;
  941. }
  942. }
  943. VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
  944. ret, ret < 0 ? last_errno : 0);
  945. vnc_disconnect_start(vs);
  946. return 0;
  947. }
  948. return ret;
  949. }
  950. void vnc_client_error(VncState *vs)
  951. {
  952. VNC_DEBUG("Closing down client sock: protocol error\n");
  953. vnc_disconnect_start(vs);
  954. }
  955. /*
  956. * Called to write a chunk of data to the client socket. The data may
  957. * be the raw data, or may have already been encoded by SASL.
  958. * The data will be written either straight onto the socket, or
  959. * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
  960. *
  961. * NB, it is theoretically possible to have 2 layers of encryption,
  962. * both SASL, and this TLS layer. It is highly unlikely in practice
  963. * though, since SASL encryption will typically be a no-op if TLS
  964. * is active
  965. *
  966. * Returns the number of bytes written, which may be less than
  967. * the requested 'datalen' if the socket would block. Returns
  968. * -1 on error, and disconnects the client socket.
  969. */
  970. long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
  971. {
  972. long ret;
  973. #ifdef CONFIG_VNC_TLS
  974. if (vs->tls.session) {
  975. ret = gnutls_write(vs->tls.session, data, datalen);
  976. if (ret < 0) {
  977. if (ret == GNUTLS_E_AGAIN)
  978. errno = EAGAIN;
  979. else
  980. errno = EIO;
  981. ret = -1;
  982. }
  983. } else
  984. #endif /* CONFIG_VNC_TLS */
  985. ret = send(vs->csock, (const void *)data, datalen, 0);
  986. VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
  987. return vnc_client_io_error(vs, ret, socket_error());
  988. }
  989. /*
  990. * Called to write buffered data to the client socket, when not
  991. * using any SASL SSF encryption layers. Will write as much data
  992. * as possible without blocking. If all buffered data is written,
  993. * will switch the FD poll() handler back to read monitoring.
  994. *
  995. * Returns the number of bytes written, which may be less than
  996. * the buffered output data if the socket would block. Returns
  997. * -1 on error, and disconnects the client socket.
  998. */
  999. static long vnc_client_write_plain(VncState *vs)
  1000. {
  1001. long ret;
  1002. #ifdef CONFIG_VNC_SASL
  1003. VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
  1004. vs->output.buffer, vs->output.capacity, vs->output.offset,
  1005. vs->sasl.waitWriteSSF);
  1006. if (vs->sasl.conn &&
  1007. vs->sasl.runSSF &&
  1008. vs->sasl.waitWriteSSF) {
  1009. ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
  1010. if (ret)
  1011. vs->sasl.waitWriteSSF -= ret;
  1012. } else
  1013. #endif /* CONFIG_VNC_SASL */
  1014. ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
  1015. if (!ret)
  1016. return 0;
  1017. buffer_advance(&vs->output, ret);
  1018. if (vs->output.offset == 0) {
  1019. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
  1020. }
  1021. return ret;
  1022. }
  1023. /*
  1024. * First function called whenever there is data to be written to
  1025. * the client socket. Will delegate actual work according to whether
  1026. * SASL SSF layers are enabled (thus requiring encryption calls)
  1027. */
  1028. static void vnc_client_write_locked(void *opaque)
  1029. {
  1030. VncState *vs = opaque;
  1031. #ifdef CONFIG_VNC_SASL
  1032. if (vs->sasl.conn &&
  1033. vs->sasl.runSSF &&
  1034. !vs->sasl.waitWriteSSF) {
  1035. vnc_client_write_sasl(vs);
  1036. } else
  1037. #endif /* CONFIG_VNC_SASL */
  1038. {
  1039. #ifdef CONFIG_VNC_WS
  1040. if (vs->encode_ws) {
  1041. vnc_client_write_ws(vs);
  1042. } else
  1043. #endif /* CONFIG_VNC_WS */
  1044. {
  1045. vnc_client_write_plain(vs);
  1046. }
  1047. }
  1048. }
  1049. void vnc_client_write(void *opaque)
  1050. {
  1051. VncState *vs = opaque;
  1052. vnc_lock_output(vs);
  1053. if (vs->output.offset
  1054. #ifdef CONFIG_VNC_WS
  1055. || vs->ws_output.offset
  1056. #endif
  1057. ) {
  1058. vnc_client_write_locked(opaque);
  1059. } else if (vs->csock != -1) {
  1060. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
  1061. }
  1062. vnc_unlock_output(vs);
  1063. }
  1064. void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
  1065. {
  1066. vs->read_handler = func;
  1067. vs->read_handler_expect = expecting;
  1068. }
  1069. /*
  1070. * Called to read a chunk of data from the client socket. The data may
  1071. * be the raw data, or may need to be further decoded by SASL.
  1072. * The data will be read either straight from to the socket, or
  1073. * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
  1074. *
  1075. * NB, it is theoretically possible to have 2 layers of encryption,
  1076. * both SASL, and this TLS layer. It is highly unlikely in practice
  1077. * though, since SASL encryption will typically be a no-op if TLS
  1078. * is active
  1079. *
  1080. * Returns the number of bytes read, which may be less than
  1081. * the requested 'datalen' if the socket would block. Returns
  1082. * -1 on error, and disconnects the client socket.
  1083. */
  1084. long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
  1085. {
  1086. long ret;
  1087. #ifdef CONFIG_VNC_TLS
  1088. if (vs->tls.session) {
  1089. ret = gnutls_read(vs->tls.session, data, datalen);
  1090. if (ret < 0) {
  1091. if (ret == GNUTLS_E_AGAIN)
  1092. errno = EAGAIN;
  1093. else
  1094. errno = EIO;
  1095. ret = -1;
  1096. }
  1097. } else
  1098. #endif /* CONFIG_VNC_TLS */
  1099. ret = qemu_recv(vs->csock, data, datalen, 0);
  1100. VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
  1101. return vnc_client_io_error(vs, ret, socket_error());
  1102. }
  1103. /*
  1104. * Called to read data from the client socket to the input buffer,
  1105. * when not using any SASL SSF encryption layers. Will read as much
  1106. * data as possible without blocking.
  1107. *
  1108. * Returns the number of bytes read. Returns -1 on error, and
  1109. * disconnects the client socket.
  1110. */
  1111. static long vnc_client_read_plain(VncState *vs)
  1112. {
  1113. int ret;
  1114. VNC_DEBUG("Read plain %p size %zd offset %zd\n",
  1115. vs->input.buffer, vs->input.capacity, vs->input.offset);
  1116. buffer_reserve(&vs->input, 4096);
  1117. ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
  1118. if (!ret)
  1119. return 0;
  1120. vs->input.offset += ret;
  1121. return ret;
  1122. }
  1123. static void vnc_jobs_bh(void *opaque)
  1124. {
  1125. VncState *vs = opaque;
  1126. vnc_jobs_consume_buffer(vs);
  1127. }
  1128. /*
  1129. * First function called whenever there is more data to be read from
  1130. * the client socket. Will delegate actual work according to whether
  1131. * SASL SSF layers are enabled (thus requiring decryption calls)
  1132. */
  1133. void vnc_client_read(void *opaque)
  1134. {
  1135. VncState *vs = opaque;
  1136. long ret;
  1137. #ifdef CONFIG_VNC_SASL
  1138. if (vs->sasl.conn && vs->sasl.runSSF)
  1139. ret = vnc_client_read_sasl(vs);
  1140. else
  1141. #endif /* CONFIG_VNC_SASL */
  1142. #ifdef CONFIG_VNC_WS
  1143. if (vs->encode_ws) {
  1144. ret = vnc_client_read_ws(vs);
  1145. if (ret == -1) {
  1146. vnc_disconnect_start(vs);
  1147. return;
  1148. } else if (ret == -2) {
  1149. vnc_client_error(vs);
  1150. return;
  1151. }
  1152. } else
  1153. #endif /* CONFIG_VNC_WS */
  1154. {
  1155. ret = vnc_client_read_plain(vs);
  1156. }
  1157. if (!ret) {
  1158. if (vs->csock == -1)
  1159. vnc_disconnect_finish(vs);
  1160. return;
  1161. }
  1162. while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
  1163. size_t len = vs->read_handler_expect;
  1164. int ret;
  1165. ret = vs->read_handler(vs, vs->input.buffer, len);
  1166. if (vs->csock == -1) {
  1167. vnc_disconnect_finish(vs);
  1168. return;
  1169. }
  1170. if (!ret) {
  1171. buffer_advance(&vs->input, len);
  1172. } else {
  1173. vs->read_handler_expect = ret;
  1174. }
  1175. }
  1176. }
  1177. void vnc_write(VncState *vs, const void *data, size_t len)
  1178. {
  1179. buffer_reserve(&vs->output, len);
  1180. if (vs->csock != -1 && buffer_empty(&vs->output)) {
  1181. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
  1182. }
  1183. buffer_append(&vs->output, data, len);
  1184. }
  1185. void vnc_write_s32(VncState *vs, int32_t value)
  1186. {
  1187. vnc_write_u32(vs, *(uint32_t *)&value);
  1188. }
  1189. void vnc_write_u32(VncState *vs, uint32_t value)
  1190. {
  1191. uint8_t buf[4];
  1192. buf[0] = (value >> 24) & 0xFF;
  1193. buf[1] = (value >> 16) & 0xFF;
  1194. buf[2] = (value >> 8) & 0xFF;
  1195. buf[3] = value & 0xFF;
  1196. vnc_write(vs, buf, 4);
  1197. }
  1198. void vnc_write_u16(VncState *vs, uint16_t value)
  1199. {
  1200. uint8_t buf[2];
  1201. buf[0] = (value >> 8) & 0xFF;
  1202. buf[1] = value & 0xFF;
  1203. vnc_write(vs, buf, 2);
  1204. }
  1205. void vnc_write_u8(VncState *vs, uint8_t value)
  1206. {
  1207. vnc_write(vs, (char *)&value, 1);
  1208. }
  1209. void vnc_flush(VncState *vs)
  1210. {
  1211. vnc_lock_output(vs);
  1212. if (vs->csock != -1 && (vs->output.offset
  1213. #ifdef CONFIG_VNC_WS
  1214. || vs->ws_output.offset
  1215. #endif
  1216. )) {
  1217. vnc_client_write_locked(vs);
  1218. }
  1219. vnc_unlock_output(vs);
  1220. }
  1221. static uint8_t read_u8(uint8_t *data, size_t offset)
  1222. {
  1223. return data[offset];
  1224. }
  1225. static uint16_t read_u16(uint8_t *data, size_t offset)
  1226. {
  1227. return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
  1228. }
  1229. static int32_t read_s32(uint8_t *data, size_t offset)
  1230. {
  1231. return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
  1232. (data[offset + 2] << 8) | data[offset + 3]);
  1233. }
  1234. uint32_t read_u32(uint8_t *data, size_t offset)
  1235. {
  1236. return ((data[offset] << 24) | (data[offset + 1] << 16) |
  1237. (data[offset + 2] << 8) | data[offset + 3]);
  1238. }
  1239. static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
  1240. {
  1241. }
  1242. static void check_pointer_type_change(Notifier *notifier, void *data)
  1243. {
  1244. VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
  1245. int absolute = kbd_mouse_is_absolute();
  1246. if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
  1247. vnc_lock_output(vs);
  1248. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1249. vnc_write_u8(vs, 0);
  1250. vnc_write_u16(vs, 1);
  1251. vnc_framebuffer_update(vs, absolute, 0,
  1252. ds_get_width(vs->ds), ds_get_height(vs->ds),
  1253. VNC_ENCODING_POINTER_TYPE_CHANGE);
  1254. vnc_unlock_output(vs);
  1255. vnc_flush(vs);
  1256. }
  1257. vs->absolute = absolute;
  1258. }
  1259. static void pointer_event(VncState *vs, int button_mask, int x, int y)
  1260. {
  1261. int buttons = 0;
  1262. int dz = 0;
  1263. if (button_mask & 0x01)
  1264. buttons |= MOUSE_EVENT_LBUTTON;
  1265. if (button_mask & 0x02)
  1266. buttons |= MOUSE_EVENT_MBUTTON;
  1267. if (button_mask & 0x04)
  1268. buttons |= MOUSE_EVENT_RBUTTON;
  1269. if (button_mask & 0x08)
  1270. dz = -1;
  1271. if (button_mask & 0x10)
  1272. dz = 1;
  1273. if (vs->absolute) {
  1274. kbd_mouse_event(ds_get_width(vs->ds) > 1 ?
  1275. x * 0x7FFF / (ds_get_width(vs->ds) - 1) : 0x4000,
  1276. ds_get_height(vs->ds) > 1 ?
  1277. y * 0x7FFF / (ds_get_height(vs->ds) - 1) : 0x4000,
  1278. dz, buttons);
  1279. } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
  1280. x -= 0x7FFF;
  1281. y -= 0x7FFF;
  1282. kbd_mouse_event(x, y, dz, buttons);
  1283. } else {
  1284. if (vs->last_x != -1)
  1285. kbd_mouse_event(x - vs->last_x,
  1286. y - vs->last_y,
  1287. dz, buttons);
  1288. vs->last_x = x;
  1289. vs->last_y = y;
  1290. }
  1291. }
  1292. static void reset_keys(VncState *vs)
  1293. {
  1294. int i;
  1295. for(i = 0; i < 256; i++) {
  1296. if (vs->modifiers_state[i]) {
  1297. if (i & SCANCODE_GREY)
  1298. kbd_put_keycode(SCANCODE_EMUL0);
  1299. kbd_put_keycode(i | SCANCODE_UP);
  1300. vs->modifiers_state[i] = 0;
  1301. }
  1302. }
  1303. }
  1304. static void press_key(VncState *vs, int keysym)
  1305. {
  1306. int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
  1307. if (keycode & SCANCODE_GREY)
  1308. kbd_put_keycode(SCANCODE_EMUL0);
  1309. kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
  1310. if (keycode & SCANCODE_GREY)
  1311. kbd_put_keycode(SCANCODE_EMUL0);
  1312. kbd_put_keycode(keycode | SCANCODE_UP);
  1313. }
  1314. static void kbd_leds(void *opaque, int ledstate)
  1315. {
  1316. VncState *vs = opaque;
  1317. int caps, num;
  1318. caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
  1319. num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0;
  1320. if (vs->modifiers_state[0x3a] != caps) {
  1321. vs->modifiers_state[0x3a] = caps;
  1322. }
  1323. if (vs->modifiers_state[0x45] != num) {
  1324. vs->modifiers_state[0x45] = num;
  1325. }
  1326. }
  1327. static void do_key_event(VncState *vs, int down, int keycode, int sym)
  1328. {
  1329. /* QEMU console switch */
  1330. switch(keycode) {
  1331. case 0x2a: /* Left Shift */
  1332. case 0x36: /* Right Shift */
  1333. case 0x1d: /* Left CTRL */
  1334. case 0x9d: /* Right CTRL */
  1335. case 0x38: /* Left ALT */
  1336. case 0xb8: /* Right ALT */
  1337. if (down)
  1338. vs->modifiers_state[keycode] = 1;
  1339. else
  1340. vs->modifiers_state[keycode] = 0;
  1341. break;
  1342. case 0x02 ... 0x0a: /* '1' to '9' keys */
  1343. if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
  1344. /* Reset the modifiers sent to the current console */
  1345. reset_keys(vs);
  1346. console_select(keycode - 0x02);
  1347. return;
  1348. }
  1349. break;
  1350. case 0x3a: /* CapsLock */
  1351. case 0x45: /* NumLock */
  1352. if (down)
  1353. vs->modifiers_state[keycode] ^= 1;
  1354. break;
  1355. }
  1356. if (down && vs->vd->lock_key_sync &&
  1357. keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
  1358. /* If the numlock state needs to change then simulate an additional
  1359. keypress before sending this one. This will happen if the user
  1360. toggles numlock away from the VNC window.
  1361. */
  1362. if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
  1363. if (!vs->modifiers_state[0x45]) {
  1364. vs->modifiers_state[0x45] = 1;
  1365. press_key(vs, 0xff7f);
  1366. }
  1367. } else {
  1368. if (vs->modifiers_state[0x45]) {
  1369. vs->modifiers_state[0x45] = 0;
  1370. press_key(vs, 0xff7f);
  1371. }
  1372. }
  1373. }
  1374. if (down && vs->vd->lock_key_sync &&
  1375. ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
  1376. /* If the capslock state needs to change then simulate an additional
  1377. keypress before sending this one. This will happen if the user
  1378. toggles capslock away from the VNC window.
  1379. */
  1380. int uppercase = !!(sym >= 'A' && sym <= 'Z');
  1381. int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
  1382. int capslock = !!(vs->modifiers_state[0x3a]);
  1383. if (capslock) {
  1384. if (uppercase == shift) {
  1385. vs->modifiers_state[0x3a] = 0;
  1386. press_key(vs, 0xffe5);
  1387. }
  1388. } else {
  1389. if (uppercase != shift) {
  1390. vs->modifiers_state[0x3a] = 1;
  1391. press_key(vs, 0xffe5);
  1392. }
  1393. }
  1394. }
  1395. if (is_graphic_console()) {
  1396. if (keycode & SCANCODE_GREY)
  1397. kbd_put_keycode(SCANCODE_EMUL0);
  1398. if (down)
  1399. kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
  1400. else
  1401. kbd_put_keycode(keycode | SCANCODE_UP);
  1402. } else {
  1403. bool numlock = vs->modifiers_state[0x45];
  1404. bool control = (vs->modifiers_state[0x1d] ||
  1405. vs->modifiers_state[0x9d]);
  1406. /* QEMU console emulation */
  1407. if (down) {
  1408. switch (keycode) {
  1409. case 0x2a: /* Left Shift */
  1410. case 0x36: /* Right Shift */
  1411. case 0x1d: /* Left CTRL */
  1412. case 0x9d: /* Right CTRL */
  1413. case 0x38: /* Left ALT */
  1414. case 0xb8: /* Right ALT */
  1415. break;
  1416. case 0xc8:
  1417. kbd_put_keysym(QEMU_KEY_UP);
  1418. break;
  1419. case 0xd0:
  1420. kbd_put_keysym(QEMU_KEY_DOWN);
  1421. break;
  1422. case 0xcb:
  1423. kbd_put_keysym(QEMU_KEY_LEFT);
  1424. break;
  1425. case 0xcd:
  1426. kbd_put_keysym(QEMU_KEY_RIGHT);
  1427. break;
  1428. case 0xd3:
  1429. kbd_put_keysym(QEMU_KEY_DELETE);
  1430. break;
  1431. case 0xc7:
  1432. kbd_put_keysym(QEMU_KEY_HOME);
  1433. break;
  1434. case 0xcf:
  1435. kbd_put_keysym(QEMU_KEY_END);
  1436. break;
  1437. case 0xc9:
  1438. kbd_put_keysym(QEMU_KEY_PAGEUP);
  1439. break;
  1440. case 0xd1:
  1441. kbd_put_keysym(QEMU_KEY_PAGEDOWN);
  1442. break;
  1443. case 0x47:
  1444. kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
  1445. break;
  1446. case 0x48:
  1447. kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
  1448. break;
  1449. case 0x49:
  1450. kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
  1451. break;
  1452. case 0x4b:
  1453. kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
  1454. break;
  1455. case 0x4c:
  1456. kbd_put_keysym('5');
  1457. break;
  1458. case 0x4d:
  1459. kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
  1460. break;
  1461. case 0x4f:
  1462. kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
  1463. break;
  1464. case 0x50:
  1465. kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
  1466. break;
  1467. case 0x51:
  1468. kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
  1469. break;
  1470. case 0x52:
  1471. kbd_put_keysym('0');
  1472. break;
  1473. case 0x53:
  1474. kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
  1475. break;
  1476. case 0xb5:
  1477. kbd_put_keysym('/');
  1478. break;
  1479. case 0x37:
  1480. kbd_put_keysym('*');
  1481. break;
  1482. case 0x4a:
  1483. kbd_put_keysym('-');
  1484. break;
  1485. case 0x4e:
  1486. kbd_put_keysym('+');
  1487. break;
  1488. case 0x9c:
  1489. kbd_put_keysym('\n');
  1490. break;
  1491. default:
  1492. if (control) {
  1493. kbd_put_keysym(sym & 0x1f);
  1494. } else {
  1495. kbd_put_keysym(sym);
  1496. }
  1497. break;
  1498. }
  1499. }
  1500. }
  1501. }
  1502. static void vnc_release_modifiers(VncState *vs)
  1503. {
  1504. static const int keycodes[] = {
  1505. /* shift, control, alt keys, both left & right */
  1506. 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
  1507. };
  1508. int i, keycode;
  1509. if (!is_graphic_console()) {
  1510. return;
  1511. }
  1512. for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
  1513. keycode = keycodes[i];
  1514. if (!vs->modifiers_state[keycode]) {
  1515. continue;
  1516. }
  1517. if (keycode & SCANCODE_GREY) {
  1518. kbd_put_keycode(SCANCODE_EMUL0);
  1519. }
  1520. kbd_put_keycode(keycode | SCANCODE_UP);
  1521. }
  1522. }
  1523. static void key_event(VncState *vs, int down, uint32_t sym)
  1524. {
  1525. int keycode;
  1526. int lsym = sym;
  1527. if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) {
  1528. lsym = lsym - 'A' + 'a';
  1529. }
  1530. keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
  1531. do_key_event(vs, down, keycode, sym);
  1532. }
  1533. static void ext_key_event(VncState *vs, int down,
  1534. uint32_t sym, uint16_t keycode)
  1535. {
  1536. /* if the user specifies a keyboard layout, always use it */
  1537. if (keyboard_layout)
  1538. key_event(vs, down, sym);
  1539. else
  1540. do_key_event(vs, down, keycode, sym);
  1541. }
  1542. static void framebuffer_update_request(VncState *vs, int incremental,
  1543. int x_position, int y_position,
  1544. int w, int h)
  1545. {
  1546. int i;
  1547. const size_t width = ds_get_width(vs->ds) / 16;
  1548. if (y_position > ds_get_height(vs->ds))
  1549. y_position = ds_get_height(vs->ds);
  1550. if (y_position + h >= ds_get_height(vs->ds))
  1551. h = ds_get_height(vs->ds) - y_position;
  1552. vs->need_update = 1;
  1553. if (!incremental) {
  1554. vs->force_update = 1;
  1555. for (i = 0; i < h; i++) {
  1556. bitmap_set(vs->dirty[y_position + i], 0, width);
  1557. bitmap_clear(vs->dirty[y_position + i], width,
  1558. VNC_DIRTY_BITS - width);
  1559. }
  1560. }
  1561. }
  1562. static void send_ext_key_event_ack(VncState *vs)
  1563. {
  1564. vnc_lock_output(vs);
  1565. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1566. vnc_write_u8(vs, 0);
  1567. vnc_write_u16(vs, 1);
  1568. vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
  1569. VNC_ENCODING_EXT_KEY_EVENT);
  1570. vnc_unlock_output(vs);
  1571. vnc_flush(vs);
  1572. }
  1573. static void send_ext_audio_ack(VncState *vs)
  1574. {
  1575. vnc_lock_output(vs);
  1576. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1577. vnc_write_u8(vs, 0);
  1578. vnc_write_u16(vs, 1);
  1579. vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
  1580. VNC_ENCODING_AUDIO);
  1581. vnc_unlock_output(vs);
  1582. vnc_flush(vs);
  1583. }
  1584. static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
  1585. {
  1586. int i;
  1587. unsigned int enc = 0;
  1588. vs->features = 0;
  1589. vs->vnc_encoding = 0;
  1590. vs->tight.compression = 9;
  1591. vs->tight.quality = -1; /* Lossless by default */
  1592. vs->absolute = -1;
  1593. /*
  1594. * Start from the end because the encodings are sent in order of preference.
  1595. * This way the preferred encoding (first encoding defined in the array)
  1596. * will be set at the end of the loop.
  1597. */
  1598. for (i = n_encodings - 1; i >= 0; i--) {
  1599. enc = encodings[i];
  1600. switch (enc) {
  1601. case VNC_ENCODING_RAW:
  1602. vs->vnc_encoding = enc;
  1603. break;
  1604. case VNC_ENCODING_COPYRECT:
  1605. vs->features |= VNC_FEATURE_COPYRECT_MASK;
  1606. break;
  1607. case VNC_ENCODING_HEXTILE:
  1608. vs->features |= VNC_FEATURE_HEXTILE_MASK;
  1609. vs->vnc_encoding = enc;
  1610. break;
  1611. case VNC_ENCODING_TIGHT:
  1612. vs->features |= VNC_FEATURE_TIGHT_MASK;
  1613. vs->vnc_encoding = enc;
  1614. break;
  1615. #ifdef CONFIG_VNC_PNG
  1616. case VNC_ENCODING_TIGHT_PNG:
  1617. vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
  1618. vs->vnc_encoding = enc;
  1619. break;
  1620. #endif
  1621. case VNC_ENCODING_ZLIB:
  1622. vs->features |= VNC_FEATURE_ZLIB_MASK;
  1623. vs->vnc_encoding = enc;
  1624. break;
  1625. case VNC_ENCODING_ZRLE:
  1626. vs->features |= VNC_FEATURE_ZRLE_MASK;
  1627. vs->vnc_encoding = enc;
  1628. break;
  1629. case VNC_ENCODING_ZYWRLE:
  1630. vs->features |= VNC_FEATURE_ZYWRLE_MASK;
  1631. vs->vnc_encoding = enc;
  1632. break;
  1633. case VNC_ENCODING_DESKTOPRESIZE:
  1634. vs->features |= VNC_FEATURE_RESIZE_MASK;
  1635. break;
  1636. case VNC_ENCODING_POINTER_TYPE_CHANGE:
  1637. vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
  1638. break;
  1639. case VNC_ENCODING_RICH_CURSOR:
  1640. vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
  1641. break;
  1642. case VNC_ENCODING_EXT_KEY_EVENT:
  1643. send_ext_key_event_ack(vs);
  1644. break;
  1645. case VNC_ENCODING_AUDIO:
  1646. send_ext_audio_ack(vs);
  1647. break;
  1648. case VNC_ENCODING_WMVi:
  1649. vs->features |= VNC_FEATURE_WMVI_MASK;
  1650. break;
  1651. case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
  1652. vs->tight.compression = (enc & 0x0F);
  1653. break;
  1654. case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
  1655. if (vs->vd->lossy) {
  1656. vs->tight.quality = (enc & 0x0F);
  1657. }
  1658. break;
  1659. default:
  1660. VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
  1661. break;
  1662. }
  1663. }
  1664. vnc_desktop_resize(vs);
  1665. check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
  1666. }
  1667. static void set_pixel_conversion(VncState *vs)
  1668. {
  1669. pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
  1670. if (fmt == VNC_SERVER_FB_FORMAT) {
  1671. vs->write_pixels = vnc_write_pixels_copy;
  1672. vnc_hextile_set_pixel_conversion(vs, 0);
  1673. } else {
  1674. vs->write_pixels = vnc_write_pixels_generic;
  1675. vnc_hextile_set_pixel_conversion(vs, 1);
  1676. }
  1677. }
  1678. static void set_pixel_format(VncState *vs,
  1679. int bits_per_pixel, int depth,
  1680. int big_endian_flag, int true_color_flag,
  1681. int red_max, int green_max, int blue_max,
  1682. int red_shift, int green_shift, int blue_shift)
  1683. {
  1684. if (!true_color_flag) {
  1685. vnc_client_error(vs);
  1686. return;
  1687. }
  1688. vs->client_pf.rmax = red_max;
  1689. vs->client_pf.rbits = hweight_long(red_max);
  1690. vs->client_pf.rshift = red_shift;
  1691. vs->client_pf.rmask = red_max << red_shift;
  1692. vs->client_pf.gmax = green_max;
  1693. vs->client_pf.gbits = hweight_long(green_max);
  1694. vs->client_pf.gshift = green_shift;
  1695. vs->client_pf.gmask = green_max << green_shift;
  1696. vs->client_pf.bmax = blue_max;
  1697. vs->client_pf.bbits = hweight_long(blue_max);
  1698. vs->client_pf.bshift = blue_shift;
  1699. vs->client_pf.bmask = blue_max << blue_shift;
  1700. vs->client_pf.bits_per_pixel = bits_per_pixel;
  1701. vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
  1702. vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
  1703. vs->client_be = big_endian_flag;
  1704. set_pixel_conversion(vs);
  1705. vga_hw_invalidate();
  1706. vga_hw_update();
  1707. }
  1708. static void pixel_format_message (VncState *vs) {
  1709. char pad[3] = { 0, 0, 0 };
  1710. vs->client_pf = qemu_default_pixelformat(32);
  1711. vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
  1712. vnc_write_u8(vs, vs->client_pf.depth); /* depth */
  1713. #ifdef HOST_WORDS_BIGENDIAN
  1714. vnc_write_u8(vs, 1); /* big-endian-flag */
  1715. #else
  1716. vnc_write_u8(vs, 0); /* big-endian-flag */
  1717. #endif
  1718. vnc_write_u8(vs, 1); /* true-color-flag */
  1719. vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */
  1720. vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */
  1721. vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */
  1722. vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */
  1723. vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */
  1724. vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */
  1725. vnc_write(vs, pad, 3); /* padding */
  1726. vnc_hextile_set_pixel_conversion(vs, 0);
  1727. vs->write_pixels = vnc_write_pixels_copy;
  1728. }
  1729. static void vnc_dpy_setdata(DisplayState *ds)
  1730. {
  1731. VncDisplay *vd = ds->opaque;
  1732. qemu_pixman_image_unref(vd->guest.fb);
  1733. vd->guest.fb = pixman_image_ref(ds->surface->image);
  1734. vd->guest.format = ds->surface->format;
  1735. vnc_dpy_update(ds, 0, 0, ds_get_width(ds), ds_get_height(ds));
  1736. }
  1737. static void vnc_colordepth(VncState *vs)
  1738. {
  1739. if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
  1740. /* Sending a WMVi message to notify the client*/
  1741. vnc_lock_output(vs);
  1742. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1743. vnc_write_u8(vs, 0);
  1744. vnc_write_u16(vs, 1); /* number of rects */
  1745. vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds),
  1746. ds_get_height(vs->ds), VNC_ENCODING_WMVi);
  1747. pixel_format_message(vs);
  1748. vnc_unlock_output(vs);
  1749. vnc_flush(vs);
  1750. } else {
  1751. set_pixel_conversion(vs);
  1752. }
  1753. }
  1754. static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
  1755. {
  1756. int i;
  1757. uint16_t limit;
  1758. VncDisplay *vd = vs->vd;
  1759. if (data[0] > 3) {
  1760. vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
  1761. if (!qemu_timer_expired(vd->timer, qemu_get_clock_ms(rt_clock) + vd->timer_interval))
  1762. qemu_mod_timer(vd->timer, qemu_get_clock_ms(rt_clock) + vd->timer_interval);
  1763. }
  1764. switch (data[0]) {
  1765. case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
  1766. if (len == 1)
  1767. return 20;
  1768. set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
  1769. read_u8(data, 6), read_u8(data, 7),
  1770. read_u16(data, 8), read_u16(data, 10),
  1771. read_u16(data, 12), read_u8(data, 14),
  1772. read_u8(data, 15), read_u8(data, 16));
  1773. break;
  1774. case VNC_MSG_CLIENT_SET_ENCODINGS:
  1775. if (len == 1)
  1776. return 4;
  1777. if (len == 4) {
  1778. limit = read_u16(data, 2);
  1779. if (limit > 0)
  1780. return 4 + (limit * 4);
  1781. } else
  1782. limit = read_u16(data, 2);
  1783. for (i = 0; i < limit; i++) {
  1784. int32_t val = read_s32(data, 4 + (i * 4));
  1785. memcpy(data + 4 + (i * 4), &val, sizeof(val));
  1786. }
  1787. set_encodings(vs, (int32_t *)(data + 4), limit);
  1788. break;
  1789. case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
  1790. if (len == 1)
  1791. return 10;
  1792. framebuffer_update_request(vs,
  1793. read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
  1794. read_u16(data, 6), read_u16(data, 8));
  1795. break;
  1796. case VNC_MSG_CLIENT_KEY_EVENT:
  1797. if (len == 1)
  1798. return 8;
  1799. key_event(vs, read_u8(data, 1), read_u32(data, 4));
  1800. break;
  1801. case VNC_MSG_CLIENT_POINTER_EVENT:
  1802. if (len == 1)
  1803. return 6;
  1804. pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
  1805. break;
  1806. case VNC_MSG_CLIENT_CUT_TEXT:
  1807. if (len == 1)
  1808. return 8;
  1809. if (len == 8) {
  1810. uint32_t dlen = read_u32(data, 4);
  1811. if (dlen > 0)
  1812. return 8 + dlen;
  1813. }
  1814. client_cut_text(vs, read_u32(data, 4), data + 8);
  1815. break;
  1816. case VNC_MSG_CLIENT_QEMU:
  1817. if (len == 1)
  1818. return 2;
  1819. switch (read_u8(data, 1)) {
  1820. case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
  1821. if (len == 2)
  1822. return 12;
  1823. ext_key_event(vs, read_u16(data, 2),
  1824. read_u32(data, 4), read_u32(data, 8));
  1825. break;
  1826. case VNC_MSG_CLIENT_QEMU_AUDIO:
  1827. if (len == 2)
  1828. return 4;
  1829. switch (read_u16 (data, 2)) {
  1830. case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
  1831. audio_add(vs);
  1832. break;
  1833. case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
  1834. audio_del(vs);
  1835. break;
  1836. case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
  1837. if (len == 4)
  1838. return 10;
  1839. switch (read_u8(data, 4)) {
  1840. case 0: vs->as.fmt = AUD_FMT_U8; break;
  1841. case 1: vs->as.fmt = AUD_FMT_S8; break;
  1842. case 2: vs->as.fmt = AUD_FMT_U16; break;
  1843. case 3: vs->as.fmt = AUD_FMT_S16; break;
  1844. case 4: vs->as.fmt = AUD_FMT_U32; break;
  1845. case 5: vs->as.fmt = AUD_FMT_S32; break;
  1846. default:
  1847. printf("Invalid audio format %d\n", read_u8(data, 4));
  1848. vnc_client_error(vs);
  1849. break;
  1850. }
  1851. vs->as.nchannels = read_u8(data, 5);
  1852. if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
  1853. printf("Invalid audio channel coount %d\n",
  1854. read_u8(data, 5));
  1855. vnc_client_error(vs);
  1856. break;
  1857. }
  1858. vs->as.freq = read_u32(data, 6);
  1859. break;
  1860. default:
  1861. printf ("Invalid audio message %d\n", read_u8(data, 4));
  1862. vnc_client_error(vs);
  1863. break;
  1864. }
  1865. break;
  1866. default:
  1867. printf("Msg: %d\n", read_u16(data, 0));
  1868. vnc_client_error(vs);
  1869. break;
  1870. }
  1871. break;
  1872. default:
  1873. printf("Msg: %d\n", data[0]);
  1874. vnc_client_error(vs);
  1875. break;
  1876. }
  1877. vnc_read_when(vs, protocol_client_msg, 1);
  1878. return 0;
  1879. }
  1880. static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
  1881. {
  1882. char buf[1024];
  1883. VncShareMode mode;
  1884. int size;
  1885. mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
  1886. switch (vs->vd->share_policy) {
  1887. case VNC_SHARE_POLICY_IGNORE:
  1888. /*
  1889. * Ignore the shared flag. Nothing to do here.
  1890. *
  1891. * Doesn't conform to the rfb spec but is traditional qemu
  1892. * behavior, thus left here as option for compatibility
  1893. * reasons.
  1894. */
  1895. break;
  1896. case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
  1897. /*
  1898. * Policy: Allow clients ask for exclusive access.
  1899. *
  1900. * Implementation: When a client asks for exclusive access,
  1901. * disconnect all others. Shared connects are allowed as long
  1902. * as no exclusive connection exists.
  1903. *
  1904. * This is how the rfb spec suggests to handle the shared flag.
  1905. */
  1906. if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
  1907. VncState *client;
  1908. QTAILQ_FOREACH(client, &vs->vd->clients, next) {
  1909. if (vs == client) {
  1910. continue;
  1911. }
  1912. if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
  1913. client->share_mode != VNC_SHARE_MODE_SHARED) {
  1914. continue;
  1915. }
  1916. vnc_disconnect_start(client);
  1917. }
  1918. }
  1919. if (mode == VNC_SHARE_MODE_SHARED) {
  1920. if (vs->vd->num_exclusive > 0) {
  1921. vnc_disconnect_start(vs);
  1922. return 0;
  1923. }
  1924. }
  1925. break;
  1926. case VNC_SHARE_POLICY_FORCE_SHARED:
  1927. /*
  1928. * Policy: Shared connects only.
  1929. * Implementation: Disallow clients asking for exclusive access.
  1930. *
  1931. * Useful for shared desktop sessions where you don't want
  1932. * someone forgetting to say -shared when running the vnc
  1933. * client disconnect everybody else.
  1934. */
  1935. if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
  1936. vnc_disconnect_start(vs);
  1937. return 0;
  1938. }
  1939. break;
  1940. }
  1941. vnc_set_share_mode(vs, mode);
  1942. vs->client_width = ds_get_width(vs->ds);
  1943. vs->client_height = ds_get_height(vs->ds);
  1944. vnc_write_u16(vs, vs->client_width);
  1945. vnc_write_u16(vs, vs->client_height);
  1946. pixel_format_message(vs);
  1947. if (qemu_name)
  1948. size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
  1949. else
  1950. size = snprintf(buf, sizeof(buf), "QEMU");
  1951. vnc_write_u32(vs, size);
  1952. vnc_write(vs, buf, size);
  1953. vnc_flush(vs);
  1954. vnc_client_cache_auth(vs);
  1955. vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED);
  1956. vnc_read_when(vs, protocol_client_msg, 1);
  1957. return 0;
  1958. }
  1959. void start_client_init(VncState *vs)
  1960. {
  1961. vnc_read_when(vs, protocol_client_init, 1);
  1962. }
  1963. static void make_challenge(VncState *vs)
  1964. {
  1965. int i;
  1966. srand(time(NULL)+getpid()+getpid()*987654+rand());
  1967. for (i = 0 ; i < sizeof(vs->challenge) ; i++)
  1968. vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
  1969. }
  1970. static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
  1971. {
  1972. unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
  1973. int i, j, pwlen;
  1974. unsigned char key[8];
  1975. time_t now = time(NULL);
  1976. if (!vs->vd->password) {
  1977. VNC_DEBUG("No password configured on server");
  1978. goto reject;
  1979. }
  1980. if (vs->vd->expires < now) {
  1981. VNC_DEBUG("Password is expired");
  1982. goto reject;
  1983. }
  1984. memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
  1985. /* Calculate the expected challenge response */
  1986. pwlen = strlen(vs->vd->password);
  1987. for (i=0; i<sizeof(key); i++)
  1988. key[i] = i<pwlen ? vs->vd->password[i] : 0;
  1989. deskey(key, EN0);
  1990. for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
  1991. des(response+j, response+j);
  1992. /* Compare expected vs actual challenge response */
  1993. if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
  1994. VNC_DEBUG("Client challenge response did not match\n");
  1995. goto reject;
  1996. } else {
  1997. VNC_DEBUG("Accepting VNC challenge response\n");
  1998. vnc_write_u32(vs, 0); /* Accept auth */
  1999. vnc_flush(vs);
  2000. start_client_init(vs);
  2001. }
  2002. return 0;
  2003. reject:
  2004. vnc_write_u32(vs, 1); /* Reject auth */
  2005. if (vs->minor >= 8) {
  2006. static const char err[] = "Authentication failed";
  2007. vnc_write_u32(vs, sizeof(err));
  2008. vnc_write(vs, err, sizeof(err));
  2009. }
  2010. vnc_flush(vs);
  2011. vnc_client_error(vs);
  2012. return 0;
  2013. }
  2014. void start_auth_vnc(VncState *vs)
  2015. {
  2016. make_challenge(vs);
  2017. /* Send client a 'random' challenge */
  2018. vnc_write(vs, vs->challenge, sizeof(vs->challenge));
  2019. vnc_flush(vs);
  2020. vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
  2021. }
  2022. static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
  2023. {
  2024. /* We only advertise 1 auth scheme at a time, so client
  2025. * must pick the one we sent. Verify this */
  2026. if (data[0] != vs->auth) { /* Reject auth */
  2027. VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
  2028. vnc_write_u32(vs, 1);
  2029. if (vs->minor >= 8) {
  2030. static const char err[] = "Authentication failed";
  2031. vnc_write_u32(vs, sizeof(err));
  2032. vnc_write(vs, err, sizeof(err));
  2033. }
  2034. vnc_client_error(vs);
  2035. } else { /* Accept requested auth */
  2036. VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
  2037. switch (vs->auth) {
  2038. case VNC_AUTH_NONE:
  2039. VNC_DEBUG("Accept auth none\n");
  2040. if (vs->minor >= 8) {
  2041. vnc_write_u32(vs, 0); /* Accept auth completion */
  2042. vnc_flush(vs);
  2043. }
  2044. start_client_init(vs);
  2045. break;
  2046. case VNC_AUTH_VNC:
  2047. VNC_DEBUG("Start VNC auth\n");
  2048. start_auth_vnc(vs);
  2049. break;
  2050. #ifdef CONFIG_VNC_TLS
  2051. case VNC_AUTH_VENCRYPT:
  2052. VNC_DEBUG("Accept VeNCrypt auth\n");
  2053. start_auth_vencrypt(vs);
  2054. break;
  2055. #endif /* CONFIG_VNC_TLS */
  2056. #ifdef CONFIG_VNC_SASL
  2057. case VNC_AUTH_SASL:
  2058. VNC_DEBUG("Accept SASL auth\n");
  2059. start_auth_sasl(vs);
  2060. break;
  2061. #endif /* CONFIG_VNC_SASL */
  2062. default: /* Should not be possible, but just in case */
  2063. VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
  2064. vnc_write_u8(vs, 1);
  2065. if (vs->minor >= 8) {
  2066. static const char err[] = "Authentication failed";
  2067. vnc_write_u32(vs, sizeof(err));
  2068. vnc_write(vs, err, sizeof(err));
  2069. }
  2070. vnc_client_error(vs);
  2071. }
  2072. }
  2073. return 0;
  2074. }
  2075. static int protocol_version(VncState *vs, uint8_t *version, size_t len)
  2076. {
  2077. char local[13];
  2078. memcpy(local, version, 12);
  2079. local[12] = 0;
  2080. if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
  2081. VNC_DEBUG("Malformed protocol version %s\n", local);
  2082. vnc_client_error(vs);
  2083. return 0;
  2084. }
  2085. VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
  2086. if (vs->major != 3 ||
  2087. (vs->minor != 3 &&
  2088. vs->minor != 4 &&
  2089. vs->minor != 5 &&
  2090. vs->minor != 7 &&
  2091. vs->minor != 8)) {
  2092. VNC_DEBUG("Unsupported client version\n");
  2093. vnc_write_u32(vs, VNC_AUTH_INVALID);
  2094. vnc_flush(vs);
  2095. vnc_client_error(vs);
  2096. return 0;
  2097. }
  2098. /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
  2099. * as equivalent to v3.3 by servers
  2100. */
  2101. if (vs->minor == 4 || vs->minor == 5)
  2102. vs->minor = 3;
  2103. if (vs->minor == 3) {
  2104. if (vs->auth == VNC_AUTH_NONE) {
  2105. VNC_DEBUG("Tell client auth none\n");
  2106. vnc_write_u32(vs, vs->auth);
  2107. vnc_flush(vs);
  2108. start_client_init(vs);
  2109. } else if (vs->auth == VNC_AUTH_VNC) {
  2110. VNC_DEBUG("Tell client VNC auth\n");
  2111. vnc_write_u32(vs, vs->auth);
  2112. vnc_flush(vs);
  2113. start_auth_vnc(vs);
  2114. } else {
  2115. VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
  2116. vnc_write_u32(vs, VNC_AUTH_INVALID);
  2117. vnc_flush(vs);
  2118. vnc_client_error(vs);
  2119. }
  2120. } else {
  2121. VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
  2122. vnc_write_u8(vs, 1); /* num auth */
  2123. vnc_write_u8(vs, vs->auth);
  2124. vnc_read_when(vs, protocol_client_auth, 1);
  2125. vnc_flush(vs);
  2126. }
  2127. return 0;
  2128. }
  2129. static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
  2130. {
  2131. struct VncSurface *vs = &vd->guest;
  2132. return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
  2133. }
  2134. void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
  2135. {
  2136. int i, j;
  2137. w = (x + w) / VNC_STAT_RECT;
  2138. h = (y + h) / VNC_STAT_RECT;
  2139. x /= VNC_STAT_RECT;
  2140. y /= VNC_STAT_RECT;
  2141. for (j = y; j <= h; j++) {
  2142. for (i = x; i <= w; i++) {
  2143. vs->lossy_rect[j][i] = 1;
  2144. }
  2145. }
  2146. }
  2147. static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
  2148. {
  2149. VncState *vs;
  2150. int sty = y / VNC_STAT_RECT;
  2151. int stx = x / VNC_STAT_RECT;
  2152. int has_dirty = 0;
  2153. y = y / VNC_STAT_RECT * VNC_STAT_RECT;
  2154. x = x / VNC_STAT_RECT * VNC_STAT_RECT;
  2155. QTAILQ_FOREACH(vs, &vd->clients, next) {
  2156. int j;
  2157. /* kernel send buffers are full -> refresh later */
  2158. if (vs->output.offset) {
  2159. continue;
  2160. }
  2161. if (!vs->lossy_rect[sty][stx]) {
  2162. continue;
  2163. }
  2164. vs->lossy_rect[sty][stx] = 0;
  2165. for (j = 0; j < VNC_STAT_RECT; ++j) {
  2166. bitmap_set(vs->dirty[y + j], x / 16, VNC_STAT_RECT / 16);
  2167. }
  2168. has_dirty++;
  2169. }
  2170. return has_dirty;
  2171. }
  2172. static int vnc_update_stats(VncDisplay *vd, struct timeval * tv)
  2173. {
  2174. int width = pixman_image_get_width(vd->guest.fb);
  2175. int height = pixman_image_get_height(vd->guest.fb);
  2176. int x, y;
  2177. struct timeval res;
  2178. int has_dirty = 0;
  2179. for (y = 0; y < height; y += VNC_STAT_RECT) {
  2180. for (x = 0; x < width; x += VNC_STAT_RECT) {
  2181. VncRectStat *rect = vnc_stat_rect(vd, x, y);
  2182. rect->updated = false;
  2183. }
  2184. }
  2185. qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
  2186. if (timercmp(&vd->guest.last_freq_check, &res, >)) {
  2187. return has_dirty;
  2188. }
  2189. vd->guest.last_freq_check = *tv;
  2190. for (y = 0; y < height; y += VNC_STAT_RECT) {
  2191. for (x = 0; x < width; x += VNC_STAT_RECT) {
  2192. VncRectStat *rect= vnc_stat_rect(vd, x, y);
  2193. int count = ARRAY_SIZE(rect->times);
  2194. struct timeval min, max;
  2195. if (!timerisset(&rect->times[count - 1])) {
  2196. continue ;
  2197. }
  2198. max = rect->times[(rect->idx + count - 1) % count];
  2199. qemu_timersub(tv, &max, &res);
  2200. if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
  2201. rect->freq = 0;
  2202. has_dirty += vnc_refresh_lossy_rect(vd, x, y);
  2203. memset(rect->times, 0, sizeof (rect->times));
  2204. continue ;
  2205. }
  2206. min = rect->times[rect->idx];
  2207. max = rect->times[(rect->idx + count - 1) % count];
  2208. qemu_timersub(&max, &min, &res);
  2209. rect->freq = res.tv_sec + res.tv_usec / 1000000.;
  2210. rect->freq /= count;
  2211. rect->freq = 1. / rect->freq;
  2212. }
  2213. }
  2214. return has_dirty;
  2215. }
  2216. double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
  2217. {
  2218. int i, j;
  2219. double total = 0;
  2220. int num = 0;
  2221. x = (x / VNC_STAT_RECT) * VNC_STAT_RECT;
  2222. y = (y / VNC_STAT_RECT) * VNC_STAT_RECT;
  2223. for (j = y; j <= y + h; j += VNC_STAT_RECT) {
  2224. for (i = x; i <= x + w; i += VNC_STAT_RECT) {
  2225. total += vnc_stat_rect(vs->vd, i, j)->freq;
  2226. num++;
  2227. }
  2228. }
  2229. if (num) {
  2230. return total / num;
  2231. } else {
  2232. return 0;
  2233. }
  2234. }
  2235. static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
  2236. {
  2237. VncRectStat *rect;
  2238. rect = vnc_stat_rect(vd, x, y);
  2239. if (rect->updated) {
  2240. return ;
  2241. }
  2242. rect->times[rect->idx] = *tv;
  2243. rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
  2244. rect->updated = true;
  2245. }
  2246. static int vnc_refresh_server_surface(VncDisplay *vd)
  2247. {
  2248. int width = pixman_image_get_width(vd->guest.fb);
  2249. int height = pixman_image_get_height(vd->guest.fb);
  2250. int y;
  2251. uint8_t *guest_row;
  2252. uint8_t *server_row;
  2253. int cmp_bytes;
  2254. VncState *vs;
  2255. int has_dirty = 0;
  2256. pixman_image_t *tmpbuf = NULL;
  2257. struct timeval tv = { 0, 0 };
  2258. if (!vd->non_adaptive) {
  2259. gettimeofday(&tv, NULL);
  2260. has_dirty = vnc_update_stats(vd, &tv);
  2261. }
  2262. /*
  2263. * Walk through the guest dirty map.
  2264. * Check and copy modified bits from guest to server surface.
  2265. * Update server dirty map.
  2266. */
  2267. cmp_bytes = 64;
  2268. if (cmp_bytes > vnc_server_fb_stride(vd)) {
  2269. cmp_bytes = vnc_server_fb_stride(vd);
  2270. }
  2271. if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
  2272. int width = pixman_image_get_width(vd->server);
  2273. tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
  2274. }
  2275. guest_row = (uint8_t *)pixman_image_get_data(vd->guest.fb);
  2276. server_row = (uint8_t *)pixman_image_get_data(vd->server);
  2277. for (y = 0; y < height; y++) {
  2278. if (!bitmap_empty(vd->guest.dirty[y], VNC_DIRTY_BITS)) {
  2279. int x;
  2280. uint8_t *guest_ptr;
  2281. uint8_t *server_ptr;
  2282. if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
  2283. qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
  2284. guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
  2285. } else {
  2286. guest_ptr = guest_row;
  2287. }
  2288. server_ptr = server_row;
  2289. for (x = 0; x + 15 < width;
  2290. x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
  2291. if (!test_and_clear_bit((x / 16), vd->guest.dirty[y]))
  2292. continue;
  2293. if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0)
  2294. continue;
  2295. memcpy(server_ptr, guest_ptr, cmp_bytes);
  2296. if (!vd->non_adaptive)
  2297. vnc_rect_updated(vd, x, y, &tv);
  2298. QTAILQ_FOREACH(vs, &vd->clients, next) {
  2299. set_bit((x / 16), vs->dirty[y]);
  2300. }
  2301. has_dirty++;
  2302. }
  2303. }
  2304. guest_row += pixman_image_get_stride(vd->guest.fb);
  2305. server_row += pixman_image_get_stride(vd->server);
  2306. }
  2307. qemu_pixman_image_unref(tmpbuf);
  2308. return has_dirty;
  2309. }
  2310. static void vnc_refresh(void *opaque)
  2311. {
  2312. VncDisplay *vd = opaque;
  2313. VncState *vs, *vn;
  2314. int has_dirty, rects = 0;
  2315. vga_hw_update();
  2316. if (vnc_trylock_display(vd)) {
  2317. vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
  2318. qemu_mod_timer(vd->timer, qemu_get_clock_ms(rt_clock) +
  2319. vd->timer_interval);
  2320. return;
  2321. }
  2322. has_dirty = vnc_refresh_server_surface(vd);
  2323. vnc_unlock_display(vd);
  2324. QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
  2325. rects += vnc_update_client(vs, has_dirty);
  2326. /* vs might be free()ed here */
  2327. }
  2328. /* vd->timer could be NULL now if the last client disconnected,
  2329. * in this case don't update the timer */
  2330. if (vd->timer == NULL)
  2331. return;
  2332. if (has_dirty && rects) {
  2333. vd->timer_interval /= 2;
  2334. if (vd->timer_interval < VNC_REFRESH_INTERVAL_BASE)
  2335. vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
  2336. } else {
  2337. vd->timer_interval += VNC_REFRESH_INTERVAL_INC;
  2338. if (vd->timer_interval > VNC_REFRESH_INTERVAL_MAX)
  2339. vd->timer_interval = VNC_REFRESH_INTERVAL_MAX;
  2340. }
  2341. qemu_mod_timer(vd->timer, qemu_get_clock_ms(rt_clock) + vd->timer_interval);
  2342. }
  2343. static void vnc_init_timer(VncDisplay *vd)
  2344. {
  2345. vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
  2346. if (vd->timer == NULL && !QTAILQ_EMPTY(&vd->clients)) {
  2347. vd->timer = qemu_new_timer_ms(rt_clock, vnc_refresh, vd);
  2348. vnc_dpy_resize(vd->ds);
  2349. vnc_refresh(vd);
  2350. }
  2351. }
  2352. static void vnc_remove_timer(VncDisplay *vd)
  2353. {
  2354. if (vd->timer != NULL && QTAILQ_EMPTY(&vd->clients)) {
  2355. qemu_del_timer(vd->timer);
  2356. qemu_free_timer(vd->timer);
  2357. vd->timer = NULL;
  2358. }
  2359. }
  2360. static void vnc_connect(VncDisplay *vd, int csock, int skipauth, bool websocket)
  2361. {
  2362. VncState *vs = g_malloc0(sizeof(VncState));
  2363. int i;
  2364. vs->csock = csock;
  2365. if (skipauth) {
  2366. vs->auth = VNC_AUTH_NONE;
  2367. #ifdef CONFIG_VNC_TLS
  2368. vs->subauth = VNC_AUTH_INVALID;
  2369. #endif
  2370. } else {
  2371. vs->auth = vd->auth;
  2372. #ifdef CONFIG_VNC_TLS
  2373. vs->subauth = vd->subauth;
  2374. #endif
  2375. }
  2376. vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
  2377. for (i = 0; i < VNC_STAT_ROWS; ++i) {
  2378. vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
  2379. }
  2380. VNC_DEBUG("New client on socket %d\n", csock);
  2381. dcl->idle = 0;
  2382. qemu_set_nonblock(vs->csock);
  2383. #ifdef CONFIG_VNC_WS
  2384. if (websocket) {
  2385. vs->websocket = 1;
  2386. qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read, NULL, vs);
  2387. } else
  2388. #endif /* CONFIG_VNC_WS */
  2389. {
  2390. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
  2391. }
  2392. vnc_client_cache_addr(vs);
  2393. vnc_qmp_event(vs, QEVENT_VNC_CONNECTED);
  2394. vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
  2395. vs->vd = vd;
  2396. #ifdef CONFIG_VNC_WS
  2397. if (!vs->websocket)
  2398. #endif
  2399. {
  2400. vnc_init_state(vs);
  2401. }
  2402. }
  2403. void vnc_init_state(VncState *vs)
  2404. {
  2405. vs->initialized = true;
  2406. VncDisplay *vd = vs->vd;
  2407. vs->ds = vd->ds;
  2408. vs->last_x = -1;
  2409. vs->last_y = -1;
  2410. vs->as.freq = 44100;
  2411. vs->as.nchannels = 2;
  2412. vs->as.fmt = AUD_FMT_S16;
  2413. vs->as.endianness = 0;
  2414. qemu_mutex_init(&vs->output_mutex);
  2415. vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
  2416. QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
  2417. vga_hw_update();
  2418. vnc_write(vs, "RFB 003.008\n", 12);
  2419. vnc_flush(vs);
  2420. vnc_read_when(vs, protocol_version, 12);
  2421. reset_keys(vs);
  2422. if (vs->vd->lock_key_sync)
  2423. vs->led = qemu_add_led_event_handler(kbd_leds, vs);
  2424. vs->mouse_mode_notifier.notify = check_pointer_type_change;
  2425. qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
  2426. vnc_init_timer(vd);
  2427. /* vs might be free()ed here */
  2428. }
  2429. static void vnc_listen_read(void *opaque, bool websocket)
  2430. {
  2431. VncDisplay *vs = opaque;
  2432. struct sockaddr_in addr;
  2433. socklen_t addrlen = sizeof(addr);
  2434. int csock;
  2435. /* Catch-up */
  2436. vga_hw_update();
  2437. #ifdef CONFIG_VNC_WS
  2438. if (websocket) {
  2439. csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
  2440. } else
  2441. #endif /* CONFIG_VNC_WS */
  2442. {
  2443. csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
  2444. }
  2445. if (csock != -1) {
  2446. vnc_connect(vs, csock, 0, websocket);
  2447. }
  2448. }
  2449. static void vnc_listen_regular_read(void *opaque)
  2450. {
  2451. vnc_listen_read(opaque, 0);
  2452. }
  2453. #ifdef CONFIG_VNC_WS
  2454. static void vnc_listen_websocket_read(void *opaque)
  2455. {
  2456. vnc_listen_read(opaque, 1);
  2457. }
  2458. #endif /* CONFIG_VNC_WS */
  2459. void vnc_display_init(DisplayState *ds)
  2460. {
  2461. VncDisplay *vs = g_malloc0(sizeof(*vs));
  2462. dcl = g_malloc0(sizeof(DisplayChangeListener));
  2463. ds->opaque = vs;
  2464. dcl->idle = 1;
  2465. vnc_display = vs;
  2466. vs->lsock = -1;
  2467. #ifdef CONFIG_VNC_WS
  2468. vs->lwebsock = -1;
  2469. #endif
  2470. vs->ds = ds;
  2471. QTAILQ_INIT(&vs->clients);
  2472. vs->expires = TIME_MAX;
  2473. if (keyboard_layout)
  2474. vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
  2475. else
  2476. vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
  2477. if (!vs->kbd_layout)
  2478. exit(1);
  2479. qemu_mutex_init(&vs->mutex);
  2480. vnc_start_worker_thread();
  2481. dcl->dpy_gfx_copy = vnc_dpy_copy;
  2482. dcl->dpy_gfx_update = vnc_dpy_update;
  2483. dcl->dpy_gfx_resize = vnc_dpy_resize;
  2484. dcl->dpy_gfx_setdata = vnc_dpy_setdata;
  2485. dcl->dpy_mouse_set = vnc_mouse_set;
  2486. dcl->dpy_cursor_define = vnc_dpy_cursor_define;
  2487. register_displaychangelistener(ds, dcl);
  2488. }
  2489. static void vnc_display_close(DisplayState *ds)
  2490. {
  2491. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2492. if (!vs)
  2493. return;
  2494. if (vs->display) {
  2495. g_free(vs->display);
  2496. vs->display = NULL;
  2497. }
  2498. if (vs->lsock != -1) {
  2499. qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
  2500. close(vs->lsock);
  2501. vs->lsock = -1;
  2502. }
  2503. #ifdef CONFIG_VNC_WS
  2504. g_free(vs->ws_display);
  2505. vs->ws_display = NULL;
  2506. if (vs->lwebsock != -1) {
  2507. qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL);
  2508. close(vs->lwebsock);
  2509. vs->lwebsock = -1;
  2510. }
  2511. #endif /* CONFIG_VNC_WS */
  2512. vs->auth = VNC_AUTH_INVALID;
  2513. #ifdef CONFIG_VNC_TLS
  2514. vs->subauth = VNC_AUTH_INVALID;
  2515. vs->tls.x509verify = 0;
  2516. #endif
  2517. }
  2518. static int vnc_display_disable_login(DisplayState *ds)
  2519. {
  2520. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2521. if (!vs) {
  2522. return -1;
  2523. }
  2524. if (vs->password) {
  2525. g_free(vs->password);
  2526. }
  2527. vs->password = NULL;
  2528. if (vs->auth == VNC_AUTH_NONE) {
  2529. vs->auth = VNC_AUTH_VNC;
  2530. }
  2531. return 0;
  2532. }
  2533. int vnc_display_password(DisplayState *ds, const char *password)
  2534. {
  2535. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2536. if (!vs) {
  2537. return -EINVAL;
  2538. }
  2539. if (!password) {
  2540. /* This is not the intention of this interface but err on the side
  2541. of being safe */
  2542. return vnc_display_disable_login(ds);
  2543. }
  2544. if (vs->password) {
  2545. g_free(vs->password);
  2546. vs->password = NULL;
  2547. }
  2548. vs->password = g_strdup(password);
  2549. if (vs->auth == VNC_AUTH_NONE) {
  2550. vs->auth = VNC_AUTH_VNC;
  2551. }
  2552. return 0;
  2553. }
  2554. int vnc_display_pw_expire(DisplayState *ds, time_t expires)
  2555. {
  2556. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2557. if (!vs) {
  2558. return -EINVAL;
  2559. }
  2560. vs->expires = expires;
  2561. return 0;
  2562. }
  2563. char *vnc_display_local_addr(DisplayState *ds)
  2564. {
  2565. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2566. return vnc_socket_local_addr("%s:%s", vs->lsock);
  2567. }
  2568. void vnc_display_open(DisplayState *ds, const char *display, Error **errp)
  2569. {
  2570. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2571. const char *options;
  2572. int password = 0;
  2573. int reverse = 0;
  2574. #ifdef CONFIG_VNC_TLS
  2575. int tls = 0, x509 = 0;
  2576. #endif
  2577. #ifdef CONFIG_VNC_SASL
  2578. int sasl = 0;
  2579. int saslErr;
  2580. #endif
  2581. #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
  2582. int acl = 0;
  2583. #endif
  2584. int lock_key_sync = 1;
  2585. if (!vnc_display) {
  2586. error_setg(errp, "VNC display not active");
  2587. return;
  2588. }
  2589. vnc_display_close(ds);
  2590. if (strcmp(display, "none") == 0)
  2591. return;
  2592. vs->display = g_strdup(display);
  2593. vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
  2594. options = display;
  2595. while ((options = strchr(options, ','))) {
  2596. options++;
  2597. if (strncmp(options, "password", 8) == 0) {
  2598. if (fips_get_state()) {
  2599. error_setg(errp,
  2600. "VNC password auth disabled due to FIPS mode, "
  2601. "consider using the VeNCrypt or SASL authentication "
  2602. "methods as an alternative");
  2603. goto fail;
  2604. }
  2605. password = 1; /* Require password auth */
  2606. } else if (strncmp(options, "reverse", 7) == 0) {
  2607. reverse = 1;
  2608. } else if (strncmp(options, "no-lock-key-sync", 16) == 0) {
  2609. lock_key_sync = 0;
  2610. #ifdef CONFIG_VNC_SASL
  2611. } else if (strncmp(options, "sasl", 4) == 0) {
  2612. sasl = 1; /* Require SASL auth */
  2613. #endif
  2614. #ifdef CONFIG_VNC_WS
  2615. } else if (strncmp(options, "websocket", 9) == 0) {
  2616. char *start, *end;
  2617. vs->websocket = 1;
  2618. /* Check for 'websocket=<port>' */
  2619. start = strchr(options, '=');
  2620. end = strchr(options, ',');
  2621. if (start && (!end || (start < end))) {
  2622. int len = end ? end-(start+1) : strlen(start+1);
  2623. if (len < 6) {
  2624. /* extract the host specification from display */
  2625. char *host = NULL, *port = NULL, *host_end = NULL;
  2626. port = g_strndup(start + 1, len);
  2627. /* ipv6 hosts have colons */
  2628. end = strchr(display, ',');
  2629. host_end = g_strrstr_len(display, end - display, ":");
  2630. if (host_end) {
  2631. host = g_strndup(display, host_end - display + 1);
  2632. } else {
  2633. host = g_strndup(":", 1);
  2634. }
  2635. vs->ws_display = g_strconcat(host, port, NULL);
  2636. g_free(host);
  2637. g_free(port);
  2638. }
  2639. }
  2640. #endif /* CONFIG_VNC_WS */
  2641. #ifdef CONFIG_VNC_TLS
  2642. } else if (strncmp(options, "tls", 3) == 0) {
  2643. tls = 1; /* Require TLS */
  2644. } else if (strncmp(options, "x509", 4) == 0) {
  2645. char *start, *end;
  2646. x509 = 1; /* Require x509 certificates */
  2647. if (strncmp(options, "x509verify", 10) == 0)
  2648. vs->tls.x509verify = 1; /* ...and verify client certs */
  2649. /* Now check for 'x509=/some/path' postfix
  2650. * and use that to setup x509 certificate/key paths */
  2651. start = strchr(options, '=');
  2652. end = strchr(options, ',');
  2653. if (start && (!end || (start < end))) {
  2654. int len = end ? end-(start+1) : strlen(start+1);
  2655. char *path = g_strndup(start + 1, len);
  2656. VNC_DEBUG("Trying certificate path '%s'\n", path);
  2657. if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
  2658. error_setg(errp, "Failed to find x509 certificates/keys in %s", path);
  2659. g_free(path);
  2660. goto fail;
  2661. }
  2662. g_free(path);
  2663. } else {
  2664. error_setg(errp, "No certificate path provided");
  2665. goto fail;
  2666. }
  2667. #endif
  2668. #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
  2669. } else if (strncmp(options, "acl", 3) == 0) {
  2670. acl = 1;
  2671. #endif
  2672. } else if (strncmp(options, "lossy", 5) == 0) {
  2673. vs->lossy = true;
  2674. } else if (strncmp(options, "non-adaptive", 12) == 0) {
  2675. vs->non_adaptive = true;
  2676. } else if (strncmp(options, "share=", 6) == 0) {
  2677. if (strncmp(options+6, "ignore", 6) == 0) {
  2678. vs->share_policy = VNC_SHARE_POLICY_IGNORE;
  2679. } else if (strncmp(options+6, "allow-exclusive", 15) == 0) {
  2680. vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
  2681. } else if (strncmp(options+6, "force-shared", 12) == 0) {
  2682. vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
  2683. } else {
  2684. error_setg(errp, "unknown vnc share= option");
  2685. goto fail;
  2686. }
  2687. }
  2688. }
  2689. #ifdef CONFIG_VNC_TLS
  2690. if (acl && x509 && vs->tls.x509verify) {
  2691. if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
  2692. fprintf(stderr, "Failed to create x509 dname ACL\n");
  2693. exit(1);
  2694. }
  2695. }
  2696. #endif
  2697. #ifdef CONFIG_VNC_SASL
  2698. if (acl && sasl) {
  2699. if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
  2700. fprintf(stderr, "Failed to create username ACL\n");
  2701. exit(1);
  2702. }
  2703. }
  2704. #endif
  2705. /*
  2706. * Combinations we support here:
  2707. *
  2708. * - no-auth (clear text, no auth)
  2709. * - password (clear text, weak auth)
  2710. * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
  2711. * - tls (encrypt, weak anonymous creds, no auth)
  2712. * - tls + password (encrypt, weak anonymous creds, weak auth)
  2713. * - tls + sasl (encrypt, weak anonymous creds, good auth)
  2714. * - tls + x509 (encrypt, good x509 creds, no auth)
  2715. * - tls + x509 + password (encrypt, good x509 creds, weak auth)
  2716. * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
  2717. *
  2718. * NB1. TLS is a stackable auth scheme.
  2719. * NB2. the x509 schemes have option to validate a client cert dname
  2720. */
  2721. if (password) {
  2722. #ifdef CONFIG_VNC_TLS
  2723. if (tls) {
  2724. vs->auth = VNC_AUTH_VENCRYPT;
  2725. if (x509) {
  2726. VNC_DEBUG("Initializing VNC server with x509 password auth\n");
  2727. vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
  2728. } else {
  2729. VNC_DEBUG("Initializing VNC server with TLS password auth\n");
  2730. vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
  2731. }
  2732. } else {
  2733. #endif /* CONFIG_VNC_TLS */
  2734. VNC_DEBUG("Initializing VNC server with password auth\n");
  2735. vs->auth = VNC_AUTH_VNC;
  2736. #ifdef CONFIG_VNC_TLS
  2737. vs->subauth = VNC_AUTH_INVALID;
  2738. }
  2739. #endif /* CONFIG_VNC_TLS */
  2740. #ifdef CONFIG_VNC_SASL
  2741. } else if (sasl) {
  2742. #ifdef CONFIG_VNC_TLS
  2743. if (tls) {
  2744. vs->auth = VNC_AUTH_VENCRYPT;
  2745. if (x509) {
  2746. VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
  2747. vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
  2748. } else {
  2749. VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
  2750. vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
  2751. }
  2752. } else {
  2753. #endif /* CONFIG_VNC_TLS */
  2754. VNC_DEBUG("Initializing VNC server with SASL auth\n");
  2755. vs->auth = VNC_AUTH_SASL;
  2756. #ifdef CONFIG_VNC_TLS
  2757. vs->subauth = VNC_AUTH_INVALID;
  2758. }
  2759. #endif /* CONFIG_VNC_TLS */
  2760. #endif /* CONFIG_VNC_SASL */
  2761. } else {
  2762. #ifdef CONFIG_VNC_TLS
  2763. if (tls) {
  2764. vs->auth = VNC_AUTH_VENCRYPT;
  2765. if (x509) {
  2766. VNC_DEBUG("Initializing VNC server with x509 no auth\n");
  2767. vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
  2768. } else {
  2769. VNC_DEBUG("Initializing VNC server with TLS no auth\n");
  2770. vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
  2771. }
  2772. } else {
  2773. #endif
  2774. VNC_DEBUG("Initializing VNC server with no auth\n");
  2775. vs->auth = VNC_AUTH_NONE;
  2776. #ifdef CONFIG_VNC_TLS
  2777. vs->subauth = VNC_AUTH_INVALID;
  2778. }
  2779. #endif
  2780. }
  2781. #ifdef CONFIG_VNC_SASL
  2782. if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
  2783. error_setg(errp, "Failed to initialize SASL auth: %s",
  2784. sasl_errstring(saslErr, NULL, NULL));
  2785. goto fail;
  2786. }
  2787. #endif
  2788. vs->lock_key_sync = lock_key_sync;
  2789. if (reverse) {
  2790. /* connect to viewer */
  2791. int csock;
  2792. vs->lsock = -1;
  2793. #ifdef CONFIG_VNC_WS
  2794. vs->lwebsock = -1;
  2795. #endif
  2796. if (strncmp(display, "unix:", 5) == 0) {
  2797. csock = unix_connect(display+5, errp);
  2798. } else {
  2799. csock = inet_connect(display, errp);
  2800. }
  2801. if (csock < 0) {
  2802. goto fail;
  2803. }
  2804. vnc_connect(vs, csock, 0, 0);
  2805. } else {
  2806. /* listen for connects */
  2807. char *dpy;
  2808. dpy = g_malloc(256);
  2809. if (strncmp(display, "unix:", 5) == 0) {
  2810. pstrcpy(dpy, 256, "unix:");
  2811. vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
  2812. } else {
  2813. vs->lsock = inet_listen(display, dpy, 256,
  2814. SOCK_STREAM, 5900, errp);
  2815. if (vs->lsock < 0) {
  2816. g_free(dpy);
  2817. goto fail;
  2818. }
  2819. #ifdef CONFIG_VNC_WS
  2820. if (vs->websocket) {
  2821. if (vs->ws_display) {
  2822. vs->lwebsock = inet_listen(vs->ws_display, NULL, 256,
  2823. SOCK_STREAM, 0, errp);
  2824. } else {
  2825. vs->lwebsock = inet_listen(vs->display, NULL, 256,
  2826. SOCK_STREAM, 5700, errp);
  2827. }
  2828. if (vs->lwebsock < 0) {
  2829. if (vs->lsock) {
  2830. close(vs->lsock);
  2831. vs->lsock = -1;
  2832. }
  2833. g_free(dpy);
  2834. goto fail;
  2835. }
  2836. }
  2837. #endif /* CONFIG_VNC_WS */
  2838. }
  2839. g_free(vs->display);
  2840. vs->display = dpy;
  2841. qemu_set_fd_handler2(vs->lsock, NULL,
  2842. vnc_listen_regular_read, NULL, vs);
  2843. #ifdef CONFIG_VNC_WS
  2844. if (vs->websocket) {
  2845. qemu_set_fd_handler2(vs->lwebsock, NULL,
  2846. vnc_listen_websocket_read, NULL, vs);
  2847. }
  2848. #endif /* CONFIG_VNC_WS */
  2849. }
  2850. return;
  2851. fail:
  2852. g_free(vs->display);
  2853. vs->display = NULL;
  2854. #ifdef CONFIG_VNC_WS
  2855. g_free(vs->ws_display);
  2856. vs->ws_display = NULL;
  2857. #endif /* CONFIG_VNC_WS */
  2858. }
  2859. void vnc_display_add_client(DisplayState *ds, int csock, int skipauth)
  2860. {
  2861. VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
  2862. vnc_connect(vs, csock, skipauth, 0);
  2863. }