Errno is useful

This commit is contained in:
Adrien Bourmault 2021-07-12 15:12:29 +02:00
parent dd59e34110
commit 442c76e96c
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 23 additions and 16 deletions

View File

@ -23,6 +23,7 @@
#include "../include/cmds.h" #include "../include/cmds.h"
#include <netdb.h> #include <netdb.h>
#include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h> #include <fcntl.h>
@ -48,7 +49,7 @@ void *serverCommunicationInstance(void *server)
char **argv = NULL; char **argv = NULL;
char receiveBuff[RECEIVE_BUFFER_SIZE]; char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE]; char sendBuff[SEND_BUFFER_SIZE];
int tokenIndex, bytesReceived; int tokenIndex, bytesReceived, clientPort;
char clientIP[16]; char clientIP[16];
args = (ServerCommunication_t*) server; args = (ServerCommunication_t*) server;
@ -58,12 +59,12 @@ void *serverCommunicationInstance(void *server)
// Zeroing buffer // Zeroing buffer
bzero(receiveBuff, RECEIVE_BUFFER_SIZE); bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
printLog("Waiting for commands...\n"); //printLog("Waiting for commands...\n");
// Read the message from client and copy it in buffer // Read the message from client and copy it in buffer
bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0); bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0);
if (bytesReceived == -1) { if (bytesReceived == -1) {
printLog("Could not receive data!\n"); printLog("Could not receive data! (%s)\n", strerror(errno));
break; break;
}; };
@ -77,10 +78,13 @@ void *serverCommunicationInstance(void *server)
clientIP, clientIP,
args->socklen); args->socklen);
// Get port number from client
clientPort = ntohs(args->clientAddr.sin_port);
// Print buffer which contains the client request // Print buffer which contains the client request
printLog("Client %s:%d request : '%s'\n", printLog("Client %s:%d request : '%s'\n",
clientIP, clientIP,
ntohs(args->clientAddr.sin_port), clientPort,
receiveBuff); receiveBuff);
// get args in an array // get args in an array
@ -110,7 +114,7 @@ void *serverCommunicationInstance(void *server)
close(args->sockfd); close(args->sockfd);
if (argv) if (argv)
free(argv); free(argv);
printLog("Disconnected from\n"); printLog("Disconnected from %s:%d\n", clientIP, clientPort);
return NULL; return NULL;
} }
@ -137,7 +141,7 @@ static void *serverMain(void *server)
// Create socket // Create socket
args->sockfd = socket(AF_INET, SOCK_STREAM, 0); args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) { if (args->sockfd == -1) {
printLog("Socket creation failed!\n"); printLog("Socket creation failed! (%s)\n", strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
@ -145,14 +149,14 @@ static void *serverMain(void *server)
// Get socket flags // Get socket flags
flags = fcntl(args->sockfd, F_GETFL); flags = fcntl(args->sockfd, F_GETFL);
if (flags == -1) { if (flags == -1) {
printLog("Socket parameters getting failed!\n"); printLog("Socket parameters getting failed! (%s)\n", strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
// Change socket flags to non-blocking // Change socket flags to non-blocking
if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) { if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
printLog("Socket non-blocking setting failed!\n"); printLog("Socket non-blocking setting failed! (%s)\n", strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
@ -166,24 +170,26 @@ static void *serverMain(void *server)
servaddr.sin_port = htons(PORT); servaddr.sin_port = htons(PORT);
// Binding newly created socket // Binding newly created socket
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1) { if ((bind(args->sockfd, (struct sockaddr*)&servaddr,
printLog("Socket bind failed!\n"); sizeof(servaddr))) == -1) {
printLog("Socket bind failed! (%s)\n", strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
// Now server is ready to listen and verification // Now server is ready to listen and verification
if (listen(args->sockfd, MAX_CONNECTION) == -1) { if (listen(args->sockfd, MAX_CONNECTION) == -1) {
printLog("Socket listening failed!\n"); printLog("Socket listening failed! (%s)\n", strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
socklen = sizeof(struct sockaddr_in); socklen = sizeof(struct sockaddr_in);
if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen) // Get server socket address structure
if (getsockname(args->sockfd, (struct sockaddr *)&servaddr, &socklen)
== -1) { == -1) {
printLog("Could not get sock name!\n"); printLog("Could not get socket structure! (%s)\n", strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
@ -199,7 +205,7 @@ static void *serverMain(void *server)
if (connfd < 0) { if (connfd < 0) {
// If error is not due to lack of clients connecting, this is error // If error is not due to lack of clients connecting, this is error
if (errno != EWOULDBLOCK && errno != EAGAIN) { if (errno != EWOULDBLOCK && errno != EAGAIN) {
printLog("Server acccept failed!\n"); printLog("Server acccept failed! (%s)\n", strerror(errno));
goto serverExiting; goto serverExiting;
} }
sleep(1); sleep(1);
@ -224,13 +230,14 @@ static void *serverMain(void *server)
serverCommunicationInstance, serverCommunicationInstance,
(void*)&serverSlots[serverSlotIndex]); (void*)&serverSlots[serverSlotIndex]);
if(threadStatus != 0) { if(threadStatus != 0) {
printLog("Error from pthread: %d\n", threadStatus); printLog("Error from pthread: %d (%s)\n",
threadStatus, strerror(errno));
args->returnValue = 1; args->returnValue = 1;
goto serverExiting; goto serverExiting;
} }
serverSlotIndex++; serverSlotIndex++;
printLog("Accepted connection. Server will now listen...\n"); //printLog("Accepted connection. Server will now listen...\n");
} }
} }