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