5 #include <sys/select.h>
6 #include <sys/socket.h>
12 const int tcp_port_rdp = 3389;
13 int create_server_socket();
18 int server_sock = create_server_socket();
20 iso_recv_connect(server_sock);
21 printf("Got connection.\n");
23 printf("Client closed.\n");
27 int create_server_socket()
29 int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
30 const unsigned int one = 1, zero = 0;
31 struct sockaddr_in addr;
34 setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
35 ioctl(server_sock, FIONBIO, &zero);
37 addr.sin_family = AF_INET;
38 addr.sin_addr.s_addr = INADDR_ANY;
39 addr.sin_port = htons(tcp_port_rdp);
42 err = bind(server_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
47 /* try to recover from recoverable errors... */
48 if (errno == ENOMEM || errno == EADDRINUSE) {
49 puts("Waiting 1 sec before trying again...");
58 listen(server_sock, 20);
62 void handle_input_pdu(STREAM s)
65 uint16 message_type, device_flags, param1, param2;
69 in_uint16_le(s, num_events); // number of events
70 in_uint8s(s, 2); // pad
72 for (i = 0; i < num_events; ++i) {
73 rdp_recv_input(s, &time, &message_type, &device_flags, ¶m1, ¶m2);
74 printf("Input event at time %u\n", time);
76 switch (message_type) {
77 case RDP_INPUT_SYNCHRONIZE:
78 printf("- Type: Synchronize (ignored)\n");
80 case RDP_INPUT_CODEPOINT:
81 printf("- Type: Codepoint (ignored)\n");
83 case RDP_INPUT_VIRTKEY:
84 printf("- Type: Virtual key (ignored)\n");
86 case RDP_INPUT_SCANCODE:
87 printf("- Type: Scancode (ignored)\n");
90 printf("- Type: Mouse\n");
91 printf("- Device flags: %x\n", device_flags);
92 printf("- Position: (%u,%u)\n", param1, param2);
95 rdp_send_bitmap_update(param1, param2);
99 printf("- Unknown type %x\n", message_type);
106 struct ServerInitialization {
107 unsigned short width;
108 unsigned short height;
109 unsigned char pixelformat[16]; // FIXME
110 unsigned int name_len;
116 int vnc_sock = tcp_connect("127.0.0.1", 5901);
117 struct ServerInitialization si;
120 if (read(vnc_sock, buf, 12) != 12)
121 error("short read on handshake\n");
122 write(vnc_sock, "RFB 003.003\n", 12);
125 if (read(vnc_sock, buf, 4) != 4)
126 error("short read on auth\n");
129 error("auth needed or connection failed\n");
133 write(vnc_sock, buf, 1);
135 // read the server initialization
136 if (read(vnc_sock, &si, sizeof(struct ServerInitialization)) != sizeof(struct ServerInitialization))
137 error("short read on SI");
139 printf("Server is %u x %u\n", ntohs(si.width), ntohs(si.height));
141 if (read(vnc_sock, buf, ntohl(si.name_len)) != ntohl(si.name_len))
142 error("short read on server name\n");
144 printf("Server name is '%*s' (%u bytes)\n", ntohl(si.name_len), buf, ntohl(si.name_len));
146 // we can only accept raw encoding
147 buf[0] = 2; // message type
148 buf[1] = 0; // padding
149 buf[2] = 0; // number of encodings
151 buf[4] = 0; // raw encoding
153 write(vnc_sock, buf, 5);
155 // request the entire framebuffer
156 buf[0] = 3; // message type
157 buf[1] = 0; // incremental
162 buf[6] = 640 >> 8; // width
164 buf[8] = 480 >> 8; // height
167 write(vnc_sock, buf, 10);
169 printf("Connected to VNC!\n");
176 int vnc_sock = vnc_init();
178 if (!mcs_recv_connect_initial())
179 error("MCS_CONNECT_INITIAL recv failed");
180 mcs_send_connect_response();
183 uint8 type, data_pdu_type;
188 FD_SET(tcp_get_socket(), &readfs);
189 FD_SET(vnc_sock, &readfs);
191 select(FD_SETSIZE, &readfs, NULL, NULL, NULL);
193 // activity on RDP socket?
194 if (FD_ISSET(tcp_get_socket(), &readfs)) {
195 if ((s = rdp_recv(&type)) != NULL) {
196 if (type != RDP_PDU_DATA) {
197 printf("Unknown RDP packet of type %u\n", type);
201 in_uint8s(s, 8); /* shareid, pad, streamid, length */
202 in_uint8(s, data_pdu_type);
203 in_uint8s(s, 3); /* compress_type, compress_len */
205 switch (data_pdu_type) {
206 case RDP_DATA_PDU_INPUT:
207 printf("Input PDU\n");
211 printf("Unknown data PDU type %u\n", data_pdu_type);
216 // activity on VNC socket?
217 if (FD_ISSET(vnc_sock, &readfs)) {
219 read(vnc_sock, buf, 1);
222 // frame buffer update!
223 printf("Frame buffer update\n");
226 printf("Unknown server message %x\n", buf[0]);