2 * Remote framebuffer server
3 * NetWatch system management mode administration console
5 * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved.
6 * This program is free software; you can redistribute and/or modify it under
7 * the terms found in the file LICENSE in the root of this source tree.
18 #include "lwip/stats.h"
22 #define SET_PIXEL_FORMAT 0
23 #define SET_ENCODINGS 2
24 #define FB_UPDATE_REQUEST 3
26 #define POINTER_EVENT 5
27 #define CLIENT_CUT_TEXT 6
29 #define RFB_BUF_SIZE 1536
31 #define SCREEN_CHUNKS_X 8
32 #define SCREEN_CHUNKS_Y 8
48 struct server_init_message {
51 struct pixel_format fmt;
56 struct fb_update_req {
72 struct key_event_pkt {
79 struct pointer_event_pkt {
86 struct text_event_pkt {
93 struct update_header {
113 char data[RFB_BUF_SIZE];
117 char next_update_incremental;
118 char update_requested;
120 struct fb_update_req client_interest_area;
128 uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y];
134 uint32_t chunk_width;
135 uint32_t chunk_height;
137 uint32_t chunk_bytes_sent;
139 uint32_t chunk_checksum;
141 int chunk_actually_sent;
147 static struct server_init_message server_info;
149 static void init_server_info() {
150 server_info.name_length = htonl(8);
151 memcpy(server_info.name_string, "NetWatch", 8);
154 static void update_server_info() {
156 outputf("RFB: setting fmt %d", fb->curmode.format);
157 server_info.fb_width = htons(fb->curmode.xres);
158 server_info.fb_height = htons(fb->curmode.yres);
159 switch (fb->curmode.format) {
161 server_info.fmt.bpp = 32;
162 server_info.fmt.depth = 24;
163 server_info.fmt.big_endian = 0;
164 server_info.fmt.true_color = 1;
165 server_info.fmt.red_max = htons(255);
166 server_info.fmt.green_max = htons(255);
167 server_info.fmt.blue_max = htons(255);
168 server_info.fmt.red_shift = 0;
169 server_info.fmt.green_shift = 8;
170 server_info.fmt.blue_shift = 16;
173 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
177 outputf("RFB: fb null");
181 static int advance_chunk(struct rfb_state *state) {
183 state->chunk_xnum += 1;
185 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
186 state->chunk_ynum += 1;
187 state->chunk_xnum = 0;
190 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
191 state->chunk_ynum = 0;
192 state->send_state = SST_IDLE;
193 if (!(state->chunk_actually_sent))
194 state->try_in_a_bit = 1;
201 static int ceildiv(int a, int b) {
209 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
210 struct update_header hdr;
217 switch (state->send_state) {
222 if (state->update_requested) {
223 outputf("RFB send: update requested");
224 state->update_requested = 0;
225 state->chunk_actually_sent = 0;
226 state->send_state = SST_HEADER;
231 /* FALL THROUGH to SST_HEADER */
235 /* Calculate the width and height for this chunk, remembering
236 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
237 * height, we may need to have shorter chunks at the edge of
240 state->chunk_width = ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X);
241 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
242 totaldim = state->chunk_width * (state->chunk_xnum + 1);
243 if (totaldim > fb->curmode.xres) {
244 state->chunk_width -= (totaldim - fb->curmode.xres);
247 state->chunk_height = ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y);
248 state->chunk_ypos = state->chunk_height
250 totaldim = state->chunk_height * (state->chunk_ynum + 1);
251 if (totaldim > fb->curmode.yres) {
252 state->chunk_height -= (totaldim - fb->curmode.yres);
255 /* Do we _actually_ need to send this chunk? */
256 if (fb->checksum_rect) {
257 state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
258 state->chunk_width, state->chunk_height);
260 if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
261 if (advance_chunk(state))
265 /* Checksum gets set in data block, AFTER the data has been sent. */
268 state->chunk_actually_sent = 1;
272 state->chunk_bytes_sent = 0;
273 hdr.nrects = htons(1);
274 hdr.xpos = htons(state->chunk_xpos);
275 hdr.ypos = htons(state->chunk_ypos);
276 hdr.width = htons(state->chunk_width);
277 hdr.height= htons(state->chunk_height);
278 hdr.enctype = htonl(0);
280 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
284 outputf("RFB: header send error %d", err);
286 /* Try again later. */
290 state->send_state = SST_DATA;
293 fb->copy_pixels(state->blockbuf,
294 state->chunk_xpos, state->chunk_ypos,
295 state->chunk_width, state->chunk_height);
297 /* FALL THROUGH to SST_DATA */
301 bytes_left = 4 * state->chunk_width * state->chunk_height - state->chunk_bytes_sent;
303 if (bytes_left == 0) {
304 state->send_state = SST_HEADER;
305 state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum;
306 if (advance_chunk(state))
312 if (bytes_left > 1400) {
316 err = tcp_write(pcb, state->blockbuf + state->chunk_bytes_sent,
317 bytes_left, TCP_WRITE_FLAG_COPY);
320 state->chunk_bytes_sent += bytes_left;
323 outputf("RFB: send error %d", err);
328 if (tcp_sndbuf(pcb) == 0) {
334 if (tcp_output(pcb) != ERR_OK)
335 outputf("RFB: tcp_output bailed in send_fsm?");
338 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
339 struct rfb_state *state = arg;
340 send_fsm(pcb, state);
344 static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
345 struct rfb_state *state = arg;
346 if (state->try_in_a_bit) {
347 state->try_in_a_bit--;
348 if (!(state->try_in_a_bit)) {
349 state->update_requested = 1;
352 send_fsm(pcb, state);
359 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
360 outputf("close_conn: bailing");
364 mem_free(state->blockbuf);
367 outputf("close_conn: done");
376 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
380 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
383 switch(state->state) {
385 if (state->writepos < 12) return NEEDMORE;
387 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
389 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
390 /* Spec states that "RFB 003.005", an incorrect value,
391 * should be treated by the server as 3.3. */
393 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
395 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
398 outputf("RFB: Negotiation fail");
402 outputf("RFB: Negotiated v3.%d", state->version);
404 state->readpos += 12;
405 state->state = ST_CLIENTINIT;
407 /* We support one security type, currently "none".
408 * Send that and SecurityResult. */
409 if (state->version >= 7) {
410 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
412 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
420 if (state->version >= 7) {
421 /* Ignore the security type and ClientInit */
422 if (state->writepos < 2) return NEEDMORE;
425 /* Just ClientInit */
426 if (state->writepos < 1) return NEEDMORE;
430 state->state = ST_MAIN;
432 outputf("RFB: Sending server info", state->version);
433 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
439 if (state->writepos < 1) return NEEDMORE;
441 switch (state->data[0]) {
443 case SET_PIXEL_FORMAT:
445 if (state->writepos < (sizeof(struct pixel_format) + 4))
447 outputf("RFB: SetPixelFormat");
449 struct pixel_format * new_fmt =
450 (struct pixel_format *)(&state->data[4]);
454 state->readpos += sizeof(struct pixel_format) + 4;
458 if (state->writepos < 4) return NEEDMORE;
460 struct set_encs_req * req = (struct set_encs_req *)state->data;
462 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
464 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
465 if (state->writepos < pktsize) return NEEDMORE;
467 for (i = 0; i < ntohs(req->num); i++) {
468 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
472 state->readpos += pktsize;
475 case FB_UPDATE_REQUEST:
476 if (state->writepos < sizeof(struct fb_update_req))
478 outputf("RFB: UpdateRequest");
480 state->update_requested = 1;
481 memcpy(&state->client_interest_area, state->data,
482 sizeof(struct fb_update_req));
484 state->readpos += sizeof(struct fb_update_req);
488 if (state->writepos < sizeof(struct key_event_pkt))
491 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
493 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
494 kbd_inject_keysym(htonl(p->keysym), p->downflag);
496 state->readpos += sizeof(struct key_event_pkt);
500 if (state->writepos < sizeof(struct pointer_event_pkt))
502 outputf("RFB: Pointer");
506 state->readpos += sizeof(struct pointer_event_pkt);
509 case CLIENT_CUT_TEXT:
510 if (state->writepos < sizeof(struct text_event_pkt))
512 outputf("RFB: Cut Text");
514 struct text_event_pkt * pkt =
515 (struct text_event_pkt *)state->data;
517 if (state->writepos < sizeof(struct text_event_pkt)
523 state->readpos += sizeof(struct text_event_pkt)
528 outputf("RFB: Bad command: %d", state->data[0]);
532 outputf("RFB: Bad state");
537 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
538 struct pbuf *p, err_t err) {
539 struct rfb_state *state = arg;
545 outputf("RFB: recv err %d", err);
546 /* FIXME do something better here? */
551 outputf("RFB: Connection closed");
552 close_conn(pcb, state);
556 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
558 outputf("RFB: Overflow!");
559 close_conn(pcb, state);
563 copylen = pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
565 outputf("RFB: Processing %d, wp %d, cp %d", p->tot_len, state->writepos, copylen);
567 state->writepos += p->tot_len;
569 tcp_recved(pcb, p->tot_len);
573 switch (recv_fsm(pcb, state)) {
575 outputf("RFB FSM: blocking");
579 if (state->readpos == state->writepos) {
585 state->data + state->readpos,
586 state->writepos - state->readpos);
587 state->writepos -= state->readpos;
593 outputf("RFB: Protocol error");
594 close_conn(pcb, state);
601 /* Kick off a send. */
602 if (state->send_state == SST_IDLE && state->update_requested) {
603 send_fsm(pcb, state);
609 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
610 struct rfb_state *state;
613 LWIP_UNUSED_ARG(arg);
614 LWIP_UNUSED_ARG(err);
616 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
620 outputf("rfb_accept: out of memory\n");
624 memset(state, 0, sizeof(struct rfb_state));
626 blockbuf = mem_malloc(ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X)
627 * ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y) * 4);
631 outputf("rfb_accept: out of memory allocating blockbuf\n");
636 state->blockbuf = blockbuf;
637 state->state = ST_BEGIN;
638 state->send_state = SST_IDLE;
640 /* XXX: update_server_info() should be called from the 64ms timer, and deal
641 * with screen resizes appropriately. */
642 update_server_info();
645 tcp_recv(pcb, rfb_recv);
646 tcp_sent(pcb, rfb_sent);
647 tcp_poll(pcb, rfb_poll, 1);
649 tcp_err(pcb, rfb_err);
651 tcp_write(pcb, "RFB 003.008\n", 12, 0);
663 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
664 pcb = tcp_listen(pcb);
665 tcp_accept(pcb, rfb_accept);