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>
28 #define CLOSE_SOCKET(socket) closesocket(socket)
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #define CLOSE_SOCKET(socket) close(socket)
35 #ifdef __HAVE_PTHREAD_H__
45 snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
48 return strerror(errno);
52 #include <guacamole/client.h>
53 #include <guacamole/log.h>
55 typedef struct client_thread_data {
62 void* start_client_thread(void* data) {
65 client_thread_data* thread_data = (client_thread_data*) data;
67 GUAC_LOG_INFO("Spawning client");
69 /* Load and start client */
70 client = guac_get_client(thread_data->fd);
73 GUAC_LOG_ERROR("Client retrieval failed");
77 guac_start_client(client);
79 guac_free_client(client);
82 if (CLOSE_SOCKET(thread_data->fd) < 0) {
83 GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
88 GUAC_LOG_INFO("Client finished");
94 int main(int argc, char* argv[]) {
98 struct sockaddr_in server_addr;
102 struct sockaddr_in client_addr;
103 unsigned int client_addr_len;
104 int connected_socket_fd;
107 int listen_port = 4822; /* Default port */
113 /* Parse arguments */
114 while ((opt = getopt(argc, argv, "l:")) != -1) {
116 listen_port = atoi(optarg);
117 if (listen_port <= 0) {
118 fprintf(stderr, "Invalid port: %s\n", optarg);
123 fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
128 /* Get binding address */
129 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
130 server_addr.sin_family = AF_INET;
131 server_addr.sin_addr.s_addr = INADDR_ANY;
132 server_addr.sin_port = htons(listen_port);
136 if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
137 fprintf(stderr, "Error creating socket.");
143 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
145 fprintf(stderr, "Error opening socket: %s\n", lasterror());
149 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
150 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
153 /* Bind socket to address */
154 if (bind(socket_fd, (struct sockaddr*) &server_addr,
155 sizeof(server_addr)) < 0) {
156 fprintf(stderr, "Error binding socket: %s\n", lasterror());
160 /* Fork into background */
165 if (daemon_pid == -1) {
166 fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
170 /* If parent, exit */
171 else if (daemon_pid != 0) {
175 GUAC_LOG_INFO("fork() not defined at compile time.");
176 GUAC_LOG_INFO("guacd running in foreground only.");
179 /* Otherwise, this is the daemon */
180 GUAC_LOG_INFO("Started, listening on port %i", listen_port);
188 client_thread_data* data;
190 /* Listen for connections */
191 if (listen(socket_fd, 5) < 0) {
192 GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror());
196 /* Accept connection */
197 client_addr_len = sizeof(client_addr);
198 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
199 if (connected_socket_fd < 0) {
200 GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror());
204 data = malloc(sizeof(client_thread_data));
205 data->fd = connected_socket_fd;
208 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
209 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
213 GUAC_LOG_INFO("POSIX threads support not present at compile time.");
214 GUAC_LOG_INFO("guacd handling one connection at a time.");
215 start_client_thread(data);
221 if (CLOSE_SOCKET(socket_fd) < 0) {
222 GUAC_LOG_ERROR("Could not close socket: %s", lasterror());