2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacd.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
42 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 #ifdef HAVE_LIBPTHREAD
53 #elif defined(__MINGW32__)
60 #include <guacamole/client.h>
61 #include <guacamole/log.h>
63 /* Windows / MINGW32 handles closing sockets differently */
65 #define CLOSE_SOCKET(socket) closesocket(socket)
67 #define CLOSE_SOCKET(socket) close(socket)
71 /* Cross-platform strerror()/errno clone */
75 snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
78 return strerror(errno);
83 typedef struct client_thread_data {
90 void* start_client_thread(void* data) {
93 client_thread_data* thread_data = (client_thread_data*) data;
95 GUAC_LOG_INFO("Spawning client");
97 /* Load and start client */
98 client = guac_get_client(thread_data->fd);
100 if (client == NULL) {
101 GUAC_LOG_ERROR("Client retrieval failed");
105 guac_start_client(client);
107 guac_free_client(client);
110 if (CLOSE_SOCKET(thread_data->fd) < 0) {
111 GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
116 GUAC_LOG_INFO("Client finished");
122 int main(int argc, char* argv[]) {
126 struct sockaddr_in server_addr;
130 struct sockaddr_in client_addr;
131 unsigned int client_addr_len;
132 int connected_socket_fd;
135 int listen_port = 4822; /* Default port */
138 char* pidfile = NULL;
144 /* Structure for holding winsock version info */
148 /* Parse arguments */
149 while ((opt = getopt(argc, argv, "l:p:")) != -1) {
151 listen_port = atoi(optarg);
152 if (listen_port <= 0) {
153 fprintf(stderr, "Invalid port: %s\n", optarg);
157 else if (opt == 'p') {
158 pidfile = strdup(optarg);
161 fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
166 /* Get binding address */
167 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
168 server_addr.sin_family = AF_INET;
169 server_addr.sin_addr.s_addr = INADDR_ANY;
170 server_addr.sin_port = htons(listen_port);
173 /* If compiling for Windows, init winsock. */
174 if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
175 fprintf(stderr, "Error creating socket.");
181 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
183 fprintf(stderr, "Error opening socket: %s\n", lasterror());
187 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
188 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
191 /* Bind socket to address */
192 if (bind(socket_fd, (struct sockaddr*) &server_addr,
193 sizeof(server_addr)) < 0) {
194 fprintf(stderr, "Error binding socket: %s\n", lasterror());
198 /* Fork into background */
203 if (daemon_pid == -1) {
204 fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
208 /* If parent, write PID file and exit */
209 else if (daemon_pid != 0) {
211 if (pidfile != NULL) {
213 /* Attempt to open pidfile and write PID */
214 FILE* pidf = fopen(pidfile, "w");
216 fprintf(pidf, "%d\n", daemon_pid);
220 /* Warn on failure */
222 fprintf(stderr, "WARNING: Could not write PID file: %s\n", lasterror());
231 GUAC_LOG_INFO("fork() not defined at compile time.");
232 GUAC_LOG_INFO("guacd running in foreground only.");
235 /* Otherwise, this is the daemon */
236 GUAC_LOG_INFO("Started, listening on port %i", listen_port);
241 #ifdef HAVE_LIBPTHREAD
244 client_thread_data* data;
246 /* Listen for connections */
247 if (listen(socket_fd, 5) < 0) {
248 GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror());
252 /* Accept connection */
253 client_addr_len = sizeof(client_addr);
254 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
255 if (connected_socket_fd < 0) {
256 GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror());
260 data = malloc(sizeof(client_thread_data));
261 data->fd = connected_socket_fd;
263 #ifdef HAVE_LIBPTHREAD
264 if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
265 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
269 if (_beginthread(start_client_thread, 0, (void*) data) == -1L) {
270 GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
274 #warning THREAD SUPPORT NOT FOUND! guacd will only be able to handle one connection at a time.
275 GUAC_LOG_INFO("Thread support not present at compile time.");
276 GUAC_LOG_INFO("guacd handling one connection at a time.");
277 start_client_thread(data);
283 if (CLOSE_SOCKET(socket_fd) < 0) {
284 GUAC_LOG_ERROR("Could not close socket: %s", lasterror());