5 #include <sys/select.h>
6 #include <sys/socket.h>
11 #include "scancodes.h"
13 const int tcp_port_rdp = 3389;
14 int create_server_socket();
19 int server_sock = create_server_socket();
21 iso_recv_connect(server_sock);
22 printf("Got connection.\n");
24 printf("Client closed.\n");
28 int create_server_socket()
30 int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
31 const unsigned int one = 1, zero = 0;
32 struct sockaddr_in addr;
35 setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
36 ioctl(server_sock, FIONBIO, &zero);
38 addr.sin_family = AF_INET;
39 addr.sin_addr.s_addr = INADDR_ANY;
40 addr.sin_port = htons(tcp_port_rdp);
43 err = bind(server_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
48 /* try to recover from recoverable errors... */
49 if (errno == ENOMEM || errno == EADDRINUSE) {
50 puts("Waiting 1 sec before trying again...");
59 listen(server_sock, 20);
63 int map_scancode_to_vnc_key(int scancode)
67 case SCANCODE_CHAR_1 ... SCANCODE_CHAR_9:
68 return scancode + '1' - SCANCODE_CHAR_1;
71 case SCANCODE_CHAR_BACKSPACE:
73 case SCANCODE_CHAR_TAB:
75 case SCANCODE_CHAR_Q ... SCANCODE_CHAR_P:
76 return "QWERTYUIOP"[scancode - SCANCODE_CHAR_Q];
77 case SCANCODE_CHAR_A ... SCANCODE_CHAR_L:
78 return "ASDFGHJKL"[scancode - SCANCODE_CHAR_A];
79 case SCANCODE_CHAR_Z ... SCANCODE_CHAR_M:
80 return "ZXCVBNM"[scancode - SCANCODE_CHAR_Z];
82 printf("Unknown scancode %x, ignoring\n", scancode);
87 void handle_input_pdu(STREAM s, int vnc_sock)
90 uint16 message_type, device_flags, param1, param2;
94 static int mouse1_down = 0, mouse2_down = 0;
97 in_uint16_le(s, num_events); // number of events
98 in_uint8s(s, 2); // pad
100 for (i = 0; i < num_events; ++i) {
101 rdp_recv_input(s, &time, &message_type, &device_flags, ¶m1, ¶m2);
102 printf("Input event at time %u\n", time);
104 switch (message_type) {
105 case RDP_INPUT_SYNCHRONIZE:
106 printf("- Type: Synchronize (ignored)\n");
108 case RDP_INPUT_CODEPOINT:
109 printf("- Type: Codepoint (ignored)\n");
111 case RDP_INPUT_VIRTKEY:
112 printf("- Type: Virtual key (ignored)\n");
114 case RDP_INPUT_SCANCODE:
115 printf("- Type: Scancode\n");
116 printf("- Device flags: %x\n", device_flags);
117 printf("- Key: %x\n", param1);
119 vnc_key = map_scancode_to_vnc_key(param1);
122 buf[0] = 4; // message type
123 buf[1] = (device_flags & KBD_FLAG_DOWN) ? 1 : 0; // down flag
124 buf[2] = 0; // padding
126 buf[4] = vnc_key >> 24; // keysym
127 buf[5] = (vnc_key >> 16) & 0xff;
128 buf[6] = (vnc_key >> 8) & 0xff;
129 buf[7] = vnc_key & 0xff;
130 write(vnc_sock, buf, 8);
134 case RDP_INPUT_MOUSE:
135 printf("- Type: Mouse\n");
136 printf("- Device flags: %x\n", device_flags);
137 printf("- Position: (%u,%u)\n", param1, param2);
139 if (device_flags & MOUSE_FLAG_BUTTON1)
140 mouse1_down = (device_flags & MOUSE_FLAG_DOWN) ? 0x01 : 0;
141 if (device_flags & MOUSE_FLAG_BUTTON2)
142 mouse2_down = (device_flags & MOUSE_FLAG_DOWN) ? 0x02 : 0;
144 buf[0] = 5; // message type
145 buf[1] = mouse1_down | mouse2_down; // button mask
146 buf[2] = param1 >> 8;
147 buf[3] = param1 & 0xff;
148 buf[4] = param2 >> 8;
149 buf[5] = param2 & 0xff;
150 write(vnc_sock, buf, 6);
154 printf("- Unknown type %x\n", message_type);
160 // re-request the entire framebuffer
161 buf[0] = 3; // message type
162 buf[1] = 1; // incremental
167 buf[6] = 640 >> 8; // width
169 buf[8] = 480 >> 8; // height
172 write(vnc_sock, buf, 10);
175 struct ServerInitialization {
176 unsigned short width;
177 unsigned short height;
178 unsigned char pixelformat[16]; // FIXME
179 unsigned int name_len;
185 int vnc_sock = tcp_connect("127.0.0.1", 5901);
186 struct ServerInitialization si;
189 if (read(vnc_sock, buf, 12) != 12)
190 error("short read on handshake\n");
191 write(vnc_sock, "RFB 003.003\n", 12);
194 if (read(vnc_sock, buf, 4) != 4)
195 error("short read on auth\n");
198 error("auth needed or connection failed\n");
202 write(vnc_sock, buf, 1);
204 // read the server initialization
205 if (read(vnc_sock, &si, sizeof(struct ServerInitialization)) != sizeof(struct ServerInitialization))
206 error("short read on SI");
208 printf("Server is %u x %u\n", ntohs(si.width), ntohs(si.height));
210 if (read(vnc_sock, buf, ntohl(si.name_len)) != ntohl(si.name_len))
211 error("short read on server name\n");
213 // printf("Server name is '%*s' (%u bytes)\n", ntohl(si.name_len), buf, ntohl(si.name_len));
215 // we can only accept raw encoding
216 buf[0] = 2; // message type
217 buf[1] = 0; // padding
218 buf[2] = 0; // number of encodings
220 buf[4] = 0; // raw encoding
225 write(vnc_sock, buf, 8);
227 // request the entire framebuffer
228 buf[0] = 3; // message type
229 buf[1] = 0; // incremental
234 buf[6] = 640 >> 8; // width
236 buf[8] = 480 >> 8; // height
239 write(vnc_sock, buf, 10);
241 printf("Connected to VNC!\n");
246 struct vnc_rectangle {
247 unsigned short x, y, width, height;
248 unsigned int encoding;
251 void handle_vnc_fbupdate(int vnc_sock)
253 struct vnc_rectangle rect;
254 unsigned char *data, *ptr, *src, *dst;
255 unsigned short num_rect;
256 int ret, data_left, i, xt, yt;
257 unsigned char smallblock[64 * 64 * 3];
259 if (read(vnc_sock, &num_rect, 2) != 2)
260 error("short read on num_rect\n");
262 for (i = 0; i < ntohs(num_rect); ++i) {
265 if (read(vnc_sock, &rect, sizeof(struct vnc_rectangle)) != sizeof(struct vnc_rectangle))
266 error("short read on vnc_rectangle\n");
268 if (ntohl(rect.encoding) != 0)
269 error("unsupported encoding\n");
271 width = ntohs(rect.width);
272 height = ntohs(rect.height);
274 printf("UPDATE: %ux%u at (%u,%u)\n", width, height,
275 ntohs(rect.x), ntohs(rect.y));
277 ptr = data = xmalloc(width * height * 4);
278 data_left = width * height * 4;
280 while (data_left > 0) {
281 ret = read(vnc_sock, ptr, data_left);
283 error("error on data read\n");
289 // push our 32-bit RGB data into small enough chunks
290 // (64x64) so we are sure we can send them without
291 // everything crashing and stuff :-)
292 for (yt = 0; yt < (height + 63) / 64; ++yt) {
293 for (xt = 0; xt < (width + 63) / 64; ++xt) {
295 int bw = width - xt * 64;
296 int bh = height - yt * 64;
303 for (y = 0; y < bh; ++y) {
304 src = data + ((yt * 64 + (bh - 1 - y)) * width + (xt * 64)) * 4;
305 for (x = 0; x < bw; ++x) {
313 rdp_send_bitmap_update(ntohs(rect.x) + xt * 64, ntohs(rect.y) + yt * 64, bw, bh, smallblock);
323 int vnc_sock = vnc_init();
324 int listen_on_vnc = 0;
326 if (!mcs_recv_connect_initial())
327 error("MCS_CONNECT_INITIAL recv failed");
328 mcs_send_connect_response();
331 uint8 type, data_pdu_type;
336 FD_SET(tcp_get_socket(), &readfs);
337 FD_SET(vnc_sock, &readfs);
339 select(FD_SETSIZE, &readfs, NULL, NULL, NULL);
341 // activity on RDP socket?
342 if (FD_ISSET(tcp_get_socket(), &readfs)) {
343 if ((s = rdp_recv(&type)) != NULL) {
344 if (type != RDP_PDU_DATA) {
345 printf("Unknown RDP packet of type %u\n", type);
349 in_uint8s(s, 8); /* shareid, pad, streamid, length */
350 in_uint8(s, data_pdu_type);
351 in_uint8s(s, 3); /* compress_type, compress_len */
353 switch (data_pdu_type) {
354 case RDP_DATA_PDU_INPUT:
355 handle_input_pdu(s, vnc_sock);
359 printf("Unknown data PDU type %u\n", data_pdu_type);
364 // activity on VNC socket?
365 if (FD_ISSET(vnc_sock, &readfs) && listen_on_vnc) {
366 unsigned char buf[256];
368 printf("Activity on VNC socket!\n");
370 if (read(vnc_sock, buf, 1) != 1)
371 error("short read on vnc_sock\n");
375 // frame buffer update!
376 printf("Framebuffer update\n");
377 if (read(vnc_sock, buf, 1) != 1)
378 error("short read on vnc_sock\n");
379 handle_vnc_fbupdate(vnc_sock);
385 printf("New text in clipboard, ignoring\n");
386 if (read(vnc_sock, buf, 3) != 3)
387 error("short read on vnc_sock\n");
388 if (read(vnc_sock, buf, 4) != 4)
389 error("short read on vnc_sock\n");
390 if (read(vnc_sock, (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3], 4) != 4)
391 error("short read on vnc_sock\n");
394 printf("Unknown VNC server message %x\n", buf[0]);