gem-graph-server/src/server.c

242 lines
8.3 KiB
C

//=-------------------------------------------------------------------------=//
// Server management module //
// //
// Copyright © 2021 The Gem-graph Project //
// //
// This file is part of gem-graph. //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
#include "../include/base.h"
#include "../include/cmds.h"
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Server init function //
// -------------------------------------------------------------------------- //
void ServerInit(Server_t *server)
{
pthread_create(&server->id, NULL, serverMain, server);
}
#define SEND_BUFFER_SIZE 80 * 24
#define RECEIVE_BUFFER_SIZE 80
void *serverCommunicationInstance(void *server)
{
ServerCommunication_t *args;
char **argv = NULL;
char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE];
int tokenIndex, bytesReceived;
char clientIP[16];
args = (ServerCommunication_t*) server;
//Accept and incoming connection
while(!args->pleaseStop) {
// Zeroing buffer
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
printLog("Waiting for commands...\n");
// Read the message from client and copy it in buffer
bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0);
if (bytesReceived == -1) {
printLog("Could not receive data!\n");
break;
};
// Ignore null-sized request
if (bytesReceived == 0)
break;
// Get ip address from client
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(args->clientAddr.sin_port),
receiveBuff);
// get args in an array
tokenIndex = 0;
argv = (char**) realloc(argv, 1 * sizeof(char*));
argv[0] = strtok(receiveBuff, " ");
while (argv[tokenIndex]) {
tokenIndex++;
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
argv[tokenIndex] = strtok(NULL, " ");
}
// Zeroing buffer
bzero(sendBuff, RECEIVE_BUFFER_SIZE);
// Execute command by first arg in cmdList
for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) {
cmdList[i].execute(sendBuff, argv, args->parent);
}
}
// and send that buffer to client
send(args->sockfd, sendBuff, SEND_BUFFER_SIZE, 0);
}
close(args->sockfd);
if (argv)
free(argv);
printLog("Disconnected from\n");
return NULL;
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
#define PORT 9000
#define MAX_CONNECTION 100
static void *serverMain(void *server)
{
Server_t *args;
ServerCommunication_t serverSlots[MAX_CONNECTION] = {0};
int connfd, flags, threadStatus, serverSlotIndex = 0;
uint socklen;
struct sockaddr_in servaddr;
char clientIP[16];
// Get args
args = (Server_t*) server;
args->returnValue = 0;
printLog("Server #%lu online\n", args->id);
// Create socket
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) {
printLog("Socket creation failed!\n");
args->returnValue = 1;
goto serverExiting;
}
// Get socket flags
flags = fcntl(args->sockfd, F_GETFL);
if (flags == -1) {
printLog("Socket parameters getting failed!\n");
args->returnValue = 1;
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");
args->returnValue = 1;
goto serverExiting;
}
// Prepare binding structure
bzero(&servaddr, sizeof(servaddr));
// Assign IP and PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1) {
printLog("Socket bind failed!\n");
args->returnValue = 1;
goto serverExiting;
}
// Now server is ready to listen and verification
if (listen(args->sockfd, MAX_CONNECTION) == -1) {
printLog("Socket listening failed!\n");
args->returnValue = 1;
goto serverExiting;
}
socklen = sizeof(struct sockaddr_in);
if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen)
== -1) {
printLog("Could not get sock name!\n");
args->returnValue = 1;
goto serverExiting;
}
printLog("Server listening...\n");
while (!args->pleaseStop) {
// Accept the data packet from client
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 && errno != EAGAIN) {
printLog("Server acccept failed!\n");
goto serverExiting;
}
sleep(1);
} else {
// Client connected
// get IP addr from client
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;
// Create thread
threadStatus = pthread_create(&serverSlots[serverSlotIndex].id,
NULL,
serverCommunicationInstance,
(void*)&serverSlots[serverSlotIndex]);
if(threadStatus != 0) {
printLog("Error from pthread: %d\n", threadStatus);
args->returnValue = 1;
goto serverExiting;
}
serverSlotIndex++;
printLog("Accepted connection. Server will now listen...\n");
}
}
serverExiting:
close(args->sockfd);
printLog("Server #%lu offline\n", args->id);
return NULL;
}