]> Joshua Wise's Git repositories - netwatch.git/blob - net/rfb.c
eff9c41dc6267decbe69f3752246d962e16f2121
[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), 0);
181                 tcp_output(pcb);
182
183                 state->update_pos = 0;
184                 state->send_state = SST_SENDING;
185
186                 /* FALL THROUGH */
187
188         case SST_SENDING:
189
190                 while (1) {
191                         left = state->frame_bytes - state->update_pos;
192
193                         if (left == 0) {
194                                 state->send_state = SST_IDLE;
195                                 break;
196                         }
197
198                         if (left > tcp_mss(pcb)) {
199                                 sndlength = tcp_mss(pcb);
200                         } else {
201                                 sndlength = left;
202                         }
203
204                         do {
205                                 err = tcp_write(pcb, fb->fbaddr + state->update_pos, sndlength, 0);
206                                 if (err == ERR_MEM) {
207                                         outputf("RFB: ERR_MEM sending %d", sndlength);
208                                         sndlength /= 2;
209                                 }
210                         } while (err == ERR_MEM && sndlength > 1);
211
212                         if (err == ERR_OK) {
213                                 outputf("RFB: attempting send %d", sndlength);
214                         } else {
215                                 outputf("RFB: send error %d", err);
216                                 break;
217                         }
218
219                         if (tcp_output(pcb) != ERR_OK) {
220                                 outputf("RFB: tcp_output has had enough");
221                                 break;
222                         }
223
224                         state->update_pos += sndlength;
225
226                         if (tcp_sndbuf(pcb) == 0) {
227                                 break;
228                         }
229                 }
230
231                 break;
232         }
233 }
234
235 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
236         struct rfb_state *state = arg;
237         send_fsm(pcb, state);
238         return ERR_OK;
239 }
240
241 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
242         tcp_arg(pcb, NULL);
243         tcp_sent(pcb, NULL);
244         tcp_recv(pcb, NULL);
245         mem_free(state);
246         tcp_close(pcb);
247 }
248
249 enum fsm_result {
250         NEEDMORE,
251         OK,
252         FAIL
253 };
254
255 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
256         int i;
257         int pktsize;
258
259         outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
260                 state->writepos);
261
262         switch(state->state) {
263         case ST_BEGIN:
264                 if (state->writepos < 12) return NEEDMORE;
265
266                 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
267                         state->version = 3;
268                 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
269                         /* Spec states that "RFB 003.005", an incorrect value,
270                          * should be treated by the server as 3.3. */
271                         state->version = 3;
272                 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
273                         state->version = 7;
274                 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
275                         state->version = 8;
276                 } else {
277                         outputf("RFB: Negotiation fail");
278                         return FAIL;
279                 }
280
281                 outputf("RFB: Negotiated v3.%d", state->version);
282
283                 state->readpos += 12;
284                 state->state = ST_CLIENTINIT;
285
286                 /* We support one security type, currently "none".
287                  * Send that and SecurityResult. */
288                 if (state->version >= 7) {
289                         tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
290                 } else {
291                         tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
292                 }
293
294                 tcp_output(pcb);
295
296                 return OK;
297
298         case ST_CLIENTINIT:
299                 if (state->version >= 7) {
300                         /* Ignore the security type and ClientInit */
301                         if (state->writepos < 2) return NEEDMORE;
302                         state->readpos += 2;
303                 } else {
304                         /* Just ClientInit */
305                         if (state->writepos < 1) return NEEDMORE;
306                         state->readpos += 1;
307                 }
308
309                 state->state = ST_MAIN;
310
311                 outputf("RFB: Sending server info", state->version);
312                 tcp_write(pcb, &server_info, sizeof(server_info), 0);
313                 tcp_output(pcb);
314
315                 return OK;
316
317         case ST_MAIN:
318                 if (state->writepos < 1) return NEEDMORE;
319
320                 outputf("RFB: cmd %d", state->data[0]);
321                 switch (state->data[0]) {
322
323                 case SET_PIXEL_FORMAT:
324                         /* SetPixelFormat */
325                         if (state->writepos < (sizeof(struct pixel_format) + 4))
326                                 return NEEDMORE;
327                         outputf("RFB: SetPixelFormat");
328 /*
329                         struct pixel_format * new_fmt =
330                                 (struct pixel_format *)(&state->data[4]);
331 */
332                         /* XXX ... */
333
334                         state->readpos += sizeof(struct pixel_format) + 4;
335                         return OK;
336
337                 case SET_ENCODINGS:
338                         if (state->writepos < 4) return NEEDMORE;
339
340                         struct set_encs_req * req = (struct set_encs_req *)state->data;
341
342                         pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
343
344                         outputf("RFB: SetEncodings [%d]", ntohs(req->num));
345                         if (state->writepos < pktsize) return NEEDMORE;
346
347                         for (i = 0; i < ntohs(req->num); i++) {
348                                 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
349                                 /* XXX ... */
350
351                         }
352
353                         state->readpos += pktsize;
354                         return OK;
355
356                 case FB_UPDATE_REQUEST:
357                         if (state->writepos < sizeof(struct fb_update_req))
358                                 return NEEDMORE;
359                         outputf("RFB: UpdateRequest");
360
361                         state->update_requested = 1;
362                         memcpy(&state->client_interest_area, state->data,
363                                sizeof(struct fb_update_req)); 
364
365                         state->readpos += sizeof(struct fb_update_req);
366                         return OK;
367
368                 case KEY_EVENT:
369                         if (state->writepos < sizeof(struct key_event_pkt))
370                                 return NEEDMORE;
371                         outputf("RFB: Key");
372
373                         /* XXX stub */
374
375                         state->readpos += sizeof(struct key_event_pkt);
376                         return OK;
377
378                 case POINTER_EVENT:
379                         if (state->writepos < sizeof(struct pointer_event_pkt))
380                                 return NEEDMORE;
381                         outputf("RFB: Pointer");
382
383                         /* XXX stub */
384
385                         state->readpos += sizeof(struct pointer_event_pkt);
386                         return OK;
387
388                 case CLIENT_CUT_TEXT:
389                         if (state->writepos < sizeof(struct text_event_pkt))
390                                 return NEEDMORE;
391                         outputf("RFB: Cut Text");
392
393                         struct text_event_pkt * pkt =
394                                 (struct text_event_pkt *)state->data;
395
396                         if (state->writepos < sizeof(struct text_event_pkt)
397                                               + pkt->length)
398                                 return NEEDMORE;
399
400                         /* XXX stub */
401
402                         state->readpos += sizeof(struct text_event_pkt)
403                                           + pkt->length;
404                         return OK;
405
406                 default:
407                         outputf("RFB: Bad command: %d", state->data[0]);
408                 }
409         default:
410                 outputf("RFB: Bad state");
411                 return FAIL;
412         }
413 }
414
415 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
416                       struct pbuf *p, err_t err) {
417         struct rfb_state *state = arg;
418
419         if (state == NULL) 
420
421         if (err != ERR_OK) {
422                 outputf("RFB: recv err %d", err);
423                 /* FIXME do something better here? */
424                 return ERR_OK;
425         }
426
427         if (p == NULL) {
428                 outputf("RFB: Connection closed");
429                 close_conn(pcb, state);
430                 return ERR_OK;
431         }
432
433         if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
434                 /* Overflow! */
435                 outputf("RFB: Overflow!");
436                 close_conn(pcb, state);
437                 return ERR_OK;
438         }
439
440         outputf("RFB: Processing %d", p->tot_len);
441         pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
442         state->writepos += p->tot_len;
443
444         tcp_recved(pcb, p->tot_len);
445         pbuf_free(p);
446
447         while (1) {
448                 switch (recv_fsm(pcb, state)) {
449                 case NEEDMORE:
450                         outputf("RFB FSM: blocking");
451                         /* Need more data */
452                         return ERR_OK;
453
454                 case OK:
455                         outputf("RFB FSM: ok");
456
457                         /* Might as well send now... */
458                         if (state->send_state == SST_IDLE
459                             && state->update_requested) {
460                                 send_fsm(pcb, state);
461                         }
462
463                         if (state->readpos == state->writepos) {
464                                 state->readpos = 0;
465                                 state->writepos = 0;
466                                 return ERR_OK;
467                         } else {
468                                 memmove(state->data,
469                                         state->data + state->readpos,
470                                         state->writepos - state->readpos);
471                         }
472                         break;
473                 case FAIL:
474                         /* Shit */
475                         outputf("RFB: Protocol error");
476                         close_conn(pcb, state);
477                         return ERR_OK;
478                 }
479         }
480 }       
481                 
482 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
483         struct rfb_state *state;
484
485         LWIP_UNUSED_ARG(arg);
486         LWIP_UNUSED_ARG(err);
487
488         state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
489
490         state->state = ST_BEGIN;
491         state->readpos = 0;
492         state->writepos = 0;
493         state->update_requested = 0;
494         state->send_state = SST_IDLE;
495
496         /* XXX: update_server_info() should be called from the 64ms timer, and deal
497          * with screen resizes appropriately. */
498         update_server_info();
499
500         if (!state)
501         {
502                 outputf("rfb_accept: out of memory\n");
503                 return ERR_MEM;
504         }
505
506         tcp_arg(pcb, state);
507         tcp_recv(pcb, rfb_recv);
508         tcp_sent(pcb, rfb_sent);
509 /*
510         tcp_err(pcb, rfb_err);
511         tcp_poll(pcb, rfb_poll, 2);
512 */
513         tcp_write(pcb, "RFB 003.008\n", 12, 0);
514         tcp_output(pcb);
515
516         return ERR_OK;
517 }
518
519 void rfb_init() {
520         struct tcp_pcb *pcb;
521
522         init_server_info();
523
524         pcb = tcp_new();
525         tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
526         pcb = tcp_listen(pcb);
527         tcp_accept(pcb, rfb_accept);
528 }
This page took 0.046544 seconds and 2 git commands to generate.