6 #include "../aseg-paging/keyboard.h"
 
  12 #define SET_PIXEL_FORMAT        0
 
  13 #define SET_ENCODINGS           2
 
  14 #define FB_UPDATE_REQUEST       3
 
  16 #define POINTER_EVENT           5
 
  17 #define CLIENT_CUT_TEXT         6
 
  19 #define RFB_BUF_SIZE    64
 
  35 struct server_init_message {
 
  38         struct pixel_format fmt;
 
  43 struct fb_update_req {
 
  59 struct key_event_pkt {
 
  66 struct pointer_event_pkt {
 
  73 struct text_event_pkt {
 
  80 struct update_header {
 
 100         char data[RFB_BUF_SIZE];
 
 104         char next_update_incremental;
 
 105         char update_requested;
 
 107         struct fb_update_req client_interest_area;
 
 116         uint32_t frame_bytes;
 
 119 static struct server_init_message server_info;
 
 121 static void init_server_info() {
 
 122         server_info.name_length = htonl(8);
 
 123         memcpy(server_info.name_string, "NetWatch", 8);
 
 126 static void update_server_info() {
 
 128                 outputf("RFB: setting fmt %d", fb->curmode.format);
 
 129                 server_info.fb_width = htons(fb->curmode.xres);
 
 130                 server_info.fb_height = htons(fb->curmode.yres);
 
 131                 switch (fb->curmode.format) {
 
 133                         server_info.fmt.bpp = 32;
 
 134                         server_info.fmt.depth = 24;
 
 135                         server_info.fmt.big_endian = 0;
 
 136                         server_info.fmt.true_color = 1;
 
 137                         server_info.fmt.red_max = htons(255);
 
 138                         server_info.fmt.green_max = htons(255);
 
 139                         server_info.fmt.blue_max = htons(255);
 
 140                         server_info.fmt.red_shift = 0;
 
 141                         server_info.fmt.green_shift = 8;
 
 142                         server_info.fmt.blue_shift = 16;
 
 145                         outputf("RFB: unknown fb fmt %d", fb->curmode.format);
 
 149                 outputf("RFB: fb null");
 
 153 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
 
 154         struct update_header hdr;
 
 158         switch (state->send_state) {
 
 161                 if (state->update_requested) {
 
 162                         outputf("RFB send: update requested");
 
 163                         state->update_requested = 0;
 
 164                         state->send_state = SST_NEEDS_UPDATE;
 
 169                 /* FALL THROUGH to SST_NEEDS_UPDATE*/
 
 170         case SST_NEEDS_UPDATE:
 
 171                 outputf("RFB send: sending header");
 
 173                 state->frame_bytes = fb->curmode.xres * fb->curmode.yres * fb->curmode.bytestride;
 
 175                 hdr.nrects = htons(1);
 
 178                 hdr.width = htons(fb->curmode.xres);
 
 179                 hdr.height = htons(fb->curmode.yres);
 
 180                 hdr.enctype = htonl(0);
 
 181                 tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
 
 183                 state->update_pos = 0;
 
 184                 state->send_state = SST_SENDING;
 
 186                 /* FALL THROUGH to SST_SENDING*/
 
 189                         left = state->frame_bytes - state->update_pos;
 
 192                                 state->send_state = SST_IDLE;
 
 196                         if (left > 8192)        /* Sounds good enough to me. */
 
 201                                 err = tcp_write(pcb, fb->fbaddr + state->update_pos, sndlength, TCP_WRITE_FLAG_COPY /* The card can't DMA from there. */);
 
 202                                 if (err == ERR_MEM)             /* Back down until lwip says we've got space. */
 
 204                         } while (err == ERR_MEM && sndlength > 1);
 
 208                                         outputf("RFB: send error %d", err);
 
 210                                 /* We'll just give up for now and come back when we have space later. */
 
 214                         state->update_pos += sndlength;
 
 216                         if (tcp_sndbuf(pcb) == 0) {
 
 224         if (tcp_output(pcb) != ERR_OK)
 
 225                 outputf("RFB: tcp_output bailed in send_fsm?");
 
 228 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
 
 229         struct rfb_state *state = arg;
 
 230         send_fsm(pcb, state);
 
 234 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
 
 248 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
 
 252         outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
 
 255         switch(state->state) {
 
 257                 if (state->writepos < 12) return NEEDMORE;
 
 259                 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
 
 261                 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
 
 262                         /* Spec states that "RFB 003.005", an incorrect value,
 
 263                          * should be treated by the server as 3.3. */
 
 265                 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
 
 267                 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
 
 270                         outputf("RFB: Negotiation fail");
 
 274                 outputf("RFB: Negotiated v3.%d", state->version);
 
 276                 state->readpos += 12;
 
 277                 state->state = ST_CLIENTINIT;
 
 279                 /* We support one security type, currently "none".
 
 280                  * Send that and SecurityResult. */
 
 281                 if (state->version >= 7) {
 
 282                         tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
 
 284                         tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
 
 292                 if (state->version >= 7) {
 
 293                         /* Ignore the security type and ClientInit */
 
 294                         if (state->writepos < 2) return NEEDMORE;
 
 297                         /* Just ClientInit */
 
 298                         if (state->writepos < 1) return NEEDMORE;
 
 302                 state->state = ST_MAIN;
 
 304                 outputf("RFB: Sending server info", state->version);
 
 305                 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
 
 311                 if (state->writepos < 1) return NEEDMORE;
 
 313                 outputf("RFB: cmd %d", state->data[0]);
 
 314                 switch (state->data[0]) {
 
 316                 case SET_PIXEL_FORMAT:
 
 318                         if (state->writepos < (sizeof(struct pixel_format) + 4))
 
 320                         outputf("RFB: SetPixelFormat");
 
 322                         struct pixel_format * new_fmt =
 
 323                                 (struct pixel_format *)(&state->data[4]);
 
 327                         state->readpos += sizeof(struct pixel_format) + 4;
 
 331                         if (state->writepos < 4) return NEEDMORE;
 
 333                         struct set_encs_req * req = (struct set_encs_req *)state->data;
 
 335                         pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
 
 337                         outputf("RFB: SetEncodings [%d]", ntohs(req->num));
 
 338                         if (state->writepos < pktsize) return NEEDMORE;
 
 340                         for (i = 0; i < ntohs(req->num); i++) {
 
 341                                 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
 
 345                         state->readpos += pktsize;
 
 348                 case FB_UPDATE_REQUEST:
 
 349                         if (state->writepos < sizeof(struct fb_update_req))
 
 351                         outputf("RFB: UpdateRequest");
 
 353                         state->update_requested = 1;
 
 354                         memcpy(&state->client_interest_area, state->data,
 
 355                                sizeof(struct fb_update_req)); 
 
 357                         state->readpos += sizeof(struct fb_update_req);
 
 361                         if (state->writepos < sizeof(struct key_event_pkt))
 
 364                         struct key_event_pkt * p = (struct key_event_pkt *)state->data;
 
 366                         outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
 
 367                         kbd_inject_keysym(htonl(p->keysym), p->downflag);
 
 369                         state->readpos += sizeof(struct key_event_pkt);
 
 373                         if (state->writepos < sizeof(struct pointer_event_pkt))
 
 375                         outputf("RFB: Pointer");
 
 379                         state->readpos += sizeof(struct pointer_event_pkt);
 
 382                 case CLIENT_CUT_TEXT:
 
 383                         if (state->writepos < sizeof(struct text_event_pkt))
 
 385                         outputf("RFB: Cut Text");
 
 387                         struct text_event_pkt * pkt =
 
 388                                 (struct text_event_pkt *)state->data;
 
 390                         if (state->writepos < sizeof(struct text_event_pkt)
 
 396                         state->readpos += sizeof(struct text_event_pkt)
 
 401                         outputf("RFB: Bad command: %d", state->data[0]);
 
 404                 outputf("RFB: Bad state");
 
 409 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
 
 410                       struct pbuf *p, err_t err) {
 
 411         struct rfb_state *state = arg;
 
 416                 outputf("RFB: recv err %d", err);
 
 417                 /* FIXME do something better here? */
 
 422                 outputf("RFB: Connection closed");
 
 423                 close_conn(pcb, state);
 
 427         if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
 
 429                 outputf("RFB: Overflow!");
 
 430                 close_conn(pcb, state);
 
 434         outputf("RFB: Processing %d", p->tot_len);
 
 435         pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
 
 436         state->writepos += p->tot_len;
 
 438         tcp_recved(pcb, p->tot_len);
 
 442                 switch (recv_fsm(pcb, state)) {
 
 444                         outputf("RFB FSM: blocking");
 
 449                         outputf("RFB FSM: ok");
 
 451                         /* Kick off a send. */
 
 452                         if (state->send_state == SST_IDLE
 
 453                             && state->update_requested) {
 
 454                                 send_fsm(pcb, state);
 
 457                         if (state->readpos == state->writepos) {
 
 463                                         state->data + state->readpos,
 
 464                                         state->writepos - state->readpos);
 
 469                         outputf("RFB: Protocol error");
 
 470                         close_conn(pcb, state);
 
 476 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
 
 477         struct rfb_state *state;
 
 479         LWIP_UNUSED_ARG(arg);
 
 480         LWIP_UNUSED_ARG(err);
 
 482         state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
 
 484         state->state = ST_BEGIN;
 
 487         state->update_requested = 0;
 
 488         state->send_state = SST_IDLE;
 
 490         /* XXX: update_server_info() should be called from the 64ms timer, and deal
 
 491          * with screen resizes appropriately. */
 
 492         update_server_info();
 
 496                 outputf("rfb_accept: out of memory\n");
 
 501         tcp_recv(pcb, rfb_recv);
 
 502         tcp_sent(pcb, rfb_sent);
 
 504         tcp_err(pcb, rfb_err);
 
 505         tcp_poll(pcb, rfb_poll, 2);
 
 507         tcp_write(pcb, "RFB 003.008\n", 12, 0);
 
 519         tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
 
 520         pcb = tcp_listen(pcb);
 
 521         tcp_accept(pcb, rfb_accept);