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_PTHREAD_H__
39 #include <guacamole/client.h>
40 #include <guacamole/guaclog.h>
42 typedef struct client_thread_data {
49 void* start_client_thread(void* data) {
52 client_thread_data* thread_data = (client_thread_data*) data;
54 GUAC_LOG_INFO("Spawning client");
56 /* Load and start client */
57 client = guac_get_client(thread_data->fd);
60 GUAC_LOG_ERROR("Client retrieval failed");
64 guac_start_client(client);
66 guac_free_client(client);
69 if (close(thread_data->fd) < 0) {
70 GUAC_LOG_ERROR("Error closing connection: %s", strerror(errno));
75 GUAC_LOG_INFO("Client finished");
81 int main(int argc, char* argv[]) {
85 struct sockaddr_in server_addr;
89 struct sockaddr_in client_addr;
90 unsigned int client_addr_len;
91 int connected_socket_fd;
94 int listen_port = 4822; /* Default port */
100 /* Parse arguments */
101 while ((opt = getopt(argc, argv, "l:")) != -1) {
103 listen_port = atoi(optarg);
104 if (listen_port <= 0) {
105 fprintf(stderr, "Invalid port: %s\n", optarg);
110 fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
115 /* Get binding address */
116 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
117 server_addr.sin_family = AF_INET;
118 server_addr.sin_addr.s_addr = INADDR_ANY;
119 server_addr.sin_port = htons(listen_port);
122 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
124 fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
128 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
129 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", strerror(errno));
132 /* Bind socket to address */
133 if (bind(socket_fd, (struct sockaddr*) &server_addr,
134 sizeof(server_addr)) < 0) {
135 fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
139 /* Fork into background */
144 if (daemon_pid == -1) {
145 fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno));
149 /* If parent, exit */
150 else if (daemon_pid != 0) {
154 GUAC_LOG_INFO("fork() not defined at compile time.");
155 GUAC_LOG_INFO("guacd running in foreground only.");
158 /* Otherwise, this is the daemon */
159 GUAC_LOG_INFO("Started, listening on port %i", listen_port);
167 client_thread_data* data;
169 /* Listen for connections */
170 if (listen(socket_fd, 5) < 0) {
171 GUAC_LOG_ERROR("Could not listen on socket: %s", strerror(errno));
175 /* Accept connection */
176 client_addr_len = sizeof(client_addr);
177 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
178 if (connected_socket_fd < 0) {
179 GUAC_LOG_ERROR("Could not accept client connection: %s", strerror(errno));
183 data = malloc(sizeof(client_thread_data));
184 data->fd = connected_socket_fd;
187 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
188 GUAC_LOG_ERROR("Could not create client thread: %s", strerror(errno));
192 GUAC_LOG_INFO("POSIX threads support not present at compile time.");
193 GUAC_LOG_INFO("guacd handling one connection at a time.");
194 start_client_thread(data);
200 if (close(socket_fd) < 0) {
201 GUAC_LOG_ERROR("Could not close socket: %s", strerror(errno));