From 5ec6e86f6e6998080e335f9735cf3790d8e82543 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 9 Jul 2021 20:34:32 +0200 Subject: [PATCH] Multiclient with IP:port detection --- include/base.h | 3 +++ src/cli.c | 2 +- src/server.c | 47 +++++++++++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/include/base.h b/include/base.h index 3831fb1..49ccbc2 100644 --- a/include/base.h +++ b/include/base.h @@ -28,6 +28,7 @@ #include #include #include +#include #define BASE_H @@ -130,6 +131,8 @@ struct { bool pleaseStop; Server_t *parent; int sockfd; + struct sockaddr_in clientAddr; + socklen_t socklen; } typedef ServerCommunication_t; /* -------------------------------------------------------------------------- */ diff --git a/src/cli.c b/src/cli.c index 83f37c2..7a349ec 100644 --- a/src/cli.c +++ b/src/cli.c @@ -209,7 +209,7 @@ void connectedCommunication(int sockfd) // Detect null-string returned if (receiveBuff[0] == 0) { - printLog("An error occured or invalid command!\n"); + printf("An error occured or invalid command!\n"); } printf("%s\n", receiveBuff); diff --git a/src/server.c b/src/server.c index 8ed5f4e..a33280a 100644 --- a/src/server.c +++ b/src/server.c @@ -23,7 +23,6 @@ #include "../include/cmds.h" #include -#include #include #include #include @@ -47,12 +46,10 @@ void *serverCommunicationInstance(void *server) { ServerCommunication_t *args; char **argv = NULL; - char clientIP[16]; char receiveBuff[RECEIVE_BUFFER_SIZE]; char sendBuff[SEND_BUFFER_SIZE]; int tokenIndex, bytesReceived; - struct sockaddr_in clientAddr; - socklen_t clientAddrSize = sizeof(clientAddr); + char clientIP[16]; args = (ServerCommunication_t*) server; @@ -64,9 +61,7 @@ void *serverCommunicationInstance(void *server) printLog("Waiting for commands...\n"); // Read the message from client and copy it in buffer - bytesReceived = recvfrom(args->sockfd, receiveBuff, sizeof(receiveBuff), - 0, (struct sockaddr *)&clientAddr, - &clientAddrSize); + bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0); if (bytesReceived == -1) { printLog("Could not receive data!\n"); break; @@ -76,12 +71,15 @@ void *serverCommunicationInstance(void *server) if (bytesReceived == 0) break; - // get IP addr from client - inet_ntop(AF_INET, &(clientAddr.sin_addr), clientIP, clientAddrSize); + inet_ntop(AF_INET, + &args->clientAddr.sin_addr, + clientIP, + args->socklen); // Print buffer which contains the client request - printLog("Client %s:%d request : '%s'\n", clientIP, - ntohs(clientAddr.sin_port), + printLog("Client %s:%d request : '%s'\n", + clientIP, + ntohs(args->clientAddr.sin_port), receiveBuff); // get args in an array @@ -105,13 +103,13 @@ void *serverCommunicationInstance(void *server) } // and send that buffer to client - sendto(args->sockfd, sendBuff, sizeof(sendBuff), 0, - (struct sockaddr *)&clientAddr, clientAddrSize); + send(args->sockfd, sendBuff, sizeof(sendBuff), 0); } close(args->sockfd); + if (argv) + free(argv); printLog("End of communications \n"); - free(argv); return NULL; } @@ -127,7 +125,7 @@ static void *serverMain(void *server) ServerCommunication_t serverSlots[MAX_CONNECTION] = {0}; int connfd, flags, threadStatus, serverSlotIndex = 0; uint socklen; - struct sockaddr_in servaddr, clientAddr; + struct sockaddr_in servaddr; char clientIP[16]; // Get args @@ -141,12 +139,14 @@ static void *serverMain(void *server) goto serverExiting; } + // Get socket flags flags = fcntl(args->sockfd, F_GETFL); if (flags == -1) { printLog("Socket parameters getting failed!\n"); goto serverExiting; } + // Change socket flags to non-blocking if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) { printLog("Socket non-blocking setting failed!\n"); goto serverExiting; @@ -172,7 +172,7 @@ static void *serverMain(void *server) goto serverExiting; } - socklen = sizeof(clientAddr); + socklen = sizeof(struct sockaddr_in); if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen) == -1) { @@ -185,10 +185,12 @@ static void *serverMain(void *server) while (!args->pleaseStop) { // Accept the data packet from client - connfd = accept(args->sockfd, (struct sockaddr*)&clientAddr, &socklen); + connfd = accept(args->sockfd, + (struct sockaddr*) &serverSlots[serverSlotIndex].clientAddr, + &socklen); if (connfd < 0) { // If error is not due to lack of clients connecting, this is error - if (errno != EWOULDBLOCK) { + if (errno != EWOULDBLOCK && errno != EAGAIN) { printLog("Server acccept failed!\n"); goto serverExiting; } @@ -196,10 +198,15 @@ static void *serverMain(void *server) } else { // Client connected // get IP addr from client - inet_ntop(AF_INET, &(clientAddr.sin_addr), clientIP, socklen); - printLog("Client accepted from %s\n", clientIP); //TODO pass that thing to communicator ? + inet_ntop(AF_INET, + &(serverSlots[serverSlotIndex].clientAddr.sin_addr), + clientIP, socklen); + printLog("Client accepted from %s:%d\n", + clientIP, + ntohs(serverSlots[serverSlotIndex].clientAddr.sin_port)); // Populate communicator slot + serverSlots[serverSlotIndex].socklen = socklen; serverSlots[serverSlotIndex].sockfd = connfd; serverSlots[serverSlotIndex].parent = args;