From e5074c2c588864eb48e7482371f524bba5dc5dd8 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 10 Feb 2011 00:27:31 -0800 Subject: [PATCH] Cross-platform close socket macro, init winsock if MINGW32, MINGW32 support(ish) for strerror --- guacd/src/daemon.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/guacd/src/daemon.c b/guacd/src/daemon.c index cd4257ba..19f0b5de 100644 --- a/guacd/src/daemon.c +++ b/guacd/src/daemon.c @@ -25,9 +25,11 @@ #ifdef __MINGW32__ #include +#define CLOSE_SOCKET(socket) closesocket(socket) #else #include #include +#define CLOSE_SOCKET(socket) close(socket) #endif #ifdef __HAVE_PTHREAD_H__ @@ -36,8 +38,19 @@ #include +char error[65536]; + +char* lasterror() { +#ifdef __MINGW32__ + snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError()); + return error; +#else + return strerror(errno); +#endif +} + #include -#include +#include typedef struct client_thread_data { @@ -66,8 +79,8 @@ void* start_client_thread(void* data) { guac_free_client(client); /* Close socket */ - if (close(thread_data->fd) < 0) { - GUAC_LOG_ERROR("Error closing connection: %s", strerror(errno)); + if (CLOSE_SOCKET(thread_data->fd) < 0) { + GUAC_LOG_ERROR("Error closing connection: %s", lasterror()); free(data); return NULL; } @@ -118,21 +131,29 @@ int main(int argc, char* argv[]) { server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(listen_port); +#ifdef __MINGW32__ + WSADATA wsadata; + if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) { + fprintf(stderr, "Error creating socket."); + exit(EXIT_FAILURE); + } +#endif + /* Get socket */ socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (socket_fd < 0) { - fprintf(stderr, "Error opening socket: %s\n", strerror(errno)); + fprintf(stderr, "Error opening socket: %s\n", lasterror()); exit(EXIT_FAILURE); } if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) { - fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", strerror(errno)); + fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror()); } /* Bind socket to address */ if (bind(socket_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) { - fprintf(stderr, "Error binding socket: %s\n", strerror(errno)); + fprintf(stderr, "Error binding socket: %s\n", lasterror()); exit(EXIT_FAILURE); } @@ -142,7 +163,7 @@ int main(int argc, char* argv[]) { /* If error, fail */ if (daemon_pid == -1) { - fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno)); + fprintf(stderr, "Error forking daemon process: %s\n", lasterror()); exit(EXIT_FAILURE); } @@ -168,7 +189,7 @@ int main(int argc, char* argv[]) { /* Listen for connections */ if (listen(socket_fd, 5) < 0) { - GUAC_LOG_ERROR("Could not listen on socket: %s", strerror(errno)); + GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror()); return 3; } @@ -176,7 +197,7 @@ int main(int argc, char* argv[]) { client_addr_len = sizeof(client_addr); connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len); if (connected_socket_fd < 0) { - GUAC_LOG_ERROR("Could not accept client connection: %s", strerror(errno)); + GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror()); return 3; } @@ -185,7 +206,7 @@ int main(int argc, char* argv[]) { #ifdef pthread_t if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) { - GUAC_LOG_ERROR("Could not create client thread: %s", strerror(errno)); + GUAC_LOG_ERROR("Could not create client thread: %s", lasterror()); return 3; } #else @@ -197,8 +218,8 @@ int main(int argc, char* argv[]) { } /* Close socket */ - if (close(socket_fd) < 0) { - GUAC_LOG_ERROR("Could not close socket: %s", strerror(errno)); + if (CLOSE_SOCKET(socket_fd) < 0) { + GUAC_LOG_ERROR("Could not close socket: %s", lasterror()); return 3; }