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_lindex;
129 uint32_t chunk_checksum;
131 int chunk_actually_sent;
135 static struct server_init_message server_info;
137 static void init_server_info() {
138 server_info.name_length = htonl(8);
139 memcpy(server_info.name_string, "NetWatch", 8);
142 static void update_server_info() {
144 outputf("RFB: setting fmt %d", fb->curmode.format);
145 server_info.fb_width = htons(fb->curmode.xres);
146 server_info.fb_height = htons(fb->curmode.yres);
147 switch (fb->curmode.format) {
149 server_info.fmt.bpp = 32;
150 server_info.fmt.depth = 24;
151 server_info.fmt.big_endian = 0;
152 server_info.fmt.true_color = 1;
153 server_info.fmt.red_max = htons(255);
154 server_info.fmt.green_max = htons(255);
155 server_info.fmt.blue_max = htons(255);
156 server_info.fmt.red_shift = 0;
157 server_info.fmt.green_shift = 8;
158 server_info.fmt.blue_shift = 16;
161 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
165 outputf("RFB: fb null");
169 static int advance_chunk(struct rfb_state *state) {
171 state->chunk_xnum += 1;
173 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
174 state->chunk_ynum += 1;
175 state->chunk_xnum = 0;
178 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
179 state->chunk_ynum = 0;
180 state->send_state = SST_IDLE;
181 if (!(state->chunk_actually_sent))
182 state->try_in_a_bit = 2;
189 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
190 struct update_header hdr;
192 unsigned char * lptr;
198 switch (state->send_state) {
203 if (state->update_requested) {
204 outputf("RFB send: update requested");
205 state->update_requested = 0;
206 state->chunk_actually_sent = 0;
207 state->send_state = SST_HEADER;
212 /* FALL THROUGH to SST_HEADER */
216 /* Calculate the width and height for this chunk, remembering
217 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
218 * height, we may need to have shorter chunks at the edge of
221 state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X;
222 if (fb->curmode.xres % SCREEN_CHUNKS_X != 0)
223 state->chunk_width += 1;
224 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
225 totaldim = state->chunk_width * (state->chunk_xnum + 1);
226 if (totaldim > fb->curmode.xres) {
227 state->chunk_width -= (totaldim - fb->curmode.xres);
230 state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y;
231 if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0)
232 state->chunk_height += 1;
233 state->chunk_ypos = state->chunk_height
235 totaldim = state->chunk_height * (state->chunk_ynum + 1);
236 if (totaldim > fb->curmode.yres) {
237 state->chunk_height -= (totaldim - fb->curmode.yres);
240 /* Do we _actually_ need to send this chunk? */
241 if (fb->checksum_rect) {
242 state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
243 state->chunk_width, state->chunk_height);
245 if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
246 if (advance_chunk(state))
250 /* Checksum gets set in data block, AFTER the data has been sent. */
253 outputf("actually sent");
254 state->chunk_actually_sent = 1;
258 state->chunk_lindex = 0;
259 hdr.nrects = htons(1);
260 hdr.xpos = htons(state->chunk_xpos);
261 hdr.ypos = htons(state->chunk_ypos);
262 hdr.width = htons(state->chunk_width);
263 hdr.height= htons(state->chunk_height);
264 hdr.enctype = htonl(0);
265 lines_left = state->chunk_height;
267 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
271 outputf("RFB: header send error %d", err);
273 /* Try again later. */
277 state->send_state = SST_DATA;
279 /* FALL THROUGH to SST_DATA */
283 lines_left = state->chunk_height - state->chunk_lindex;
285 if (lines_left == 0) {
286 state->send_state = SST_HEADER;
287 state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum;
288 if (advance_chunk(state))
294 + (fb->curmode.xres * fb->curmode.bytestride
295 * (state->chunk_ypos + state->chunk_lindex))
296 + (state->chunk_xpos * fb->curmode.bytestride);
298 /* The network card can't DMA from video RAM,
299 * so use TCP_WRITE_FLAG_COPY. */
300 err = tcp_write(pcb, lptr,
301 fb->curmode.bytestride * state->chunk_width, TCP_WRITE_FLAG_COPY);
304 state->chunk_lindex += 1;
307 outputf("RFB: send error %d", err);
312 if (tcp_sndbuf(pcb) == 0) {
318 if (tcp_output(pcb) != ERR_OK)
319 outputf("RFB: tcp_output bailed in send_fsm?");
322 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
323 struct rfb_state *state = arg;
324 send_fsm(pcb, state);
328 static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
329 struct rfb_state *state = arg;
330 send_fsm(pcb, state);
331 if (state->try_in_a_bit) {
332 state->try_in_a_bit--;
333 if (!(state->try_in_a_bit)) {
334 state->update_requested = 1;
343 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
344 outputf("close_conn: bailing");
350 outputf("close_conn: done");
359 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
363 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
366 switch(state->state) {
368 if (state->writepos < 12) return NEEDMORE;
370 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
372 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
373 /* Spec states that "RFB 003.005", an incorrect value,
374 * should be treated by the server as 3.3. */
376 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
378 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
381 outputf("RFB: Negotiation fail");
385 outputf("RFB: Negotiated v3.%d", state->version);
387 state->readpos += 12;
388 state->state = ST_CLIENTINIT;
390 /* We support one security type, currently "none".
391 * Send that and SecurityResult. */
392 if (state->version >= 7) {
393 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
395 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
403 if (state->version >= 7) {
404 /* Ignore the security type and ClientInit */
405 if (state->writepos < 2) return NEEDMORE;
408 /* Just ClientInit */
409 if (state->writepos < 1) return NEEDMORE;
413 state->state = ST_MAIN;
415 outputf("RFB: Sending server info", state->version);
416 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
422 if (state->writepos < 1) return NEEDMORE;
424 outputf("RFB: cmd %d", state->data[0]);
425 switch (state->data[0]) {
427 case SET_PIXEL_FORMAT:
429 if (state->writepos < (sizeof(struct pixel_format) + 4))
431 outputf("RFB: SetPixelFormat");
433 struct pixel_format * new_fmt =
434 (struct pixel_format *)(&state->data[4]);
438 state->readpos += sizeof(struct pixel_format) + 4;
442 if (state->writepos < 4) return NEEDMORE;
444 struct set_encs_req * req = (struct set_encs_req *)state->data;
446 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
448 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
449 if (state->writepos < pktsize) return NEEDMORE;
451 for (i = 0; i < ntohs(req->num); i++) {
452 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
456 state->readpos += pktsize;
459 case FB_UPDATE_REQUEST:
460 if (state->writepos < sizeof(struct fb_update_req))
462 outputf("RFB: UpdateRequest");
464 state->update_requested = 1;
465 memcpy(&state->client_interest_area, state->data,
466 sizeof(struct fb_update_req));
468 state->readpos += sizeof(struct fb_update_req);
472 if (state->writepos < sizeof(struct key_event_pkt))
475 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
477 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
478 kbd_inject_keysym(htonl(p->keysym), p->downflag);
480 state->readpos += sizeof(struct key_event_pkt);
484 if (state->writepos < sizeof(struct pointer_event_pkt))
486 outputf("RFB: Pointer");
490 state->readpos += sizeof(struct pointer_event_pkt);
493 case CLIENT_CUT_TEXT:
494 if (state->writepos < sizeof(struct text_event_pkt))
496 outputf("RFB: Cut Text");
498 struct text_event_pkt * pkt =
499 (struct text_event_pkt *)state->data;
501 if (state->writepos < sizeof(struct text_event_pkt)
507 state->readpos += sizeof(struct text_event_pkt)
512 outputf("RFB: Bad command: %d", state->data[0]);
516 outputf("RFB: Bad state");
521 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
522 struct pbuf *p, err_t err) {
523 struct rfb_state *state = arg;
529 outputf("RFB: recv err %d", err);
530 /* FIXME do something better here? */
535 outputf("RFB: Connection closed");
536 close_conn(pcb, state);
540 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
542 outputf("RFB: Overflow!");
543 close_conn(pcb, state);
547 copylen = pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
548 outputf("RFB: Processing %d, wp %d, cp %d", p->tot_len, state->writepos, copylen);
549 state->writepos += p->tot_len;
551 tcp_recved(pcb, p->tot_len);
555 switch (recv_fsm(pcb, state)) {
557 outputf("RFB FSM: blocking");
561 outputf("RFB FSM: ok");
563 if (state->readpos == state->writepos) {
569 state->data + state->readpos,
570 state->writepos - state->readpos);
571 state->writepos -= state->readpos;
577 outputf("RFB: Protocol error");
578 close_conn(pcb, state);
585 /* Kick off a send. */
586 if (state->send_state == SST_IDLE && state->update_requested) {
587 send_fsm(pcb, state);
593 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
594 struct rfb_state *state;
596 LWIP_UNUSED_ARG(arg);
597 LWIP_UNUSED_ARG(err);
599 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
601 memset(state, 0, sizeof(struct rfb_state));
603 state->state = ST_BEGIN;
604 state->send_state = SST_IDLE;
606 /* XXX: update_server_info() should be called from the 64ms timer, and deal
607 * with screen resizes appropriately. */
608 update_server_info();
612 outputf("rfb_accept: out of memory\n");
617 tcp_recv(pcb, rfb_recv);
618 tcp_sent(pcb, rfb_sent);
619 tcp_poll(pcb, rfb_poll, 1);
621 tcp_err(pcb, rfb_err);
623 tcp_write(pcb, "RFB 003.008\n", 12, 0);
635 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
636 pcb = tcp_listen(pcb);
637 tcp_accept(pcb, rfb_accept);