Fill in some blanks on our crypto header. Change to tsstud again for protodiff.
[rdpsrv] / protodiff.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/select.h>
6 #include <sys/socket.h>
7 #include <sys/ioctl.h>
8 #include <arpa/inet.h>
9
10 #include "rdesktop.h"
11 #include "scancodes.h"
12
13 const int tcp_port_rdp = 3389;
14 int create_server_socket();
15
16 int main()
17 {
18         int server_sock = create_server_socket();
19         int sock_serv, sock_real, sock_fake;
20         unsigned char buf[4096], buf1[4096], buf2[4096];
21         int len1, len2, i;
22         
23         tcp_recv_connect(server_sock);
24         sock_serv = tcp_get_socket();
25         
26         printf("Got connection.\n");
27
28         sock_real = tcp_connect("glasur.uka.no", 3389);
29         sock_fake = tcp_connect("127.0.0.1", 3390);
30
31         for ( ;; ) {
32                 fd_set readfs;
33                 FD_ZERO(&readfs);
34                 FD_SET(sock_serv, &readfs);
35                 FD_SET(sock_real, &readfs);
36                 FD_SET(sock_fake, &readfs);
37                 
38                 select(FD_SETSIZE, &readfs, NULL, NULL, NULL);
39
40                 if (FD_ISSET(sock_serv, &readfs)) {
41                         // data from the client
42                         len1 = read(sock_serv, buf, 4096);
43
44                         if (len1 <= 0) {
45                                 perror("read()");
46                                 exit(1);
47                         }
48
49                         printf("<== ");
50                         for (i = 0; i < len1; ++i)
51                                 printf(" 0x%02x", buf[i]);
52                         printf("\n");
53
54                         if (write(sock_real, buf, len1) != len1) {
55                                 perror("write_real");
56                                 exit(1);
57                         }
58                         if (write(sock_fake, buf, len1) != len1) {
59                                 perror("write_fake");
60                                 exit(1);
61                         }       
62                 } else if (FD_ISSET(sock_real, &readfs) || FD_ISSET(sock_fake, &readfs)) {
63                         len1 = len2 = 0;
64
65                         if (FD_ISSET(sock_real, &readfs)) {
66                                 len1 = read(sock_real, buf1, 4096);
67                                 if (len1 <= 0) {
68                                         perror("read_real");
69                                         exit(1);
70                                 }
71                         }
72                         if (FD_ISSET(sock_fake, &readfs)) {
73                                 len2 = read(sock_fake, buf2, 4096);
74                                 if (len2 <= 0) {
75                                         perror("read_fake");
76                                         exit(1);
77                                 }
78                         }
79
80                         printf("R==>");
81                         for (i = 0; i < len1; ++i)
82                                 printf(" 0x%02x", buf1[i]);
83                         printf("\n");
84
85                         printf("F==>");
86                         for (i = 0; i < len2; ++i)
87                                 printf(" 0x%02x", buf2[i]);
88                         printf("\n");
89                         
90                         if (write(sock_serv, buf1, len1) != len1) {
91                                 perror("write_serv");
92                                 exit(1);
93                         }       
94                 }
95         
96                 // give the clients a bit of time to synchronize
97                 usleep(300000); 
98         }
99 }
100
101 int create_server_socket()
102 {
103         int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
104         const unsigned int one = 1, zero = 0;
105         struct sockaddr_in addr;
106         int err;
107
108         setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
109         ioctl(server_sock, FIONBIO, &zero);
110
111         addr.sin_family = AF_INET;
112         addr.sin_addr.s_addr = INADDR_ANY;
113         addr.sin_port = htons(tcp_port_rdp);
114
115         do {
116                 err = bind(server_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
117
118                 if (err == -1) {
119                         perror("bind()");
120
121                         /* try to recover from recoverable errors... */
122                         if (errno == ENOMEM || errno == EADDRINUSE) {
123                                 puts("Waiting 1 sec before trying again...");
124                                 sleep(1);
125                         } else {
126                                 puts("Giving up.");
127                                 exit(1);
128                         }
129                 }
130         } while (err == -1);
131
132         listen(server_sock, 20);
133         return server_sock;
134 }
135