]> Joshua Wise's Git repositories - netwatch.git/blame - net/rfb.c
Fix LICENSE.
[netwatch.git] / net / rfb.c
CommitLineData
3c4e084d
JP
1/* rfb.c
2 * Remote framebuffer server
3 * NetWatch system management mode administration console
4 *
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.
8 *
9 */
10
7da36fbd
JP
11#include <stdint.h>
12#include <minilib.h>
13#include <output.h>
14#include <fb.h>
33e49b2e 15#include <keyboard.h>
ec949d20 16
7da36fbd 17#include "lwip/tcp.h"
b59a6e50 18#include "lwip/stats.h"
7da36fbd
JP
19
20#include "rfb.h"
21
22#define SET_PIXEL_FORMAT 0
23#define SET_ENCODINGS 2
24#define FB_UPDATE_REQUEST 3
25#define KEY_EVENT 4
26#define POINTER_EVENT 5
27#define CLIENT_CUT_TEXT 6
28
e9654237 29#define RFB_BUF_SIZE 1536
7da36fbd 30
ec5f9ca5 31#define SCREEN_CHUNKS_X 8
b496c2b1
JP
32#define SCREEN_CHUNKS_Y 8
33
7da36fbd
JP
34struct pixel_format {
35 uint8_t bpp;
36 uint8_t depth;
37 uint8_t big_endian;
38 uint8_t true_color;
39 uint16_t red_max;
40 uint16_t green_max;
41 uint16_t blue_max;
42 uint8_t red_shift;
43 uint8_t green_shift;
44 uint8_t blue_shift;
45 uint8_t padding[3];
46};
47
48struct server_init_message {
49 uint16_t fb_width;
50 uint16_t fb_height;
51 struct pixel_format fmt;
52 uint32_t name_length;
53 char name_string[8];
54};
55
56struct fb_update_req {
57 uint8_t msgtype;
58 uint8_t incremental;
59 uint16_t xpos;
60 uint16_t ypos;
61 uint16_t width;
62 uint16_t height;
63};
64
65struct set_encs_req {
66 uint8_t msgtype;
67 uint8_t padding;
68 uint16_t num;
69 int32_t encodings[];
70};
71
72struct key_event_pkt {
73 uint8_t msgtype;
74 uint8_t downflag;
75 uint8_t pad[2];
76 uint32_t keysym;
77};
78
79struct pointer_event_pkt {
80 uint8_t msgtype;
81 uint8_t button_mask;
82 uint16_t x;
83 uint16_t y;
84};
85
86struct text_event_pkt {
87 uint8_t msgtype;
88 uint8_t padding[3];
89 uint32_t length;
90 char text[];
91};
92
8ceb0515
JP
93struct update_header {
94 uint8_t msgtype;
95 uint8_t padding;
96 uint16_t nrects;
97 uint16_t xpos;
98 uint16_t ypos;
99 uint16_t width;
100 uint16_t height;
101 int32_t enctype;
102};
103
7da36fbd
JP
104struct rfb_state {
105 enum {
751f179d 106 ST_BEGIN = 0,
7da36fbd
JP
107 ST_CLIENTINIT,
108 ST_MAIN
109 } state;
110 int version;
111 int encs_remaining;
112
113 char data[RFB_BUF_SIZE];
114 int readpos;
115 int writepos;
116
117 char next_update_incremental;
8ceb0515
JP
118 char update_requested;
119
7da36fbd
JP
120 struct fb_update_req client_interest_area;
121
8ceb0515 122 enum {
751f179d 123 SST_IDLE = 0,
d9b82f8c
JP
124 SST_HEADER,
125 SST_DATA
8ceb0515
JP
126 } send_state;
127
99a4c846 128 uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y];
b496c2b1
JP
129
130 uint32_t chunk_xnum;
131 uint32_t chunk_ynum;
132 uint32_t chunk_xpos;
133 uint32_t chunk_ypos;
134 uint32_t chunk_width;
135 uint32_t chunk_height;
136
ec5f9ca5 137 uint32_t chunk_bytes_sent;
d136cf4c
JW
138
139 uint32_t chunk_checksum;
d9b82f8c 140
751f179d
JP
141 int chunk_actually_sent;
142 int try_in_a_bit;
ec5f9ca5
JP
143
144 char * blockbuf;
7da36fbd
JP
145};
146
147static struct server_init_message server_info;
148
149static void init_server_info() {
80726808 150 server_info.name_length = htonl(8);
7da36fbd
JP
151 memcpy(server_info.name_string, "NetWatch", 8);
152}
153
154static void update_server_info() {
155 if (fb != NULL) {
80726808
JP
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);
7da36fbd
JP
159 switch (fb->curmode.format) {
160 case FB_RGB888:
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;
80726808
JP
165 server_info.fmt.red_max = htons(255);
166 server_info.fmt.green_max = htons(255);
167 server_info.fmt.blue_max = htons(255);
7da36fbd
JP
168 server_info.fmt.red_shift = 0;
169 server_info.fmt.green_shift = 8;
170 server_info.fmt.blue_shift = 16;
171 break;
172 default:
173 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
174 break;
175 }
80726808
JP
176 } else {
177 outputf("RFB: fb null");
7da36fbd
JP
178 }
179}
180
d9b82f8c
JP
181static int advance_chunk(struct rfb_state *state) {
182
183 state->chunk_xnum += 1;
184
185 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
186 state->chunk_ynum += 1;
187 state->chunk_xnum = 0;
188 }
189
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))
8ba85474 194 state->try_in_a_bit = 1;
d9b82f8c
JP
195 return 1;
196 }
197
198 return 0;
199}
200
ec5f9ca5
JP
201static int ceildiv(int a, int b) {
202 int res = a / b;
203 if (a % b != 0) {
204 res++;
205 }
206 return res;
207}
208
8ceb0515
JP
209static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
210 struct update_header hdr;
ec5f9ca5 211 int bytes_left;
b496c2b1 212 int totaldim;
8ceb0515
JP
213 err_t err;
214
d9b82f8c 215 while(1) {
b496c2b1 216
d9b82f8c 217 switch (state->send_state) {
b496c2b1 218
d9b82f8c
JP
219 case SST_IDLE:
220 /* Nothing to do */
8ceb0515 221
d9b82f8c
JP
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;
227 } else {
228 return;
229 }
230
231 /* FALL THROUGH to SST_HEADER */
232
233 case SST_HEADER:
234
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
238 * the screen. */
239
ec5f9ca5 240 state->chunk_width = ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X);
d9b82f8c
JP
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);
245 }
b496c2b1 246
ec5f9ca5 247 state->chunk_height = ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y);
d9b82f8c
JP
248 state->chunk_ypos = state->chunk_height
249 * state->chunk_ynum;
250 totaldim = state->chunk_height * (state->chunk_ynum + 1);
251 if (totaldim > fb->curmode.yres) {
252 state->chunk_height -= (totaldim - fb->curmode.yres);
253 }
8ceb0515 254
d9b82f8c
JP
255 /* Do we _actually_ need to send this chunk? */
256 if (fb->checksum_rect) {
d136cf4c
JW
257 state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
258 state->chunk_width, state->chunk_height);
d9b82f8c 259
d136cf4c 260 if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
d9b82f8c
JP
261 if (advance_chunk(state))
262 return;
263 continue;
d9b82f8c 264 }
d136cf4c 265 /* Checksum gets set in data block, AFTER the data has been sent. */
d9b82f8c 266 }
b496c2b1 267
d9b82f8c 268 state->chunk_actually_sent = 1;
b496c2b1 269
d9b82f8c
JP
270 /* Send a header */
271 hdr.msgtype = 0;
ec5f9ca5 272 state->chunk_bytes_sent = 0;
d9b82f8c
JP
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);
b496c2b1 279
d9b82f8c 280 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
b496c2b1 281
d9b82f8c
JP
282 if (err != ERR_OK) {
283 if (err != ERR_MEM)
284 outputf("RFB: header send error %d", err);
b496c2b1 285
d9b82f8c
JP
286 /* Try again later. */
287 return;
288 }
b496c2b1 289
d9b82f8c 290 state->send_state = SST_DATA;
b496c2b1 291
ec5f9ca5
JP
292 /* Snag the data. */
293 fb->copy_pixels(state->blockbuf,
294 state->chunk_xpos, state->chunk_ypos,
295 state->chunk_width, state->chunk_height);
296
d9b82f8c 297 /* FALL THROUGH to SST_DATA */
99a4c846 298
d9b82f8c 299 case SST_DATA:
8ceb0515 300
ec5f9ca5 301 bytes_left = 4 * state->chunk_width * state->chunk_height - state->chunk_bytes_sent;
b496c2b1 302
ec5f9ca5 303 if (bytes_left == 0) {
d9b82f8c 304 state->send_state = SST_HEADER;
d136cf4c 305 state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum;
d9b82f8c
JP
306 if (advance_chunk(state))
307 return;
308 break;
309 }
b496c2b1 310
ec5f9ca5
JP
311 /* That's enough. */
312 if (bytes_left > 1400) {
313 bytes_left = 1400;
314 }
b496c2b1 315
ec5f9ca5
JP
316 err = tcp_write(pcb, state->blockbuf + state->chunk_bytes_sent,
317 bytes_left, TCP_WRITE_FLAG_COPY);
bbfab433 318
d9b82f8c 319 if (err == ERR_OK) {
ec5f9ca5 320 state->chunk_bytes_sent += bytes_left;
d9b82f8c 321 } else {
8cc40c4d
JW
322 if (err != ERR_MEM)
323 outputf("RFB: send error %d", err);
b496c2b1 324
d9b82f8c 325 return;
bbfab433 326 }
b496c2b1 327
bbfab433 328 if (tcp_sndbuf(pcb) == 0) {
d9b82f8c 329 return;
bbfab433 330 }
8ceb0515 331 }
8ceb0515 332 }
075bbc71
JW
333
334 if (tcp_output(pcb) != ERR_OK)
075bbc71 335 outputf("RFB: tcp_output bailed in send_fsm?");
8ceb0515
JP
336}
337
338static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
339 struct rfb_state *state = arg;
340 send_fsm(pcb, state);
341 return ERR_OK;
80726808
JP
342}
343
b59a6e50
JP
344static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
345 struct rfb_state *state = arg;
751f179d
JP
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;
350 }
351 }
8ba85474 352 send_fsm(pcb, state);
99a4c846 353/*
b59a6e50 354 stats_display();
99a4c846 355*/
b59a6e50
JP
356 return ERR_OK;
357}
358
7da36fbd 359static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
779b8ca8 360 outputf("close_conn: bailing");
7da36fbd
JP
361 tcp_arg(pcb, NULL);
362 tcp_sent(pcb, NULL);
363 tcp_recv(pcb, NULL);
ec5f9ca5 364 mem_free(state->blockbuf);
ce29e1cc 365 mem_free(state);
7da36fbd 366 tcp_close(pcb);
779b8ca8 367 outputf("close_conn: done");
7da36fbd
JP
368}
369
370enum fsm_result {
371 NEEDMORE,
372 OK,
373 FAIL
374};
375
376static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
80726808
JP
377 int i;
378 int pktsize;
056e0e18 379/*
80726808
JP
380 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
381 state->writepos);
056e0e18 382*/
7da36fbd
JP
383 switch(state->state) {
384 case ST_BEGIN:
385 if (state->writepos < 12) return NEEDMORE;
386
387 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
388 state->version = 3;
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. */
392 state->version = 3;
393 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
394 state->version = 7;
395 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
396 state->version = 8;
397 } else {
398 outputf("RFB: Negotiation fail");
399 return FAIL;
400 }
401
402 outputf("RFB: Negotiated v3.%d", state->version);
403
404 state->readpos += 12;
405 state->state = ST_CLIENTINIT;
406
80726808
JP
407 /* We support one security type, currently "none".
408 * Send that and SecurityResult. */
7da36fbd 409 if (state->version >= 7) {
80726808 410 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
7da36fbd 411 } else {
80726808 412 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
7da36fbd
JP
413 }
414
7da36fbd
JP
415 tcp_output(pcb);
416
417 return OK;
418
419 case ST_CLIENTINIT:
80726808
JP
420 if (state->version >= 7) {
421 /* Ignore the security type and ClientInit */
422 if (state->writepos < 2) return NEEDMORE;
423 state->readpos += 2;
424 } else {
425 /* Just ClientInit */
426 if (state->writepos < 1) return NEEDMORE;
427 state->readpos += 1;
428 }
429
7da36fbd
JP
430 state->state = ST_MAIN;
431
80726808 432 outputf("RFB: Sending server info", state->version);
075bbc71 433 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
7da36fbd
JP
434 tcp_output(pcb);
435
436 return OK;
437
438 case ST_MAIN:
439 if (state->writepos < 1) return NEEDMORE;
440
441 switch (state->data[0]) {
442
443 case SET_PIXEL_FORMAT:
444 /* SetPixelFormat */
445 if (state->writepos < (sizeof(struct pixel_format) + 4))
446 return NEEDMORE;
80726808 447 outputf("RFB: SetPixelFormat");
7da36fbd
JP
448/*
449 struct pixel_format * new_fmt =
450 (struct pixel_format *)(&state->data[4]);
451*/
452 /* XXX ... */
453
454 state->readpos += sizeof(struct pixel_format) + 4;
455 return OK;
456
457 case SET_ENCODINGS:
458 if (state->writepos < 4) return NEEDMORE;
459
460 struct set_encs_req * req = (struct set_encs_req *)state->data;
461
80726808 462 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
7da36fbd 463
80726808
JP
464 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
465 if (state->writepos < pktsize) return NEEDMORE;
466
467 for (i = 0; i < ntohs(req->num); i++) {
468 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
469 /* XXX ... */
80726808
JP
470 }
471
472 state->readpos += pktsize;
7da36fbd
JP
473 return OK;
474
475 case FB_UPDATE_REQUEST:
476 if (state->writepos < sizeof(struct fb_update_req))
477 return NEEDMORE;
80726808 478 outputf("RFB: UpdateRequest");
7da36fbd 479
8ceb0515 480 state->update_requested = 1;
7da36fbd
JP
481 memcpy(&state->client_interest_area, state->data,
482 sizeof(struct fb_update_req));
483
484 state->readpos += sizeof(struct fb_update_req);
485 return OK;
486
487 case KEY_EVENT:
488 if (state->writepos < sizeof(struct key_event_pkt))
489 return NEEDMORE;
490
ec949d20 491 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
dadcd4fc
JP
492
493 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
ec949d20 494 kbd_inject_keysym(htonl(p->keysym), p->downflag);
7da36fbd
JP
495
496 state->readpos += sizeof(struct key_event_pkt);
497 return OK;
498
499 case POINTER_EVENT:
500 if (state->writepos < sizeof(struct pointer_event_pkt))
501 return NEEDMORE;
80726808 502 outputf("RFB: Pointer");
7da36fbd
JP
503
504 /* XXX stub */
505
506 state->readpos += sizeof(struct pointer_event_pkt);
507 return OK;
508
509 case CLIENT_CUT_TEXT:
510 if (state->writepos < sizeof(struct text_event_pkt))
511 return NEEDMORE;
80726808 512 outputf("RFB: Cut Text");
7da36fbd
JP
513
514 struct text_event_pkt * pkt =
515 (struct text_event_pkt *)state->data;
516
517 if (state->writepos < sizeof(struct text_event_pkt)
d9b82f8c 518 + pkt->length)
7da36fbd
JP
519 return NEEDMORE;
520
521 /* XXX stub */
522
523 state->readpos += sizeof(struct text_event_pkt)
d9b82f8c 524 + pkt->length;
7da36fbd
JP
525 return OK;
526
527 default:
528 outputf("RFB: Bad command: %d", state->data[0]);
779b8ca8 529 return FAIL;
7da36fbd
JP
530 }
531 default:
532 outputf("RFB: Bad state");
533 return FAIL;
534 }
535}
536
537static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
d9b82f8c 538 struct pbuf *p, err_t err) {
7da36fbd 539 struct rfb_state *state = arg;
779b8ca8 540 uint16_t copylen;
7da36fbd
JP
541
542 if (state == NULL)
543
544 if (err != ERR_OK) {
545 outputf("RFB: recv err %d", err);
546 /* FIXME do something better here? */
547 return ERR_OK;
548 }
549
550 if (p == NULL) {
551 outputf("RFB: Connection closed");
552 close_conn(pcb, state);
553 return ERR_OK;
554 }
555
556 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
557 /* Overflow! */
558 outputf("RFB: Overflow!");
559 close_conn(pcb, state);
560 return ERR_OK;
561 }
562
779b8ca8 563 copylen = pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
8ba85474 564
779b8ca8 565 outputf("RFB: Processing %d, wp %d, cp %d", p->tot_len, state->writepos, copylen);
8ba85474 566
80726808
JP
567 state->writepos += p->tot_len;
568
7da36fbd
JP
569 tcp_recved(pcb, p->tot_len);
570 pbuf_free(p);
571
572 while (1) {
573 switch (recv_fsm(pcb, state)) {
574 case NEEDMORE:
80726808 575 outputf("RFB FSM: blocking");
779b8ca8 576 goto doneprocessing;
7da36fbd
JP
577
578 case OK:
579 if (state->readpos == state->writepos) {
580 state->readpos = 0;
581 state->writepos = 0;
779b8ca8 582 goto doneprocessing;
7da36fbd
JP
583 } else {
584 memmove(state->data,
d9b82f8c
JP
585 state->data + state->readpos,
586 state->writepos - state->readpos);
779b8ca8
JP
587 state->writepos -= state->readpos;
588 state->readpos = 0;
7da36fbd
JP
589 }
590 break;
591 case FAIL:
592 /* Shit */
593 outputf("RFB: Protocol error");
594 close_conn(pcb, state);
595 return ERR_OK;
596 }
597 }
779b8ca8
JP
598
599doneprocessing:
600
601 /* Kick off a send. */
602 if (state->send_state == SST_IDLE && state->update_requested) {
603 send_fsm(pcb, state);
604 }
605
606 return ERR_OK;
7da36fbd
JP
607}
608
609static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
610 struct rfb_state *state;
ec5f9ca5 611 char * blockbuf;
7da36fbd
JP
612
613 LWIP_UNUSED_ARG(arg);
614 LWIP_UNUSED_ARG(err);
615
616 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
617
ec5f9ca5
JP
618 if (!state)
619 {
620 outputf("rfb_accept: out of memory\n");
621 return ERR_MEM;
622 }
623
751f179d
JP
624 memset(state, 0, sizeof(struct rfb_state));
625
ec5f9ca5
JP
626 blockbuf = mem_malloc(ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X)
627 * ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y) * 4);
628
629 if (!blockbuf)
630 {
631 outputf("rfb_accept: out of memory allocating blockbuf\n");
632 mem_free(state);
633 return ERR_MEM;
634 }
635
636 state->blockbuf = blockbuf;
80726808 637 state->state = ST_BEGIN;
8ceb0515 638 state->send_state = SST_IDLE;
80726808 639
7da36fbd
JP
640 /* XXX: update_server_info() should be called from the 64ms timer, and deal
641 * with screen resizes appropriately. */
642 update_server_info();
643
7da36fbd
JP
644 tcp_arg(pcb, state);
645 tcp_recv(pcb, rfb_recv);
8ceb0515 646 tcp_sent(pcb, rfb_sent);
b59a6e50 647 tcp_poll(pcb, rfb_poll, 1);
7da36fbd
JP
648/*
649 tcp_err(pcb, rfb_err);
7da36fbd
JP
650*/
651 tcp_write(pcb, "RFB 003.008\n", 12, 0);
652 tcp_output(pcb);
653
654 return ERR_OK;
655}
656
657void rfb_init() {
658 struct tcp_pcb *pcb;
659
660 init_server_info();
661
662 pcb = tcp_new();
663 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
664 pcb = tcp_listen(pcb);
665 tcp_accept(pcb, rfb_accept);
666}
This page took 0.093294 seconds and 4 git commands to generate.