diff --git a/lib/tcp-listener.c b/lib/tcp-listener.c index fa962dc..c1662c6 100644 --- a/lib/tcp-listener.c +++ b/lib/tcp-listener.c @@ -24,14 +24,16 @@ struct addrinfo* tcp_createIPv4Socket(SOCKET *s,int port,bool wildcard){ sprintf( sport, "%d", port); int res = getaddrinfo(NULL, sport, &hint, &result); if (res != 0) { - logger_log("Error creating address information! Error code: %d", errno); + logger_log("Error creating address information!"); + printLastError(); return NULL; } //Creating listening socket *s = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (*s == INVALID_SOCKET) { - logger_log("Error creating socket! Error: %d", errno); + logger_log("Error creating socket!"); + printLastError(); return NULL; } return result; @@ -41,12 +43,7 @@ int tcp_bindnlisten(SOCKET s,struct addrinfo* addr,int conn_count){ int res = bind(s, addr->ai_addr, addr->ai_addrlen); if (res == SOCKET_ERROR) { logger_log("Error binding socket!"); - int r = errno; - switch(r){ - default: - logger_log("Error: %s",strerror(errno)); - break; - } + printLastError(); freeaddrinfo(addr); return 1; } @@ -55,7 +52,8 @@ int tcp_bindnlisten(SOCKET s,struct addrinfo* addr,int conn_count){ res = listen(s, conn_count); if (res == -1) { - logger_log("Error starting listening! Error: %d", errno); + logger_log("Error starting listening!"); + printLastError(); closesocket(s); return 2; } @@ -66,7 +64,8 @@ struct sockaddr_in tcp_getAddr_in(SOCKET s) { struct sockaddr_in sin; socklen_t len = sizeof(sin); if (getsockname(s, (struct sockaddr *) &sin, &len) == -1) { - logger_log("Error at getsockname!Error code: %d", errno); + logger_log("Error at getsockname!"); + printLastError(); closesocket(s); exit(1); } @@ -76,6 +75,21 @@ int tcp_getSockPort(SOCKET s){ struct sockaddr_in sin = tcp_getAddr_in(s); return ntohs(sin.sin_port); } + +void printLastError() { +#ifdef _WIN32 + wchar_t *s = NULL; + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, WSAGetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&s, 0, NULL); + logger_log("%S\n", s); + LocalFree(s); +#else + logger_log("%s", strerror(errno)); +#endif +} + #if defined(__linux__) || defined(__CYGWIN__) int closesocket(SOCKET s) { return close(s); diff --git a/lib/tcp-listener.h b/lib/tcp-listener.h index 469ed7b..d7cfd19 100644 --- a/lib/tcp-listener.h +++ b/lib/tcp-listener.h @@ -47,4 +47,6 @@ struct sockaddr_in tcp_getAddr_in(SOCKET socket); * @param[in] socket * @return A socket portszáma */ -int tcp_getSockPort(SOCKET socket); \ No newline at end of file +int tcp_getSockPort(SOCKET socket); + +void printLastError(); \ No newline at end of file diff --git a/main.c b/main.c index 508af73..fa0dcaf 100644 --- a/main.c +++ b/main.c @@ -39,6 +39,7 @@ int main(void) { int r1 = WSAStartup(MAKEWORD(2,2),&ws); if(r1 != 0){ logger_log("Error at WSAStartup."); + printLastError(); free(config.pairs); return EXIT_FAILURE; } diff --git a/modules/webio.c b/modules/webio.c index 4a6754d..5fe2ed4 100644 --- a/modules/webio.c +++ b/modules/webio.c @@ -183,7 +183,7 @@ static int webio_handleGETrequest(SOCKET client, WebIO wio, char *file) { char puf[len]; while (total_bytes_sent < length) { bytes_sent = sendfile(client,fd,0,length); - if(bytes_sent == -1) logger_log("Error: %s",strerror(errno)); + if(bytes_sent == -1) printLastError(); total_bytes_sent += bytes_sent; } } @@ -232,7 +232,8 @@ static int webio_handlePOSTrequest(SOCKET client, WebIO wio, Map post) { sprintf(buf, "@message=%s", map_getValue(post, "message")); res = send(wio.list->array[i].socket, buf, DEFAULT_BUFLEN, 0); if (res == SOCKET_ERROR) { - logger_log("Error sending message.Error: %d", errno); + logger_log("Error sending message!"); + printLastError(); return 2; } logger_log("Message sent to %s", map_getValue(post, "id"));