vnc.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136
  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 "qemu/osdep.h"
  27. #include "vnc.h"
  28. #include "vnc-jobs.h"
  29. #include "trace.h"
  30. #include "hw/qdev-core.h"
  31. #include "sysemu/sysemu.h"
  32. #include "qemu/error-report.h"
  33. #include "qemu/main-loop.h"
  34. #include "qemu/module.h"
  35. #include "qemu/option.h"
  36. #include "qemu/sockets.h"
  37. #include "qemu/timer.h"
  38. #include "authz/list.h"
  39. #include "qemu/config-file.h"
  40. #include "qapi/qapi-emit-events.h"
  41. #include "qapi/qapi-events-ui.h"
  42. #include "qapi/error.h"
  43. #include "qapi/qapi-commands-ui.h"
  44. #include "ui/input.h"
  45. #include "crypto/hash.h"
  46. #include "crypto/tlscredsanon.h"
  47. #include "crypto/tlscredsx509.h"
  48. #include "crypto/random.h"
  49. #include "qom/object_interfaces.h"
  50. #include "qemu/cutils.h"
  51. #include "io/dns-resolver.h"
  52. #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
  53. #define VNC_REFRESH_INTERVAL_INC 50
  54. #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
  55. static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
  56. static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
  57. #include "vnc_keysym.h"
  58. #include "crypto/cipher.h"
  59. static QTAILQ_HEAD(, VncDisplay) vnc_displays =
  60. QTAILQ_HEAD_INITIALIZER(vnc_displays);
  61. static int vnc_cursor_define(VncState *vs);
  62. static void vnc_update_throttle_offset(VncState *vs);
  63. static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
  64. {
  65. #ifdef _VNC_DEBUG
  66. static const char *mn[] = {
  67. [0] = "undefined",
  68. [VNC_SHARE_MODE_CONNECTING] = "connecting",
  69. [VNC_SHARE_MODE_SHARED] = "shared",
  70. [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive",
  71. [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
  72. };
  73. fprintf(stderr, "%s/%p: %s -> %s\n", __func__,
  74. vs->ioc, mn[vs->share_mode], mn[mode]);
  75. #endif
  76. switch (vs->share_mode) {
  77. case VNC_SHARE_MODE_CONNECTING:
  78. vs->vd->num_connecting--;
  79. break;
  80. case VNC_SHARE_MODE_SHARED:
  81. vs->vd->num_shared--;
  82. break;
  83. case VNC_SHARE_MODE_EXCLUSIVE:
  84. vs->vd->num_exclusive--;
  85. break;
  86. default:
  87. break;
  88. }
  89. vs->share_mode = mode;
  90. switch (vs->share_mode) {
  91. case VNC_SHARE_MODE_CONNECTING:
  92. vs->vd->num_connecting++;
  93. break;
  94. case VNC_SHARE_MODE_SHARED:
  95. vs->vd->num_shared++;
  96. break;
  97. case VNC_SHARE_MODE_EXCLUSIVE:
  98. vs->vd->num_exclusive++;
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. static void vnc_init_basic_info(SocketAddress *addr,
  105. VncBasicInfo *info,
  106. Error **errp)
  107. {
  108. switch (addr->type) {
  109. case SOCKET_ADDRESS_TYPE_INET:
  110. info->host = g_strdup(addr->u.inet.host);
  111. info->service = g_strdup(addr->u.inet.port);
  112. if (addr->u.inet.ipv6) {
  113. info->family = NETWORK_ADDRESS_FAMILY_IPV6;
  114. } else {
  115. info->family = NETWORK_ADDRESS_FAMILY_IPV4;
  116. }
  117. break;
  118. case SOCKET_ADDRESS_TYPE_UNIX:
  119. info->host = g_strdup("");
  120. info->service = g_strdup(addr->u.q_unix.path);
  121. info->family = NETWORK_ADDRESS_FAMILY_UNIX;
  122. break;
  123. case SOCKET_ADDRESS_TYPE_VSOCK:
  124. case SOCKET_ADDRESS_TYPE_FD:
  125. error_setg(errp, "Unsupported socket address type %s",
  126. SocketAddressType_str(addr->type));
  127. break;
  128. default:
  129. abort();
  130. }
  131. return;
  132. }
  133. static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc,
  134. VncBasicInfo *info,
  135. Error **errp)
  136. {
  137. SocketAddress *addr = NULL;
  138. if (!ioc) {
  139. error_setg(errp, "No listener socket available");
  140. return;
  141. }
  142. addr = qio_channel_socket_get_local_address(ioc, errp);
  143. if (!addr) {
  144. return;
  145. }
  146. vnc_init_basic_info(addr, info, errp);
  147. qapi_free_SocketAddress(addr);
  148. }
  149. static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc,
  150. VncBasicInfo *info,
  151. Error **errp)
  152. {
  153. SocketAddress *addr = NULL;
  154. addr = qio_channel_socket_get_remote_address(ioc, errp);
  155. if (!addr) {
  156. return;
  157. }
  158. vnc_init_basic_info(addr, info, errp);
  159. qapi_free_SocketAddress(addr);
  160. }
  161. static const char *vnc_auth_name(VncDisplay *vd) {
  162. switch (vd->auth) {
  163. case VNC_AUTH_INVALID:
  164. return "invalid";
  165. case VNC_AUTH_NONE:
  166. return "none";
  167. case VNC_AUTH_VNC:
  168. return "vnc";
  169. case VNC_AUTH_RA2:
  170. return "ra2";
  171. case VNC_AUTH_RA2NE:
  172. return "ra2ne";
  173. case VNC_AUTH_TIGHT:
  174. return "tight";
  175. case VNC_AUTH_ULTRA:
  176. return "ultra";
  177. case VNC_AUTH_TLS:
  178. return "tls";
  179. case VNC_AUTH_VENCRYPT:
  180. switch (vd->subauth) {
  181. case VNC_AUTH_VENCRYPT_PLAIN:
  182. return "vencrypt+plain";
  183. case VNC_AUTH_VENCRYPT_TLSNONE:
  184. return "vencrypt+tls+none";
  185. case VNC_AUTH_VENCRYPT_TLSVNC:
  186. return "vencrypt+tls+vnc";
  187. case VNC_AUTH_VENCRYPT_TLSPLAIN:
  188. return "vencrypt+tls+plain";
  189. case VNC_AUTH_VENCRYPT_X509NONE:
  190. return "vencrypt+x509+none";
  191. case VNC_AUTH_VENCRYPT_X509VNC:
  192. return "vencrypt+x509+vnc";
  193. case VNC_AUTH_VENCRYPT_X509PLAIN:
  194. return "vencrypt+x509+plain";
  195. case VNC_AUTH_VENCRYPT_TLSSASL:
  196. return "vencrypt+tls+sasl";
  197. case VNC_AUTH_VENCRYPT_X509SASL:
  198. return "vencrypt+x509+sasl";
  199. default:
  200. return "vencrypt";
  201. }
  202. case VNC_AUTH_SASL:
  203. return "sasl";
  204. }
  205. return "unknown";
  206. }
  207. static VncServerInfo *vnc_server_info_get(VncDisplay *vd)
  208. {
  209. VncServerInfo *info;
  210. Error *err = NULL;
  211. if (!vd->listener || !vd->listener->nsioc) {
  212. return NULL;
  213. }
  214. info = g_malloc0(sizeof(*info));
  215. vnc_init_basic_info_from_server_addr(vd->listener->sioc[0],
  216. qapi_VncServerInfo_base(info), &err);
  217. info->has_auth = true;
  218. info->auth = g_strdup(vnc_auth_name(vd));
  219. if (err) {
  220. qapi_free_VncServerInfo(info);
  221. info = NULL;
  222. error_free(err);
  223. }
  224. return info;
  225. }
  226. static void vnc_client_cache_auth(VncState *client)
  227. {
  228. if (!client->info) {
  229. return;
  230. }
  231. if (client->tls) {
  232. client->info->x509_dname =
  233. qcrypto_tls_session_get_peer_name(client->tls);
  234. client->info->has_x509_dname =
  235. client->info->x509_dname != NULL;
  236. }
  237. #ifdef CONFIG_VNC_SASL
  238. if (client->sasl.conn &&
  239. client->sasl.username) {
  240. client->info->has_sasl_username = true;
  241. client->info->sasl_username = g_strdup(client->sasl.username);
  242. }
  243. #endif
  244. }
  245. static void vnc_client_cache_addr(VncState *client)
  246. {
  247. Error *err = NULL;
  248. client->info = g_malloc0(sizeof(*client->info));
  249. vnc_init_basic_info_from_remote_addr(client->sioc,
  250. qapi_VncClientInfo_base(client->info),
  251. &err);
  252. client->info->websocket = client->websocket;
  253. if (err) {
  254. qapi_free_VncClientInfo(client->info);
  255. client->info = NULL;
  256. error_free(err);
  257. }
  258. }
  259. static void vnc_qmp_event(VncState *vs, QAPIEvent event)
  260. {
  261. VncServerInfo *si;
  262. if (!vs->info) {
  263. return;
  264. }
  265. si = vnc_server_info_get(vs->vd);
  266. if (!si) {
  267. return;
  268. }
  269. switch (event) {
  270. case QAPI_EVENT_VNC_CONNECTED:
  271. qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info));
  272. break;
  273. case QAPI_EVENT_VNC_INITIALIZED:
  274. qapi_event_send_vnc_initialized(si, vs->info);
  275. break;
  276. case QAPI_EVENT_VNC_DISCONNECTED:
  277. qapi_event_send_vnc_disconnected(si, vs->info);
  278. break;
  279. default:
  280. break;
  281. }
  282. qapi_free_VncServerInfo(si);
  283. }
  284. static VncClientInfo *qmp_query_vnc_client(const VncState *client)
  285. {
  286. VncClientInfo *info;
  287. Error *err = NULL;
  288. info = g_malloc0(sizeof(*info));
  289. vnc_init_basic_info_from_remote_addr(client->sioc,
  290. qapi_VncClientInfo_base(info),
  291. &err);
  292. if (err) {
  293. error_free(err);
  294. qapi_free_VncClientInfo(info);
  295. return NULL;
  296. }
  297. info->websocket = client->websocket;
  298. if (client->tls) {
  299. info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls);
  300. info->has_x509_dname = info->x509_dname != NULL;
  301. }
  302. #ifdef CONFIG_VNC_SASL
  303. if (client->sasl.conn && client->sasl.username) {
  304. info->has_sasl_username = true;
  305. info->sasl_username = g_strdup(client->sasl.username);
  306. }
  307. #endif
  308. return info;
  309. }
  310. static VncDisplay *vnc_display_find(const char *id)
  311. {
  312. VncDisplay *vd;
  313. if (id == NULL) {
  314. return QTAILQ_FIRST(&vnc_displays);
  315. }
  316. QTAILQ_FOREACH(vd, &vnc_displays, next) {
  317. if (strcmp(id, vd->id) == 0) {
  318. return vd;
  319. }
  320. }
  321. return NULL;
  322. }
  323. static VncClientInfoList *qmp_query_client_list(VncDisplay *vd)
  324. {
  325. VncClientInfoList *cinfo, *prev = NULL;
  326. VncState *client;
  327. QTAILQ_FOREACH(client, &vd->clients, next) {
  328. cinfo = g_new0(VncClientInfoList, 1);
  329. cinfo->value = qmp_query_vnc_client(client);
  330. cinfo->next = prev;
  331. prev = cinfo;
  332. }
  333. return prev;
  334. }
  335. VncInfo *qmp_query_vnc(Error **errp)
  336. {
  337. VncInfo *info = g_malloc0(sizeof(*info));
  338. VncDisplay *vd = vnc_display_find(NULL);
  339. SocketAddress *addr = NULL;
  340. if (vd == NULL || !vd->listener || !vd->listener->nsioc) {
  341. info->enabled = false;
  342. } else {
  343. info->enabled = true;
  344. /* for compatibility with the original command */
  345. info->has_clients = true;
  346. info->clients = qmp_query_client_list(vd);
  347. addr = qio_channel_socket_get_local_address(vd->listener->sioc[0],
  348. errp);
  349. if (!addr) {
  350. goto out_error;
  351. }
  352. switch (addr->type) {
  353. case SOCKET_ADDRESS_TYPE_INET:
  354. info->host = g_strdup(addr->u.inet.host);
  355. info->service = g_strdup(addr->u.inet.port);
  356. if (addr->u.inet.ipv6) {
  357. info->family = NETWORK_ADDRESS_FAMILY_IPV6;
  358. } else {
  359. info->family = NETWORK_ADDRESS_FAMILY_IPV4;
  360. }
  361. break;
  362. case SOCKET_ADDRESS_TYPE_UNIX:
  363. info->host = g_strdup("");
  364. info->service = g_strdup(addr->u.q_unix.path);
  365. info->family = NETWORK_ADDRESS_FAMILY_UNIX;
  366. break;
  367. case SOCKET_ADDRESS_TYPE_VSOCK:
  368. case SOCKET_ADDRESS_TYPE_FD:
  369. error_setg(errp, "Unsupported socket address type %s",
  370. SocketAddressType_str(addr->type));
  371. goto out_error;
  372. default:
  373. abort();
  374. }
  375. info->has_host = true;
  376. info->has_service = true;
  377. info->has_family = true;
  378. info->has_auth = true;
  379. info->auth = g_strdup(vnc_auth_name(vd));
  380. }
  381. qapi_free_SocketAddress(addr);
  382. return info;
  383. out_error:
  384. qapi_free_SocketAddress(addr);
  385. qapi_free_VncInfo(info);
  386. return NULL;
  387. }
  388. static void qmp_query_auth(int auth, int subauth,
  389. VncPrimaryAuth *qmp_auth,
  390. VncVencryptSubAuth *qmp_vencrypt,
  391. bool *qmp_has_vencrypt);
  392. static VncServerInfo2List *qmp_query_server_entry(QIOChannelSocket *ioc,
  393. bool websocket,
  394. int auth,
  395. int subauth,
  396. VncServerInfo2List *prev)
  397. {
  398. VncServerInfo2List *list;
  399. VncServerInfo2 *info;
  400. Error *err = NULL;
  401. SocketAddress *addr;
  402. addr = qio_channel_socket_get_local_address(ioc, &err);
  403. if (!addr) {
  404. error_free(err);
  405. return prev;
  406. }
  407. info = g_new0(VncServerInfo2, 1);
  408. vnc_init_basic_info(addr, qapi_VncServerInfo2_base(info), &err);
  409. qapi_free_SocketAddress(addr);
  410. if (err) {
  411. qapi_free_VncServerInfo2(info);
  412. error_free(err);
  413. return prev;
  414. }
  415. info->websocket = websocket;
  416. qmp_query_auth(auth, subauth, &info->auth,
  417. &info->vencrypt, &info->has_vencrypt);
  418. list = g_new0(VncServerInfo2List, 1);
  419. list->value = info;
  420. list->next = prev;
  421. return list;
  422. }
  423. static void qmp_query_auth(int auth, int subauth,
  424. VncPrimaryAuth *qmp_auth,
  425. VncVencryptSubAuth *qmp_vencrypt,
  426. bool *qmp_has_vencrypt)
  427. {
  428. switch (auth) {
  429. case VNC_AUTH_VNC:
  430. *qmp_auth = VNC_PRIMARY_AUTH_VNC;
  431. break;
  432. case VNC_AUTH_RA2:
  433. *qmp_auth = VNC_PRIMARY_AUTH_RA2;
  434. break;
  435. case VNC_AUTH_RA2NE:
  436. *qmp_auth = VNC_PRIMARY_AUTH_RA2NE;
  437. break;
  438. case VNC_AUTH_TIGHT:
  439. *qmp_auth = VNC_PRIMARY_AUTH_TIGHT;
  440. break;
  441. case VNC_AUTH_ULTRA:
  442. *qmp_auth = VNC_PRIMARY_AUTH_ULTRA;
  443. break;
  444. case VNC_AUTH_TLS:
  445. *qmp_auth = VNC_PRIMARY_AUTH_TLS;
  446. break;
  447. case VNC_AUTH_VENCRYPT:
  448. *qmp_auth = VNC_PRIMARY_AUTH_VENCRYPT;
  449. *qmp_has_vencrypt = true;
  450. switch (subauth) {
  451. case VNC_AUTH_VENCRYPT_PLAIN:
  452. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN;
  453. break;
  454. case VNC_AUTH_VENCRYPT_TLSNONE:
  455. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE;
  456. break;
  457. case VNC_AUTH_VENCRYPT_TLSVNC:
  458. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC;
  459. break;
  460. case VNC_AUTH_VENCRYPT_TLSPLAIN:
  461. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN;
  462. break;
  463. case VNC_AUTH_VENCRYPT_X509NONE:
  464. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE;
  465. break;
  466. case VNC_AUTH_VENCRYPT_X509VNC:
  467. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC;
  468. break;
  469. case VNC_AUTH_VENCRYPT_X509PLAIN:
  470. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN;
  471. break;
  472. case VNC_AUTH_VENCRYPT_TLSSASL:
  473. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL;
  474. break;
  475. case VNC_AUTH_VENCRYPT_X509SASL:
  476. *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL;
  477. break;
  478. default:
  479. *qmp_has_vencrypt = false;
  480. break;
  481. }
  482. break;
  483. case VNC_AUTH_SASL:
  484. *qmp_auth = VNC_PRIMARY_AUTH_SASL;
  485. break;
  486. case VNC_AUTH_NONE:
  487. default:
  488. *qmp_auth = VNC_PRIMARY_AUTH_NONE;
  489. break;
  490. }
  491. }
  492. VncInfo2List *qmp_query_vnc_servers(Error **errp)
  493. {
  494. VncInfo2List *item, *prev = NULL;
  495. VncInfo2 *info;
  496. VncDisplay *vd;
  497. DeviceState *dev;
  498. size_t i;
  499. QTAILQ_FOREACH(vd, &vnc_displays, next) {
  500. info = g_new0(VncInfo2, 1);
  501. info->id = g_strdup(vd->id);
  502. info->clients = qmp_query_client_list(vd);
  503. qmp_query_auth(vd->auth, vd->subauth, &info->auth,
  504. &info->vencrypt, &info->has_vencrypt);
  505. if (vd->dcl.con) {
  506. dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con),
  507. "device", NULL));
  508. info->has_display = true;
  509. info->display = g_strdup(dev->id);
  510. }
  511. for (i = 0; vd->listener != NULL && i < vd->listener->nsioc; i++) {
  512. info->server = qmp_query_server_entry(
  513. vd->listener->sioc[i], false, vd->auth, vd->subauth,
  514. info->server);
  515. }
  516. for (i = 0; vd->wslistener != NULL && i < vd->wslistener->nsioc; i++) {
  517. info->server = qmp_query_server_entry(
  518. vd->wslistener->sioc[i], true, vd->ws_auth,
  519. vd->ws_subauth, info->server);
  520. }
  521. item = g_new0(VncInfo2List, 1);
  522. item->value = info;
  523. item->next = prev;
  524. prev = item;
  525. }
  526. return prev;
  527. }
  528. /* TODO
  529. 1) Get the queue working for IO.
  530. 2) there is some weirdness when using the -S option (the screen is grey
  531. and not totally invalidated
  532. 3) resolutions > 1024
  533. */
  534. static int vnc_update_client(VncState *vs, int has_dirty);
  535. static void vnc_disconnect_start(VncState *vs);
  536. static void vnc_colordepth(VncState *vs);
  537. static void framebuffer_update_request(VncState *vs, int incremental,
  538. int x_position, int y_position,
  539. int w, int h);
  540. static void vnc_refresh(DisplayChangeListener *dcl);
  541. static int vnc_refresh_server_surface(VncDisplay *vd);
  542. static int vnc_width(VncDisplay *vd)
  543. {
  544. return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
  545. VNC_DIRTY_PIXELS_PER_BIT));
  546. }
  547. static int vnc_height(VncDisplay *vd)
  548. {
  549. return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds));
  550. }
  551. static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT],
  552. VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT),
  553. VncDisplay *vd,
  554. int x, int y, int w, int h)
  555. {
  556. int width = vnc_width(vd);
  557. int height = vnc_height(vd);
  558. /* this is needed this to ensure we updated all affected
  559. * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
  560. w += (x % VNC_DIRTY_PIXELS_PER_BIT);
  561. x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
  562. x = MIN(x, width);
  563. y = MIN(y, height);
  564. w = MIN(x + w, width) - x;
  565. h = MIN(y + h, height);
  566. for (; y < h; y++) {
  567. bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
  568. DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
  569. }
  570. }
  571. static void vnc_dpy_update(DisplayChangeListener *dcl,
  572. int x, int y, int w, int h)
  573. {
  574. VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
  575. struct VncSurface *s = &vd->guest;
  576. vnc_set_area_dirty(s->dirty, vd, x, y, w, h);
  577. }
  578. void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
  579. int32_t encoding)
  580. {
  581. vnc_write_u16(vs, x);
  582. vnc_write_u16(vs, y);
  583. vnc_write_u16(vs, w);
  584. vnc_write_u16(vs, h);
  585. vnc_write_s32(vs, encoding);
  586. }
  587. static void vnc_desktop_resize(VncState *vs)
  588. {
  589. if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
  590. return;
  591. }
  592. if (vs->client_width == pixman_image_get_width(vs->vd->server) &&
  593. vs->client_height == pixman_image_get_height(vs->vd->server)) {
  594. return;
  595. }
  596. assert(pixman_image_get_width(vs->vd->server) < 65536 &&
  597. pixman_image_get_width(vs->vd->server) >= 0);
  598. assert(pixman_image_get_height(vs->vd->server) < 65536 &&
  599. pixman_image_get_height(vs->vd->server) >= 0);
  600. vs->client_width = pixman_image_get_width(vs->vd->server);
  601. vs->client_height = pixman_image_get_height(vs->vd->server);
  602. vnc_lock_output(vs);
  603. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  604. vnc_write_u8(vs, 0);
  605. vnc_write_u16(vs, 1); /* number of rects */
  606. vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
  607. VNC_ENCODING_DESKTOPRESIZE);
  608. vnc_unlock_output(vs);
  609. vnc_flush(vs);
  610. }
  611. static void vnc_abort_display_jobs(VncDisplay *vd)
  612. {
  613. VncState *vs;
  614. QTAILQ_FOREACH(vs, &vd->clients, next) {
  615. vnc_lock_output(vs);
  616. vs->abort = true;
  617. vnc_unlock_output(vs);
  618. }
  619. QTAILQ_FOREACH(vs, &vd->clients, next) {
  620. vnc_jobs_join(vs);
  621. }
  622. QTAILQ_FOREACH(vs, &vd->clients, next) {
  623. vnc_lock_output(vs);
  624. if (vs->update == VNC_STATE_UPDATE_NONE &&
  625. vs->job_update != VNC_STATE_UPDATE_NONE) {
  626. /* job aborted before completion */
  627. vs->update = vs->job_update;
  628. vs->job_update = VNC_STATE_UPDATE_NONE;
  629. }
  630. vs->abort = false;
  631. vnc_unlock_output(vs);
  632. }
  633. }
  634. int vnc_server_fb_stride(VncDisplay *vd)
  635. {
  636. return pixman_image_get_stride(vd->server);
  637. }
  638. void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
  639. {
  640. uint8_t *ptr;
  641. ptr = (uint8_t *)pixman_image_get_data(vd->server);
  642. ptr += y * vnc_server_fb_stride(vd);
  643. ptr += x * VNC_SERVER_FB_BYTES;
  644. return ptr;
  645. }
  646. static void vnc_update_server_surface(VncDisplay *vd)
  647. {
  648. int width, height;
  649. qemu_pixman_image_unref(vd->server);
  650. vd->server = NULL;
  651. if (QTAILQ_EMPTY(&vd->clients)) {
  652. return;
  653. }
  654. width = vnc_width(vd);
  655. height = vnc_height(vd);
  656. vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
  657. width, height,
  658. NULL, 0);
  659. memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
  660. vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0,
  661. width, height);
  662. }
  663. static bool vnc_check_pageflip(DisplaySurface *s1,
  664. DisplaySurface *s2)
  665. {
  666. return (s1 != NULL &&
  667. s2 != NULL &&
  668. surface_width(s1) == surface_width(s2) &&
  669. surface_height(s1) == surface_height(s2) &&
  670. surface_format(s1) == surface_format(s2));
  671. }
  672. static void vnc_dpy_switch(DisplayChangeListener *dcl,
  673. DisplaySurface *surface)
  674. {
  675. static const char placeholder_msg[] =
  676. "Display output is not active.";
  677. static DisplaySurface *placeholder;
  678. VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
  679. bool pageflip = vnc_check_pageflip(vd->ds, surface);
  680. VncState *vs;
  681. if (surface == NULL) {
  682. if (placeholder == NULL) {
  683. placeholder = qemu_create_message_surface(640, 480, placeholder_msg);
  684. }
  685. surface = placeholder;
  686. }
  687. vnc_abort_display_jobs(vd);
  688. vd->ds = surface;
  689. /* guest surface */
  690. qemu_pixman_image_unref(vd->guest.fb);
  691. vd->guest.fb = pixman_image_ref(surface->image);
  692. vd->guest.format = surface->format;
  693. if (pageflip) {
  694. vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0,
  695. surface_width(surface),
  696. surface_height(surface));
  697. return;
  698. }
  699. /* server surface */
  700. vnc_update_server_surface(vd);
  701. QTAILQ_FOREACH(vs, &vd->clients, next) {
  702. vnc_colordepth(vs);
  703. vnc_desktop_resize(vs);
  704. if (vs->vd->cursor) {
  705. vnc_cursor_define(vs);
  706. }
  707. memset(vs->dirty, 0x00, sizeof(vs->dirty));
  708. vnc_set_area_dirty(vs->dirty, vd, 0, 0,
  709. vnc_width(vd),
  710. vnc_height(vd));
  711. vnc_update_throttle_offset(vs);
  712. }
  713. }
  714. /* fastest code */
  715. static void vnc_write_pixels_copy(VncState *vs,
  716. void *pixels, int size)
  717. {
  718. vnc_write(vs, pixels, size);
  719. }
  720. /* slowest but generic code. */
  721. void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
  722. {
  723. uint8_t r, g, b;
  724. #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
  725. r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
  726. g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8;
  727. b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8;
  728. #else
  729. # error need some bits here if you change VNC_SERVER_FB_FORMAT
  730. #endif
  731. v = (r << vs->client_pf.rshift) |
  732. (g << vs->client_pf.gshift) |
  733. (b << vs->client_pf.bshift);
  734. switch (vs->client_pf.bytes_per_pixel) {
  735. case 1:
  736. buf[0] = v;
  737. break;
  738. case 2:
  739. if (vs->client_be) {
  740. buf[0] = v >> 8;
  741. buf[1] = v;
  742. } else {
  743. buf[1] = v >> 8;
  744. buf[0] = v;
  745. }
  746. break;
  747. default:
  748. case 4:
  749. if (vs->client_be) {
  750. buf[0] = v >> 24;
  751. buf[1] = v >> 16;
  752. buf[2] = v >> 8;
  753. buf[3] = v;
  754. } else {
  755. buf[3] = v >> 24;
  756. buf[2] = v >> 16;
  757. buf[1] = v >> 8;
  758. buf[0] = v;
  759. }
  760. break;
  761. }
  762. }
  763. static void vnc_write_pixels_generic(VncState *vs,
  764. void *pixels1, int size)
  765. {
  766. uint8_t buf[4];
  767. if (VNC_SERVER_FB_BYTES == 4) {
  768. uint32_t *pixels = pixels1;
  769. int n, i;
  770. n = size >> 2;
  771. for (i = 0; i < n; i++) {
  772. vnc_convert_pixel(vs, buf, pixels[i]);
  773. vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
  774. }
  775. }
  776. }
  777. int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  778. {
  779. int i;
  780. uint8_t *row;
  781. VncDisplay *vd = vs->vd;
  782. row = vnc_server_fb_ptr(vd, x, y);
  783. for (i = 0; i < h; i++) {
  784. vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
  785. row += vnc_server_fb_stride(vd);
  786. }
  787. return 1;
  788. }
  789. int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  790. {
  791. int n = 0;
  792. bool encode_raw = false;
  793. size_t saved_offs = vs->output.offset;
  794. switch(vs->vnc_encoding) {
  795. case VNC_ENCODING_ZLIB:
  796. n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
  797. break;
  798. case VNC_ENCODING_HEXTILE:
  799. vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
  800. n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
  801. break;
  802. case VNC_ENCODING_TIGHT:
  803. n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
  804. break;
  805. case VNC_ENCODING_TIGHT_PNG:
  806. n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
  807. break;
  808. case VNC_ENCODING_ZRLE:
  809. n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
  810. break;
  811. case VNC_ENCODING_ZYWRLE:
  812. n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
  813. break;
  814. default:
  815. encode_raw = true;
  816. break;
  817. }
  818. /* If the client has the same pixel format as our internal buffer and
  819. * a RAW encoding would need less space fall back to RAW encoding to
  820. * save bandwidth and processing power in the client. */
  821. if (!encode_raw && vs->write_pixels == vnc_write_pixels_copy &&
  822. 12 + h * w * VNC_SERVER_FB_BYTES <= (vs->output.offset - saved_offs)) {
  823. vs->output.offset = saved_offs;
  824. encode_raw = true;
  825. }
  826. if (encode_raw) {
  827. vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
  828. n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
  829. }
  830. return n;
  831. }
  832. static void vnc_mouse_set(DisplayChangeListener *dcl,
  833. int x, int y, int visible)
  834. {
  835. /* can we ask the client(s) to move the pointer ??? */
  836. }
  837. static int vnc_cursor_define(VncState *vs)
  838. {
  839. QEMUCursor *c = vs->vd->cursor;
  840. int isize;
  841. if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
  842. vnc_lock_output(vs);
  843. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  844. vnc_write_u8(vs, 0); /* padding */
  845. vnc_write_u16(vs, 1); /* # of rects */
  846. vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
  847. VNC_ENCODING_RICH_CURSOR);
  848. isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
  849. vnc_write_pixels_generic(vs, c->data, isize);
  850. vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
  851. vnc_unlock_output(vs);
  852. return 0;
  853. }
  854. return -1;
  855. }
  856. static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
  857. QEMUCursor *c)
  858. {
  859. VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
  860. VncState *vs;
  861. cursor_put(vd->cursor);
  862. g_free(vd->cursor_mask);
  863. vd->cursor = c;
  864. cursor_get(vd->cursor);
  865. vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
  866. vd->cursor_mask = g_malloc0(vd->cursor_msize);
  867. cursor_get_mono_mask(c, 0, vd->cursor_mask);
  868. QTAILQ_FOREACH(vs, &vd->clients, next) {
  869. vnc_cursor_define(vs);
  870. }
  871. }
  872. static int find_and_clear_dirty_height(VncState *vs,
  873. int y, int last_x, int x, int height)
  874. {
  875. int h;
  876. for (h = 1; h < (height - y); h++) {
  877. if (!test_bit(last_x, vs->dirty[y + h])) {
  878. break;
  879. }
  880. bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
  881. }
  882. return h;
  883. }
  884. /*
  885. * Figure out how much pending data we should allow in the output
  886. * buffer before we throttle incremental display updates, and/or
  887. * drop audio samples.
  888. *
  889. * We allow for equiv of 1 full display's worth of FB updates,
  890. * and 1 second of audio samples. If audio backlog was larger
  891. * than that the client would already suffering awful audio
  892. * glitches, so dropping samples is no worse really).
  893. */
  894. static void vnc_update_throttle_offset(VncState *vs)
  895. {
  896. size_t offset =
  897. vs->client_width * vs->client_height * vs->client_pf.bytes_per_pixel;
  898. if (vs->audio_cap) {
  899. int bps;
  900. switch (vs->as.fmt) {
  901. default:
  902. case AUDIO_FORMAT_U8:
  903. case AUDIO_FORMAT_S8:
  904. bps = 1;
  905. break;
  906. case AUDIO_FORMAT_U16:
  907. case AUDIO_FORMAT_S16:
  908. bps = 2;
  909. break;
  910. case AUDIO_FORMAT_U32:
  911. case AUDIO_FORMAT_S32:
  912. bps = 4;
  913. break;
  914. }
  915. offset += vs->as.freq * bps * vs->as.nchannels;
  916. }
  917. /* Put a floor of 1MB on offset, so that if we have a large pending
  918. * buffer and the display is resized to a small size & back again
  919. * we don't suddenly apply a tiny send limit
  920. */
  921. offset = MAX(offset, 1024 * 1024);
  922. if (vs->throttle_output_offset != offset) {
  923. trace_vnc_client_throttle_threshold(
  924. vs, vs->ioc, vs->throttle_output_offset, offset, vs->client_width,
  925. vs->client_height, vs->client_pf.bytes_per_pixel, vs->audio_cap);
  926. }
  927. vs->throttle_output_offset = offset;
  928. }
  929. static bool vnc_should_update(VncState *vs)
  930. {
  931. switch (vs->update) {
  932. case VNC_STATE_UPDATE_NONE:
  933. break;
  934. case VNC_STATE_UPDATE_INCREMENTAL:
  935. /* Only allow incremental updates if the pending send queue
  936. * is less than the permitted threshold, and the job worker
  937. * is completely idle.
  938. */
  939. if (vs->output.offset < vs->throttle_output_offset &&
  940. vs->job_update == VNC_STATE_UPDATE_NONE) {
  941. return true;
  942. }
  943. trace_vnc_client_throttle_incremental(
  944. vs, vs->ioc, vs->job_update, vs->output.offset);
  945. break;
  946. case VNC_STATE_UPDATE_FORCE:
  947. /* Only allow forced updates if the pending send queue
  948. * does not contain a previous forced update, and the
  949. * job worker is completely idle.
  950. *
  951. * Note this means we'll queue a forced update, even if
  952. * the output buffer size is otherwise over the throttle
  953. * output limit.
  954. */
  955. if (vs->force_update_offset == 0 &&
  956. vs->job_update == VNC_STATE_UPDATE_NONE) {
  957. return true;
  958. }
  959. trace_vnc_client_throttle_forced(
  960. vs, vs->ioc, vs->job_update, vs->force_update_offset);
  961. break;
  962. }
  963. return false;
  964. }
  965. static int vnc_update_client(VncState *vs, int has_dirty)
  966. {
  967. VncDisplay *vd = vs->vd;
  968. VncJob *job;
  969. int y;
  970. int height, width;
  971. int n = 0;
  972. if (vs->disconnecting) {
  973. vnc_disconnect_finish(vs);
  974. return 0;
  975. }
  976. vs->has_dirty += has_dirty;
  977. if (!vnc_should_update(vs)) {
  978. return 0;
  979. }
  980. if (!vs->has_dirty && vs->update != VNC_STATE_UPDATE_FORCE) {
  981. return 0;
  982. }
  983. /*
  984. * Send screen updates to the vnc client using the server
  985. * surface and server dirty map. guest surface updates
  986. * happening in parallel don't disturb us, the next pass will
  987. * send them to the client.
  988. */
  989. job = vnc_job_new(vs);
  990. height = pixman_image_get_height(vd->server);
  991. width = pixman_image_get_width(vd->server);
  992. y = 0;
  993. for (;;) {
  994. int x, h;
  995. unsigned long x2;
  996. unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
  997. height * VNC_DIRTY_BPL(vs),
  998. y * VNC_DIRTY_BPL(vs));
  999. if (offset == height * VNC_DIRTY_BPL(vs)) {
  1000. /* no more dirty bits */
  1001. break;
  1002. }
  1003. y = offset / VNC_DIRTY_BPL(vs);
  1004. x = offset % VNC_DIRTY_BPL(vs);
  1005. x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
  1006. VNC_DIRTY_BPL(vs), x);
  1007. bitmap_clear(vs->dirty[y], x, x2 - x);
  1008. h = find_and_clear_dirty_height(vs, y, x, x2, height);
  1009. x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
  1010. if (x2 > x) {
  1011. n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
  1012. (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
  1013. }
  1014. if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) {
  1015. y += h;
  1016. if (y == height) {
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. vs->job_update = vs->update;
  1022. vs->update = VNC_STATE_UPDATE_NONE;
  1023. vnc_job_push(job);
  1024. vs->has_dirty = 0;
  1025. return n;
  1026. }
  1027. /* audio */
  1028. static void audio_capture_notify(void *opaque, audcnotification_e cmd)
  1029. {
  1030. VncState *vs = opaque;
  1031. assert(vs->magic == VNC_MAGIC);
  1032. switch (cmd) {
  1033. case AUD_CNOTIFY_DISABLE:
  1034. vnc_lock_output(vs);
  1035. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
  1036. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
  1037. vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
  1038. vnc_unlock_output(vs);
  1039. vnc_flush(vs);
  1040. break;
  1041. case AUD_CNOTIFY_ENABLE:
  1042. vnc_lock_output(vs);
  1043. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
  1044. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
  1045. vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
  1046. vnc_unlock_output(vs);
  1047. vnc_flush(vs);
  1048. break;
  1049. }
  1050. }
  1051. static void audio_capture_destroy(void *opaque)
  1052. {
  1053. }
  1054. static void audio_capture(void *opaque, void *buf, int size)
  1055. {
  1056. VncState *vs = opaque;
  1057. assert(vs->magic == VNC_MAGIC);
  1058. vnc_lock_output(vs);
  1059. if (vs->output.offset < vs->throttle_output_offset) {
  1060. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
  1061. vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
  1062. vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
  1063. vnc_write_u32(vs, size);
  1064. vnc_write(vs, buf, size);
  1065. } else {
  1066. trace_vnc_client_throttle_audio(vs, vs->ioc, vs->output.offset);
  1067. }
  1068. vnc_unlock_output(vs);
  1069. vnc_flush(vs);
  1070. }
  1071. static void audio_add(VncState *vs)
  1072. {
  1073. struct audio_capture_ops ops;
  1074. if (vs->audio_cap) {
  1075. error_report("audio already running");
  1076. return;
  1077. }
  1078. ops.notify = audio_capture_notify;
  1079. ops.destroy = audio_capture_destroy;
  1080. ops.capture = audio_capture;
  1081. vs->audio_cap = AUD_add_capture(vs->vd->audio_state, &vs->as, &ops, vs);
  1082. if (!vs->audio_cap) {
  1083. error_report("Failed to add audio capture");
  1084. }
  1085. }
  1086. static void audio_del(VncState *vs)
  1087. {
  1088. if (vs->audio_cap) {
  1089. AUD_del_capture(vs->audio_cap, vs);
  1090. vs->audio_cap = NULL;
  1091. }
  1092. }
  1093. static void vnc_disconnect_start(VncState *vs)
  1094. {
  1095. if (vs->disconnecting) {
  1096. return;
  1097. }
  1098. trace_vnc_client_disconnect_start(vs, vs->ioc);
  1099. vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
  1100. if (vs->ioc_tag) {
  1101. g_source_remove(vs->ioc_tag);
  1102. vs->ioc_tag = 0;
  1103. }
  1104. qio_channel_close(vs->ioc, NULL);
  1105. vs->disconnecting = TRUE;
  1106. }
  1107. void vnc_disconnect_finish(VncState *vs)
  1108. {
  1109. int i;
  1110. trace_vnc_client_disconnect_finish(vs, vs->ioc);
  1111. vnc_jobs_join(vs); /* Wait encoding jobs */
  1112. vnc_lock_output(vs);
  1113. vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
  1114. buffer_free(&vs->input);
  1115. buffer_free(&vs->output);
  1116. qapi_free_VncClientInfo(vs->info);
  1117. vnc_zlib_clear(vs);
  1118. vnc_tight_clear(vs);
  1119. vnc_zrle_clear(vs);
  1120. #ifdef CONFIG_VNC_SASL
  1121. vnc_sasl_client_cleanup(vs);
  1122. #endif /* CONFIG_VNC_SASL */
  1123. audio_del(vs);
  1124. qkbd_state_lift_all_keys(vs->vd->kbd);
  1125. if (vs->mouse_mode_notifier.notify != NULL) {
  1126. qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
  1127. }
  1128. QTAILQ_REMOVE(&vs->vd->clients, vs, next);
  1129. if (QTAILQ_EMPTY(&vs->vd->clients)) {
  1130. /* last client gone */
  1131. vnc_update_server_surface(vs->vd);
  1132. }
  1133. vnc_unlock_output(vs);
  1134. qemu_mutex_destroy(&vs->output_mutex);
  1135. if (vs->bh != NULL) {
  1136. qemu_bh_delete(vs->bh);
  1137. }
  1138. buffer_free(&vs->jobs_buffer);
  1139. for (i = 0; i < VNC_STAT_ROWS; ++i) {
  1140. g_free(vs->lossy_rect[i]);
  1141. }
  1142. g_free(vs->lossy_rect);
  1143. object_unref(OBJECT(vs->ioc));
  1144. vs->ioc = NULL;
  1145. object_unref(OBJECT(vs->sioc));
  1146. vs->sioc = NULL;
  1147. vs->magic = 0;
  1148. g_free(vs->zrle);
  1149. g_free(vs->tight);
  1150. g_free(vs);
  1151. }
  1152. size_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
  1153. {
  1154. if (ret <= 0) {
  1155. if (ret == 0) {
  1156. trace_vnc_client_eof(vs, vs->ioc);
  1157. vnc_disconnect_start(vs);
  1158. } else if (ret != QIO_CHANNEL_ERR_BLOCK) {
  1159. trace_vnc_client_io_error(vs, vs->ioc,
  1160. errp ? error_get_pretty(*errp) :
  1161. "Unknown");
  1162. vnc_disconnect_start(vs);
  1163. }
  1164. if (errp) {
  1165. error_free(*errp);
  1166. *errp = NULL;
  1167. }
  1168. return 0;
  1169. }
  1170. return ret;
  1171. }
  1172. void vnc_client_error(VncState *vs)
  1173. {
  1174. VNC_DEBUG("Closing down client sock: protocol error\n");
  1175. vnc_disconnect_start(vs);
  1176. }
  1177. /*
  1178. * Called to write a chunk of data to the client socket. The data may
  1179. * be the raw data, or may have already been encoded by SASL.
  1180. * The data will be written either straight onto the socket, or
  1181. * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
  1182. *
  1183. * NB, it is theoretically possible to have 2 layers of encryption,
  1184. * both SASL, and this TLS layer. It is highly unlikely in practice
  1185. * though, since SASL encryption will typically be a no-op if TLS
  1186. * is active
  1187. *
  1188. * Returns the number of bytes written, which may be less than
  1189. * the requested 'datalen' if the socket would block. Returns
  1190. * 0 on I/O error, and disconnects the client socket.
  1191. */
  1192. size_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
  1193. {
  1194. Error *err = NULL;
  1195. ssize_t ret;
  1196. ret = qio_channel_write(
  1197. vs->ioc, (const char *)data, datalen, &err);
  1198. VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
  1199. return vnc_client_io_error(vs, ret, &err);
  1200. }
  1201. /*
  1202. * Called to write buffered data to the client socket, when not
  1203. * using any SASL SSF encryption layers. Will write as much data
  1204. * as possible without blocking. If all buffered data is written,
  1205. * will switch the FD poll() handler back to read monitoring.
  1206. *
  1207. * Returns the number of bytes written, which may be less than
  1208. * the buffered output data if the socket would block. Returns
  1209. * 0 on I/O error, and disconnects the client socket.
  1210. */
  1211. static size_t vnc_client_write_plain(VncState *vs)
  1212. {
  1213. size_t offset;
  1214. size_t ret;
  1215. #ifdef CONFIG_VNC_SASL
  1216. VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
  1217. vs->output.buffer, vs->output.capacity, vs->output.offset,
  1218. vs->sasl.waitWriteSSF);
  1219. if (vs->sasl.conn &&
  1220. vs->sasl.runSSF &&
  1221. vs->sasl.waitWriteSSF) {
  1222. ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
  1223. if (ret)
  1224. vs->sasl.waitWriteSSF -= ret;
  1225. } else
  1226. #endif /* CONFIG_VNC_SASL */
  1227. ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
  1228. if (!ret)
  1229. return 0;
  1230. if (ret >= vs->force_update_offset) {
  1231. if (vs->force_update_offset != 0) {
  1232. trace_vnc_client_unthrottle_forced(vs, vs->ioc);
  1233. }
  1234. vs->force_update_offset = 0;
  1235. } else {
  1236. vs->force_update_offset -= ret;
  1237. }
  1238. offset = vs->output.offset;
  1239. buffer_advance(&vs->output, ret);
  1240. if (offset >= vs->throttle_output_offset &&
  1241. vs->output.offset < vs->throttle_output_offset) {
  1242. trace_vnc_client_unthrottle_incremental(vs, vs->ioc, vs->output.offset);
  1243. }
  1244. if (vs->output.offset == 0) {
  1245. if (vs->ioc_tag) {
  1246. g_source_remove(vs->ioc_tag);
  1247. }
  1248. vs->ioc_tag = qio_channel_add_watch(
  1249. vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
  1250. }
  1251. return ret;
  1252. }
  1253. /*
  1254. * First function called whenever there is data to be written to
  1255. * the client socket. Will delegate actual work according to whether
  1256. * SASL SSF layers are enabled (thus requiring encryption calls)
  1257. */
  1258. static void vnc_client_write_locked(VncState *vs)
  1259. {
  1260. #ifdef CONFIG_VNC_SASL
  1261. if (vs->sasl.conn &&
  1262. vs->sasl.runSSF &&
  1263. !vs->sasl.waitWriteSSF) {
  1264. vnc_client_write_sasl(vs);
  1265. } else
  1266. #endif /* CONFIG_VNC_SASL */
  1267. {
  1268. vnc_client_write_plain(vs);
  1269. }
  1270. }
  1271. static void vnc_client_write(VncState *vs)
  1272. {
  1273. assert(vs->magic == VNC_MAGIC);
  1274. vnc_lock_output(vs);
  1275. if (vs->output.offset) {
  1276. vnc_client_write_locked(vs);
  1277. } else if (vs->ioc != NULL) {
  1278. if (vs->ioc_tag) {
  1279. g_source_remove(vs->ioc_tag);
  1280. }
  1281. vs->ioc_tag = qio_channel_add_watch(
  1282. vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
  1283. }
  1284. vnc_unlock_output(vs);
  1285. }
  1286. void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
  1287. {
  1288. vs->read_handler = func;
  1289. vs->read_handler_expect = expecting;
  1290. }
  1291. /*
  1292. * Called to read a chunk of data from the client socket. The data may
  1293. * be the raw data, or may need to be further decoded by SASL.
  1294. * The data will be read either straight from to the socket, or
  1295. * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
  1296. *
  1297. * NB, it is theoretically possible to have 2 layers of encryption,
  1298. * both SASL, and this TLS layer. It is highly unlikely in practice
  1299. * though, since SASL encryption will typically be a no-op if TLS
  1300. * is active
  1301. *
  1302. * Returns the number of bytes read, which may be less than
  1303. * the requested 'datalen' if the socket would block. Returns
  1304. * 0 on I/O error or EOF, and disconnects the client socket.
  1305. */
  1306. size_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
  1307. {
  1308. ssize_t ret;
  1309. Error *err = NULL;
  1310. ret = qio_channel_read(
  1311. vs->ioc, (char *)data, datalen, &err);
  1312. VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
  1313. return vnc_client_io_error(vs, ret, &err);
  1314. }
  1315. /*
  1316. * Called to read data from the client socket to the input buffer,
  1317. * when not using any SASL SSF encryption layers. Will read as much
  1318. * data as possible without blocking.
  1319. *
  1320. * Returns the number of bytes read, which may be less than
  1321. * the requested 'datalen' if the socket would block. Returns
  1322. * 0 on I/O error or EOF, and disconnects the client socket.
  1323. */
  1324. static size_t vnc_client_read_plain(VncState *vs)
  1325. {
  1326. size_t ret;
  1327. VNC_DEBUG("Read plain %p size %zd offset %zd\n",
  1328. vs->input.buffer, vs->input.capacity, vs->input.offset);
  1329. buffer_reserve(&vs->input, 4096);
  1330. ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
  1331. if (!ret)
  1332. return 0;
  1333. vs->input.offset += ret;
  1334. return ret;
  1335. }
  1336. static void vnc_jobs_bh(void *opaque)
  1337. {
  1338. VncState *vs = opaque;
  1339. assert(vs->magic == VNC_MAGIC);
  1340. vnc_jobs_consume_buffer(vs);
  1341. }
  1342. /*
  1343. * First function called whenever there is more data to be read from
  1344. * the client socket. Will delegate actual work according to whether
  1345. * SASL SSF layers are enabled (thus requiring decryption calls)
  1346. * Returns 0 on success, -1 if client disconnected
  1347. */
  1348. static int vnc_client_read(VncState *vs)
  1349. {
  1350. size_t ret;
  1351. #ifdef CONFIG_VNC_SASL
  1352. if (vs->sasl.conn && vs->sasl.runSSF)
  1353. ret = vnc_client_read_sasl(vs);
  1354. else
  1355. #endif /* CONFIG_VNC_SASL */
  1356. ret = vnc_client_read_plain(vs);
  1357. if (!ret) {
  1358. if (vs->disconnecting) {
  1359. vnc_disconnect_finish(vs);
  1360. return -1;
  1361. }
  1362. return 0;
  1363. }
  1364. while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
  1365. size_t len = vs->read_handler_expect;
  1366. int ret;
  1367. ret = vs->read_handler(vs, vs->input.buffer, len);
  1368. if (vs->disconnecting) {
  1369. vnc_disconnect_finish(vs);
  1370. return -1;
  1371. }
  1372. if (!ret) {
  1373. buffer_advance(&vs->input, len);
  1374. } else {
  1375. vs->read_handler_expect = ret;
  1376. }
  1377. }
  1378. return 0;
  1379. }
  1380. gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED,
  1381. GIOCondition condition, void *opaque)
  1382. {
  1383. VncState *vs = opaque;
  1384. assert(vs->magic == VNC_MAGIC);
  1385. if (condition & G_IO_IN) {
  1386. if (vnc_client_read(vs) < 0) {
  1387. /* vs is free()ed here */
  1388. return TRUE;
  1389. }
  1390. }
  1391. if (condition & G_IO_OUT) {
  1392. vnc_client_write(vs);
  1393. }
  1394. if (vs->disconnecting) {
  1395. if (vs->ioc_tag != 0) {
  1396. g_source_remove(vs->ioc_tag);
  1397. }
  1398. vs->ioc_tag = 0;
  1399. }
  1400. return TRUE;
  1401. }
  1402. /*
  1403. * Scale factor to apply to vs->throttle_output_offset when checking for
  1404. * hard limit. Worst case normal usage could be x2, if we have a complete
  1405. * incremental update and complete forced update in the output buffer.
  1406. * So x3 should be good enough, but we pick x5 to be conservative and thus
  1407. * (hopefully) never trigger incorrectly.
  1408. */
  1409. #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
  1410. void vnc_write(VncState *vs, const void *data, size_t len)
  1411. {
  1412. assert(vs->magic == VNC_MAGIC);
  1413. if (vs->disconnecting) {
  1414. return;
  1415. }
  1416. /* Protection against malicious client/guest to prevent our output
  1417. * buffer growing without bound if client stops reading data. This
  1418. * should rarely trigger, because we have earlier throttling code
  1419. * which stops issuing framebuffer updates and drops audio data
  1420. * if the throttle_output_offset value is exceeded. So we only reach
  1421. * this higher level if a huge number of pseudo-encodings get
  1422. * triggered while data can't be sent on the socket.
  1423. *
  1424. * NB throttle_output_offset can be zero during early protocol
  1425. * handshake, or from the job thread's VncState clone
  1426. */
  1427. if (vs->throttle_output_offset != 0 &&
  1428. (vs->output.offset / VNC_THROTTLE_OUTPUT_LIMIT_SCALE) >
  1429. vs->throttle_output_offset) {
  1430. trace_vnc_client_output_limit(vs, vs->ioc, vs->output.offset,
  1431. vs->throttle_output_offset);
  1432. vnc_disconnect_start(vs);
  1433. return;
  1434. }
  1435. buffer_reserve(&vs->output, len);
  1436. if (vs->ioc != NULL && buffer_empty(&vs->output)) {
  1437. if (vs->ioc_tag) {
  1438. g_source_remove(vs->ioc_tag);
  1439. }
  1440. vs->ioc_tag = qio_channel_add_watch(
  1441. vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
  1442. }
  1443. buffer_append(&vs->output, data, len);
  1444. }
  1445. void vnc_write_s32(VncState *vs, int32_t value)
  1446. {
  1447. vnc_write_u32(vs, *(uint32_t *)&value);
  1448. }
  1449. void vnc_write_u32(VncState *vs, uint32_t value)
  1450. {
  1451. uint8_t buf[4];
  1452. buf[0] = (value >> 24) & 0xFF;
  1453. buf[1] = (value >> 16) & 0xFF;
  1454. buf[2] = (value >> 8) & 0xFF;
  1455. buf[3] = value & 0xFF;
  1456. vnc_write(vs, buf, 4);
  1457. }
  1458. void vnc_write_u16(VncState *vs, uint16_t value)
  1459. {
  1460. uint8_t buf[2];
  1461. buf[0] = (value >> 8) & 0xFF;
  1462. buf[1] = value & 0xFF;
  1463. vnc_write(vs, buf, 2);
  1464. }
  1465. void vnc_write_u8(VncState *vs, uint8_t value)
  1466. {
  1467. vnc_write(vs, (char *)&value, 1);
  1468. }
  1469. void vnc_flush(VncState *vs)
  1470. {
  1471. vnc_lock_output(vs);
  1472. if (vs->ioc != NULL && vs->output.offset) {
  1473. vnc_client_write_locked(vs);
  1474. }
  1475. if (vs->disconnecting) {
  1476. if (vs->ioc_tag != 0) {
  1477. g_source_remove(vs->ioc_tag);
  1478. }
  1479. vs->ioc_tag = 0;
  1480. }
  1481. vnc_unlock_output(vs);
  1482. }
  1483. static uint8_t read_u8(uint8_t *data, size_t offset)
  1484. {
  1485. return data[offset];
  1486. }
  1487. static uint16_t read_u16(uint8_t *data, size_t offset)
  1488. {
  1489. return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
  1490. }
  1491. static int32_t read_s32(uint8_t *data, size_t offset)
  1492. {
  1493. return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
  1494. (data[offset + 2] << 8) | data[offset + 3]);
  1495. }
  1496. uint32_t read_u32(uint8_t *data, size_t offset)
  1497. {
  1498. return ((data[offset] << 24) | (data[offset + 1] << 16) |
  1499. (data[offset + 2] << 8) | data[offset + 3]);
  1500. }
  1501. static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
  1502. {
  1503. }
  1504. static void check_pointer_type_change(Notifier *notifier, void *data)
  1505. {
  1506. VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
  1507. int absolute = qemu_input_is_absolute();
  1508. if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
  1509. vnc_lock_output(vs);
  1510. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1511. vnc_write_u8(vs, 0);
  1512. vnc_write_u16(vs, 1);
  1513. vnc_framebuffer_update(vs, absolute, 0,
  1514. pixman_image_get_width(vs->vd->server),
  1515. pixman_image_get_height(vs->vd->server),
  1516. VNC_ENCODING_POINTER_TYPE_CHANGE);
  1517. vnc_unlock_output(vs);
  1518. vnc_flush(vs);
  1519. }
  1520. vs->absolute = absolute;
  1521. }
  1522. static void pointer_event(VncState *vs, int button_mask, int x, int y)
  1523. {
  1524. static uint32_t bmap[INPUT_BUTTON__MAX] = {
  1525. [INPUT_BUTTON_LEFT] = 0x01,
  1526. [INPUT_BUTTON_MIDDLE] = 0x02,
  1527. [INPUT_BUTTON_RIGHT] = 0x04,
  1528. [INPUT_BUTTON_WHEEL_UP] = 0x08,
  1529. [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
  1530. };
  1531. QemuConsole *con = vs->vd->dcl.con;
  1532. int width = pixman_image_get_width(vs->vd->server);
  1533. int height = pixman_image_get_height(vs->vd->server);
  1534. if (vs->last_bmask != button_mask) {
  1535. qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
  1536. vs->last_bmask = button_mask;
  1537. }
  1538. if (vs->absolute) {
  1539. qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width);
  1540. qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height);
  1541. } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
  1542. qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
  1543. qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
  1544. } else {
  1545. if (vs->last_x != -1) {
  1546. qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
  1547. qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
  1548. }
  1549. vs->last_x = x;
  1550. vs->last_y = y;
  1551. }
  1552. qemu_input_event_sync();
  1553. }
  1554. static void press_key(VncState *vs, QKeyCode qcode)
  1555. {
  1556. qkbd_state_key_event(vs->vd->kbd, qcode, true);
  1557. qkbd_state_key_event(vs->vd->kbd, qcode, false);
  1558. }
  1559. static void vnc_led_state_change(VncState *vs)
  1560. {
  1561. if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
  1562. return;
  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, 1, 1, VNC_ENCODING_LED_STATE);
  1569. vnc_write_u8(vs, vs->vd->ledstate);
  1570. vnc_unlock_output(vs);
  1571. vnc_flush(vs);
  1572. }
  1573. static void kbd_leds(void *opaque, int ledstate)
  1574. {
  1575. VncDisplay *vd = opaque;
  1576. VncState *client;
  1577. trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
  1578. (ledstate & QEMU_NUM_LOCK_LED),
  1579. (ledstate & QEMU_SCROLL_LOCK_LED));
  1580. if (ledstate == vd->ledstate) {
  1581. return;
  1582. }
  1583. vd->ledstate = ledstate;
  1584. QTAILQ_FOREACH(client, &vd->clients, next) {
  1585. vnc_led_state_change(client);
  1586. }
  1587. }
  1588. static void do_key_event(VncState *vs, int down, int keycode, int sym)
  1589. {
  1590. QKeyCode qcode = qemu_input_key_number_to_qcode(keycode);
  1591. /* QEMU console switch */
  1592. switch (qcode) {
  1593. case Q_KEY_CODE_1 ... Q_KEY_CODE_9: /* '1' to '9' keys */
  1594. if (vs->vd->dcl.con == NULL && down &&
  1595. qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CTRL) &&
  1596. qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_ALT)) {
  1597. /* Reset the modifiers sent to the current console */
  1598. qkbd_state_lift_all_keys(vs->vd->kbd);
  1599. console_select(qcode - Q_KEY_CODE_1);
  1600. return;
  1601. }
  1602. default:
  1603. break;
  1604. }
  1605. /* Turn off the lock state sync logic if the client support the led
  1606. state extension.
  1607. */
  1608. if (down && vs->vd->lock_key_sync &&
  1609. !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
  1610. keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
  1611. /* If the numlock state needs to change then simulate an additional
  1612. keypress before sending this one. This will happen if the user
  1613. toggles numlock away from the VNC window.
  1614. */
  1615. if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
  1616. if (!qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK)) {
  1617. trace_vnc_key_sync_numlock(true);
  1618. press_key(vs, Q_KEY_CODE_NUM_LOCK);
  1619. }
  1620. } else {
  1621. if (qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK)) {
  1622. trace_vnc_key_sync_numlock(false);
  1623. press_key(vs, Q_KEY_CODE_NUM_LOCK);
  1624. }
  1625. }
  1626. }
  1627. if (down && vs->vd->lock_key_sync &&
  1628. !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
  1629. ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
  1630. /* If the capslock state needs to change then simulate an additional
  1631. keypress before sending this one. This will happen if the user
  1632. toggles capslock away from the VNC window.
  1633. */
  1634. int uppercase = !!(sym >= 'A' && sym <= 'Z');
  1635. bool shift = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_SHIFT);
  1636. bool capslock = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CAPSLOCK);
  1637. if (capslock) {
  1638. if (uppercase == shift) {
  1639. trace_vnc_key_sync_capslock(false);
  1640. press_key(vs, Q_KEY_CODE_CAPS_LOCK);
  1641. }
  1642. } else {
  1643. if (uppercase != shift) {
  1644. trace_vnc_key_sync_capslock(true);
  1645. press_key(vs, Q_KEY_CODE_CAPS_LOCK);
  1646. }
  1647. }
  1648. }
  1649. qkbd_state_key_event(vs->vd->kbd, qcode, down);
  1650. if (!qemu_console_is_graphic(NULL)) {
  1651. bool numlock = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK);
  1652. bool control = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CTRL);
  1653. /* QEMU console emulation */
  1654. if (down) {
  1655. switch (keycode) {
  1656. case 0x2a: /* Left Shift */
  1657. case 0x36: /* Right Shift */
  1658. case 0x1d: /* Left CTRL */
  1659. case 0x9d: /* Right CTRL */
  1660. case 0x38: /* Left ALT */
  1661. case 0xb8: /* Right ALT */
  1662. break;
  1663. case 0xc8:
  1664. kbd_put_keysym(QEMU_KEY_UP);
  1665. break;
  1666. case 0xd0:
  1667. kbd_put_keysym(QEMU_KEY_DOWN);
  1668. break;
  1669. case 0xcb:
  1670. kbd_put_keysym(QEMU_KEY_LEFT);
  1671. break;
  1672. case 0xcd:
  1673. kbd_put_keysym(QEMU_KEY_RIGHT);
  1674. break;
  1675. case 0xd3:
  1676. kbd_put_keysym(QEMU_KEY_DELETE);
  1677. break;
  1678. case 0xc7:
  1679. kbd_put_keysym(QEMU_KEY_HOME);
  1680. break;
  1681. case 0xcf:
  1682. kbd_put_keysym(QEMU_KEY_END);
  1683. break;
  1684. case 0xc9:
  1685. kbd_put_keysym(QEMU_KEY_PAGEUP);
  1686. break;
  1687. case 0xd1:
  1688. kbd_put_keysym(QEMU_KEY_PAGEDOWN);
  1689. break;
  1690. case 0x47:
  1691. kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
  1692. break;
  1693. case 0x48:
  1694. kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
  1695. break;
  1696. case 0x49:
  1697. kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
  1698. break;
  1699. case 0x4b:
  1700. kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
  1701. break;
  1702. case 0x4c:
  1703. kbd_put_keysym('5');
  1704. break;
  1705. case 0x4d:
  1706. kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
  1707. break;
  1708. case 0x4f:
  1709. kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
  1710. break;
  1711. case 0x50:
  1712. kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
  1713. break;
  1714. case 0x51:
  1715. kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
  1716. break;
  1717. case 0x52:
  1718. kbd_put_keysym('0');
  1719. break;
  1720. case 0x53:
  1721. kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
  1722. break;
  1723. case 0xb5:
  1724. kbd_put_keysym('/');
  1725. break;
  1726. case 0x37:
  1727. kbd_put_keysym('*');
  1728. break;
  1729. case 0x4a:
  1730. kbd_put_keysym('-');
  1731. break;
  1732. case 0x4e:
  1733. kbd_put_keysym('+');
  1734. break;
  1735. case 0x9c:
  1736. kbd_put_keysym('\n');
  1737. break;
  1738. default:
  1739. if (control) {
  1740. kbd_put_keysym(sym & 0x1f);
  1741. } else {
  1742. kbd_put_keysym(sym);
  1743. }
  1744. break;
  1745. }
  1746. }
  1747. }
  1748. }
  1749. static const char *code2name(int keycode)
  1750. {
  1751. return QKeyCode_str(qemu_input_key_number_to_qcode(keycode));
  1752. }
  1753. static void key_event(VncState *vs, int down, uint32_t sym)
  1754. {
  1755. int keycode;
  1756. int lsym = sym;
  1757. if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
  1758. lsym = lsym - 'A' + 'a';
  1759. }
  1760. keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF,
  1761. vs->vd->kbd, down) & SCANCODE_KEYMASK;
  1762. trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
  1763. do_key_event(vs, down, keycode, sym);
  1764. }
  1765. static void ext_key_event(VncState *vs, int down,
  1766. uint32_t sym, uint16_t keycode)
  1767. {
  1768. /* if the user specifies a keyboard layout, always use it */
  1769. if (keyboard_layout) {
  1770. key_event(vs, down, sym);
  1771. } else {
  1772. trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
  1773. do_key_event(vs, down, keycode, sym);
  1774. }
  1775. }
  1776. static void framebuffer_update_request(VncState *vs, int incremental,
  1777. int x, int y, int w, int h)
  1778. {
  1779. if (incremental) {
  1780. if (vs->update != VNC_STATE_UPDATE_FORCE) {
  1781. vs->update = VNC_STATE_UPDATE_INCREMENTAL;
  1782. }
  1783. } else {
  1784. vs->update = VNC_STATE_UPDATE_FORCE;
  1785. vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h);
  1786. }
  1787. }
  1788. static void send_ext_key_event_ack(VncState *vs)
  1789. {
  1790. vnc_lock_output(vs);
  1791. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1792. vnc_write_u8(vs, 0);
  1793. vnc_write_u16(vs, 1);
  1794. vnc_framebuffer_update(vs, 0, 0,
  1795. pixman_image_get_width(vs->vd->server),
  1796. pixman_image_get_height(vs->vd->server),
  1797. VNC_ENCODING_EXT_KEY_EVENT);
  1798. vnc_unlock_output(vs);
  1799. vnc_flush(vs);
  1800. }
  1801. static void send_ext_audio_ack(VncState *vs)
  1802. {
  1803. vnc_lock_output(vs);
  1804. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  1805. vnc_write_u8(vs, 0);
  1806. vnc_write_u16(vs, 1);
  1807. vnc_framebuffer_update(vs, 0, 0,
  1808. pixman_image_get_width(vs->vd->server),
  1809. pixman_image_get_height(vs->vd->server),
  1810. VNC_ENCODING_AUDIO);
  1811. vnc_unlock_output(vs);
  1812. vnc_flush(vs);
  1813. }
  1814. static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
  1815. {
  1816. int i;
  1817. unsigned int enc = 0;
  1818. vs->features = 0;
  1819. vs->vnc_encoding = 0;
  1820. vs->tight->compression = 9;
  1821. vs->tight->quality = -1; /* Lossless by default */
  1822. vs->absolute = -1;
  1823. /*
  1824. * Start from the end because the encodings are sent in order of preference.
  1825. * This way the preferred encoding (first encoding defined in the array)
  1826. * will be set at the end of the loop.
  1827. */
  1828. for (i = n_encodings - 1; i >= 0; i--) {
  1829. enc = encodings[i];
  1830. switch (enc) {
  1831. case VNC_ENCODING_RAW:
  1832. vs->vnc_encoding = enc;
  1833. break;
  1834. case VNC_ENCODING_COPYRECT:
  1835. vs->features |= VNC_FEATURE_COPYRECT_MASK;
  1836. break;
  1837. case VNC_ENCODING_HEXTILE:
  1838. vs->features |= VNC_FEATURE_HEXTILE_MASK;
  1839. vs->vnc_encoding = enc;
  1840. break;
  1841. case VNC_ENCODING_TIGHT:
  1842. vs->features |= VNC_FEATURE_TIGHT_MASK;
  1843. vs->vnc_encoding = enc;
  1844. break;
  1845. #ifdef CONFIG_VNC_PNG
  1846. case VNC_ENCODING_TIGHT_PNG:
  1847. vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
  1848. vs->vnc_encoding = enc;
  1849. break;
  1850. #endif
  1851. case VNC_ENCODING_ZLIB:
  1852. vs->features |= VNC_FEATURE_ZLIB_MASK;
  1853. vs->vnc_encoding = enc;
  1854. break;
  1855. case VNC_ENCODING_ZRLE:
  1856. vs->features |= VNC_FEATURE_ZRLE_MASK;
  1857. vs->vnc_encoding = enc;
  1858. break;
  1859. case VNC_ENCODING_ZYWRLE:
  1860. vs->features |= VNC_FEATURE_ZYWRLE_MASK;
  1861. vs->vnc_encoding = enc;
  1862. break;
  1863. case VNC_ENCODING_DESKTOPRESIZE:
  1864. vs->features |= VNC_FEATURE_RESIZE_MASK;
  1865. break;
  1866. case VNC_ENCODING_POINTER_TYPE_CHANGE:
  1867. vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
  1868. break;
  1869. case VNC_ENCODING_RICH_CURSOR:
  1870. vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
  1871. if (vs->vd->cursor) {
  1872. vnc_cursor_define(vs);
  1873. }
  1874. break;
  1875. case VNC_ENCODING_EXT_KEY_EVENT:
  1876. send_ext_key_event_ack(vs);
  1877. break;
  1878. case VNC_ENCODING_AUDIO:
  1879. send_ext_audio_ack(vs);
  1880. break;
  1881. case VNC_ENCODING_WMVi:
  1882. vs->features |= VNC_FEATURE_WMVI_MASK;
  1883. break;
  1884. case VNC_ENCODING_LED_STATE:
  1885. vs->features |= VNC_FEATURE_LED_STATE_MASK;
  1886. break;
  1887. case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
  1888. vs->tight->compression = (enc & 0x0F);
  1889. break;
  1890. case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
  1891. if (vs->vd->lossy) {
  1892. vs->tight->quality = (enc & 0x0F);
  1893. }
  1894. break;
  1895. default:
  1896. VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
  1897. break;
  1898. }
  1899. }
  1900. vnc_desktop_resize(vs);
  1901. check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
  1902. vnc_led_state_change(vs);
  1903. }
  1904. static void set_pixel_conversion(VncState *vs)
  1905. {
  1906. pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
  1907. if (fmt == VNC_SERVER_FB_FORMAT) {
  1908. vs->write_pixels = vnc_write_pixels_copy;
  1909. vnc_hextile_set_pixel_conversion(vs, 0);
  1910. } else {
  1911. vs->write_pixels = vnc_write_pixels_generic;
  1912. vnc_hextile_set_pixel_conversion(vs, 1);
  1913. }
  1914. }
  1915. static void send_color_map(VncState *vs)
  1916. {
  1917. int i;
  1918. vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES);
  1919. vnc_write_u8(vs, 0); /* padding */
  1920. vnc_write_u16(vs, 0); /* first color */
  1921. vnc_write_u16(vs, 256); /* # of colors */
  1922. for (i = 0; i < 256; i++) {
  1923. PixelFormat *pf = &vs->client_pf;
  1924. vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits)));
  1925. vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits)));
  1926. vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits)));
  1927. }
  1928. }
  1929. static void set_pixel_format(VncState *vs, int bits_per_pixel,
  1930. int big_endian_flag, int true_color_flag,
  1931. int red_max, int green_max, int blue_max,
  1932. int red_shift, int green_shift, int blue_shift)
  1933. {
  1934. if (!true_color_flag) {
  1935. /* Expose a reasonable default 256 color map */
  1936. bits_per_pixel = 8;
  1937. red_max = 7;
  1938. green_max = 7;
  1939. blue_max = 3;
  1940. red_shift = 0;
  1941. green_shift = 3;
  1942. blue_shift = 6;
  1943. }
  1944. switch (bits_per_pixel) {
  1945. case 8:
  1946. case 16:
  1947. case 32:
  1948. break;
  1949. default:
  1950. vnc_client_error(vs);
  1951. return;
  1952. }
  1953. vs->client_pf.rmax = red_max ? red_max : 0xFF;
  1954. vs->client_pf.rbits = ctpopl(red_max);
  1955. vs->client_pf.rshift = red_shift;
  1956. vs->client_pf.rmask = red_max << red_shift;
  1957. vs->client_pf.gmax = green_max ? green_max : 0xFF;
  1958. vs->client_pf.gbits = ctpopl(green_max);
  1959. vs->client_pf.gshift = green_shift;
  1960. vs->client_pf.gmask = green_max << green_shift;
  1961. vs->client_pf.bmax = blue_max ? blue_max : 0xFF;
  1962. vs->client_pf.bbits = ctpopl(blue_max);
  1963. vs->client_pf.bshift = blue_shift;
  1964. vs->client_pf.bmask = blue_max << blue_shift;
  1965. vs->client_pf.bits_per_pixel = bits_per_pixel;
  1966. vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
  1967. vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
  1968. vs->client_be = big_endian_flag;
  1969. if (!true_color_flag) {
  1970. send_color_map(vs);
  1971. }
  1972. set_pixel_conversion(vs);
  1973. graphic_hw_invalidate(vs->vd->dcl.con);
  1974. graphic_hw_update(vs->vd->dcl.con);
  1975. }
  1976. static void pixel_format_message (VncState *vs) {
  1977. char pad[3] = { 0, 0, 0 };
  1978. vs->client_pf = qemu_default_pixelformat(32);
  1979. vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
  1980. vnc_write_u8(vs, vs->client_pf.depth); /* depth */
  1981. #ifdef HOST_WORDS_BIGENDIAN
  1982. vnc_write_u8(vs, 1); /* big-endian-flag */
  1983. #else
  1984. vnc_write_u8(vs, 0); /* big-endian-flag */
  1985. #endif
  1986. vnc_write_u8(vs, 1); /* true-color-flag */
  1987. vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */
  1988. vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */
  1989. vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */
  1990. vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */
  1991. vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */
  1992. vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */
  1993. vnc_write(vs, pad, 3); /* padding */
  1994. vnc_hextile_set_pixel_conversion(vs, 0);
  1995. vs->write_pixels = vnc_write_pixels_copy;
  1996. }
  1997. static void vnc_colordepth(VncState *vs)
  1998. {
  1999. if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
  2000. /* Sending a WMVi message to notify the client*/
  2001. vnc_lock_output(vs);
  2002. vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  2003. vnc_write_u8(vs, 0);
  2004. vnc_write_u16(vs, 1); /* number of rects */
  2005. vnc_framebuffer_update(vs, 0, 0,
  2006. pixman_image_get_width(vs->vd->server),
  2007. pixman_image_get_height(vs->vd->server),
  2008. VNC_ENCODING_WMVi);
  2009. pixel_format_message(vs);
  2010. vnc_unlock_output(vs);
  2011. vnc_flush(vs);
  2012. } else {
  2013. set_pixel_conversion(vs);
  2014. }
  2015. }
  2016. static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
  2017. {
  2018. int i;
  2019. uint16_t limit;
  2020. uint32_t freq;
  2021. VncDisplay *vd = vs->vd;
  2022. if (data[0] > 3) {
  2023. update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
  2024. }
  2025. switch (data[0]) {
  2026. case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
  2027. if (len == 1)
  2028. return 20;
  2029. set_pixel_format(vs, read_u8(data, 4),
  2030. read_u8(data, 6), read_u8(data, 7),
  2031. read_u16(data, 8), read_u16(data, 10),
  2032. read_u16(data, 12), read_u8(data, 14),
  2033. read_u8(data, 15), read_u8(data, 16));
  2034. break;
  2035. case VNC_MSG_CLIENT_SET_ENCODINGS:
  2036. if (len == 1)
  2037. return 4;
  2038. if (len == 4) {
  2039. limit = read_u16(data, 2);
  2040. if (limit > 0)
  2041. return 4 + (limit * 4);
  2042. } else
  2043. limit = read_u16(data, 2);
  2044. for (i = 0; i < limit; i++) {
  2045. int32_t val = read_s32(data, 4 + (i * 4));
  2046. memcpy(data + 4 + (i * 4), &val, sizeof(val));
  2047. }
  2048. set_encodings(vs, (int32_t *)(data + 4), limit);
  2049. break;
  2050. case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
  2051. if (len == 1)
  2052. return 10;
  2053. framebuffer_update_request(vs,
  2054. read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
  2055. read_u16(data, 6), read_u16(data, 8));
  2056. break;
  2057. case VNC_MSG_CLIENT_KEY_EVENT:
  2058. if (len == 1)
  2059. return 8;
  2060. key_event(vs, read_u8(data, 1), read_u32(data, 4));
  2061. break;
  2062. case VNC_MSG_CLIENT_POINTER_EVENT:
  2063. if (len == 1)
  2064. return 6;
  2065. pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
  2066. break;
  2067. case VNC_MSG_CLIENT_CUT_TEXT:
  2068. if (len == 1) {
  2069. return 8;
  2070. }
  2071. if (len == 8) {
  2072. uint32_t dlen = read_u32(data, 4);
  2073. if (dlen > (1 << 20)) {
  2074. error_report("vnc: client_cut_text msg payload has %u bytes"
  2075. " which exceeds our limit of 1MB.", dlen);
  2076. vnc_client_error(vs);
  2077. break;
  2078. }
  2079. if (dlen > 0) {
  2080. return 8 + dlen;
  2081. }
  2082. }
  2083. client_cut_text(vs, read_u32(data, 4), data + 8);
  2084. break;
  2085. case VNC_MSG_CLIENT_QEMU:
  2086. if (len == 1)
  2087. return 2;
  2088. switch (read_u8(data, 1)) {
  2089. case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
  2090. if (len == 2)
  2091. return 12;
  2092. ext_key_event(vs, read_u16(data, 2),
  2093. read_u32(data, 4), read_u32(data, 8));
  2094. break;
  2095. case VNC_MSG_CLIENT_QEMU_AUDIO:
  2096. if (len == 2)
  2097. return 4;
  2098. switch (read_u16 (data, 2)) {
  2099. case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
  2100. audio_add(vs);
  2101. break;
  2102. case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
  2103. audio_del(vs);
  2104. break;
  2105. case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
  2106. if (len == 4)
  2107. return 10;
  2108. switch (read_u8(data, 4)) {
  2109. case 0: vs->as.fmt = AUDIO_FORMAT_U8; break;
  2110. case 1: vs->as.fmt = AUDIO_FORMAT_S8; break;
  2111. case 2: vs->as.fmt = AUDIO_FORMAT_U16; break;
  2112. case 3: vs->as.fmt = AUDIO_FORMAT_S16; break;
  2113. case 4: vs->as.fmt = AUDIO_FORMAT_U32; break;
  2114. case 5: vs->as.fmt = AUDIO_FORMAT_S32; break;
  2115. default:
  2116. VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4));
  2117. vnc_client_error(vs);
  2118. break;
  2119. }
  2120. vs->as.nchannels = read_u8(data, 5);
  2121. if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
  2122. VNC_DEBUG("Invalid audio channel count %d\n",
  2123. read_u8(data, 5));
  2124. vnc_client_error(vs);
  2125. break;
  2126. }
  2127. freq = read_u32(data, 6);
  2128. /* No official limit for protocol, but 48khz is a sensible
  2129. * upper bound for trustworthy clients, and this limit
  2130. * protects calculations involving 'vs->as.freq' later.
  2131. */
  2132. if (freq > 48000) {
  2133. VNC_DEBUG("Invalid audio frequency %u > 48000", freq);
  2134. vnc_client_error(vs);
  2135. break;
  2136. }
  2137. vs->as.freq = freq;
  2138. break;
  2139. default:
  2140. VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4));
  2141. vnc_client_error(vs);
  2142. break;
  2143. }
  2144. break;
  2145. default:
  2146. VNC_DEBUG("Msg: %d\n", read_u16(data, 0));
  2147. vnc_client_error(vs);
  2148. break;
  2149. }
  2150. break;
  2151. default:
  2152. VNC_DEBUG("Msg: %d\n", data[0]);
  2153. vnc_client_error(vs);
  2154. break;
  2155. }
  2156. vnc_update_throttle_offset(vs);
  2157. vnc_read_when(vs, protocol_client_msg, 1);
  2158. return 0;
  2159. }
  2160. static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
  2161. {
  2162. char buf[1024];
  2163. VncShareMode mode;
  2164. int size;
  2165. mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
  2166. switch (vs->vd->share_policy) {
  2167. case VNC_SHARE_POLICY_IGNORE:
  2168. /*
  2169. * Ignore the shared flag. Nothing to do here.
  2170. *
  2171. * Doesn't conform to the rfb spec but is traditional qemu
  2172. * behavior, thus left here as option for compatibility
  2173. * reasons.
  2174. */
  2175. break;
  2176. case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
  2177. /*
  2178. * Policy: Allow clients ask for exclusive access.
  2179. *
  2180. * Implementation: When a client asks for exclusive access,
  2181. * disconnect all others. Shared connects are allowed as long
  2182. * as no exclusive connection exists.
  2183. *
  2184. * This is how the rfb spec suggests to handle the shared flag.
  2185. */
  2186. if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
  2187. VncState *client;
  2188. QTAILQ_FOREACH(client, &vs->vd->clients, next) {
  2189. if (vs == client) {
  2190. continue;
  2191. }
  2192. if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
  2193. client->share_mode != VNC_SHARE_MODE_SHARED) {
  2194. continue;
  2195. }
  2196. vnc_disconnect_start(client);
  2197. }
  2198. }
  2199. if (mode == VNC_SHARE_MODE_SHARED) {
  2200. if (vs->vd->num_exclusive > 0) {
  2201. vnc_disconnect_start(vs);
  2202. return 0;
  2203. }
  2204. }
  2205. break;
  2206. case VNC_SHARE_POLICY_FORCE_SHARED:
  2207. /*
  2208. * Policy: Shared connects only.
  2209. * Implementation: Disallow clients asking for exclusive access.
  2210. *
  2211. * Useful for shared desktop sessions where you don't want
  2212. * someone forgetting to say -shared when running the vnc
  2213. * client disconnect everybody else.
  2214. */
  2215. if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
  2216. vnc_disconnect_start(vs);
  2217. return 0;
  2218. }
  2219. break;
  2220. }
  2221. vnc_set_share_mode(vs, mode);
  2222. if (vs->vd->num_shared > vs->vd->connections_limit) {
  2223. vnc_disconnect_start(vs);
  2224. return 0;
  2225. }
  2226. assert(pixman_image_get_width(vs->vd->server) < 65536 &&
  2227. pixman_image_get_width(vs->vd->server) >= 0);
  2228. assert(pixman_image_get_height(vs->vd->server) < 65536 &&
  2229. pixman_image_get_height(vs->vd->server) >= 0);
  2230. vs->client_width = pixman_image_get_width(vs->vd->server);
  2231. vs->client_height = pixman_image_get_height(vs->vd->server);
  2232. vnc_write_u16(vs, vs->client_width);
  2233. vnc_write_u16(vs, vs->client_height);
  2234. pixel_format_message(vs);
  2235. if (qemu_name) {
  2236. size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
  2237. if (size > sizeof(buf)) {
  2238. size = sizeof(buf);
  2239. }
  2240. } else {
  2241. size = snprintf(buf, sizeof(buf), "QEMU");
  2242. }
  2243. vnc_write_u32(vs, size);
  2244. vnc_write(vs, buf, size);
  2245. vnc_flush(vs);
  2246. vnc_client_cache_auth(vs);
  2247. vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
  2248. vnc_read_when(vs, protocol_client_msg, 1);
  2249. return 0;
  2250. }
  2251. void start_client_init(VncState *vs)
  2252. {
  2253. vnc_read_when(vs, protocol_client_init, 1);
  2254. }
  2255. static void authentication_failed(VncState *vs)
  2256. {
  2257. vnc_write_u32(vs, 1); /* Reject auth */
  2258. if (vs->minor >= 8) {
  2259. static const char err[] = "Authentication failed";
  2260. vnc_write_u32(vs, sizeof(err));
  2261. vnc_write(vs, err, sizeof(err));
  2262. }
  2263. vnc_flush(vs);
  2264. vnc_client_error(vs);
  2265. }
  2266. static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
  2267. {
  2268. unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
  2269. size_t i, pwlen;
  2270. unsigned char key[8];
  2271. time_t now = time(NULL);
  2272. QCryptoCipher *cipher = NULL;
  2273. Error *err = NULL;
  2274. if (!vs->vd->password) {
  2275. trace_vnc_auth_fail(vs, vs->auth, "password is not set", "");
  2276. goto reject;
  2277. }
  2278. if (vs->vd->expires < now) {
  2279. trace_vnc_auth_fail(vs, vs->auth, "password is expired", "");
  2280. goto reject;
  2281. }
  2282. memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
  2283. /* Calculate the expected challenge response */
  2284. pwlen = strlen(vs->vd->password);
  2285. for (i=0; i<sizeof(key); i++)
  2286. key[i] = i<pwlen ? vs->vd->password[i] : 0;
  2287. cipher = qcrypto_cipher_new(
  2288. QCRYPTO_CIPHER_ALG_DES_RFB,
  2289. QCRYPTO_CIPHER_MODE_ECB,
  2290. key, G_N_ELEMENTS(key),
  2291. &err);
  2292. if (!cipher) {
  2293. trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher",
  2294. error_get_pretty(err));
  2295. error_free(err);
  2296. goto reject;
  2297. }
  2298. if (qcrypto_cipher_encrypt(cipher,
  2299. vs->challenge,
  2300. response,
  2301. VNC_AUTH_CHALLENGE_SIZE,
  2302. &err) < 0) {
  2303. trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response",
  2304. error_get_pretty(err));
  2305. error_free(err);
  2306. goto reject;
  2307. }
  2308. /* Compare expected vs actual challenge response */
  2309. if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
  2310. trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", "");
  2311. goto reject;
  2312. } else {
  2313. trace_vnc_auth_pass(vs, vs->auth);
  2314. vnc_write_u32(vs, 0); /* Accept auth */
  2315. vnc_flush(vs);
  2316. start_client_init(vs);
  2317. }
  2318. qcrypto_cipher_free(cipher);
  2319. return 0;
  2320. reject:
  2321. authentication_failed(vs);
  2322. qcrypto_cipher_free(cipher);
  2323. return 0;
  2324. }
  2325. void start_auth_vnc(VncState *vs)
  2326. {
  2327. Error *err = NULL;
  2328. if (qcrypto_random_bytes(vs->challenge, sizeof(vs->challenge), &err)) {
  2329. trace_vnc_auth_fail(vs, vs->auth, "cannot get random bytes",
  2330. error_get_pretty(err));
  2331. error_free(err);
  2332. authentication_failed(vs);
  2333. return;
  2334. }
  2335. /* Send client a 'random' challenge */
  2336. vnc_write(vs, vs->challenge, sizeof(vs->challenge));
  2337. vnc_flush(vs);
  2338. vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
  2339. }
  2340. static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
  2341. {
  2342. /* We only advertise 1 auth scheme at a time, so client
  2343. * must pick the one we sent. Verify this */
  2344. if (data[0] != vs->auth) { /* Reject auth */
  2345. trace_vnc_auth_reject(vs, vs->auth, (int)data[0]);
  2346. authentication_failed(vs);
  2347. } else { /* Accept requested auth */
  2348. trace_vnc_auth_start(vs, vs->auth);
  2349. switch (vs->auth) {
  2350. case VNC_AUTH_NONE:
  2351. if (vs->minor >= 8) {
  2352. vnc_write_u32(vs, 0); /* Accept auth completion */
  2353. vnc_flush(vs);
  2354. }
  2355. trace_vnc_auth_pass(vs, vs->auth);
  2356. start_client_init(vs);
  2357. break;
  2358. case VNC_AUTH_VNC:
  2359. start_auth_vnc(vs);
  2360. break;
  2361. case VNC_AUTH_VENCRYPT:
  2362. start_auth_vencrypt(vs);
  2363. break;
  2364. #ifdef CONFIG_VNC_SASL
  2365. case VNC_AUTH_SASL:
  2366. start_auth_sasl(vs);
  2367. break;
  2368. #endif /* CONFIG_VNC_SASL */
  2369. default: /* Should not be possible, but just in case */
  2370. trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", "");
  2371. authentication_failed(vs);
  2372. }
  2373. }
  2374. return 0;
  2375. }
  2376. static int protocol_version(VncState *vs, uint8_t *version, size_t len)
  2377. {
  2378. char local[13];
  2379. memcpy(local, version, 12);
  2380. local[12] = 0;
  2381. if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
  2382. VNC_DEBUG("Malformed protocol version %s\n", local);
  2383. vnc_client_error(vs);
  2384. return 0;
  2385. }
  2386. VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
  2387. if (vs->major != 3 ||
  2388. (vs->minor != 3 &&
  2389. vs->minor != 4 &&
  2390. vs->minor != 5 &&
  2391. vs->minor != 7 &&
  2392. vs->minor != 8)) {
  2393. VNC_DEBUG("Unsupported client version\n");
  2394. vnc_write_u32(vs, VNC_AUTH_INVALID);
  2395. vnc_flush(vs);
  2396. vnc_client_error(vs);
  2397. return 0;
  2398. }
  2399. /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
  2400. * as equivalent to v3.3 by servers
  2401. */
  2402. if (vs->minor == 4 || vs->minor == 5)
  2403. vs->minor = 3;
  2404. if (vs->minor == 3) {
  2405. trace_vnc_auth_start(vs, vs->auth);
  2406. if (vs->auth == VNC_AUTH_NONE) {
  2407. vnc_write_u32(vs, vs->auth);
  2408. vnc_flush(vs);
  2409. trace_vnc_auth_pass(vs, vs->auth);
  2410. start_client_init(vs);
  2411. } else if (vs->auth == VNC_AUTH_VNC) {
  2412. VNC_DEBUG("Tell client VNC auth\n");
  2413. vnc_write_u32(vs, vs->auth);
  2414. vnc_flush(vs);
  2415. start_auth_vnc(vs);
  2416. } else {
  2417. trace_vnc_auth_fail(vs, vs->auth,
  2418. "Unsupported auth method for v3.3", "");
  2419. vnc_write_u32(vs, VNC_AUTH_INVALID);
  2420. vnc_flush(vs);
  2421. vnc_client_error(vs);
  2422. }
  2423. } else {
  2424. vnc_write_u8(vs, 1); /* num auth */
  2425. vnc_write_u8(vs, vs->auth);
  2426. vnc_read_when(vs, protocol_client_auth, 1);
  2427. vnc_flush(vs);
  2428. }
  2429. return 0;
  2430. }
  2431. static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
  2432. {
  2433. struct VncSurface *vs = &vd->guest;
  2434. return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
  2435. }
  2436. void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
  2437. {
  2438. int i, j;
  2439. w = (x + w) / VNC_STAT_RECT;
  2440. h = (y + h) / VNC_STAT_RECT;
  2441. x /= VNC_STAT_RECT;
  2442. y /= VNC_STAT_RECT;
  2443. for (j = y; j <= h; j++) {
  2444. for (i = x; i <= w; i++) {
  2445. vs->lossy_rect[j][i] = 1;
  2446. }
  2447. }
  2448. }
  2449. static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
  2450. {
  2451. VncState *vs;
  2452. int sty = y / VNC_STAT_RECT;
  2453. int stx = x / VNC_STAT_RECT;
  2454. int has_dirty = 0;
  2455. y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT);
  2456. x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT);
  2457. QTAILQ_FOREACH(vs, &vd->clients, next) {
  2458. int j;
  2459. /* kernel send buffers are full -> refresh later */
  2460. if (vs->output.offset) {
  2461. continue;
  2462. }
  2463. if (!vs->lossy_rect[sty][stx]) {
  2464. continue;
  2465. }
  2466. vs->lossy_rect[sty][stx] = 0;
  2467. for (j = 0; j < VNC_STAT_RECT; ++j) {
  2468. bitmap_set(vs->dirty[y + j],
  2469. x / VNC_DIRTY_PIXELS_PER_BIT,
  2470. VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
  2471. }
  2472. has_dirty++;
  2473. }
  2474. return has_dirty;
  2475. }
  2476. static int vnc_update_stats(VncDisplay *vd, struct timeval * tv)
  2477. {
  2478. int width = MIN(pixman_image_get_width(vd->guest.fb),
  2479. pixman_image_get_width(vd->server));
  2480. int height = MIN(pixman_image_get_height(vd->guest.fb),
  2481. pixman_image_get_height(vd->server));
  2482. int x, y;
  2483. struct timeval res;
  2484. int has_dirty = 0;
  2485. for (y = 0; y < height; y += VNC_STAT_RECT) {
  2486. for (x = 0; x < width; x += VNC_STAT_RECT) {
  2487. VncRectStat *rect = vnc_stat_rect(vd, x, y);
  2488. rect->updated = false;
  2489. }
  2490. }
  2491. qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
  2492. if (timercmp(&vd->guest.last_freq_check, &res, >)) {
  2493. return has_dirty;
  2494. }
  2495. vd->guest.last_freq_check = *tv;
  2496. for (y = 0; y < height; y += VNC_STAT_RECT) {
  2497. for (x = 0; x < width; x += VNC_STAT_RECT) {
  2498. VncRectStat *rect= vnc_stat_rect(vd, x, y);
  2499. int count = ARRAY_SIZE(rect->times);
  2500. struct timeval min, max;
  2501. if (!timerisset(&rect->times[count - 1])) {
  2502. continue ;
  2503. }
  2504. max = rect->times[(rect->idx + count - 1) % count];
  2505. qemu_timersub(tv, &max, &res);
  2506. if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
  2507. rect->freq = 0;
  2508. has_dirty += vnc_refresh_lossy_rect(vd, x, y);
  2509. memset(rect->times, 0, sizeof (rect->times));
  2510. continue ;
  2511. }
  2512. min = rect->times[rect->idx];
  2513. max = rect->times[(rect->idx + count - 1) % count];
  2514. qemu_timersub(&max, &min, &res);
  2515. rect->freq = res.tv_sec + res.tv_usec / 1000000.;
  2516. rect->freq /= count;
  2517. rect->freq = 1. / rect->freq;
  2518. }
  2519. }
  2520. return has_dirty;
  2521. }
  2522. double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
  2523. {
  2524. int i, j;
  2525. double total = 0;
  2526. int num = 0;
  2527. x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT);
  2528. y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT);
  2529. for (j = y; j <= y + h; j += VNC_STAT_RECT) {
  2530. for (i = x; i <= x + w; i += VNC_STAT_RECT) {
  2531. total += vnc_stat_rect(vs->vd, i, j)->freq;
  2532. num++;
  2533. }
  2534. }
  2535. if (num) {
  2536. return total / num;
  2537. } else {
  2538. return 0;
  2539. }
  2540. }
  2541. static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
  2542. {
  2543. VncRectStat *rect;
  2544. rect = vnc_stat_rect(vd, x, y);
  2545. if (rect->updated) {
  2546. return ;
  2547. }
  2548. rect->times[rect->idx] = *tv;
  2549. rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
  2550. rect->updated = true;
  2551. }
  2552. static int vnc_refresh_server_surface(VncDisplay *vd)
  2553. {
  2554. int width = MIN(pixman_image_get_width(vd->guest.fb),
  2555. pixman_image_get_width(vd->server));
  2556. int height = MIN(pixman_image_get_height(vd->guest.fb),
  2557. pixman_image_get_height(vd->server));
  2558. int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0;
  2559. uint8_t *guest_row0 = NULL, *server_row0;
  2560. VncState *vs;
  2561. int has_dirty = 0;
  2562. pixman_image_t *tmpbuf = NULL;
  2563. struct timeval tv = { 0, 0 };
  2564. if (!vd->non_adaptive) {
  2565. gettimeofday(&tv, NULL);
  2566. has_dirty = vnc_update_stats(vd, &tv);
  2567. }
  2568. /*
  2569. * Walk through the guest dirty map.
  2570. * Check and copy modified bits from guest to server surface.
  2571. * Update server dirty map.
  2572. */
  2573. server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
  2574. server_stride = guest_stride = guest_ll =
  2575. pixman_image_get_stride(vd->server);
  2576. cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES,
  2577. server_stride);
  2578. if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
  2579. int width = pixman_image_get_width(vd->server);
  2580. tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
  2581. } else {
  2582. int guest_bpp =
  2583. PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb));
  2584. guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
  2585. guest_stride = pixman_image_get_stride(vd->guest.fb);
  2586. guest_ll = pixman_image_get_width(vd->guest.fb)
  2587. * DIV_ROUND_UP(guest_bpp, 8);
  2588. }
  2589. line_bytes = MIN(server_stride, guest_ll);
  2590. for (;;) {
  2591. int x;
  2592. uint8_t *guest_ptr, *server_ptr;
  2593. unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
  2594. height * VNC_DIRTY_BPL(&vd->guest),
  2595. y * VNC_DIRTY_BPL(&vd->guest));
  2596. if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
  2597. /* no more dirty bits */
  2598. break;
  2599. }
  2600. y = offset / VNC_DIRTY_BPL(&vd->guest);
  2601. x = offset % VNC_DIRTY_BPL(&vd->guest);
  2602. server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
  2603. if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
  2604. qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
  2605. guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
  2606. } else {
  2607. guest_ptr = guest_row0 + y * guest_stride;
  2608. }
  2609. guest_ptr += x * cmp_bytes;
  2610. for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
  2611. x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
  2612. int _cmp_bytes = cmp_bytes;
  2613. if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
  2614. continue;
  2615. }
  2616. if ((x + 1) * cmp_bytes > line_bytes) {
  2617. _cmp_bytes = line_bytes - x * cmp_bytes;
  2618. }
  2619. assert(_cmp_bytes >= 0);
  2620. if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) {
  2621. continue;
  2622. }
  2623. memcpy(server_ptr, guest_ptr, _cmp_bytes);
  2624. if (!vd->non_adaptive) {
  2625. vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
  2626. y, &tv);
  2627. }
  2628. QTAILQ_FOREACH(vs, &vd->clients, next) {
  2629. set_bit(x, vs->dirty[y]);
  2630. }
  2631. has_dirty++;
  2632. }
  2633. y++;
  2634. }
  2635. qemu_pixman_image_unref(tmpbuf);
  2636. return has_dirty;
  2637. }
  2638. static void vnc_refresh(DisplayChangeListener *dcl)
  2639. {
  2640. VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
  2641. VncState *vs, *vn;
  2642. int has_dirty, rects = 0;
  2643. if (QTAILQ_EMPTY(&vd->clients)) {
  2644. update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
  2645. return;
  2646. }
  2647. graphic_hw_update(vd->dcl.con);
  2648. if (vnc_trylock_display(vd)) {
  2649. update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
  2650. return;
  2651. }
  2652. has_dirty = vnc_refresh_server_surface(vd);
  2653. vnc_unlock_display(vd);
  2654. QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
  2655. rects += vnc_update_client(vs, has_dirty);
  2656. /* vs might be free()ed here */
  2657. }
  2658. if (has_dirty && rects) {
  2659. vd->dcl.update_interval /= 2;
  2660. if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
  2661. vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
  2662. }
  2663. } else {
  2664. vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
  2665. if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
  2666. vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
  2667. }
  2668. }
  2669. }
  2670. static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc,
  2671. bool skipauth, bool websocket)
  2672. {
  2673. VncState *vs = g_new0(VncState, 1);
  2674. bool first_client = QTAILQ_EMPTY(&vd->clients);
  2675. int i;
  2676. trace_vnc_client_connect(vs, sioc);
  2677. vs->zrle = g_new0(VncZrle, 1);
  2678. vs->tight = g_new0(VncTight, 1);
  2679. vs->magic = VNC_MAGIC;
  2680. vs->sioc = sioc;
  2681. object_ref(OBJECT(vs->sioc));
  2682. vs->ioc = QIO_CHANNEL(sioc);
  2683. object_ref(OBJECT(vs->ioc));
  2684. vs->vd = vd;
  2685. buffer_init(&vs->input, "vnc-input/%p", sioc);
  2686. buffer_init(&vs->output, "vnc-output/%p", sioc);
  2687. buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc);
  2688. buffer_init(&vs->tight->tight, "vnc-tight/%p", sioc);
  2689. buffer_init(&vs->tight->zlib, "vnc-tight-zlib/%p", sioc);
  2690. buffer_init(&vs->tight->gradient, "vnc-tight-gradient/%p", sioc);
  2691. #ifdef CONFIG_VNC_JPEG
  2692. buffer_init(&vs->tight->jpeg, "vnc-tight-jpeg/%p", sioc);
  2693. #endif
  2694. #ifdef CONFIG_VNC_PNG
  2695. buffer_init(&vs->tight->png, "vnc-tight-png/%p", sioc);
  2696. #endif
  2697. buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc);
  2698. buffer_init(&vs->zrle->zrle, "vnc-zrle/%p", sioc);
  2699. buffer_init(&vs->zrle->fb, "vnc-zrle-fb/%p", sioc);
  2700. buffer_init(&vs->zrle->zlib, "vnc-zrle-zlib/%p", sioc);
  2701. if (skipauth) {
  2702. vs->auth = VNC_AUTH_NONE;
  2703. vs->subauth = VNC_AUTH_INVALID;
  2704. } else {
  2705. if (websocket) {
  2706. vs->auth = vd->ws_auth;
  2707. vs->subauth = VNC_AUTH_INVALID;
  2708. } else {
  2709. vs->auth = vd->auth;
  2710. vs->subauth = vd->subauth;
  2711. }
  2712. }
  2713. VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
  2714. sioc, websocket, vs->auth, vs->subauth);
  2715. vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
  2716. for (i = 0; i < VNC_STAT_ROWS; ++i) {
  2717. vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS);
  2718. }
  2719. VNC_DEBUG("New client on socket %p\n", vs->sioc);
  2720. update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
  2721. qio_channel_set_blocking(vs->ioc, false, NULL);
  2722. if (vs->ioc_tag) {
  2723. g_source_remove(vs->ioc_tag);
  2724. }
  2725. if (websocket) {
  2726. vs->websocket = 1;
  2727. if (vd->tlscreds) {
  2728. vs->ioc_tag = qio_channel_add_watch(
  2729. vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL);
  2730. } else {
  2731. vs->ioc_tag = qio_channel_add_watch(
  2732. vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL);
  2733. }
  2734. } else {
  2735. vs->ioc_tag = qio_channel_add_watch(
  2736. vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
  2737. }
  2738. vnc_client_cache_addr(vs);
  2739. vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
  2740. vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
  2741. vs->last_x = -1;
  2742. vs->last_y = -1;
  2743. vs->as.freq = 44100;
  2744. vs->as.nchannels = 2;
  2745. vs->as.fmt = AUDIO_FORMAT_S16;
  2746. vs->as.endianness = 0;
  2747. qemu_mutex_init(&vs->output_mutex);
  2748. vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
  2749. QTAILQ_INSERT_TAIL(&vd->clients, vs, next);
  2750. if (first_client) {
  2751. vnc_update_server_surface(vd);
  2752. }
  2753. graphic_hw_update(vd->dcl.con);
  2754. if (!vs->websocket) {
  2755. vnc_start_protocol(vs);
  2756. }
  2757. if (vd->num_connecting > vd->connections_limit) {
  2758. QTAILQ_FOREACH(vs, &vd->clients, next) {
  2759. if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) {
  2760. vnc_disconnect_start(vs);
  2761. return;
  2762. }
  2763. }
  2764. }
  2765. }
  2766. void vnc_start_protocol(VncState *vs)
  2767. {
  2768. vnc_write(vs, "RFB 003.008\n", 12);
  2769. vnc_flush(vs);
  2770. vnc_read_when(vs, protocol_version, 12);
  2771. vs->mouse_mode_notifier.notify = check_pointer_type_change;
  2772. qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
  2773. }
  2774. static void vnc_listen_io(QIONetListener *listener,
  2775. QIOChannelSocket *cioc,
  2776. void *opaque)
  2777. {
  2778. VncDisplay *vd = opaque;
  2779. bool isWebsock = listener == vd->wslistener;
  2780. qio_channel_set_name(QIO_CHANNEL(cioc),
  2781. isWebsock ? "vnc-ws-server" : "vnc-server");
  2782. qio_channel_set_delay(QIO_CHANNEL(cioc), false);
  2783. vnc_connect(vd, cioc, false, isWebsock);
  2784. }
  2785. static const DisplayChangeListenerOps dcl_ops = {
  2786. .dpy_name = "vnc",
  2787. .dpy_refresh = vnc_refresh,
  2788. .dpy_gfx_update = vnc_dpy_update,
  2789. .dpy_gfx_switch = vnc_dpy_switch,
  2790. .dpy_gfx_check_format = qemu_pixman_check_format,
  2791. .dpy_mouse_set = vnc_mouse_set,
  2792. .dpy_cursor_define = vnc_dpy_cursor_define,
  2793. };
  2794. void vnc_display_init(const char *id, Error **errp)
  2795. {
  2796. VncDisplay *vd;
  2797. if (vnc_display_find(id) != NULL) {
  2798. return;
  2799. }
  2800. vd = g_malloc0(sizeof(*vd));
  2801. vd->id = strdup(id);
  2802. QTAILQ_INSERT_TAIL(&vnc_displays, vd, next);
  2803. QTAILQ_INIT(&vd->clients);
  2804. vd->expires = TIME_MAX;
  2805. if (keyboard_layout) {
  2806. trace_vnc_key_map_init(keyboard_layout);
  2807. vd->kbd_layout = init_keyboard_layout(name2keysym,
  2808. keyboard_layout, errp);
  2809. } else {
  2810. vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us", errp);
  2811. }
  2812. if (!vd->kbd_layout) {
  2813. return;
  2814. }
  2815. vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
  2816. vd->connections_limit = 32;
  2817. qemu_mutex_init(&vd->mutex);
  2818. vnc_start_worker_thread();
  2819. vd->dcl.ops = &dcl_ops;
  2820. register_displaychangelistener(&vd->dcl);
  2821. vd->kbd = qkbd_state_init(vd->dcl.con);
  2822. }
  2823. static void vnc_display_close(VncDisplay *vd)
  2824. {
  2825. if (!vd) {
  2826. return;
  2827. }
  2828. vd->is_unix = false;
  2829. if (vd->listener) {
  2830. qio_net_listener_disconnect(vd->listener);
  2831. object_unref(OBJECT(vd->listener));
  2832. }
  2833. vd->listener = NULL;
  2834. if (vd->wslistener) {
  2835. qio_net_listener_disconnect(vd->wslistener);
  2836. object_unref(OBJECT(vd->wslistener));
  2837. }
  2838. vd->wslistener = NULL;
  2839. vd->auth = VNC_AUTH_INVALID;
  2840. vd->subauth = VNC_AUTH_INVALID;
  2841. if (vd->tlscreds) {
  2842. object_unparent(OBJECT(vd->tlscreds));
  2843. vd->tlscreds = NULL;
  2844. }
  2845. if (vd->tlsauthz) {
  2846. object_unparent(OBJECT(vd->tlsauthz));
  2847. vd->tlsauthz = NULL;
  2848. }
  2849. g_free(vd->tlsauthzid);
  2850. vd->tlsauthzid = NULL;
  2851. if (vd->lock_key_sync) {
  2852. qemu_remove_led_event_handler(vd->led);
  2853. vd->led = NULL;
  2854. }
  2855. #ifdef CONFIG_VNC_SASL
  2856. if (vd->sasl.authz) {
  2857. object_unparent(OBJECT(vd->sasl.authz));
  2858. vd->sasl.authz = NULL;
  2859. }
  2860. g_free(vd->sasl.authzid);
  2861. vd->sasl.authzid = NULL;
  2862. #endif
  2863. }
  2864. int vnc_display_password(const char *id, const char *password)
  2865. {
  2866. VncDisplay *vd = vnc_display_find(id);
  2867. if (!vd) {
  2868. return -EINVAL;
  2869. }
  2870. if (vd->auth == VNC_AUTH_NONE) {
  2871. error_printf_unless_qmp("If you want use passwords please enable "
  2872. "password auth using '-vnc ${dpy},password'.\n");
  2873. return -EINVAL;
  2874. }
  2875. g_free(vd->password);
  2876. vd->password = g_strdup(password);
  2877. return 0;
  2878. }
  2879. int vnc_display_pw_expire(const char *id, time_t expires)
  2880. {
  2881. VncDisplay *vd = vnc_display_find(id);
  2882. if (!vd) {
  2883. return -EINVAL;
  2884. }
  2885. vd->expires = expires;
  2886. return 0;
  2887. }
  2888. static void vnc_display_print_local_addr(VncDisplay *vd)
  2889. {
  2890. SocketAddress *addr;
  2891. Error *err = NULL;
  2892. if (!vd->listener || !vd->listener->nsioc) {
  2893. return;
  2894. }
  2895. addr = qio_channel_socket_get_local_address(vd->listener->sioc[0], &err);
  2896. if (!addr) {
  2897. return;
  2898. }
  2899. if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
  2900. qapi_free_SocketAddress(addr);
  2901. return;
  2902. }
  2903. error_printf_unless_qmp("VNC server running on %s:%s\n",
  2904. addr->u.inet.host,
  2905. addr->u.inet.port);
  2906. qapi_free_SocketAddress(addr);
  2907. }
  2908. static QemuOptsList qemu_vnc_opts = {
  2909. .name = "vnc",
  2910. .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head),
  2911. .implied_opt_name = "vnc",
  2912. .desc = {
  2913. {
  2914. .name = "vnc",
  2915. .type = QEMU_OPT_STRING,
  2916. },{
  2917. .name = "websocket",
  2918. .type = QEMU_OPT_STRING,
  2919. },{
  2920. .name = "tls-creds",
  2921. .type = QEMU_OPT_STRING,
  2922. },{
  2923. .name = "share",
  2924. .type = QEMU_OPT_STRING,
  2925. },{
  2926. .name = "display",
  2927. .type = QEMU_OPT_STRING,
  2928. },{
  2929. .name = "head",
  2930. .type = QEMU_OPT_NUMBER,
  2931. },{
  2932. .name = "connections",
  2933. .type = QEMU_OPT_NUMBER,
  2934. },{
  2935. .name = "to",
  2936. .type = QEMU_OPT_NUMBER,
  2937. },{
  2938. .name = "ipv4",
  2939. .type = QEMU_OPT_BOOL,
  2940. },{
  2941. .name = "ipv6",
  2942. .type = QEMU_OPT_BOOL,
  2943. },{
  2944. .name = "password",
  2945. .type = QEMU_OPT_BOOL,
  2946. },{
  2947. .name = "reverse",
  2948. .type = QEMU_OPT_BOOL,
  2949. },{
  2950. .name = "lock-key-sync",
  2951. .type = QEMU_OPT_BOOL,
  2952. },{
  2953. .name = "key-delay-ms",
  2954. .type = QEMU_OPT_NUMBER,
  2955. },{
  2956. .name = "sasl",
  2957. .type = QEMU_OPT_BOOL,
  2958. },{
  2959. .name = "acl",
  2960. .type = QEMU_OPT_BOOL,
  2961. },{
  2962. .name = "tls-authz",
  2963. .type = QEMU_OPT_STRING,
  2964. },{
  2965. .name = "sasl-authz",
  2966. .type = QEMU_OPT_STRING,
  2967. },{
  2968. .name = "lossy",
  2969. .type = QEMU_OPT_BOOL,
  2970. },{
  2971. .name = "non-adaptive",
  2972. .type = QEMU_OPT_BOOL,
  2973. },{
  2974. .name = "audiodev",
  2975. .type = QEMU_OPT_STRING,
  2976. },
  2977. { /* end of list */ }
  2978. },
  2979. };
  2980. static int
  2981. vnc_display_setup_auth(int *auth,
  2982. int *subauth,
  2983. QCryptoTLSCreds *tlscreds,
  2984. bool password,
  2985. bool sasl,
  2986. bool websocket,
  2987. Error **errp)
  2988. {
  2989. /*
  2990. * We have a choice of 3 authentication options
  2991. *
  2992. * 1. none
  2993. * 2. vnc
  2994. * 3. sasl
  2995. *
  2996. * The channel can be run in 2 modes
  2997. *
  2998. * 1. clear
  2999. * 2. tls
  3000. *
  3001. * And TLS can use 2 types of credentials
  3002. *
  3003. * 1. anon
  3004. * 2. x509
  3005. *
  3006. * We thus have 9 possible logical combinations
  3007. *
  3008. * 1. clear + none
  3009. * 2. clear + vnc
  3010. * 3. clear + sasl
  3011. * 4. tls + anon + none
  3012. * 5. tls + anon + vnc
  3013. * 6. tls + anon + sasl
  3014. * 7. tls + x509 + none
  3015. * 8. tls + x509 + vnc
  3016. * 9. tls + x509 + sasl
  3017. *
  3018. * These need to be mapped into the VNC auth schemes
  3019. * in an appropriate manner. In regular VNC, all the
  3020. * TLS options get mapped into VNC_AUTH_VENCRYPT
  3021. * sub-auth types.
  3022. *
  3023. * In websockets, the https:// protocol already provides
  3024. * TLS support, so there is no need to make use of the
  3025. * VeNCrypt extension. Furthermore, websockets browser
  3026. * clients could not use VeNCrypt even if they wanted to,
  3027. * as they cannot control when the TLS handshake takes
  3028. * place. Thus there is no option but to rely on https://,
  3029. * meaning combinations 4->6 and 7->9 will be mapped to
  3030. * VNC auth schemes in the same way as combos 1->3.
  3031. *
  3032. * Regardless of fact that we have a different mapping to
  3033. * VNC auth mechs for plain VNC vs websockets VNC, the end
  3034. * result has the same security characteristics.
  3035. */
  3036. if (websocket || !tlscreds) {
  3037. if (password) {
  3038. VNC_DEBUG("Initializing VNC server with password auth\n");
  3039. *auth = VNC_AUTH_VNC;
  3040. } else if (sasl) {
  3041. VNC_DEBUG("Initializing VNC server with SASL auth\n");
  3042. *auth = VNC_AUTH_SASL;
  3043. } else {
  3044. VNC_DEBUG("Initializing VNC server with no auth\n");
  3045. *auth = VNC_AUTH_NONE;
  3046. }
  3047. *subauth = VNC_AUTH_INVALID;
  3048. } else {
  3049. bool is_x509 = object_dynamic_cast(OBJECT(tlscreds),
  3050. TYPE_QCRYPTO_TLS_CREDS_X509) != NULL;
  3051. bool is_anon = object_dynamic_cast(OBJECT(tlscreds),
  3052. TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL;
  3053. if (!is_x509 && !is_anon) {
  3054. error_setg(errp,
  3055. "Unsupported TLS cred type %s",
  3056. object_get_typename(OBJECT(tlscreds)));
  3057. return -1;
  3058. }
  3059. *auth = VNC_AUTH_VENCRYPT;
  3060. if (password) {
  3061. if (is_x509) {
  3062. VNC_DEBUG("Initializing VNC server with x509 password auth\n");
  3063. *subauth = VNC_AUTH_VENCRYPT_X509VNC;
  3064. } else {
  3065. VNC_DEBUG("Initializing VNC server with TLS password auth\n");
  3066. *subauth = VNC_AUTH_VENCRYPT_TLSVNC;
  3067. }
  3068. } else if (sasl) {
  3069. if (is_x509) {
  3070. VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
  3071. *subauth = VNC_AUTH_VENCRYPT_X509SASL;
  3072. } else {
  3073. VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
  3074. *subauth = VNC_AUTH_VENCRYPT_TLSSASL;
  3075. }
  3076. } else {
  3077. if (is_x509) {
  3078. VNC_DEBUG("Initializing VNC server with x509 no auth\n");
  3079. *subauth = VNC_AUTH_VENCRYPT_X509NONE;
  3080. } else {
  3081. VNC_DEBUG("Initializing VNC server with TLS no auth\n");
  3082. *subauth = VNC_AUTH_VENCRYPT_TLSNONE;
  3083. }
  3084. }
  3085. }
  3086. return 0;
  3087. }
  3088. static int vnc_display_get_address(const char *addrstr,
  3089. bool websocket,
  3090. bool reverse,
  3091. int displaynum,
  3092. int to,
  3093. bool has_ipv4,
  3094. bool has_ipv6,
  3095. bool ipv4,
  3096. bool ipv6,
  3097. SocketAddress **retaddr,
  3098. Error **errp)
  3099. {
  3100. int ret = -1;
  3101. SocketAddress *addr = NULL;
  3102. addr = g_new0(SocketAddress, 1);
  3103. if (strncmp(addrstr, "unix:", 5) == 0) {
  3104. addr->type = SOCKET_ADDRESS_TYPE_UNIX;
  3105. addr->u.q_unix.path = g_strdup(addrstr + 5);
  3106. if (websocket) {
  3107. error_setg(errp, "UNIX sockets not supported with websock");
  3108. goto cleanup;
  3109. }
  3110. if (to) {
  3111. error_setg(errp, "Port range not support with UNIX socket");
  3112. goto cleanup;
  3113. }
  3114. ret = 0;
  3115. } else {
  3116. const char *port;
  3117. size_t hostlen;
  3118. unsigned long long baseport = 0;
  3119. InetSocketAddress *inet;
  3120. port = strrchr(addrstr, ':');
  3121. if (!port) {
  3122. if (websocket) {
  3123. hostlen = 0;
  3124. port = addrstr;
  3125. } else {
  3126. error_setg(errp, "no vnc port specified");
  3127. goto cleanup;
  3128. }
  3129. } else {
  3130. hostlen = port - addrstr;
  3131. port++;
  3132. if (*port == '\0') {
  3133. error_setg(errp, "vnc port cannot be empty");
  3134. goto cleanup;
  3135. }
  3136. }
  3137. addr->type = SOCKET_ADDRESS_TYPE_INET;
  3138. inet = &addr->u.inet;
  3139. if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
  3140. inet->host = g_strndup(addrstr + 1, hostlen - 2);
  3141. } else {
  3142. inet->host = g_strndup(addrstr, hostlen);
  3143. }
  3144. /* plain VNC port is just an offset, for websocket
  3145. * port is absolute */
  3146. if (websocket) {
  3147. if (g_str_equal(addrstr, "") ||
  3148. g_str_equal(addrstr, "on")) {
  3149. if (displaynum == -1) {
  3150. error_setg(errp, "explicit websocket port is required");
  3151. goto cleanup;
  3152. }
  3153. inet->port = g_strdup_printf(
  3154. "%d", displaynum + 5700);
  3155. if (to) {
  3156. inet->has_to = true;
  3157. inet->to = to + 5700;
  3158. }
  3159. } else {
  3160. inet->port = g_strdup(port);
  3161. }
  3162. } else {
  3163. int offset = reverse ? 0 : 5900;
  3164. if (parse_uint_full(port, &baseport, 10) < 0) {
  3165. error_setg(errp, "can't convert to a number: %s", port);
  3166. goto cleanup;
  3167. }
  3168. if (baseport > 65535 ||
  3169. baseport + offset > 65535) {
  3170. error_setg(errp, "port %s out of range", port);
  3171. goto cleanup;
  3172. }
  3173. inet->port = g_strdup_printf(
  3174. "%d", (int)baseport + offset);
  3175. if (to) {
  3176. inet->has_to = true;
  3177. inet->to = to + offset;
  3178. }
  3179. }
  3180. inet->ipv4 = ipv4;
  3181. inet->has_ipv4 = has_ipv4;
  3182. inet->ipv6 = ipv6;
  3183. inet->has_ipv6 = has_ipv6;
  3184. ret = baseport;
  3185. }
  3186. *retaddr = addr;
  3187. cleanup:
  3188. if (ret < 0) {
  3189. qapi_free_SocketAddress(addr);
  3190. }
  3191. return ret;
  3192. }
  3193. static void vnc_free_addresses(SocketAddress ***retsaddr,
  3194. size_t *retnsaddr)
  3195. {
  3196. size_t i;
  3197. for (i = 0; i < *retnsaddr; i++) {
  3198. qapi_free_SocketAddress((*retsaddr)[i]);
  3199. }
  3200. g_free(*retsaddr);
  3201. *retsaddr = NULL;
  3202. *retnsaddr = 0;
  3203. }
  3204. static int vnc_display_get_addresses(QemuOpts *opts,
  3205. bool reverse,
  3206. SocketAddress ***retsaddr,
  3207. size_t *retnsaddr,
  3208. SocketAddress ***retwsaddr,
  3209. size_t *retnwsaddr,
  3210. Error **errp)
  3211. {
  3212. SocketAddress *saddr = NULL;
  3213. SocketAddress *wsaddr = NULL;
  3214. QemuOptsIter addriter;
  3215. const char *addr;
  3216. int to = qemu_opt_get_number(opts, "to", 0);
  3217. bool has_ipv4 = qemu_opt_get(opts, "ipv4");
  3218. bool has_ipv6 = qemu_opt_get(opts, "ipv6");
  3219. bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false);
  3220. bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false);
  3221. int displaynum = -1;
  3222. int ret = -1;
  3223. *retsaddr = NULL;
  3224. *retnsaddr = 0;
  3225. *retwsaddr = NULL;
  3226. *retnwsaddr = 0;
  3227. addr = qemu_opt_get(opts, "vnc");
  3228. if (addr == NULL || g_str_equal(addr, "none")) {
  3229. ret = 0;
  3230. goto cleanup;
  3231. }
  3232. if (qemu_opt_get(opts, "websocket") &&
  3233. !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) {
  3234. error_setg(errp,
  3235. "SHA1 hash support is required for websockets");
  3236. goto cleanup;
  3237. }
  3238. qemu_opt_iter_init(&addriter, opts, "vnc");
  3239. while ((addr = qemu_opt_iter_next(&addriter)) != NULL) {
  3240. int rv;
  3241. rv = vnc_display_get_address(addr, false, reverse, 0, to,
  3242. has_ipv4, has_ipv6,
  3243. ipv4, ipv6,
  3244. &saddr, errp);
  3245. if (rv < 0) {
  3246. goto cleanup;
  3247. }
  3248. /* Historical compat - first listen address can be used
  3249. * to set the default websocket port
  3250. */
  3251. if (displaynum == -1) {
  3252. displaynum = rv;
  3253. }
  3254. *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1);
  3255. (*retsaddr)[(*retnsaddr)++] = saddr;
  3256. }
  3257. /* If we had multiple primary displays, we don't do defaults
  3258. * for websocket, and require explicit config instead. */
  3259. if (*retnsaddr > 1) {
  3260. displaynum = -1;
  3261. }
  3262. qemu_opt_iter_init(&addriter, opts, "websocket");
  3263. while ((addr = qemu_opt_iter_next(&addriter)) != NULL) {
  3264. if (vnc_display_get_address(addr, true, reverse, displaynum, to,
  3265. has_ipv4, has_ipv6,
  3266. ipv4, ipv6,
  3267. &wsaddr, errp) < 0) {
  3268. goto cleanup;
  3269. }
  3270. /* Historical compat - if only a single listen address was
  3271. * provided, then this is used to set the default listen
  3272. * address for websocket too
  3273. */
  3274. if (*retnsaddr == 1 &&
  3275. (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET &&
  3276. wsaddr->type == SOCKET_ADDRESS_TYPE_INET &&
  3277. g_str_equal(wsaddr->u.inet.host, "") &&
  3278. !g_str_equal((*retsaddr)[0]->u.inet.host, "")) {
  3279. g_free(wsaddr->u.inet.host);
  3280. wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host);
  3281. }
  3282. *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1);
  3283. (*retwsaddr)[(*retnwsaddr)++] = wsaddr;
  3284. }
  3285. ret = 0;
  3286. cleanup:
  3287. if (ret < 0) {
  3288. vnc_free_addresses(retsaddr, retnsaddr);
  3289. vnc_free_addresses(retwsaddr, retnwsaddr);
  3290. }
  3291. return ret;
  3292. }
  3293. static int vnc_display_connect(VncDisplay *vd,
  3294. SocketAddress **saddr,
  3295. size_t nsaddr,
  3296. SocketAddress **wsaddr,
  3297. size_t nwsaddr,
  3298. Error **errp)
  3299. {
  3300. /* connect to viewer */
  3301. QIOChannelSocket *sioc = NULL;
  3302. if (nwsaddr != 0) {
  3303. error_setg(errp, "Cannot use websockets in reverse mode");
  3304. return -1;
  3305. }
  3306. if (nsaddr != 1) {
  3307. error_setg(errp, "Expected a single address in reverse mode");
  3308. return -1;
  3309. }
  3310. /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
  3311. vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX;
  3312. sioc = qio_channel_socket_new();
  3313. qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse");
  3314. if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) {
  3315. return -1;
  3316. }
  3317. vnc_connect(vd, sioc, false, false);
  3318. object_unref(OBJECT(sioc));
  3319. return 0;
  3320. }
  3321. static int vnc_display_listen(VncDisplay *vd,
  3322. SocketAddress **saddr,
  3323. size_t nsaddr,
  3324. SocketAddress **wsaddr,
  3325. size_t nwsaddr,
  3326. Error **errp)
  3327. {
  3328. size_t i;
  3329. if (nsaddr) {
  3330. vd->listener = qio_net_listener_new();
  3331. qio_net_listener_set_name(vd->listener, "vnc-listen");
  3332. for (i = 0; i < nsaddr; i++) {
  3333. if (qio_net_listener_open_sync(vd->listener,
  3334. saddr[i], 1,
  3335. errp) < 0) {
  3336. return -1;
  3337. }
  3338. }
  3339. qio_net_listener_set_client_func(vd->listener,
  3340. vnc_listen_io, vd, NULL);
  3341. }
  3342. if (nwsaddr) {
  3343. vd->wslistener = qio_net_listener_new();
  3344. qio_net_listener_set_name(vd->wslistener, "vnc-ws-listen");
  3345. for (i = 0; i < nwsaddr; i++) {
  3346. if (qio_net_listener_open_sync(vd->wslistener,
  3347. wsaddr[i], 1,
  3348. errp) < 0) {
  3349. return -1;
  3350. }
  3351. }
  3352. qio_net_listener_set_client_func(vd->wslistener,
  3353. vnc_listen_io, vd, NULL);
  3354. }
  3355. return 0;
  3356. }
  3357. void vnc_display_open(const char *id, Error **errp)
  3358. {
  3359. VncDisplay *vd = vnc_display_find(id);
  3360. QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id);
  3361. SocketAddress **saddr = NULL, **wsaddr = NULL;
  3362. size_t nsaddr, nwsaddr;
  3363. const char *share, *device_id;
  3364. QemuConsole *con;
  3365. bool password = false;
  3366. bool reverse = false;
  3367. const char *credid;
  3368. bool sasl = false;
  3369. int acl = 0;
  3370. const char *tlsauthz;
  3371. const char *saslauthz;
  3372. int lock_key_sync = 1;
  3373. int key_delay_ms;
  3374. const char *audiodev;
  3375. if (!vd) {
  3376. error_setg(errp, "VNC display not active");
  3377. return;
  3378. }
  3379. vnc_display_close(vd);
  3380. if (!opts) {
  3381. return;
  3382. }
  3383. reverse = qemu_opt_get_bool(opts, "reverse", false);
  3384. if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr,
  3385. &wsaddr, &nwsaddr, errp) < 0) {
  3386. goto fail;
  3387. }
  3388. password = qemu_opt_get_bool(opts, "password", false);
  3389. if (password) {
  3390. if (fips_get_state()) {
  3391. error_setg(errp,
  3392. "VNC password auth disabled due to FIPS mode, "
  3393. "consider using the VeNCrypt or SASL authentication "
  3394. "methods as an alternative");
  3395. goto fail;
  3396. }
  3397. if (!qcrypto_cipher_supports(
  3398. QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) {
  3399. error_setg(errp,
  3400. "Cipher backend does not support DES RFB algorithm");
  3401. goto fail;
  3402. }
  3403. }
  3404. lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true);
  3405. key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10);
  3406. sasl = qemu_opt_get_bool(opts, "sasl", false);
  3407. #ifndef CONFIG_VNC_SASL
  3408. if (sasl) {
  3409. error_setg(errp, "VNC SASL auth requires cyrus-sasl support");
  3410. goto fail;
  3411. }
  3412. #endif /* CONFIG_VNC_SASL */
  3413. credid = qemu_opt_get(opts, "tls-creds");
  3414. if (credid) {
  3415. Object *creds;
  3416. creds = object_resolve_path_component(
  3417. object_get_objects_root(), credid);
  3418. if (!creds) {
  3419. error_setg(errp, "No TLS credentials with id '%s'",
  3420. credid);
  3421. goto fail;
  3422. }
  3423. vd->tlscreds = (QCryptoTLSCreds *)
  3424. object_dynamic_cast(creds,
  3425. TYPE_QCRYPTO_TLS_CREDS);
  3426. if (!vd->tlscreds) {
  3427. error_setg(errp, "Object with id '%s' is not TLS credentials",
  3428. credid);
  3429. goto fail;
  3430. }
  3431. object_ref(OBJECT(vd->tlscreds));
  3432. if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
  3433. error_setg(errp,
  3434. "Expecting TLS credentials with a server endpoint");
  3435. goto fail;
  3436. }
  3437. }
  3438. if (qemu_opt_get(opts, "acl")) {
  3439. error_report("The 'acl' option to -vnc is deprecated. "
  3440. "Please use the 'tls-authz' and 'sasl-authz' "
  3441. "options instead");
  3442. }
  3443. acl = qemu_opt_get_bool(opts, "acl", false);
  3444. tlsauthz = qemu_opt_get(opts, "tls-authz");
  3445. if (acl && tlsauthz) {
  3446. error_setg(errp, "'acl' option is mutually exclusive with the "
  3447. "'tls-authz' option");
  3448. goto fail;
  3449. }
  3450. if (tlsauthz && !vd->tlscreds) {
  3451. error_setg(errp, "'tls-authz' provided but TLS is not enabled");
  3452. goto fail;
  3453. }
  3454. saslauthz = qemu_opt_get(opts, "sasl-authz");
  3455. if (acl && saslauthz) {
  3456. error_setg(errp, "'acl' option is mutually exclusive with the "
  3457. "'sasl-authz' option");
  3458. goto fail;
  3459. }
  3460. if (saslauthz && !sasl) {
  3461. error_setg(errp, "'sasl-authz' provided but SASL auth is not enabled");
  3462. goto fail;
  3463. }
  3464. share = qemu_opt_get(opts, "share");
  3465. if (share) {
  3466. if (strcmp(share, "ignore") == 0) {
  3467. vd->share_policy = VNC_SHARE_POLICY_IGNORE;
  3468. } else if (strcmp(share, "allow-exclusive") == 0) {
  3469. vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
  3470. } else if (strcmp(share, "force-shared") == 0) {
  3471. vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
  3472. } else {
  3473. error_setg(errp, "unknown vnc share= option");
  3474. goto fail;
  3475. }
  3476. } else {
  3477. vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
  3478. }
  3479. vd->connections_limit = qemu_opt_get_number(opts, "connections", 32);
  3480. #ifdef CONFIG_VNC_JPEG
  3481. vd->lossy = qemu_opt_get_bool(opts, "lossy", false);
  3482. #endif
  3483. vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false);
  3484. /* adaptive updates are only used with tight encoding and
  3485. * if lossy updates are enabled so we can disable all the
  3486. * calculations otherwise */
  3487. if (!vd->lossy) {
  3488. vd->non_adaptive = true;
  3489. }
  3490. if (tlsauthz) {
  3491. vd->tlsauthzid = g_strdup(tlsauthz);
  3492. } else if (acl) {
  3493. if (strcmp(vd->id, "default") == 0) {
  3494. vd->tlsauthzid = g_strdup("vnc.x509dname");
  3495. } else {
  3496. vd->tlsauthzid = g_strdup_printf("vnc.%s.x509dname", vd->id);
  3497. }
  3498. vd->tlsauthz = QAUTHZ(qauthz_list_new(vd->tlsauthzid,
  3499. QAUTHZ_LIST_POLICY_DENY,
  3500. &error_abort));
  3501. }
  3502. #ifdef CONFIG_VNC_SASL
  3503. if (sasl) {
  3504. if (saslauthz) {
  3505. vd->sasl.authzid = g_strdup(saslauthz);
  3506. } else if (acl) {
  3507. if (strcmp(vd->id, "default") == 0) {
  3508. vd->sasl.authzid = g_strdup("vnc.username");
  3509. } else {
  3510. vd->sasl.authzid = g_strdup_printf("vnc.%s.username", vd->id);
  3511. }
  3512. vd->sasl.authz = QAUTHZ(qauthz_list_new(vd->sasl.authzid,
  3513. QAUTHZ_LIST_POLICY_DENY,
  3514. &error_abort));
  3515. }
  3516. }
  3517. #endif
  3518. if (vnc_display_setup_auth(&vd->auth, &vd->subauth,
  3519. vd->tlscreds, password,
  3520. sasl, false, errp) < 0) {
  3521. goto fail;
  3522. }
  3523. trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth);
  3524. if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth,
  3525. vd->tlscreds, password,
  3526. sasl, true, errp) < 0) {
  3527. goto fail;
  3528. }
  3529. trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth);
  3530. #ifdef CONFIG_VNC_SASL
  3531. if (sasl) {
  3532. int saslErr = sasl_server_init(NULL, "qemu");
  3533. if (saslErr != SASL_OK) {
  3534. error_setg(errp, "Failed to initialize SASL auth: %s",
  3535. sasl_errstring(saslErr, NULL, NULL));
  3536. goto fail;
  3537. }
  3538. }
  3539. #endif
  3540. vd->lock_key_sync = lock_key_sync;
  3541. if (lock_key_sync) {
  3542. vd->led = qemu_add_led_event_handler(kbd_leds, vd);
  3543. }
  3544. vd->ledstate = 0;
  3545. audiodev = qemu_opt_get(opts, "audiodev");
  3546. if (audiodev) {
  3547. vd->audio_state = audio_state_by_name(audiodev);
  3548. if (!vd->audio_state) {
  3549. error_setg(errp, "Audiodev '%s' not found", audiodev);
  3550. goto fail;
  3551. }
  3552. }
  3553. device_id = qemu_opt_get(opts, "display");
  3554. if (device_id) {
  3555. int head = qemu_opt_get_number(opts, "head", 0);
  3556. Error *err = NULL;
  3557. con = qemu_console_lookup_by_device_name(device_id, head, &err);
  3558. if (err) {
  3559. error_propagate(errp, err);
  3560. goto fail;
  3561. }
  3562. } else {
  3563. con = NULL;
  3564. }
  3565. if (con != vd->dcl.con) {
  3566. qkbd_state_free(vd->kbd);
  3567. unregister_displaychangelistener(&vd->dcl);
  3568. vd->dcl.con = con;
  3569. register_displaychangelistener(&vd->dcl);
  3570. vd->kbd = qkbd_state_init(vd->dcl.con);
  3571. }
  3572. qkbd_state_set_delay(vd->kbd, key_delay_ms);
  3573. if (saddr == NULL) {
  3574. goto cleanup;
  3575. }
  3576. if (reverse) {
  3577. if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) {
  3578. goto fail;
  3579. }
  3580. } else {
  3581. if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) {
  3582. goto fail;
  3583. }
  3584. }
  3585. if (qemu_opt_get(opts, "to")) {
  3586. vnc_display_print_local_addr(vd);
  3587. }
  3588. cleanup:
  3589. vnc_free_addresses(&saddr, &nsaddr);
  3590. vnc_free_addresses(&wsaddr, &nwsaddr);
  3591. return;
  3592. fail:
  3593. vnc_display_close(vd);
  3594. goto cleanup;
  3595. }
  3596. void vnc_display_add_client(const char *id, int csock, bool skipauth)
  3597. {
  3598. VncDisplay *vd = vnc_display_find(id);
  3599. QIOChannelSocket *sioc;
  3600. if (!vd) {
  3601. return;
  3602. }
  3603. sioc = qio_channel_socket_new_fd(csock, NULL);
  3604. if (sioc) {
  3605. qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server");
  3606. vnc_connect(vd, sioc, skipauth, false);
  3607. object_unref(OBJECT(sioc));
  3608. }
  3609. }
  3610. static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts)
  3611. {
  3612. int i = 2;
  3613. char *id;
  3614. id = g_strdup("default");
  3615. while (qemu_opts_find(olist, id)) {
  3616. g_free(id);
  3617. id = g_strdup_printf("vnc%d", i++);
  3618. }
  3619. qemu_opts_set_id(opts, id);
  3620. }
  3621. QemuOpts *vnc_parse(const char *str, Error **errp)
  3622. {
  3623. QemuOptsList *olist = qemu_find_opts("vnc");
  3624. QemuOpts *opts = qemu_opts_parse(olist, str, true, errp);
  3625. const char *id;
  3626. if (!opts) {
  3627. return NULL;
  3628. }
  3629. id = qemu_opts_id(opts);
  3630. if (!id) {
  3631. /* auto-assign id if not present */
  3632. vnc_auto_assign_id(olist, opts);
  3633. }
  3634. return opts;
  3635. }
  3636. int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp)
  3637. {
  3638. Error *local_err = NULL;
  3639. char *id = (char *)qemu_opts_id(opts);
  3640. assert(id);
  3641. vnc_display_init(id, &local_err);
  3642. if (local_err) {
  3643. error_propagate(errp, local_err);
  3644. return -1;
  3645. }
  3646. vnc_display_open(id, &local_err);
  3647. if (local_err != NULL) {
  3648. error_propagate(errp, local_err);
  3649. return -1;
  3650. }
  3651. return 0;
  3652. }
  3653. static void vnc_register_config(void)
  3654. {
  3655. qemu_add_opts(&qemu_vnc_opts);
  3656. }
  3657. opts_init(vnc_register_config);