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>
35 typedef struct client_thread_data {
38 guac_client_init_handler* client_init;
39 guac_client_registry* registry;
47 void* start_client_thread(void* data) {
50 client_thread_data* thread_data = (client_thread_data*) data;
52 syslog(LOG_INFO, "Spawning client");
54 /* Load and start client */
55 client = guac_get_client(thread_data->fd, thread_data->registry, thread_data->client_init, thread_data->argc, thread_data->argv);
58 syslog(LOG_ERR, "Client retrieval failed");
62 guac_start_client(client);
64 /* FIXME: Need to free client, but only if the client is not
65 * being used. This line will be reached if handoff occurs
67 guac_free_client(client, thread_data->registry);
70 if (close(thread_data->fd) < 0) {
71 syslog(LOG_ERR, "Error closing connection: %s", strerror(errno));
76 syslog(LOG_INFO, "Client finished");
82 int main(int argc, char* argv[]) {
85 guac_client_registry* registry;
87 /* Pluggable client */
88 void* client_plugin_handle;
91 guac_client_init_handler* client_init;
99 struct sockaddr_in server_addr;
102 struct sockaddr_in client_addr;
103 unsigned int client_addr_len;
104 int connected_socket_fd;
111 char protocol_lib[256] = "libguac_client_";
114 fprintf(stderr, "USAGE: %s LISTENPORT PROTOCOL [PROTOCOL OPTIONS]\n", argv[0]);
118 listen_port = atoi(argv[1]);
119 strcat(protocol_lib, argv[2]);
120 strcat(protocol_lib, ".so");
122 client_argc = argc - 3;
123 client_argv = &(argv[3]);
125 /* Get binding address */
126 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
127 server_addr.sin_family = AF_INET;
128 server_addr.sin_addr.s_addr = INADDR_ANY;
129 server_addr.sin_port = htons(listen_port);
132 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
134 syslog(LOG_ERR, "Error opening socket: %s", strerror(errno));
138 /* Bind socket to address */
139 if (bind(socket_fd, (struct sockaddr*) &server_addr,
140 sizeof(server_addr)) < 0) {
141 syslog(LOG_ERR, "Error binding socket: %s", strerror(errno));
145 syslog(LOG_INFO, "Loading client plugin");
147 /* Load client plugin */
148 client_plugin_handle = dlopen(protocol_lib, RTLD_LAZY);
149 if (!client_plugin_handle) {
150 syslog(LOG_ERR, "Could not open client plugin: %s", dlerror());
154 dlerror(); /* Clear errors */
156 /* Get init function */
157 alias.obj = dlsym(client_plugin_handle, "guac_client_init");
159 if ((error = dlerror()) != NULL) {
160 syslog(LOG_ERR, "Could not get guac_client_init in plugin: %s", error);
165 syslog(LOG_INFO, "Listening on port %i", listen_port);
168 /* Allocate registry */
169 registry = guac_create_client_registry();
175 client_thread_data* data;
177 /* Listen for connections */
178 if (listen(socket_fd, 5) < 0) {
179 syslog(LOG_ERR, "Could not listen on socket: %s", strerror(errno));
183 /* Accept connection */
184 client_addr_len = sizeof(client_addr);
185 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
186 if (connected_socket_fd < 0) {
187 syslog(LOG_ERR, "Could not accept client connection: %s", strerror(errno));
191 data = malloc(sizeof(client_thread_data));
193 data->fd = connected_socket_fd;
194 data->client_init = alias.client_init;
195 data->registry = registry;
196 data->argc = client_argc;
197 data->argv = client_argv;
199 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
200 syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno));
206 /* FIXME: Cleanup client registry (and all other objects) on exit */
209 if (close(socket_fd) < 0) {
210 syslog(LOG_ERR, "Could not close socket: %s", strerror(errno));
214 /* Load client plugin */
215 if (dlclose(client_plugin_handle)) {
216 syslog(LOG_ERR, "Could not close client plugin: %s", dlerror());