gem-graph-server/src/server.c

242 lines
8.3 KiB
C
Raw Normal View History

2021-06-16 18:10:04 +02:00
//=-------------------------------------------------------------------------=//
// 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"
2021-06-17 12:05:03 +02:00
#include "../include/cmds.h"
2021-06-16 18:10:04 +02:00
2021-07-08 00:10:36 +02:00
#include <netdb.h>
2021-06-16 18:10:04 +02:00
#include <sys/socket.h>
2021-07-09 18:34:54 +02:00
#include <arpa/inet.h>
2021-07-09 02:53:54 +02:00
#include <fcntl.h>
#include <errno.h>
2021-06-16 18:10:04 +02:00
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
2021-07-09 22:44:29 +02:00
// Server init function //
2021-06-16 18:10:04 +02:00
// -------------------------------------------------------------------------- //
void ServerInit(Server_t *server)
2021-06-16 18:10:04 +02:00
{
pthread_create(&server->id, NULL, serverMain, server);
2021-06-16 18:10:04 +02:00
}
2021-07-08 00:35:22 +02:00
#define SEND_BUFFER_SIZE 80 * 24
2021-07-08 00:28:34 +02:00
#define RECEIVE_BUFFER_SIZE 80
2021-07-09 02:53:54 +02:00
void *serverCommunicationInstance(void *server)
2021-06-16 18:10:04 +02:00
{
2021-07-09 02:53:54 +02:00
ServerCommunication_t *args;
2021-06-23 17:07:43 +02:00
char **argv = NULL;
2021-07-08 00:28:34 +02:00
char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE];
2021-07-09 18:34:54 +02:00
int tokenIndex, bytesReceived;
2021-07-09 20:34:32 +02:00
char clientIP[16];
2021-06-17 12:05:03 +02:00
2021-07-09 02:53:54 +02:00
args = (ServerCommunication_t*) server;
2021-06-16 18:10:04 +02:00
//Accept and incoming connection
2021-06-17 21:34:50 +02:00
while(!args->pleaseStop) {
2021-06-16 19:44:48 +02:00
2021-07-08 00:10:36 +02:00
// Zeroing buffer
2021-07-08 00:28:34 +02:00
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
2021-07-08 18:14:46 +02:00
printLog("Waiting for commands...\n");
2021-06-16 19:44:48 +02:00
2021-07-08 00:10:36 +02:00
// Read the message from client and copy it in buffer
2021-07-09 20:34:32 +02:00
bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0);
2021-07-09 18:34:54 +02:00
if (bytesReceived == -1) {
printLog("Could not receive data!\n");
break;
};
2021-07-08 18:14:46 +02:00
// Ignore null-sized request
2021-07-09 18:34:54 +02:00
if (bytesReceived == 0)
2021-07-08 18:14:46 +02:00
break;
2021-06-16 19:44:48 +02:00
2021-07-09 22:44:29 +02:00
// Get ip address from client
2021-07-09 20:34:32 +02:00
inet_ntop(AF_INET,
&args->clientAddr.sin_addr,
clientIP,
args->socklen);
2021-07-09 18:34:54 +02:00
2021-07-08 00:10:36 +02:00
// Print buffer which contains the client request
2021-07-09 20:34:32 +02:00
printLog("Client %s:%d request : '%s'\n",
clientIP,
ntohs(args->clientAddr.sin_port),
2021-07-09 18:34:54 +02:00
receiveBuff);
2021-06-17 21:34:50 +02:00
2021-06-23 17:07:43 +02:00
// get args in an array
tokenIndex = 0;
argv = (char**) realloc(argv, 1 * sizeof(char*));
2021-07-08 00:28:34 +02:00
argv[0] = strtok(receiveBuff, " ");
2021-06-23 17:07:43 +02:00
while (argv[tokenIndex]) {
tokenIndex++;
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
argv[tokenIndex] = strtok(NULL, " ");
}
2021-07-08 00:10:36 +02:00
// Zeroing buffer
2021-07-08 18:49:27 +02:00
bzero(sendBuff, RECEIVE_BUFFER_SIZE);
2021-07-08 00:10:36 +02:00
// Execute command by first arg in cmdList
2021-06-23 17:07:43 +02:00
for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) {
2021-07-09 02:53:54 +02:00
cmdList[i].execute(sendBuff, argv, args->parent);
2021-06-17 21:34:50 +02:00
}
2021-06-16 18:10:04 +02:00
}
2021-06-17 21:34:50 +02:00
2021-07-08 00:10:36 +02:00
// and send that buffer to client
2021-07-09 22:44:29 +02:00
send(args->sockfd, sendBuff, SEND_BUFFER_SIZE, 0);
2021-06-16 18:10:04 +02:00
}
2021-06-16 19:44:48 +02:00
2021-07-09 02:53:54 +02:00
close(args->sockfd);
2021-07-09 20:34:32 +02:00
if (argv)
free(argv);
2021-07-09 22:44:29 +02:00
printLog("Disconnected from\n");
2021-07-09 02:53:54 +02:00
return NULL;
2021-07-08 00:10:36 +02:00
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
#define PORT 9000
2021-07-09 18:34:54 +02:00
#define MAX_CONNECTION 100
2021-07-08 00:10:36 +02:00
static void *serverMain(void *server)
{
Server_t *args;
2021-07-09 18:34:54 +02:00
ServerCommunication_t serverSlots[MAX_CONNECTION] = {0};
int connfd, flags, threadStatus, serverSlotIndex = 0;
uint socklen;
2021-07-09 20:34:32 +02:00
struct sockaddr_in servaddr;
2021-07-09 18:34:54 +02:00
char clientIP[16];
2021-07-08 00:10:36 +02:00
// Get args
args = (Server_t*) server;
2021-07-09 20:42:44 +02:00
args->returnValue = 0;
printLog("Server #%lu online\n", args->id);
2021-07-08 00:10:36 +02:00
// Create socket
2021-07-09 02:53:54 +02:00
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) {
2021-07-08 00:10:36 +02:00
printLog("Socket creation failed!\n");
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-08 17:26:13 +02:00
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
2021-07-09 20:34:32 +02:00
// Get socket flags
2021-07-09 02:53:54 +02:00
flags = fcntl(args->sockfd, F_GETFL);
if (flags == -1) {
printLog("Socket parameters getting failed!\n");
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-09 02:53:54 +02:00
goto serverExiting;
}
2021-07-09 20:34:32 +02:00
// Change socket flags to non-blocking
2021-07-09 02:53:54 +02:00
if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
printLog("Socket non-blocking setting failed!\n");
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-09 02:53:54 +02:00
goto serverExiting;
}
2021-07-08 00:10:36 +02:00
// 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
2021-07-09 18:34:54 +02:00
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1) {
2021-07-08 00:10:36 +02:00
printLog("Socket bind failed!\n");
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-09 02:53:54 +02:00
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
// Now server is ready to listen and verification
2021-07-09 18:34:54 +02:00
if (listen(args->sockfd, MAX_CONNECTION) == -1) {
2021-07-08 00:10:36 +02:00
printLog("Socket listening failed!\n");
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-08 17:26:13 +02:00
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
2021-07-09 20:34:32 +02:00
socklen = sizeof(struct sockaddr_in);
2021-07-09 18:34:54 +02:00
if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen)
== -1) {
printLog("Could not get sock name!\n");
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-09 18:34:54 +02:00
goto serverExiting;
}
2021-07-08 00:10:36 +02:00
2021-07-09 02:53:54 +02:00
printLog("Server listening...\n");
2021-07-08 18:14:46 +02:00
while (!args->pleaseStop) {
2021-07-08 00:10:36 +02:00
2021-07-08 18:14:46 +02:00
// Accept the data packet from client
2021-07-09 20:34:32 +02:00
connfd = accept(args->sockfd,
(struct sockaddr*) &serverSlots[serverSlotIndex].clientAddr,
&socklen);
2021-07-08 18:14:46 +02:00
if (connfd < 0) {
2021-07-09 02:57:26 +02:00
// If error is not due to lack of clients connecting, this is error
2021-07-09 20:34:32 +02:00
if (errno != EWOULDBLOCK && errno != EAGAIN) {
2021-07-09 02:53:54 +02:00
printLog("Server acccept failed!\n");
goto serverExiting;
}
2021-07-09 18:34:54 +02:00
sleep(1);
2021-07-09 02:53:54 +02:00
} else {
2021-07-09 18:34:54 +02:00
// Client connected
// get IP addr from client
2021-07-09 20:34:32 +02:00
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));
2021-07-09 02:53:54 +02:00
2021-07-09 18:34:54 +02:00
// Populate communicator slot
2021-07-09 20:34:32 +02:00
serverSlots[serverSlotIndex].socklen = socklen;
2021-07-09 02:53:54 +02:00
serverSlots[serverSlotIndex].sockfd = connfd;
serverSlots[serverSlotIndex].parent = args;
2021-07-08 00:10:36 +02:00
2021-07-09 18:34:54 +02:00
// Create thread
threadStatus = pthread_create(&serverSlots[serverSlotIndex].id,
2021-07-09 02:53:54 +02:00
NULL,
serverCommunicationInstance,
(void*)&serverSlots[serverSlotIndex]);
2021-07-09 18:34:54 +02:00
if(threadStatus != 0) {
printLog("Error from pthread: %d\n", threadStatus);
2021-07-09 20:42:44 +02:00
args->returnValue = 1;
2021-07-09 18:34:54 +02:00
goto serverExiting;
}
2021-07-09 02:53:54 +02:00
2021-07-09 18:34:54 +02:00
serverSlotIndex++;
printLog("Accepted connection. Server will now listen...\n");
2021-07-09 02:53:54 +02:00
}
2021-07-08 18:14:46 +02:00
}
2021-07-08 00:10:36 +02:00
2021-07-09 02:53:54 +02:00
serverExiting:
2021-07-09 15:58:47 +02:00
close(args->sockfd);
printLog("Server #%lu offline\n", args->id);
2021-06-16 19:44:48 +02:00
return NULL;
2021-06-16 18:10:04 +02:00
}