]> git.sesse.net Git - rdpsrv/blob - rdpsrv.c
Handle RDP4-style logon packets.
[rdpsrv] / rdpsrv.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/socket.h>
6 #include <sys/ioctl.h>
7 #include <arpa/inet.h>
8
9 #include "rdesktop.h"
10
11 const int tcp_port_rdp = 3389;
12 int create_server_socket();
13 int serve_client();
14
15 int main()
16 {
17         int server_sock = create_server_socket();
18         for ( ;; ) {
19                 iso_recv_connect(server_sock);
20                 printf("Got connection.\n");
21                 serve_client();
22                 printf("Client closed.\n");
23         }
24 }
25
26 int create_server_socket()
27 {
28         int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
29         const unsigned int one = 1, zero = 0;
30         struct sockaddr_in addr;
31         int err;
32
33         setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
34         ioctl(server_sock, FIONBIO, &zero);
35
36         addr.sin_family = AF_INET;
37         addr.sin_addr.s_addr = INADDR_ANY;
38         addr.sin_port = htons(tcp_port_rdp);
39
40         do {
41                 err = bind(server_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
42
43                 if (err == -1) {
44                         perror("bind()");
45
46                         /* try to recover from recoverable errors... */
47                         if (errno == ENOMEM || errno == EADDRINUSE) {
48                                 puts("Waiting 1 sec before trying again...");
49                                 sleep(1);
50                         } else {
51                                 puts("Giving up.");
52                                 exit(1);
53                         }
54                 }
55         } while (err == -1);
56
57         listen(server_sock, 20);
58         return server_sock;
59 }
60
61
62 int serve_client()
63 {
64         if (!mcs_recv_connect_initial())
65                 error("MCS_CONNECT_INITIAL recv failed");
66         mcs_send_connect_response();
67         
68         for ( ;; ) {
69                 uint8 type;
70                 STREAM s;
71
72                 while ((s = rdp_recv(&type)) != NULL) {
73                         printf("RDP packet! type %u\n", type);
74                 }
75         }
76 }