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