Cross-platform close socket macro, init winsock if MINGW32, MINGW32 support(ish) for strerror
This commit is contained in:
parent
d7b1bc8dae
commit
e5074c2c58
@ -25,9 +25,11 @@
|
|||||||
|
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#define CLOSE_SOCKET(socket) closesocket(socket)
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#define CLOSE_SOCKET(socket) close(socket)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __HAVE_PTHREAD_H__
|
#ifdef __HAVE_PTHREAD_H__
|
||||||
@ -36,8 +38,19 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
char error[65536];
|
||||||
|
|
||||||
|
char* lasterror() {
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
|
||||||
|
return error;
|
||||||
|
#else
|
||||||
|
return strerror(errno);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#include <guacamole/client.h>
|
#include <guacamole/client.h>
|
||||||
#include <guacamole/guaclog.h>
|
#include <guacamole/log.h>
|
||||||
|
|
||||||
typedef struct client_thread_data {
|
typedef struct client_thread_data {
|
||||||
|
|
||||||
@ -66,8 +79,8 @@ void* start_client_thread(void* data) {
|
|||||||
guac_free_client(client);
|
guac_free_client(client);
|
||||||
|
|
||||||
/* Close socket */
|
/* Close socket */
|
||||||
if (close(thread_data->fd) < 0) {
|
if (CLOSE_SOCKET(thread_data->fd) < 0) {
|
||||||
GUAC_LOG_ERROR("Error closing connection: %s", strerror(errno));
|
GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
|
||||||
free(data);
|
free(data);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -118,21 +131,29 @@ int main(int argc, char* argv[]) {
|
|||||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
server_addr.sin_port = htons(listen_port);
|
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 */
|
/* Get socket */
|
||||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (socket_fd < 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);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
|
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 */
|
/* Bind socket to address */
|
||||||
if (bind(socket_fd, (struct sockaddr*) &server_addr,
|
if (bind(socket_fd, (struct sockaddr*) &server_addr,
|
||||||
sizeof(server_addr)) < 0) {
|
sizeof(server_addr)) < 0) {
|
||||||
fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
|
fprintf(stderr, "Error binding socket: %s\n", lasterror());
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,7 +163,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
/* If error, fail */
|
/* If error, fail */
|
||||||
if (daemon_pid == -1) {
|
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);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +189,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
/* Listen for connections */
|
/* Listen for connections */
|
||||||
if (listen(socket_fd, 5) < 0) {
|
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;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +197,7 @@ int main(int argc, char* argv[]) {
|
|||||||
client_addr_len = sizeof(client_addr);
|
client_addr_len = sizeof(client_addr);
|
||||||
connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
|
connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
|
||||||
if (connected_socket_fd < 0) {
|
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;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +206,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
#ifdef pthread_t
|
#ifdef pthread_t
|
||||||
if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
|
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;
|
return 3;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -197,8 +218,8 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Close socket */
|
/* Close socket */
|
||||||
if (close(socket_fd) < 0) {
|
if (CLOSE_SOCKET(socket_fd) < 0) {
|
||||||
GUAC_LOG_ERROR("Could not close socket: %s", strerror(errno));
|
GUAC_LOG_ERROR("Could not close socket: %s", lasterror());
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user