]> Joshua Wise's Git repositories - netwatch.git/blob - net/rfb.c
more poking
[netwatch.git] / net / rfb.c
1 #include <stdint.h>
2 #include <minilib.h>
3 #include <output.h>
4 #include <fb.h>
5
6 #include "../aseg-paging/keyboard.h"
7
8 #include "lwip/tcp.h"
9 #include "lwip/stats.h"
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
20 #define RFB_BUF_SIZE    64
21
22 #define SCREEN_CHUNKS_X 16
23 #define SCREEN_CHUNKS_Y 8
24
25 struct 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
39 struct 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
47 struct 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
56 struct set_encs_req {
57         uint8_t msgtype;
58         uint8_t padding;
59         uint16_t num;
60         int32_t encodings[];
61 };
62
63 struct key_event_pkt {
64         uint8_t msgtype;
65         uint8_t downflag;
66         uint8_t pad[2];
67         uint32_t keysym;
68 };
69
70 struct pointer_event_pkt {
71         uint8_t msgtype;
72         uint8_t button_mask;
73         uint16_t x;
74         uint16_t y;
75 };
76
77 struct text_event_pkt {
78         uint8_t msgtype;
79         uint8_t padding[3];
80         uint32_t length;
81         char text[];
82 };
83
84 struct 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
95 struct 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;
109         char update_requested;
110
111         struct fb_update_req client_interest_area;
112
113         enum {
114                 SST_IDLE,
115                 SST_NEEDS_UPDATE,
116                 SST_SENDING
117         } send_state;
118
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;
129 };
130
131 static struct server_init_message server_info;
132
133 static void init_server_info() {
134         server_info.name_length = htonl(8);
135         memcpy(server_info.name_string, "NetWatch", 8);
136 }
137
138 static void update_server_info() {
139         if (fb != NULL) {
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);
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;
149                         server_info.fmt.red_max = htons(255);
150                         server_info.fmt.green_max = htons(255);
151                         server_info.fmt.blue_max = htons(255);
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                 }
160         } else {
161                 outputf("RFB: fb null");
162         }
163 }
164
165 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
166         struct update_header hdr;
167         int lines_left;
168         char * lptr;
169         int totaldim;
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         
183                 /* FALL THROUGH to SST_NEEDS_UPDATE */
184
185         case SST_NEEDS_UPDATE:
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;
192                 state->send_state = SST_SENDING;
193
194                 /* FALL THROUGH to SST_SENDING */
195
196         case SST_SENDING:
197
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                                 }
274                         }
275
276                         do {
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);
299
300                         if (err != ERR_OK) {
301                                 if (err != ERR_MEM)
302                                         outputf("RFB: send error %d", err);
303
304                                 outputf("RFB: that's all for now");
305                                 break;
306                         }
307                                 
308                         if (tcp_sndbuf(pcb) == 0) {
309                                 break;
310                         }
311                 }
312
313                 break;
314         }
315         
316         if (tcp_output(pcb) != ERR_OK)
317                 outputf("RFB: tcp_output bailed in send_fsm?");
318 }
319
320 static 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;
324 }
325
326 static 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
333 static 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
341 enum fsm_result {
342         NEEDMORE,
343         OK,
344         FAIL
345 };
346
347 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
348         int i;
349         int pktsize;
350
351         outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
352                 state->writepos);
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
378                 /* We support one security type, currently "none".
379                  * Send that and SecurityResult. */
380                 if (state->version >= 7) {
381                         tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
382                 } else {
383                         tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
384                 }
385
386                 tcp_output(pcb);
387
388                 return OK;
389
390         case ST_CLIENTINIT:
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
401                 state->state = ST_MAIN;
402
403                 outputf("RFB: Sending server info", state->version);
404                 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
405                 tcp_output(pcb);
406
407                 return OK;
408
409         case ST_MAIN:
410                 if (state->writepos < 1) return NEEDMORE;
411
412                 outputf("RFB: cmd %d", state->data[0]);
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;
419                         outputf("RFB: SetPixelFormat");
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
434                         pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
435
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 ... */
442                         }
443
444                         state->readpos += pktsize;
445                         return OK;
446
447                 case FB_UPDATE_REQUEST:
448                         if (state->writepos < sizeof(struct fb_update_req))
449                                 return NEEDMORE;
450                         outputf("RFB: UpdateRequest");
451
452                         state->update_requested = 1;
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
463                         struct key_event_pkt * p = (struct key_event_pkt *)state->data;
464
465                         outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
466                         kbd_inject_keysym(htonl(p->keysym), p->downflag);
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;
474                         outputf("RFB: Pointer");
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;
484                         outputf("RFB: Cut Text");
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
508 static 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);
535         state->writepos += p->tot_len;
536
537         tcp_recved(pcb, p->tot_len);
538         pbuf_free(p);
539
540         while (1) {
541                 switch (recv_fsm(pcb, state)) {
542                 case NEEDMORE:
543                         outputf("RFB FSM: blocking");
544                         /* Need more data */
545                         return ERR_OK;
546
547                 case OK:
548                         outputf("RFB FSM: ok");
549
550                         /* Kick off a send. */
551                         if (state->send_state == SST_IDLE
552                             && state->update_requested) {
553                                 send_fsm(pcb, state);
554                         }
555
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                 
575 static 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
583         state->state = ST_BEGIN;
584         state->readpos = 0;
585         state->writepos = 0;
586         state->update_requested = 0;
587         state->send_state = SST_IDLE;
588
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);
601         tcp_sent(pcb, rfb_sent);
602         tcp_poll(pcb, rfb_poll, 1);
603 /*
604         tcp_err(pcb, rfb_err);
605 */
606         tcp_write(pcb, "RFB 003.008\n", 12, 0);
607         tcp_output(pcb);
608
609         return ERR_OK;
610 }
611
612 void 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.062694 seconds and 4 git commands to generate.