]>
Commit | Line | Data |
---|---|---|
7da36fbd JP |
1 | #include <stdint.h> |
2 | #include <minilib.h> | |
3 | #include <output.h> | |
4 | #include <fb.h> | |
33e49b2e | 5 | #include <keyboard.h> |
ec949d20 | 6 | |
7da36fbd | 7 | #include "lwip/tcp.h" |
b59a6e50 | 8 | #include "lwip/stats.h" |
7da36fbd JP |
9 | |
10 | #include "rfb.h" | |
11 | ||
12 | #define SET_PIXEL_FORMAT 0 | |
13 | #define SET_ENCODINGS 2 | |
14 | #define FB_UPDATE_REQUEST 3 | |
15 | #define KEY_EVENT 4 | |
16 | #define POINTER_EVENT 5 | |
17 | #define CLIENT_CUT_TEXT 6 | |
18 | ||
99a4c846 | 19 | #define RFB_BUF_SIZE 512 |
7da36fbd | 20 | |
d9b82f8c | 21 | #define SCREEN_CHUNKS_X 8 |
b496c2b1 JP |
22 | #define SCREEN_CHUNKS_Y 8 |
23 | ||
7da36fbd JP |
24 | struct pixel_format { |
25 | uint8_t bpp; | |
26 | uint8_t depth; | |
27 | uint8_t big_endian; | |
28 | uint8_t true_color; | |
29 | uint16_t red_max; | |
30 | uint16_t green_max; | |
31 | uint16_t blue_max; | |
32 | uint8_t red_shift; | |
33 | uint8_t green_shift; | |
34 | uint8_t blue_shift; | |
35 | uint8_t padding[3]; | |
36 | }; | |
37 | ||
38 | struct server_init_message { | |
39 | uint16_t fb_width; | |
40 | uint16_t fb_height; | |
41 | struct pixel_format fmt; | |
42 | uint32_t name_length; | |
43 | char name_string[8]; | |
44 | }; | |
45 | ||
46 | struct fb_update_req { | |
47 | uint8_t msgtype; | |
48 | uint8_t incremental; | |
49 | uint16_t xpos; | |
50 | uint16_t ypos; | |
51 | uint16_t width; | |
52 | uint16_t height; | |
53 | }; | |
54 | ||
55 | struct set_encs_req { | |
56 | uint8_t msgtype; | |
57 | uint8_t padding; | |
58 | uint16_t num; | |
59 | int32_t encodings[]; | |
60 | }; | |
61 | ||
62 | struct key_event_pkt { | |
63 | uint8_t msgtype; | |
64 | uint8_t downflag; | |
65 | uint8_t pad[2]; | |
66 | uint32_t keysym; | |
67 | }; | |
68 | ||
69 | struct pointer_event_pkt { | |
70 | uint8_t msgtype; | |
71 | uint8_t button_mask; | |
72 | uint16_t x; | |
73 | uint16_t y; | |
74 | }; | |
75 | ||
76 | struct text_event_pkt { | |
77 | uint8_t msgtype; | |
78 | uint8_t padding[3]; | |
79 | uint32_t length; | |
80 | char text[]; | |
81 | }; | |
82 | ||
8ceb0515 JP |
83 | struct update_header { |
84 | uint8_t msgtype; | |
85 | uint8_t padding; | |
86 | uint16_t nrects; | |
87 | uint16_t xpos; | |
88 | uint16_t ypos; | |
89 | uint16_t width; | |
90 | uint16_t height; | |
91 | int32_t enctype; | |
92 | }; | |
93 | ||
7da36fbd JP |
94 | struct rfb_state { |
95 | enum { | |
751f179d | 96 | ST_BEGIN = 0, |
7da36fbd JP |
97 | ST_CLIENTINIT, |
98 | ST_MAIN | |
99 | } state; | |
100 | int version; | |
101 | int encs_remaining; | |
102 | ||
103 | char data[RFB_BUF_SIZE]; | |
104 | int readpos; | |
105 | int writepos; | |
106 | ||
107 | char next_update_incremental; | |
8ceb0515 JP |
108 | char update_requested; |
109 | ||
7da36fbd JP |
110 | struct fb_update_req client_interest_area; |
111 | ||
8ceb0515 | 112 | enum { |
751f179d | 113 | SST_IDLE = 0, |
d9b82f8c JP |
114 | SST_HEADER, |
115 | SST_DATA | |
8ceb0515 JP |
116 | } send_state; |
117 | ||
99a4c846 | 118 | uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y]; |
b496c2b1 JP |
119 | |
120 | uint32_t chunk_xnum; | |
121 | uint32_t chunk_ynum; | |
122 | uint32_t chunk_xpos; | |
123 | uint32_t chunk_ypos; | |
124 | uint32_t chunk_width; | |
125 | uint32_t chunk_height; | |
126 | ||
127 | uint32_t chunk_lindex; | |
d136cf4c JW |
128 | |
129 | uint32_t chunk_checksum; | |
d9b82f8c | 130 | |
751f179d JP |
131 | int chunk_actually_sent; |
132 | int try_in_a_bit; | |
7da36fbd JP |
133 | }; |
134 | ||
135 | static struct server_init_message server_info; | |
136 | ||
137 | static void init_server_info() { | |
80726808 | 138 | server_info.name_length = htonl(8); |
7da36fbd JP |
139 | memcpy(server_info.name_string, "NetWatch", 8); |
140 | } | |
141 | ||
142 | static void update_server_info() { | |
143 | if (fb != NULL) { | |
80726808 JP |
144 | outputf("RFB: setting fmt %d", fb->curmode.format); |
145 | server_info.fb_width = htons(fb->curmode.xres); | |
146 | server_info.fb_height = htons(fb->curmode.yres); | |
7da36fbd JP |
147 | switch (fb->curmode.format) { |
148 | case FB_RGB888: | |
149 | server_info.fmt.bpp = 32; | |
150 | server_info.fmt.depth = 24; | |
151 | server_info.fmt.big_endian = 0; | |
152 | server_info.fmt.true_color = 1; | |
80726808 JP |
153 | server_info.fmt.red_max = htons(255); |
154 | server_info.fmt.green_max = htons(255); | |
155 | server_info.fmt.blue_max = htons(255); | |
7da36fbd JP |
156 | server_info.fmt.red_shift = 0; |
157 | server_info.fmt.green_shift = 8; | |
158 | server_info.fmt.blue_shift = 16; | |
159 | break; | |
160 | default: | |
161 | outputf("RFB: unknown fb fmt %d", fb->curmode.format); | |
162 | break; | |
163 | } | |
80726808 JP |
164 | } else { |
165 | outputf("RFB: fb null"); | |
7da36fbd JP |
166 | } |
167 | } | |
168 | ||
d9b82f8c JP |
169 | static int advance_chunk(struct rfb_state *state) { |
170 | ||
171 | state->chunk_xnum += 1; | |
172 | ||
173 | if (state->chunk_xnum == SCREEN_CHUNKS_X) { | |
174 | state->chunk_ynum += 1; | |
175 | state->chunk_xnum = 0; | |
176 | } | |
177 | ||
178 | if (state->chunk_ynum == SCREEN_CHUNKS_Y) { | |
179 | state->chunk_ynum = 0; | |
180 | state->send_state = SST_IDLE; | |
181 | if (!(state->chunk_actually_sent)) | |
751f179d | 182 | state->try_in_a_bit = 2; |
d9b82f8c JP |
183 | return 1; |
184 | } | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
8ceb0515 JP |
189 | static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) { |
190 | struct update_header hdr; | |
b496c2b1 | 191 | int lines_left; |
d9b82f8c | 192 | unsigned char * lptr; |
b496c2b1 | 193 | int totaldim; |
8ceb0515 JP |
194 | err_t err; |
195 | ||
d9b82f8c | 196 | while(1) { |
b496c2b1 | 197 | |
d9b82f8c | 198 | switch (state->send_state) { |
b496c2b1 | 199 | |
d9b82f8c JP |
200 | case SST_IDLE: |
201 | /* Nothing to do */ | |
8ceb0515 | 202 | |
d9b82f8c JP |
203 | if (state->update_requested) { |
204 | outputf("RFB send: update requested"); | |
205 | state->update_requested = 0; | |
206 | state->chunk_actually_sent = 0; | |
207 | state->send_state = SST_HEADER; | |
208 | } else { | |
209 | return; | |
210 | } | |
211 | ||
212 | /* FALL THROUGH to SST_HEADER */ | |
213 | ||
214 | case SST_HEADER: | |
215 | ||
216 | /* Calculate the width and height for this chunk, remembering | |
217 | * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and | |
218 | * height, we may need to have shorter chunks at the edge of | |
219 | * the screen. */ | |
220 | ||
221 | state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X; | |
222 | if (fb->curmode.xres % SCREEN_CHUNKS_X != 0) | |
223 | state->chunk_width += 1; | |
224 | state->chunk_xpos = state->chunk_width * state->chunk_xnum; | |
225 | totaldim = state->chunk_width * (state->chunk_xnum + 1); | |
226 | if (totaldim > fb->curmode.xres) { | |
227 | state->chunk_width -= (totaldim - fb->curmode.xres); | |
228 | } | |
b496c2b1 | 229 | |
d9b82f8c JP |
230 | state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y; |
231 | if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0) | |
232 | state->chunk_height += 1; | |
233 | state->chunk_ypos = state->chunk_height | |
234 | * state->chunk_ynum; | |
235 | totaldim = state->chunk_height * (state->chunk_ynum + 1); | |
236 | if (totaldim > fb->curmode.yres) { | |
237 | state->chunk_height -= (totaldim - fb->curmode.yres); | |
238 | } | |
8ceb0515 | 239 | |
d9b82f8c JP |
240 | /* Do we _actually_ need to send this chunk? */ |
241 | if (fb->checksum_rect) { | |
d136cf4c JW |
242 | state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos, |
243 | state->chunk_width, state->chunk_height); | |
d9b82f8c | 244 | |
d136cf4c | 245 | if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) { |
d9b82f8c JP |
246 | if (advance_chunk(state)) |
247 | return; | |
248 | continue; | |
d9b82f8c | 249 | } |
d136cf4c | 250 | /* Checksum gets set in data block, AFTER the data has been sent. */ |
d9b82f8c | 251 | } |
b496c2b1 | 252 | |
d9b82f8c JP |
253 | outputf("actually sent"); |
254 | state->chunk_actually_sent = 1; | |
b496c2b1 | 255 | |
d9b82f8c JP |
256 | /* Send a header */ |
257 | hdr.msgtype = 0; | |
258 | state->chunk_lindex = 0; | |
259 | hdr.nrects = htons(1); | |
260 | hdr.xpos = htons(state->chunk_xpos); | |
261 | hdr.ypos = htons(state->chunk_ypos); | |
262 | hdr.width = htons(state->chunk_width); | |
263 | hdr.height= htons(state->chunk_height); | |
264 | hdr.enctype = htonl(0); | |
265 | lines_left = state->chunk_height; | |
b496c2b1 | 266 | |
d9b82f8c | 267 | err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY); |
b496c2b1 | 268 | |
d9b82f8c JP |
269 | if (err != ERR_OK) { |
270 | if (err != ERR_MEM) | |
271 | outputf("RFB: header send error %d", err); | |
b496c2b1 | 272 | |
d9b82f8c JP |
273 | /* Try again later. */ |
274 | return; | |
275 | } | |
b496c2b1 | 276 | |
d9b82f8c | 277 | state->send_state = SST_DATA; |
b496c2b1 | 278 | |
d9b82f8c | 279 | /* FALL THROUGH to SST_DATA */ |
99a4c846 | 280 | |
d9b82f8c | 281 | case SST_DATA: |
8ceb0515 | 282 | |
d9b82f8c | 283 | lines_left = state->chunk_height - state->chunk_lindex; |
b496c2b1 | 284 | |
d9b82f8c JP |
285 | if (lines_left == 0) { |
286 | state->send_state = SST_HEADER; | |
d136cf4c | 287 | state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum; |
d9b82f8c JP |
288 | if (advance_chunk(state)) |
289 | return; | |
290 | break; | |
291 | } | |
b496c2b1 | 292 | |
d9b82f8c JP |
293 | lptr = fb->fbaddr |
294 | + (fb->curmode.xres * fb->curmode.bytestride | |
295 | * (state->chunk_ypos + state->chunk_lindex)) | |
296 | + (state->chunk_xpos * fb->curmode.bytestride); | |
b496c2b1 | 297 | |
d9b82f8c JP |
298 | /* The network card can't DMA from video RAM, |
299 | * so use TCP_WRITE_FLAG_COPY. */ | |
300 | err = tcp_write(pcb, lptr, | |
301 | fb->curmode.bytestride * state->chunk_width, TCP_WRITE_FLAG_COPY); | |
bbfab433 | 302 | |
d9b82f8c JP |
303 | if (err == ERR_OK) { |
304 | state->chunk_lindex += 1; | |
305 | } else { | |
8cc40c4d JW |
306 | if (err != ERR_MEM) |
307 | outputf("RFB: send error %d", err); | |
b496c2b1 | 308 | |
d9b82f8c | 309 | return; |
bbfab433 | 310 | } |
b496c2b1 | 311 | |
bbfab433 | 312 | if (tcp_sndbuf(pcb) == 0) { |
d9b82f8c | 313 | return; |
bbfab433 | 314 | } |
8ceb0515 | 315 | } |
8ceb0515 | 316 | } |
075bbc71 JW |
317 | |
318 | if (tcp_output(pcb) != ERR_OK) | |
075bbc71 | 319 | outputf("RFB: tcp_output bailed in send_fsm?"); |
8ceb0515 JP |
320 | } |
321 | ||
322 | static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) { | |
323 | struct rfb_state *state = arg; | |
324 | send_fsm(pcb, state); | |
325 | return ERR_OK; | |
80726808 JP |
326 | } |
327 | ||
b59a6e50 JP |
328 | static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) { |
329 | struct rfb_state *state = arg; | |
330 | send_fsm(pcb, state); | |
751f179d JP |
331 | if (state->try_in_a_bit) { |
332 | state->try_in_a_bit--; | |
333 | if (!(state->try_in_a_bit)) { | |
334 | state->update_requested = 1; | |
335 | } | |
336 | } | |
99a4c846 | 337 | /* |
b59a6e50 | 338 | stats_display(); |
99a4c846 | 339 | */ |
b59a6e50 JP |
340 | return ERR_OK; |
341 | } | |
342 | ||
7da36fbd | 343 | static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) { |
779b8ca8 | 344 | outputf("close_conn: bailing"); |
7da36fbd JP |
345 | tcp_arg(pcb, NULL); |
346 | tcp_sent(pcb, NULL); | |
347 | tcp_recv(pcb, NULL); | |
348 | mem_free(state); | |
349 | tcp_close(pcb); | |
779b8ca8 | 350 | outputf("close_conn: done"); |
7da36fbd JP |
351 | } |
352 | ||
353 | enum fsm_result { | |
354 | NEEDMORE, | |
355 | OK, | |
356 | FAIL | |
357 | }; | |
358 | ||
359 | static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) { | |
80726808 JP |
360 | int i; |
361 | int pktsize; | |
362 | ||
363 | outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos, | |
364 | state->writepos); | |
7da36fbd JP |
365 | |
366 | switch(state->state) { | |
367 | case ST_BEGIN: | |
368 | if (state->writepos < 12) return NEEDMORE; | |
369 | ||
370 | if (!strncmp(state->data, "RFB 003.003\n", 12)) { | |
371 | state->version = 3; | |
372 | } else if (!strncmp(state->data, "RFB 003.005\n", 12)) { | |
373 | /* Spec states that "RFB 003.005", an incorrect value, | |
374 | * should be treated by the server as 3.3. */ | |
375 | state->version = 3; | |
376 | } else if (!strncmp(state->data, "RFB 003.007\n", 12)) { | |
377 | state->version = 7; | |
378 | } else if (!strncmp(state->data, "RFB 003.008\n", 12)) { | |
379 | state->version = 8; | |
380 | } else { | |
381 | outputf("RFB: Negotiation fail"); | |
382 | return FAIL; | |
383 | } | |
384 | ||
385 | outputf("RFB: Negotiated v3.%d", state->version); | |
386 | ||
387 | state->readpos += 12; | |
388 | state->state = ST_CLIENTINIT; | |
389 | ||
80726808 JP |
390 | /* We support one security type, currently "none". |
391 | * Send that and SecurityResult. */ | |
7da36fbd | 392 | if (state->version >= 7) { |
80726808 | 393 | tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0); |
7da36fbd | 394 | } else { |
80726808 | 395 | tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0); |
7da36fbd JP |
396 | } |
397 | ||
7da36fbd JP |
398 | tcp_output(pcb); |
399 | ||
400 | return OK; | |
401 | ||
402 | case ST_CLIENTINIT: | |
80726808 JP |
403 | if (state->version >= 7) { |
404 | /* Ignore the security type and ClientInit */ | |
405 | if (state->writepos < 2) return NEEDMORE; | |
406 | state->readpos += 2; | |
407 | } else { | |
408 | /* Just ClientInit */ | |
409 | if (state->writepos < 1) return NEEDMORE; | |
410 | state->readpos += 1; | |
411 | } | |
412 | ||
7da36fbd JP |
413 | state->state = ST_MAIN; |
414 | ||
80726808 | 415 | outputf("RFB: Sending server info", state->version); |
075bbc71 | 416 | tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY); |
7da36fbd JP |
417 | tcp_output(pcb); |
418 | ||
419 | return OK; | |
420 | ||
421 | case ST_MAIN: | |
422 | if (state->writepos < 1) return NEEDMORE; | |
423 | ||
80726808 | 424 | outputf("RFB: cmd %d", state->data[0]); |
7da36fbd JP |
425 | switch (state->data[0]) { |
426 | ||
427 | case SET_PIXEL_FORMAT: | |
428 | /* SetPixelFormat */ | |
429 | if (state->writepos < (sizeof(struct pixel_format) + 4)) | |
430 | return NEEDMORE; | |
80726808 | 431 | outputf("RFB: SetPixelFormat"); |
7da36fbd JP |
432 | /* |
433 | struct pixel_format * new_fmt = | |
434 | (struct pixel_format *)(&state->data[4]); | |
435 | */ | |
436 | /* XXX ... */ | |
437 | ||
438 | state->readpos += sizeof(struct pixel_format) + 4; | |
439 | return OK; | |
440 | ||
441 | case SET_ENCODINGS: | |
442 | if (state->writepos < 4) return NEEDMORE; | |
443 | ||
444 | struct set_encs_req * req = (struct set_encs_req *)state->data; | |
445 | ||
80726808 | 446 | pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num)); |
7da36fbd | 447 | |
80726808 JP |
448 | outputf("RFB: SetEncodings [%d]", ntohs(req->num)); |
449 | if (state->writepos < pktsize) return NEEDMORE; | |
450 | ||
451 | for (i = 0; i < ntohs(req->num); i++) { | |
452 | outputf("RFB: Encoding: %d", ntohl(req->encodings[i])); | |
453 | /* XXX ... */ | |
80726808 JP |
454 | } |
455 | ||
456 | state->readpos += pktsize; | |
7da36fbd JP |
457 | return OK; |
458 | ||
459 | case FB_UPDATE_REQUEST: | |
460 | if (state->writepos < sizeof(struct fb_update_req)) | |
461 | return NEEDMORE; | |
80726808 | 462 | outputf("RFB: UpdateRequest"); |
7da36fbd | 463 | |
8ceb0515 | 464 | state->update_requested = 1; |
7da36fbd JP |
465 | memcpy(&state->client_interest_area, state->data, |
466 | sizeof(struct fb_update_req)); | |
467 | ||
468 | state->readpos += sizeof(struct fb_update_req); | |
469 | return OK; | |
470 | ||
471 | case KEY_EVENT: | |
472 | if (state->writepos < sizeof(struct key_event_pkt)) | |
473 | return NEEDMORE; | |
474 | ||
ec949d20 | 475 | struct key_event_pkt * p = (struct key_event_pkt *)state->data; |
dadcd4fc JP |
476 | |
477 | outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF)); | |
ec949d20 | 478 | kbd_inject_keysym(htonl(p->keysym), p->downflag); |
7da36fbd JP |
479 | |
480 | state->readpos += sizeof(struct key_event_pkt); | |
481 | return OK; | |
482 | ||
483 | case POINTER_EVENT: | |
484 | if (state->writepos < sizeof(struct pointer_event_pkt)) | |
485 | return NEEDMORE; | |
80726808 | 486 | outputf("RFB: Pointer"); |
7da36fbd JP |
487 | |
488 | /* XXX stub */ | |
489 | ||
490 | state->readpos += sizeof(struct pointer_event_pkt); | |
491 | return OK; | |
492 | ||
493 | case CLIENT_CUT_TEXT: | |
494 | if (state->writepos < sizeof(struct text_event_pkt)) | |
495 | return NEEDMORE; | |
80726808 | 496 | outputf("RFB: Cut Text"); |
7da36fbd JP |
497 | |
498 | struct text_event_pkt * pkt = | |
499 | (struct text_event_pkt *)state->data; | |
500 | ||
501 | if (state->writepos < sizeof(struct text_event_pkt) | |
d9b82f8c | 502 | + pkt->length) |
7da36fbd JP |
503 | return NEEDMORE; |
504 | ||
505 | /* XXX stub */ | |
506 | ||
507 | state->readpos += sizeof(struct text_event_pkt) | |
d9b82f8c | 508 | + pkt->length; |
7da36fbd JP |
509 | return OK; |
510 | ||
511 | default: | |
512 | outputf("RFB: Bad command: %d", state->data[0]); | |
779b8ca8 | 513 | return FAIL; |
7da36fbd JP |
514 | } |
515 | default: | |
516 | outputf("RFB: Bad state"); | |
517 | return FAIL; | |
518 | } | |
519 | } | |
520 | ||
521 | static err_t rfb_recv(void *arg, struct tcp_pcb *pcb, | |
d9b82f8c | 522 | struct pbuf *p, err_t err) { |
7da36fbd | 523 | struct rfb_state *state = arg; |
779b8ca8 | 524 | uint16_t copylen; |
7da36fbd JP |
525 | |
526 | if (state == NULL) | |
527 | ||
528 | if (err != ERR_OK) { | |
529 | outputf("RFB: recv err %d", err); | |
530 | /* FIXME do something better here? */ | |
531 | return ERR_OK; | |
532 | } | |
533 | ||
534 | if (p == NULL) { | |
535 | outputf("RFB: Connection closed"); | |
536 | close_conn(pcb, state); | |
537 | return ERR_OK; | |
538 | } | |
539 | ||
540 | if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) { | |
541 | /* Overflow! */ | |
542 | outputf("RFB: Overflow!"); | |
543 | close_conn(pcb, state); | |
544 | return ERR_OK; | |
545 | } | |
546 | ||
779b8ca8 JP |
547 | copylen = pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0); |
548 | outputf("RFB: Processing %d, wp %d, cp %d", p->tot_len, state->writepos, copylen); | |
80726808 JP |
549 | state->writepos += p->tot_len; |
550 | ||
7da36fbd JP |
551 | tcp_recved(pcb, p->tot_len); |
552 | pbuf_free(p); | |
553 | ||
554 | while (1) { | |
555 | switch (recv_fsm(pcb, state)) { | |
556 | case NEEDMORE: | |
80726808 | 557 | outputf("RFB FSM: blocking"); |
779b8ca8 | 558 | goto doneprocessing; |
7da36fbd JP |
559 | |
560 | case OK: | |
80726808 JP |
561 | outputf("RFB FSM: ok"); |
562 | ||
7da36fbd JP |
563 | if (state->readpos == state->writepos) { |
564 | state->readpos = 0; | |
565 | state->writepos = 0; | |
779b8ca8 | 566 | goto doneprocessing; |
7da36fbd JP |
567 | } else { |
568 | memmove(state->data, | |
d9b82f8c JP |
569 | state->data + state->readpos, |
570 | state->writepos - state->readpos); | |
779b8ca8 JP |
571 | state->writepos -= state->readpos; |
572 | state->readpos = 0; | |
7da36fbd JP |
573 | } |
574 | break; | |
575 | case FAIL: | |
576 | /* Shit */ | |
577 | outputf("RFB: Protocol error"); | |
578 | close_conn(pcb, state); | |
579 | return ERR_OK; | |
580 | } | |
581 | } | |
779b8ca8 JP |
582 | |
583 | doneprocessing: | |
584 | ||
585 | /* Kick off a send. */ | |
586 | if (state->send_state == SST_IDLE && state->update_requested) { | |
587 | send_fsm(pcb, state); | |
588 | } | |
589 | ||
590 | return ERR_OK; | |
7da36fbd JP |
591 | } |
592 | ||
593 | static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) { | |
594 | struct rfb_state *state; | |
595 | ||
596 | LWIP_UNUSED_ARG(arg); | |
597 | LWIP_UNUSED_ARG(err); | |
598 | ||
599 | state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state)); | |
600 | ||
751f179d JP |
601 | memset(state, 0, sizeof(struct rfb_state)); |
602 | ||
80726808 | 603 | state->state = ST_BEGIN; |
8ceb0515 | 604 | state->send_state = SST_IDLE; |
80726808 | 605 | |
7da36fbd JP |
606 | /* XXX: update_server_info() should be called from the 64ms timer, and deal |
607 | * with screen resizes appropriately. */ | |
608 | update_server_info(); | |
609 | ||
610 | if (!state) | |
611 | { | |
612 | outputf("rfb_accept: out of memory\n"); | |
613 | return ERR_MEM; | |
614 | } | |
615 | ||
616 | tcp_arg(pcb, state); | |
617 | tcp_recv(pcb, rfb_recv); | |
8ceb0515 | 618 | tcp_sent(pcb, rfb_sent); |
b59a6e50 | 619 | tcp_poll(pcb, rfb_poll, 1); |
7da36fbd JP |
620 | /* |
621 | tcp_err(pcb, rfb_err); | |
7da36fbd JP |
622 | */ |
623 | tcp_write(pcb, "RFB 003.008\n", 12, 0); | |
624 | tcp_output(pcb); | |
625 | ||
626 | return ERR_OK; | |
627 | } | |
628 | ||
629 | void rfb_init() { | |
630 | struct tcp_pcb *pcb; | |
631 | ||
632 | init_server_info(); | |
633 | ||
634 | pcb = tcp_new(); | |
635 | tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT); | |
636 | pcb = tcp_listen(pcb); | |
637 | tcp_accept(pcb, rfb_accept); | |
638 | } |