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