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__
37 #elif defined(__MINGW32)
48 snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
51 return strerror(errno);
55 #include <guacamole/client.h>
56 #include <guacamole/log.h>
58 typedef struct client_thread_data {
65 void* start_client_thread(void* data) {
68 client_thread_data* thread_data = (client_thread_data*) data;
70 GUAC_LOG_INFO("Spawning client");
72 /* Load and start client */
73 client = guac_get_client(thread_data->fd);
76 GUAC_LOG_ERROR("Client retrieval failed");
80 guac_start_client(client);
82 guac_free_client(client);
85 if (CLOSE_SOCKET(thread_data->fd) < 0) {
86 GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
91 GUAC_LOG_INFO("Client finished");
97 int main(int argc, char* argv[]) {
101 struct sockaddr_in server_addr;
105 struct sockaddr_in client_addr;
106 unsigned int client_addr_len;
107 int connected_socket_fd;
110 int listen_port = 4822; /* Default port */
116 /* Parse arguments */
117 while ((opt = getopt(argc, argv, "l:")) != -1) {
119 listen_port = atoi(optarg);
120 if (listen_port <= 0) {
121 fprintf(stderr, "Invalid port: %s\n", optarg);
126 fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
131 /* Get binding address */
132 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
133 server_addr.sin_family = AF_INET;
134 server_addr.sin_addr.s_addr = INADDR_ANY;
135 server_addr.sin_port = htons(listen_port);
139 if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
140 fprintf(stderr, "Error creating socket.");
146 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
148 fprintf(stderr, "Error opening socket: %s\n", lasterror());
152 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
153 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
156 /* Bind socket to address */
157 if (bind(socket_fd, (struct sockaddr*) &server_addr,
158 sizeof(server_addr)) < 0) {
159 fprintf(stderr, "Error binding socket: %s\n", lasterror());
163 /* Fork into background */
168 if (daemon_pid == -1) {
169 fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
173 /* If parent, exit */
174 else if (daemon_pid != 0) {
178 GUAC_LOG_INFO("fork() not defined at compile time.");
179 GUAC_LOG_INFO("guacd running in foreground only.");
182 /* Otherwise, this is the daemon */
183 GUAC_LOG_INFO("Started, listening on port %i", listen_port);
191 client_thread_data* data;
193 /* Listen for connections */
194 if (listen(socket_fd, 5) < 0) {
195 GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror());
199 /* Accept connection */
200 client_addr_len = sizeof(client_addr);
201 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
202 if (connected_socket_fd < 0) {
203 GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror());
207 data = malloc(sizeof(client_thread_data));
208 data->fd = connected_socket_fd;
211 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
212 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
216 if (_beginthread(start_client_thread, 0, (void*) data) == -1L) {
217 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
221 #warning THREAD SUPPORT NOT FOUND! guacd will only be able to handle one connection at a time.
222 GUAC_LOG_INFO("Thread support not present at compile time.");
223 GUAC_LOG_INFO("guacd handling one connection at a time.");
224 start_client_thread(data);
230 if (CLOSE_SOCKET(socket_fd) < 0) {
231 GUAC_LOG_ERROR("Could not close socket: %s", lasterror());