8 #include "lwip/stats.h"
12 #define SET_PIXEL_FORMAT 0
13 #define SET_ENCODINGS 2
14 #define FB_UPDATE_REQUEST 3
16 #define POINTER_EVENT 5
17 #define CLIENT_CUT_TEXT 6
19 #define RFB_BUF_SIZE 512
21 #define SCREEN_CHUNKS_X 8
22 #define SCREEN_CHUNKS_Y 8
38 struct server_init_message {
41 struct pixel_format fmt;
46 struct fb_update_req {
62 struct key_event_pkt {
69 struct pointer_event_pkt {
76 struct text_event_pkt {
83 struct update_header {
103 char data[RFB_BUF_SIZE];
107 char next_update_incremental;
108 char update_requested;
110 struct fb_update_req client_interest_area;
118 uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y];
124 uint32_t chunk_width;
125 uint32_t chunk_height;
127 uint32_t chunk_bytes_sent;
129 uint32_t chunk_checksum;
131 int chunk_actually_sent;
137 static struct server_init_message server_info;
139 static void init_server_info() {
140 server_info.name_length = htonl(8);
141 memcpy(server_info.name_string, "NetWatch", 8);
144 static void update_server_info() {
146 outputf("RFB: setting fmt %d", fb->curmode.format);
147 server_info.fb_width = htons(fb->curmode.xres);
148 server_info.fb_height = htons(fb->curmode.yres);
149 switch (fb->curmode.format) {
151 server_info.fmt.bpp = 32;
152 server_info.fmt.depth = 24;
153 server_info.fmt.big_endian = 0;
154 server_info.fmt.true_color = 1;
155 server_info.fmt.red_max = htons(255);
156 server_info.fmt.green_max = htons(255);
157 server_info.fmt.blue_max = htons(255);
158 server_info.fmt.red_shift = 0;
159 server_info.fmt.green_shift = 8;
160 server_info.fmt.blue_shift = 16;
163 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
167 outputf("RFB: fb null");
171 static int advance_chunk(struct rfb_state *state) {
173 state->chunk_xnum += 1;
175 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
176 state->chunk_ynum += 1;
177 state->chunk_xnum = 0;
180 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
181 state->chunk_ynum = 0;
182 state->send_state = SST_IDLE;
183 if (!(state->chunk_actually_sent))
184 state->try_in_a_bit = 2;
191 static int ceildiv(int a, int b) {
199 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
200 struct update_header hdr;
207 switch (state->send_state) {
212 if (state->update_requested) {
213 outputf("RFB send: update requested");
214 state->update_requested = 0;
215 state->chunk_actually_sent = 0;
216 state->send_state = SST_HEADER;
221 /* FALL THROUGH to SST_HEADER */
225 /* Calculate the width and height for this chunk, remembering
226 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
227 * height, we may need to have shorter chunks at the edge of
230 state->chunk_width = ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X);
231 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
232 totaldim = state->chunk_width * (state->chunk_xnum + 1);
233 if (totaldim > fb->curmode.xres) {
234 state->chunk_width -= (totaldim - fb->curmode.xres);
237 state->chunk_height = ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y);
238 state->chunk_ypos = state->chunk_height
240 totaldim = state->chunk_height * (state->chunk_ynum + 1);
241 if (totaldim > fb->curmode.yres) {
242 state->chunk_height -= (totaldim - fb->curmode.yres);
245 /* Do we _actually_ need to send this chunk? */
246 if (fb->checksum_rect) {
247 state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
248 state->chunk_width, state->chunk_height);
250 if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
251 if (advance_chunk(state))
255 /* Checksum gets set in data block, AFTER the data has been sent. */
258 outputf("actually sent");
259 state->chunk_actually_sent = 1;
263 state->chunk_bytes_sent = 0;
264 hdr.nrects = htons(1);
265 hdr.xpos = htons(state->chunk_xpos);
266 hdr.ypos = htons(state->chunk_ypos);
267 hdr.width = htons(state->chunk_width);
268 hdr.height= htons(state->chunk_height);
269 hdr.enctype = htonl(0);
271 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
275 outputf("RFB: header send error %d", err);
277 /* Try again later. */
281 state->send_state = SST_DATA;
284 fb->copy_pixels(state->blockbuf,
285 state->chunk_xpos, state->chunk_ypos,
286 state->chunk_width, state->chunk_height);
288 /* FALL THROUGH to SST_DATA */
292 bytes_left = 4 * state->chunk_width * state->chunk_height - state->chunk_bytes_sent;
294 if (bytes_left == 0) {
295 state->send_state = SST_HEADER;
296 state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum;
297 if (advance_chunk(state))
303 if (bytes_left > 1400) {
307 err = tcp_write(pcb, state->blockbuf + state->chunk_bytes_sent,
308 bytes_left, TCP_WRITE_FLAG_COPY);
311 state->chunk_bytes_sent += bytes_left;
314 outputf("RFB: send error %d", err);
319 if (tcp_sndbuf(pcb) == 0) {
325 if (tcp_output(pcb) != ERR_OK)
326 outputf("RFB: tcp_output bailed in send_fsm?");
329 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
330 struct rfb_state *state = arg;
331 send_fsm(pcb, state);
335 static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
336 struct rfb_state *state = arg;
337 send_fsm(pcb, state);
338 if (state->try_in_a_bit) {
339 state->try_in_a_bit--;
340 if (!(state->try_in_a_bit)) {
341 state->update_requested = 1;
350 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
351 outputf("close_conn: bailing");
356 mem_free(state->blockbuf);
358 outputf("close_conn: done");
367 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
371 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
374 switch(state->state) {
376 if (state->writepos < 12) return NEEDMORE;
378 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
380 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
381 /* Spec states that "RFB 003.005", an incorrect value,
382 * should be treated by the server as 3.3. */
384 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
386 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
389 outputf("RFB: Negotiation fail");
393 outputf("RFB: Negotiated v3.%d", state->version);
395 state->readpos += 12;
396 state->state = ST_CLIENTINIT;
398 /* We support one security type, currently "none".
399 * Send that and SecurityResult. */
400 if (state->version >= 7) {
401 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
403 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
411 if (state->version >= 7) {
412 /* Ignore the security type and ClientInit */
413 if (state->writepos < 2) return NEEDMORE;
416 /* Just ClientInit */
417 if (state->writepos < 1) return NEEDMORE;
421 state->state = ST_MAIN;
423 outputf("RFB: Sending server info", state->version);
424 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
430 if (state->writepos < 1) return NEEDMORE;
432 outputf("RFB: cmd %d", state->data[0]);
433 switch (state->data[0]) {
435 case SET_PIXEL_FORMAT:
437 if (state->writepos < (sizeof(struct pixel_format) + 4))
439 outputf("RFB: SetPixelFormat");
441 struct pixel_format * new_fmt =
442 (struct pixel_format *)(&state->data[4]);
446 state->readpos += sizeof(struct pixel_format) + 4;
450 if (state->writepos < 4) return NEEDMORE;
452 struct set_encs_req * req = (struct set_encs_req *)state->data;
454 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
456 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
457 if (state->writepos < pktsize) return NEEDMORE;
459 for (i = 0; i < ntohs(req->num); i++) {
460 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
464 state->readpos += pktsize;
467 case FB_UPDATE_REQUEST:
468 if (state->writepos < sizeof(struct fb_update_req))
470 outputf("RFB: UpdateRequest");
472 state->update_requested = 1;
473 memcpy(&state->client_interest_area, state->data,
474 sizeof(struct fb_update_req));
476 state->readpos += sizeof(struct fb_update_req);
480 if (state->writepos < sizeof(struct key_event_pkt))
483 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
485 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
486 kbd_inject_keysym(htonl(p->keysym), p->downflag);
488 state->readpos += sizeof(struct key_event_pkt);
492 if (state->writepos < sizeof(struct pointer_event_pkt))
494 outputf("RFB: Pointer");
498 state->readpos += sizeof(struct pointer_event_pkt);
501 case CLIENT_CUT_TEXT:
502 if (state->writepos < sizeof(struct text_event_pkt))
504 outputf("RFB: Cut Text");
506 struct text_event_pkt * pkt =
507 (struct text_event_pkt *)state->data;
509 if (state->writepos < sizeof(struct text_event_pkt)
515 state->readpos += sizeof(struct text_event_pkt)
520 outputf("RFB: Bad command: %d", state->data[0]);
524 outputf("RFB: Bad state");
529 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
530 struct pbuf *p, err_t err) {
531 struct rfb_state *state = arg;
537 outputf("RFB: recv err %d", err);
538 /* FIXME do something better here? */
543 outputf("RFB: Connection closed");
544 close_conn(pcb, state);
548 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
550 outputf("RFB: Overflow!");
551 close_conn(pcb, state);
555 copylen = pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
556 outputf("RFB: Processing %d, wp %d, cp %d", p->tot_len, state->writepos, copylen);
557 state->writepos += p->tot_len;
559 tcp_recved(pcb, p->tot_len);
563 switch (recv_fsm(pcb, state)) {
565 outputf("RFB FSM: blocking");
569 outputf("RFB FSM: ok");
571 if (state->readpos == state->writepos) {
577 state->data + state->readpos,
578 state->writepos - state->readpos);
579 state->writepos -= state->readpos;
585 outputf("RFB: Protocol error");
586 close_conn(pcb, state);
593 /* Kick off a send. */
594 if (state->send_state == SST_IDLE && state->update_requested) {
595 send_fsm(pcb, state);
601 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
602 struct rfb_state *state;
605 LWIP_UNUSED_ARG(arg);
606 LWIP_UNUSED_ARG(err);
608 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
612 outputf("rfb_accept: out of memory\n");
616 memset(state, 0, sizeof(struct rfb_state));
618 blockbuf = mem_malloc(ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X)
619 * ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y) * 4);
623 outputf("rfb_accept: out of memory allocating blockbuf\n");
628 state->blockbuf = blockbuf;
629 state->state = ST_BEGIN;
630 state->send_state = SST_IDLE;
632 /* XXX: update_server_info() should be called from the 64ms timer, and deal
633 * with screen resizes appropriately. */
634 update_server_info();
637 tcp_recv(pcb, rfb_recv);
638 tcp_sent(pcb, rfb_sent);
639 tcp_poll(pcb, rfb_poll, 1);
641 tcp_err(pcb, rfb_err);
643 tcp_write(pcb, "RFB 003.008\n", 12, 0);
655 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
656 pcb = tcp_listen(pcb);
657 tcp_accept(pcb, rfb_accept);