vnc.c 96 KB

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