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