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>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
32 #include <guacamole/client.h>
34 typedef struct client_thread_data {
41 void* start_client_thread(void* data) {
44 client_thread_data* thread_data = (client_thread_data*) data;
46 syslog(LOG_INFO, "Spawning client");
48 /* Load and start client */
49 client = guac_get_client(thread_data->fd);
52 syslog(LOG_ERR, "Client retrieval failed");
56 guac_start_client(client);
58 guac_free_client(client);
61 if (close(thread_data->fd) < 0) {
62 syslog(LOG_ERR, "Error closing connection: %s", strerror(errno));
67 syslog(LOG_INFO, "Client finished");
73 int main(int argc, char* argv[]) {
77 struct sockaddr_in server_addr;
80 struct sockaddr_in client_addr;
81 unsigned int client_addr_len;
82 int connected_socket_fd;
85 int listen_port = 4822; /* Default port */
92 while ((opt = getopt(argc, argv, "l:")) != -1) {
94 listen_port = atoi(optarg);
95 if (listen_port <= 0) {
96 fprintf(stderr, "Invalid port: %s\n", optarg);
101 fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
106 /* Get binding address */
107 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
108 server_addr.sin_family = AF_INET;
109 server_addr.sin_addr.s_addr = INADDR_ANY;
110 server_addr.sin_port = htons(listen_port);
113 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
115 fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
119 /* Bind socket to address */
120 if (bind(socket_fd, (struct sockaddr*) &server_addr,
121 sizeof(server_addr)) < 0) {
122 fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
126 /* Fork into background */
130 if (daemon_pid == -1) {
131 fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno));
135 /* If parent, exit */
136 else if (daemon_pid != 0) {
140 /* Otherwise, this is the daemon */
141 syslog(LOG_INFO, "Started, listening on port %i", listen_port);
147 client_thread_data* data;
149 /* Listen for connections */
150 if (listen(socket_fd, 5) < 0) {
151 syslog(LOG_ERR, "Could not listen on socket: %s", strerror(errno));
155 /* Accept connection */
156 client_addr_len = sizeof(client_addr);
157 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
158 if (connected_socket_fd < 0) {
159 syslog(LOG_ERR, "Could not accept client connection: %s", strerror(errno));
163 data = malloc(sizeof(client_thread_data));
164 data->fd = connected_socket_fd;
166 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
167 syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno));
174 if (close(socket_fd) < 0) {
175 syslog(LOG_ERR, "Could not close socket: %s", strerror(errno));