]> Joshua Wise's Git repositories - netwatch.git/blame_incremental - net/rfb.c
Fix LICENSE.
[netwatch.git] / net / rfb.c
... / ...
CommitLineData
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
11#include <stdint.h>
12#include <minilib.h>
13#include <output.h>
14#include <fb.h>
15#include <keyboard.h>
16
17#include "lwip/tcp.h"
18#include "lwip/stats.h"
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
29#define RFB_BUF_SIZE 1536
30
31#define SCREEN_CHUNKS_X 8
32#define SCREEN_CHUNKS_Y 8
33
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
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
104struct rfb_state {
105 enum {
106 ST_BEGIN = 0,
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;
118 char update_requested;
119
120 struct fb_update_req client_interest_area;
121
122 enum {
123 SST_IDLE = 0,
124 SST_HEADER,
125 SST_DATA
126 } send_state;
127
128 uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y];
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
137 uint32_t chunk_bytes_sent;
138
139 uint32_t chunk_checksum;
140
141 int chunk_actually_sent;
142 int try_in_a_bit;
143
144 char * blockbuf;
145};
146
147static struct server_init_message server_info;
148
149static void init_server_info() {
150 server_info.name_length = htonl(8);
151 memcpy(server_info.name_string, "NetWatch", 8);
152}
153
154static void update_server_info() {
155 if (fb != NULL) {
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) {
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;
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;
171 break;
172 default:
173 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
174 break;
175 }
176 } else {
177 outputf("RFB: fb null");
178 }
179}
180
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))
194 state->try_in_a_bit = 1;
195 return 1;
196 }
197
198 return 0;
199}
200
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
209static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
210 struct update_header hdr;
211 int bytes_left;
212 int totaldim;
213 err_t err;
214
215 while(1) {
216
217 switch (state->send_state) {
218
219 case SST_IDLE:
220 /* Nothing to do */
221
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
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);
245 }
246
247 state->chunk_height = ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y);
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 }
254
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);
259
260 if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
261 if (advance_chunk(state))
262 return;
263 continue;
264 }
265 /* Checksum gets set in data block, AFTER the data has been sent. */
266 }
267
268 state->chunk_actually_sent = 1;
269
270 /* Send a header */
271 hdr.msgtype = 0;
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);
279
280 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
281
282 if (err != ERR_OK) {
283 if (err != ERR_MEM)
284 outputf("RFB: header send error %d", err);
285
286 /* Try again later. */
287 return;
288 }
289
290 state->send_state = SST_DATA;
291
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
297 /* FALL THROUGH to SST_DATA */
298
299 case SST_DATA:
300
301 bytes_left = 4 * state->chunk_width * state->chunk_height - state->chunk_bytes_sent;
302
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))
307 return;
308 break;
309 }
310
311 /* That's enough. */
312 if (bytes_left > 1400) {
313 bytes_left = 1400;
314 }
315
316 err = tcp_write(pcb, state->blockbuf + state->chunk_bytes_sent,
317 bytes_left, TCP_WRITE_FLAG_COPY);
318
319 if (err == ERR_OK) {
320 state->chunk_bytes_sent += bytes_left;
321 } else {
322 if (err != ERR_MEM)
323 outputf("RFB: send error %d", err);
324
325 return;
326 }
327
328 if (tcp_sndbuf(pcb) == 0) {
329 return;
330 }
331 }
332 }
333
334 if (tcp_output(pcb) != ERR_OK)
335 outputf("RFB: tcp_output bailed in send_fsm?");
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;
342}
343
344static 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;
350 }
351 }
352 send_fsm(pcb, state);
353/*
354 stats_display();
355*/
356 return ERR_OK;
357}
358
359static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
360 outputf("close_conn: bailing");
361 tcp_arg(pcb, NULL);
362 tcp_sent(pcb, NULL);
363 tcp_recv(pcb, NULL);
364 mem_free(state->blockbuf);
365 mem_free(state);
366 tcp_close(pcb);
367 outputf("close_conn: done");
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) {
377 int i;
378 int pktsize;
379/*
380 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
381 state->writepos);
382*/
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
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);
411 } else {
412 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
413 }
414
415 tcp_output(pcb);
416
417 return OK;
418
419 case ST_CLIENTINIT:
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
430 state->state = ST_MAIN;
431
432 outputf("RFB: Sending server info", state->version);
433 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
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;
447 outputf("RFB: SetPixelFormat");
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
462 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
463
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 ... */
470 }
471
472 state->readpos += pktsize;
473 return OK;
474
475 case FB_UPDATE_REQUEST:
476 if (state->writepos < sizeof(struct fb_update_req))
477 return NEEDMORE;
478 outputf("RFB: UpdateRequest");
479
480 state->update_requested = 1;
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
491 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
492
493 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
494 kbd_inject_keysym(htonl(p->keysym), p->downflag);
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;
502 outputf("RFB: Pointer");
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;
512 outputf("RFB: Cut Text");
513
514 struct text_event_pkt * pkt =
515 (struct text_event_pkt *)state->data;
516
517 if (state->writepos < sizeof(struct text_event_pkt)
518 + pkt->length)
519 return NEEDMORE;
520
521 /* XXX stub */
522
523 state->readpos += sizeof(struct text_event_pkt)
524 + pkt->length;
525 return OK;
526
527 default:
528 outputf("RFB: Bad command: %d", state->data[0]);
529 return FAIL;
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,
538 struct pbuf *p, err_t err) {
539 struct rfb_state *state = arg;
540 uint16_t copylen;
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
563 copylen = pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
564
565 outputf("RFB: Processing %d, wp %d, cp %d", p->tot_len, state->writepos, copylen);
566
567 state->writepos += p->tot_len;
568
569 tcp_recved(pcb, p->tot_len);
570 pbuf_free(p);
571
572 while (1) {
573 switch (recv_fsm(pcb, state)) {
574 case NEEDMORE:
575 outputf("RFB FSM: blocking");
576 goto doneprocessing;
577
578 case OK:
579 if (state->readpos == state->writepos) {
580 state->readpos = 0;
581 state->writepos = 0;
582 goto doneprocessing;
583 } else {
584 memmove(state->data,
585 state->data + state->readpos,
586 state->writepos - state->readpos);
587 state->writepos -= state->readpos;
588 state->readpos = 0;
589 }
590 break;
591 case FAIL:
592 /* Shit */
593 outputf("RFB: Protocol error");
594 close_conn(pcb, state);
595 return ERR_OK;
596 }
597 }
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;
607}
608
609static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
610 struct rfb_state *state;
611 char * blockbuf;
612
613 LWIP_UNUSED_ARG(arg);
614 LWIP_UNUSED_ARG(err);
615
616 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
617
618 if (!state)
619 {
620 outputf("rfb_accept: out of memory\n");
621 return ERR_MEM;
622 }
623
624 memset(state, 0, sizeof(struct rfb_state));
625
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;
637 state->state = ST_BEGIN;
638 state->send_state = SST_IDLE;
639
640 /* XXX: update_server_info() should be called from the 64ms timer, and deal
641 * with screen resizes appropriately. */
642 update_server_info();
643
644 tcp_arg(pcb, state);
645 tcp_recv(pcb, rfb_recv);
646 tcp_sent(pcb, rfb_sent);
647 tcp_poll(pcb, rfb_poll, 1);
648/*
649 tcp_err(pcb, rfb_err);
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.033358 seconds and 4 git commands to generate.