Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / server / X11 / xfreerdp.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * FreeRDP X11 Server
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <sys/select.h>
27 #include <sys/signal.h>
28
29 #include <freerdp/utils/memory.h>
30
31 #include "xf_peer.h"
32 #include "xfreerdp.h"
33
34 char* xf_pcap_file = NULL;
35 boolean xf_pcap_dump_realtime = true;
36
37 void xf_server_main_loop(freerdp_listener* instance)
38 {
39         int i;
40         int fds;
41         int max_fds;
42         int rcount;
43         void* rfds[32];
44         fd_set rfds_set;
45
46         memset(rfds, 0, sizeof(rfds));
47
48         while (1)
49         {
50                 rcount = 0;
51
52                 if (instance->GetFileDescriptor(instance, rfds, &rcount) != true)
53                 {
54                         printf("Failed to get FreeRDP file descriptor\n");
55                         break;
56                 }
57
58                 max_fds = 0;
59                 FD_ZERO(&rfds_set);
60
61                 for (i = 0; i < rcount; i++)
62                 {
63                         fds = (int)(long)(rfds[i]);
64
65                         if (fds > max_fds)
66                                 max_fds = fds;
67
68                         FD_SET(fds, &rfds_set);
69                 }
70
71                 if (max_fds == 0)
72                         break;
73
74                 if (select(max_fds + 1, &rfds_set, NULL, NULL, NULL) == -1)
75                 {
76                         /* these are not really errors */
77                         if (!((errno == EAGAIN) ||
78                                 (errno == EWOULDBLOCK) ||
79                                 (errno == EINPROGRESS) ||
80                                 (errno == EINTR))) /* signal occurred */
81                         {
82                                 printf("select failed\n");
83                                 break;
84                         }
85                 }
86
87                 if (instance->CheckFileDescriptor(instance) != true)
88                 {
89                         printf("Failed to check FreeRDP file descriptor\n");
90                         break;
91                 }
92         }
93
94         instance->Close(instance);
95 }
96
97 int main(int argc, char* argv[])
98 {
99         freerdp_listener* instance;
100
101         /* ignore SIGPIPE, otherwise an SSL_write failure could crash the server */
102         signal(SIGPIPE, SIG_IGN);
103
104         instance = freerdp_listener_new();
105         instance->PeerAccepted = xf_peer_accepted;
106
107         if (argc > 1)
108                 xf_pcap_file = argv[1];
109
110         if (argc > 2 && !strcmp(argv[2], "--fast"))
111                 xf_pcap_dump_realtime = false;
112
113         /* Open the server socket and start listening. */
114         if (instance->Open(instance, NULL, 3389))
115         {
116                 /* Entering the server main loop. In a real server the listener can be run in its own thread. */
117                 xf_server_main_loop(instance);
118         }
119
120         freerdp_listener_free(instance);
121
122         return 0;
123 }
124