Changeset cf3aee19 in mainline for uspace/srv/hid/rfb/rfb.c
- Timestamp:
- 2015-06-17T23:45:24Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 523b17a
- Parents:
- fc7bf19 (diff), 2654afb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/hid/rfb/rfb.c
rfc7bf19 rcf3aee19 31 31 #include <stdlib.h> 32 32 #include <fibril_synch.h> 33 #include <inet/addr.h> 34 #include <inet/endpoint.h> 35 #include <inet/tcp.h> 33 36 #include <inttypes.h> 34 37 #include <str.h> … … 38 41 #include <io/log.h> 39 42 40 #include <net/in.h>41 #include <net/inet.h>42 #include <net/socket.h>43 44 43 #include "rfb.h" 44 45 static void rfb_new_conn(tcp_listener_t *, tcp_conn_t *); 46 47 static tcp_listen_cb_t listen_cb = { 48 .new_conn = rfb_new_conn 49 }; 50 51 static tcp_cb_t conn_cb = { 52 .connected = NULL 53 }; 45 54 46 55 /** Buffer for receiving the request. */ … … 53 62 54 63 /** Receive one character (with buffering) */ 55 static int recv_char(int fd, char *c) 56 { 64 static int recv_char(tcp_conn_t *conn, char *c) 65 { 66 size_t nrecv; 67 int rc; 68 57 69 if (rbuf_out == rbuf_in) { 58 70 rbuf_out = 0; 59 71 rbuf_in = 0; 60 72 61 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);62 if (rc <= 0)73 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv); 74 if (rc != EOK) 63 75 return rc; 64 76 65 rbuf_in = rc;77 rbuf_in = nrecv; 66 78 } 67 79 … … 71 83 72 84 /** Receive count characters (with buffering) */ 73 static int recv_chars( int fd, char *c, size_t count)85 static int recv_chars(tcp_conn_t *conn, char *c, size_t count) 74 86 { 75 87 for (size_t i = 0; i < count; i++) { 76 int rc = recv_char( fd, c);88 int rc = recv_char(conn, c); 77 89 if (rc != EOK) 78 90 return rc; … … 82 94 } 83 95 84 static int recv_skip_chars( int fd, size_t count)96 static int recv_skip_chars(tcp_conn_t *conn, size_t count) 85 97 { 86 98 for (size_t i = 0; i < count; i++) { 87 99 char c; 88 int rc = recv_char( fd, &c);100 int rc = recv_char(conn, &c); 89 101 if (rc != EOK) 90 102 return rc; … … 204 216 } 205 217 206 static int recv_message( int conn_sd, char type, void *buf, size_t size)218 static int recv_message(tcp_conn_t *conn, char type, void *buf, size_t size) 207 219 { 208 220 memcpy(buf, &type, 1); 209 return recv_chars(conn _sd, ((char *) buf) + 1, size -1);221 return recv_chars(conn, ((char *) buf) + 1, size -1); 210 222 } 211 223 … … 477 489 } 478 490 479 static int rfb_send_framebuffer_update(rfb_t *rfb, int conn_sd, bool incremental) 491 static int rfb_send_framebuffer_update(rfb_t *rfb, tcp_conn_t *conn, 492 bool incremental) 480 493 { 481 494 fibril_mutex_lock(&rfb->lock); … … 540 553 541 554 if (!rfb->pixel_format.true_color) { 542 int rc = send(conn_sd, send_palette, send_palette_size, 0);555 int rc = tcp_conn_send(conn, send_palette, send_palette_size); 543 556 if (rc != EOK) { 544 557 free(buf); … … 547 560 } 548 561 549 int rc = send(conn_sd, buf, buf_size, 0);562 int rc = tcp_conn_send(conn, buf, buf_size); 550 563 free(buf); 551 564 … … 580 593 } 581 594 582 static void rfb_socket_connection(rfb_t *rfb, int conn_sd)595 static void rfb_socket_connection(rfb_t *rfb, tcp_conn_t *conn) 583 596 { 584 597 /* Version handshake */ 585 int rc = send(conn_sd, "RFB 003.008\n", 12, 0);598 int rc = tcp_conn_send(conn, "RFB 003.008\n", 12); 586 599 if (rc != EOK) { 587 600 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server version %d", rc); … … 590 603 591 604 char client_version[12]; 592 rc = recv_chars(conn _sd, client_version, 12);605 rc = recv_chars(conn, client_version, 12); 593 606 if (rc != EOK) { 594 607 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client version: %d", rc); … … 607 620 sec_types[0] = 1; /* length */ 608 621 sec_types[1] = RFB_SECURITY_NONE; 609 rc = send(conn_sd, sec_types, 2, 0);622 rc = tcp_conn_send(conn, sec_types, 2); 610 623 if (rc != EOK) { 611 624 log_msg(LOG_DEFAULT, LVL_WARN, … … 615 628 616 629 char selected_sec_type = 0; 617 rc = recv_char(conn _sd, &selected_sec_type);630 rc = recv_char(conn, &selected_sec_type); 618 631 if (rc != EOK) { 619 632 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving security type: %d", rc); … … 626 639 } 627 640 uint32_t security_result = RFB_SECURITY_HANDSHAKE_OK; 628 rc = send(conn_sd, &security_result, sizeof(uint32_t), 0);641 rc = tcp_conn_send(conn, &security_result, sizeof(uint32_t)); 629 642 if (rc != EOK) { 630 643 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending security result: %d", rc); … … 634 647 /* Client init */ 635 648 char shared_flag; 636 rc = recv_char(conn _sd, &shared_flag);649 rc = recv_char(conn, &shared_flag); 637 650 if (rc != EOK) { 638 651 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client init: %d", rc); … … 657 670 memcpy(server_init->name, rfb->name, name_length); 658 671 fibril_mutex_unlock(&rfb->lock); 659 rc = send(conn_sd, server_init, msg_length, 0);672 rc = tcp_conn_send(conn, server_init, msg_length); 660 673 if (rc != EOK) { 661 674 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server init: %d", rc); … … 665 678 while (true) { 666 679 char message_type = 0; 667 rc = recv_char(conn _sd, &message_type);680 rc = recv_char(conn, &message_type); 668 681 if (rc != EOK) { 669 682 log_msg(LOG_DEFAULT, LVL_WARN, … … 680 693 switch (message_type) { 681 694 case RFB_CMSG_SET_PIXEL_FORMAT: 682 recv_message(conn _sd, message_type, &spf, sizeof(spf));695 recv_message(conn, message_type, &spf, sizeof(spf)); 683 696 rfb_pixel_format_to_host(&spf.pixel_format, &spf.pixel_format); 684 697 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetPixelFormat message"); … … 690 703 break; 691 704 case RFB_CMSG_SET_ENCODINGS: 692 recv_message(conn _sd, message_type, &se, sizeof(se));705 recv_message(conn, message_type, &se, sizeof(se)); 693 706 rfb_set_encodings_to_host(&se, &se); 694 707 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetEncodings message"); 695 708 for (uint16_t i = 0; i < se.count; i++) { 696 709 int32_t encoding = 0; 697 rc = recv_chars(conn _sd, (char *) &encoding, sizeof(int32_t));710 rc = recv_chars(conn, (char *) &encoding, sizeof(int32_t)); 698 711 if (rc != EOK) 699 712 return; … … 707 720 break; 708 721 case RFB_CMSG_FRAMEBUFFER_UPDATE_REQUEST: 709 recv_message(conn _sd, message_type, &fbur, sizeof(fbur));722 recv_message(conn, message_type, &fbur, sizeof(fbur)); 710 723 rfb_framebuffer_update_request_to_host(&fbur, &fbur); 711 724 log_msg(LOG_DEFAULT, LVL_DEBUG2, 712 725 "Received FramebufferUpdateRequest message"); 713 rfb_send_framebuffer_update(rfb, conn _sd, fbur.incremental);726 rfb_send_framebuffer_update(rfb, conn, fbur.incremental); 714 727 break; 715 728 case RFB_CMSG_KEY_EVENT: 716 recv_message(conn _sd, message_type, &ke, sizeof(ke));729 recv_message(conn, message_type, &ke, sizeof(ke)); 717 730 rfb_key_event_to_host(&ke, &ke); 718 731 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received KeyEvent message"); 719 732 break; 720 733 case RFB_CMSG_POINTER_EVENT: 721 recv_message(conn _sd, message_type, &pe, sizeof(pe));734 recv_message(conn, message_type, &pe, sizeof(pe)); 722 735 rfb_pointer_event_to_host(&pe, &pe); 723 736 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received PointerEvent message"); 724 737 break; 725 738 case RFB_CMSG_CLIENT_CUT_TEXT: 726 recv_message(conn _sd, message_type, &cct, sizeof(cct));739 recv_message(conn, message_type, &cct, sizeof(cct)); 727 740 rfb_client_cut_text_to_host(&cct, &cct); 728 741 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received ClientCutText message"); 729 recv_skip_chars(conn _sd, cct.length);742 recv_skip_chars(conn, cct.length); 730 743 break; 731 744 default: … … 737 750 } 738 751 739 int rfb_listen(rfb_t *rfb, uint16_t port) { 740 struct sockaddr_in addr; 741 742 addr.sin_family = AF_INET; 743 addr.sin_port = htons(port); 744 745 int rc = inet_pton(AF_INET, "127.0.0.1", (void *) 746 &addr.sin_addr.s_addr); 747 if (rc != EOK) { 748 log_msg(LOG_DEFAULT, LVL_ERROR, "Error parsing network address (%s)", 749 str_error(rc)); 750 return rc; 751 } 752 753 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 754 if (listen_sd < 0) { 755 log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listening socket (%s)", 756 str_error(listen_sd)); 757 return rc; 758 } 759 760 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr)); 761 if (rc != EOK) { 762 log_msg(LOG_DEFAULT, LVL_ERROR, "Error binding socket (%s)", 763 str_error(rc)); 764 return rc; 765 } 766 767 rc = listen(listen_sd, 2); 768 if (rc != EOK) { 769 log_msg(LOG_DEFAULT, LVL_ERROR, "listen() failed (%s)", str_error(rc)); 770 return rc; 771 } 772 773 rfb->listen_sd = listen_sd; 752 int rfb_listen(rfb_t *rfb, uint16_t port) 753 { 754 tcp_t *tcp = NULL; 755 tcp_listener_t *lst = NULL; 756 inet_ep_t ep; 757 int rc; 758 759 rc = tcp_create(&tcp); 760 if (rc != EOK) { 761 log_msg(LOG_DEFAULT, LVL_ERROR, "Error initializing TCP."); 762 goto error; 763 } 764 765 inet_ep_init(&ep); 766 ep.port = port; 767 768 rc = tcp_listener_create(tcp, &ep, &listen_cb, rfb, &conn_cb, rfb, 769 &lst); 770 if (rc != EOK) { 771 log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listener."); 772 goto error; 773 } 774 775 rfb->tcp = tcp; 776 rfb->lst = lst; 774 777 775 778 return EOK; 776 } 777 778 void rfb_accept(rfb_t *rfb) 779 { 780 while (true) { 781 struct sockaddr_in raddr; 782 socklen_t raddr_len = sizeof(raddr); 783 int conn_sd = accept(rfb->listen_sd, (struct sockaddr *) &raddr, 784 &raddr_len); 785 786 if (conn_sd < 0) { 787 log_msg(LOG_DEFAULT, LVL_WARN, "accept() failed (%s)", 788 str_error(conn_sd)); 789 continue; 790 } 791 792 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted"); 793 794 rbuf_out = 0; 795 rbuf_in = 0; 796 797 rfb_socket_connection(rfb, conn_sd); 798 closesocket(conn_sd); 799 } 800 } 779 error: 780 tcp_listener_destroy(lst); 781 tcp_destroy(tcp); 782 return rc; 783 } 784 785 static void rfb_new_conn(tcp_listener_t *lst, tcp_conn_t *conn) 786 { 787 rfb_t *rfb = (rfb_t *)tcp_listener_userptr(lst); 788 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted"); 789 790 rbuf_out = 0; 791 rbuf_in = 0; 792 793 rfb_socket_connection(rfb, conn); 794 }
Note:
See TracChangeset
for help on using the changeset viewer.