3 * Guacamole - Clientless Remote Desktop
4 * Copyright (C) 2010 Michael Jumper
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
33 #ifdef HAVE_LIBPTHREAD
35 #elif defined(__MINGW32__)
42 #include <guacamole/client.h>
43 #include <guacamole/log.h>
45 /* Windows / MINGW32 handles closing sockets differently */
47 #define CLOSE_SOCKET(socket) closesocket(socket)
49 #define CLOSE_SOCKET(socket) close(socket)
53 /* Cross-platform strerror()/errno clone */
57 snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
60 return strerror(errno);
65 typedef struct client_thread_data {
72 void* start_client_thread(void* data) {
75 client_thread_data* thread_data = (client_thread_data*) data;
77 GUAC_LOG_INFO("Spawning client");
79 /* Load and start client */
80 client = guac_get_client(thread_data->fd);
83 GUAC_LOG_ERROR("Client retrieval failed");
87 guac_start_client(client);
89 guac_free_client(client);
92 if (CLOSE_SOCKET(thread_data->fd) < 0) {
93 GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
98 GUAC_LOG_INFO("Client finished");
104 int main(int argc, char* argv[]) {
108 struct sockaddr_in server_addr;
112 struct sockaddr_in client_addr;
113 unsigned int client_addr_len;
114 int connected_socket_fd;
117 int listen_port = 4822; /* Default port */
124 /* Structure for holding winsock version info */
128 /* Parse arguments */
129 while ((opt = getopt(argc, argv, "l:")) != -1) {
131 listen_port = atoi(optarg);
132 if (listen_port <= 0) {
133 fprintf(stderr, "Invalid port: %s\n", optarg);
138 fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
143 /* Get binding address */
144 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
145 server_addr.sin_family = AF_INET;
146 server_addr.sin_addr.s_addr = INADDR_ANY;
147 server_addr.sin_port = htons(listen_port);
150 /* If compiling for Windows, init winsock. */
151 if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
152 fprintf(stderr, "Error creating socket.");
158 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
160 fprintf(stderr, "Error opening socket: %s\n", lasterror());
164 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
165 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
168 /* Bind socket to address */
169 if (bind(socket_fd, (struct sockaddr*) &server_addr,
170 sizeof(server_addr)) < 0) {
171 fprintf(stderr, "Error binding socket: %s\n", lasterror());
175 /* Fork into background */
180 if (daemon_pid == -1) {
181 fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
185 /* If parent, exit */
186 else if (daemon_pid != 0) {
190 GUAC_LOG_INFO("fork() not defined at compile time.");
191 GUAC_LOG_INFO("guacd running in foreground only.");
194 /* Otherwise, this is the daemon */
195 GUAC_LOG_INFO("Started, listening on port %i", listen_port);
200 #ifdef HAVE_LIBPTHREAD
203 client_thread_data* data;
205 /* Listen for connections */
206 if (listen(socket_fd, 5) < 0) {
207 GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror());
211 /* Accept connection */
212 client_addr_len = sizeof(client_addr);
213 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
214 if (connected_socket_fd < 0) {
215 GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror());
219 data = malloc(sizeof(client_thread_data));
220 data->fd = connected_socket_fd;
222 #ifdef HAVE_LIBPTHREAD
223 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
224 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
228 if (_beginthread(start_client_thread, 0, (void*) data) == -1L) {
229 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
233 #warning THREAD SUPPORT NOT FOUND! guacd will only be able to handle one connection at a time.
234 GUAC_LOG_INFO("Thread support not present at compile time.");
235 GUAC_LOG_INFO("guacd handling one connection at a time.");
236 start_client_thread(data);
242 if (CLOSE_SOCKET(socket_fd) < 0) {
243 GUAC_LOG_ERROR("Could not close socket: %s", lasterror());