]> Joshua Wise's Git repositories - netwatch.git/blob - net/rfb.c
RFB should flag copies properly, and only commit output when it needs to.
[netwatch.git] / net / rfb.c
1 #include <stdint.h>
2 #include <minilib.h>
3 #include <output.h>
4 #include <fb.h>
5
6 #include "lwip/tcp.h"
7
8 #include "rfb.h"
9
10 #define SET_PIXEL_FORMAT        0
11 #define SET_ENCODINGS           2
12 #define FB_UPDATE_REQUEST       3
13 #define KEY_EVENT               4
14 #define POINTER_EVENT           5
15 #define CLIENT_CUT_TEXT         6
16
17 #define RFB_BUF_SIZE    64
18
19 struct pixel_format {
20         uint8_t bpp;
21         uint8_t depth;
22         uint8_t big_endian;
23         uint8_t true_color;
24         uint16_t red_max;
25         uint16_t green_max;
26         uint16_t blue_max;
27         uint8_t red_shift;
28         uint8_t green_shift;
29         uint8_t blue_shift;
30         uint8_t padding[3];
31 };
32
33 struct server_init_message {
34         uint16_t fb_width;
35         uint16_t fb_height;
36         struct pixel_format fmt;
37         uint32_t name_length;
38         char name_string[8];
39 };
40
41 struct fb_update_req {
42         uint8_t msgtype;
43         uint8_t incremental;
44         uint16_t xpos;
45         uint16_t ypos;
46         uint16_t width;
47         uint16_t height;
48 };
49
50 struct set_encs_req {
51         uint8_t msgtype;
52         uint8_t padding;
53         uint16_t num;
54         int32_t encodings[];
55 };
56
57 struct key_event_pkt {
58         uint8_t msgtype;
59         uint8_t downflag;
60         uint8_t pad[2];
61         uint32_t keysym;
62 };
63
64 struct pointer_event_pkt {
65         uint8_t msgtype;
66         uint8_t button_mask;
67         uint16_t x;
68         uint16_t y;
69 };
70
71 struct text_event_pkt {
72         uint8_t msgtype;
73         uint8_t padding[3];
74         uint32_t length;
75         char text[];
76 };
77
78 struct update_header {
79         uint8_t msgtype;
80         uint8_t padding;
81         uint16_t nrects;
82         uint16_t xpos;
83         uint16_t ypos;
84         uint16_t width;
85         uint16_t height;
86         int32_t enctype;
87 };
88
89 struct rfb_state {
90         enum {
91                 ST_BEGIN,
92                 ST_CLIENTINIT,
93                 ST_MAIN
94         } state;
95         int version;
96         int encs_remaining;
97
98         char data[RFB_BUF_SIZE];
99         int readpos;
100         int writepos;
101
102         char next_update_incremental;
103         char update_requested;
104
105         struct fb_update_req client_interest_area;
106
107         enum {
108                 SST_IDLE,
109                 SST_NEEDS_UPDATE,
110                 SST_SENDING
111         } send_state;
112
113         uint32_t update_pos;
114         uint32_t frame_bytes;
115 };
116
117 static struct server_init_message server_info;
118
119 static void init_server_info() {
120         server_info.name_length = htonl(8);
121         memcpy(server_info.name_string, "NetWatch", 8);
122 }
123
124 static void update_server_info() {
125         if (fb != NULL) {
126                 outputf("RFB: setting fmt %d", fb->curmode.format);
127                 server_info.fb_width = htons(fb->curmode.xres);
128                 server_info.fb_height = htons(fb->curmode.yres);
129                 switch (fb->curmode.format) {
130                 case FB_RGB888:
131                         server_info.fmt.bpp = 32;
132                         server_info.fmt.depth = 24;
133                         server_info.fmt.big_endian = 0;
134                         server_info.fmt.true_color = 1;
135                         server_info.fmt.red_max = htons(255);
136                         server_info.fmt.green_max = htons(255);
137                         server_info.fmt.blue_max = htons(255);
138                         server_info.fmt.red_shift = 0;
139                         server_info.fmt.green_shift = 8;
140                         server_info.fmt.blue_shift = 16;
141                         break;
142                 default:
143                         outputf("RFB: unknown fb fmt %d", fb->curmode.format);
144                         break;
145                 }
146         } else {
147                 outputf("RFB: fb null");
148         }
149 }
150
151 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
152         struct update_header hdr;
153         int left, sndlength;
154         err_t err;
155
156         switch (state->send_state) {
157         case SST_IDLE:
158                 /* Nothing to do */
159                 if (state->update_requested) {
160                         outputf("RFB send: update requested");
161                         state->update_requested = 0;
162                         state->send_state = SST_NEEDS_UPDATE;
163                 } else {
164                         break;
165                 }
166         
167                 /* potential FALL THROUGH */
168
169         case SST_NEEDS_UPDATE:
170                 outputf("RFB send: sending header");
171                 /* Send a header */
172                 state->frame_bytes = fb->curmode.xres * fb->curmode.yres * fb->curmode.bytestride;
173                 hdr.msgtype = 0;
174                 hdr.nrects = htons(1);
175                 hdr.xpos = htons(0);
176                 hdr.ypos = htons(0);
177                 hdr.width = htons(fb->curmode.xres);
178                 hdr.height = htons(fb->curmode.yres);
179                 hdr.enctype = htonl(0);
180                 tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
181
182                 state->update_pos = 0;
183                 state->send_state = SST_SENDING;
184
185                 /* FALL THROUGH */
186
187         case SST_SENDING:
188
189                 while (1) {
190                         unsigned char mbuf[8192 /* XXX magic */];
191                         
192                         left = state->frame_bytes - state->update_pos;
193
194                         if (left == 0) {
195                                 state->send_state = SST_IDLE;
196                                 break;
197                         }
198                         
199                         if (left > 8192)
200                                 left = 8192;
201
202                         if (left > tcp_mss(pcb)) {
203                                 sndlength = tcp_mss(pcb);
204                         } else {
205                                 sndlength = left;
206                         }
207                         
208                         memcpy(mbuf, fb->fbaddr + state->update_pos, sndlength);        /* It's OK if it becomes smaller later. */
209
210                         do {
211                                 err = tcp_write(pcb, mbuf, sndlength, TCP_WRITE_FLAG_COPY /* This is my memory on the stack, thank you very much. */);
212                                 if (err == ERR_MEM) {
213                                         outputf("RFB: ERR_MEM sending %d", sndlength);
214                                         sndlength /= 2;
215                                 }
216                         } while (err == ERR_MEM && sndlength > 1);
217
218                         if (err == ERR_OK) {
219                                 outputf("RFB: attempting send %d", sndlength);
220                         } else {
221                                 outputf("RFB: send error %d", err);
222                                 break;
223                         }
224
225                         state->update_pos += sndlength;
226
227                         if (tcp_sndbuf(pcb) == 0) {
228                                 break;
229                         }
230                 }
231
232                 break;
233         }
234         
235         if (tcp_output(pcb) != ERR_OK)
236         {
237                 outputf("RFB: tcp_output bailed in send_fsm?");
238         }
239 }
240
241 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
242         struct rfb_state *state = arg;
243         send_fsm(pcb, state);
244         return ERR_OK;
245 }
246
247 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
248         tcp_arg(pcb, NULL);
249         tcp_sent(pcb, NULL);
250         tcp_recv(pcb, NULL);
251         mem_free(state);
252         tcp_close(pcb);
253 }
254
255 enum fsm_result {
256         NEEDMORE,
257         OK,
258         FAIL
259 };
260
261 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
262         int i;
263         int pktsize;
264
265         outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
266                 state->writepos);
267
268         switch(state->state) {
269         case ST_BEGIN:
270                 if (state->writepos < 12) return NEEDMORE;
271
272                 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
273                         state->version = 3;
274                 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
275                         /* Spec states that "RFB 003.005", an incorrect value,
276                          * should be treated by the server as 3.3. */
277                         state->version = 3;
278                 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
279                         state->version = 7;
280                 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
281                         state->version = 8;
282                 } else {
283                         outputf("RFB: Negotiation fail");
284                         return FAIL;
285                 }
286
287                 outputf("RFB: Negotiated v3.%d", state->version);
288
289                 state->readpos += 12;
290                 state->state = ST_CLIENTINIT;
291
292                 /* We support one security type, currently "none".
293                  * Send that and SecurityResult. */
294                 if (state->version >= 7) {
295                         tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
296                 } else {
297                         tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
298                 }
299
300                 tcp_output(pcb);
301
302                 return OK;
303
304         case ST_CLIENTINIT:
305                 if (state->version >= 7) {
306                         /* Ignore the security type and ClientInit */
307                         if (state->writepos < 2) return NEEDMORE;
308                         state->readpos += 2;
309                 } else {
310                         /* Just ClientInit */
311                         if (state->writepos < 1) return NEEDMORE;
312                         state->readpos += 1;
313                 }
314
315                 state->state = ST_MAIN;
316
317                 outputf("RFB: Sending server info", state->version);
318                 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
319                 tcp_output(pcb);
320
321                 return OK;
322
323         case ST_MAIN:
324                 if (state->writepos < 1) return NEEDMORE;
325
326                 outputf("RFB: cmd %d", state->data[0]);
327                 switch (state->data[0]) {
328
329                 case SET_PIXEL_FORMAT:
330                         /* SetPixelFormat */
331                         if (state->writepos < (sizeof(struct pixel_format) + 4))
332                                 return NEEDMORE;
333                         outputf("RFB: SetPixelFormat");
334 /*
335                         struct pixel_format * new_fmt =
336                                 (struct pixel_format *)(&state->data[4]);
337 */
338                         /* XXX ... */
339
340                         state->readpos += sizeof(struct pixel_format) + 4;
341                         return OK;
342
343                 case SET_ENCODINGS:
344                         if (state->writepos < 4) return NEEDMORE;
345
346                         struct set_encs_req * req = (struct set_encs_req *)state->data;
347
348                         pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
349
350                         outputf("RFB: SetEncodings [%d]", ntohs(req->num));
351                         if (state->writepos < pktsize) return NEEDMORE;
352
353                         for (i = 0; i < ntohs(req->num); i++) {
354                                 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
355                                 /* XXX ... */
356
357                         }
358
359                         state->readpos += pktsize;
360                         return OK;
361
362                 case FB_UPDATE_REQUEST:
363                         if (state->writepos < sizeof(struct fb_update_req))
364                                 return NEEDMORE;
365                         outputf("RFB: UpdateRequest");
366
367                         state->update_requested = 1;
368                         memcpy(&state->client_interest_area, state->data,
369                                sizeof(struct fb_update_req)); 
370
371                         state->readpos += sizeof(struct fb_update_req);
372                         return OK;
373
374                 case KEY_EVENT:
375                         if (state->writepos < sizeof(struct key_event_pkt))
376                                 return NEEDMORE;
377                         outputf("RFB: Key");
378
379                         /* XXX stub */
380
381                         state->readpos += sizeof(struct key_event_pkt);
382                         return OK;
383
384                 case POINTER_EVENT:
385                         if (state->writepos < sizeof(struct pointer_event_pkt))
386                                 return NEEDMORE;
387                         outputf("RFB: Pointer");
388
389                         /* XXX stub */
390
391                         state->readpos += sizeof(struct pointer_event_pkt);
392                         return OK;
393
394                 case CLIENT_CUT_TEXT:
395                         if (state->writepos < sizeof(struct text_event_pkt))
396                                 return NEEDMORE;
397                         outputf("RFB: Cut Text");
398
399                         struct text_event_pkt * pkt =
400                                 (struct text_event_pkt *)state->data;
401
402                         if (state->writepos < sizeof(struct text_event_pkt)
403                                               + pkt->length)
404                                 return NEEDMORE;
405
406                         /* XXX stub */
407
408                         state->readpos += sizeof(struct text_event_pkt)
409                                           + pkt->length;
410                         return OK;
411
412                 default:
413                         outputf("RFB: Bad command: %d", state->data[0]);
414                 }
415         default:
416                 outputf("RFB: Bad state");
417                 return FAIL;
418         }
419 }
420
421 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
422                       struct pbuf *p, err_t err) {
423         struct rfb_state *state = arg;
424
425         if (state == NULL) 
426
427         if (err != ERR_OK) {
428                 outputf("RFB: recv err %d", err);
429                 /* FIXME do something better here? */
430                 return ERR_OK;
431         }
432
433         if (p == NULL) {
434                 outputf("RFB: Connection closed");
435                 close_conn(pcb, state);
436                 return ERR_OK;
437         }
438
439         if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
440                 /* Overflow! */
441                 outputf("RFB: Overflow!");
442                 close_conn(pcb, state);
443                 return ERR_OK;
444         }
445
446         outputf("RFB: Processing %d", p->tot_len);
447         pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
448         state->writepos += p->tot_len;
449
450         tcp_recved(pcb, p->tot_len);
451         pbuf_free(p);
452
453         while (1) {
454                 switch (recv_fsm(pcb, state)) {
455                 case NEEDMORE:
456                         outputf("RFB FSM: blocking");
457                         /* Need more data */
458                         return ERR_OK;
459
460                 case OK:
461                         outputf("RFB FSM: ok");
462
463                         /* Might as well send now... */
464                         if (state->send_state == SST_IDLE
465                             && state->update_requested) {
466                                 send_fsm(pcb, state);
467                         }
468
469                         if (state->readpos == state->writepos) {
470                                 state->readpos = 0;
471                                 state->writepos = 0;
472                                 return ERR_OK;
473                         } else {
474                                 memmove(state->data,
475                                         state->data + state->readpos,
476                                         state->writepos - state->readpos);
477                         }
478                         break;
479                 case FAIL:
480                         /* Shit */
481                         outputf("RFB: Protocol error");
482                         close_conn(pcb, state);
483                         return ERR_OK;
484                 }
485         }
486 }       
487                 
488 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
489         struct rfb_state *state;
490
491         LWIP_UNUSED_ARG(arg);
492         LWIP_UNUSED_ARG(err);
493
494         state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
495
496         state->state = ST_BEGIN;
497         state->readpos = 0;
498         state->writepos = 0;
499         state->update_requested = 0;
500         state->send_state = SST_IDLE;
501
502         /* XXX: update_server_info() should be called from the 64ms timer, and deal
503          * with screen resizes appropriately. */
504         update_server_info();
505
506         if (!state)
507         {
508                 outputf("rfb_accept: out of memory\n");
509                 return ERR_MEM;
510         }
511
512         tcp_arg(pcb, state);
513         tcp_recv(pcb, rfb_recv);
514         tcp_sent(pcb, rfb_sent);
515 /*
516         tcp_err(pcb, rfb_err);
517         tcp_poll(pcb, rfb_poll, 2);
518 */
519         tcp_write(pcb, "RFB 003.008\n", 12, 0);
520         tcp_output(pcb);
521
522         return ERR_OK;
523 }
524
525 void rfb_init() {
526         struct tcp_pcb *pcb;
527
528         init_server_info();
529
530         pcb = tcp_new();
531         tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
532         pcb = tcp_listen(pcb);
533         tcp_accept(pcb, rfb_accept);
534 }
This page took 0.042826 seconds and 4 git commands to generate.