Basic input sanatization:

Created new library to deal with user input.
Implemented check in client to prevent C-d spamming the server.
C-d now exits.
Implemented check in client to prevent clients sending messages containing only newlines
to the server.
This commit is contained in:
Barry Kane 2021-08-19 23:07:58 +01:00
parent 2c093903a4
commit 849a80bd37
6 changed files with 50 additions and 16 deletions

View File

@ -7,6 +7,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "misc/texteffects.h" #include "misc/texteffects.h"
#include "misc/inputhandling.h"
#define MAX 1024 #define MAX 1024
#define PORT 5000 #define PORT 5000
#define SA struct sockaddr #define SA struct sockaddr
@ -20,11 +21,16 @@ void * messageSender(void * sockfd)
{ {
bzero(sendBuffer, MAX); bzero(sendBuffer, MAX);
printf("COMM-LINK> "); printf("COMM-LINK> ");
fgets(sendBuffer, MAX, stdin); if(fgets(sendBuffer, MAX, stdin) == NULL)
if(sendBuffer[0] != '\n');
{ {
write((long)sockfd, sendBuffer, MAX); exit(0);
} }
userInputSanatize(sendBuffer, MAX);
if(sendBuffer[0] == '\n')
{
continue;
}
write((long)sockfd, sendBuffer, MAX);
} }
} }
@ -35,9 +41,9 @@ void * messageReceiver(void * sockfd)
while (1) while (1)
{ {
read((long)sockfd, receiveBuffer, MAX); read((long)sockfd, receiveBuffer, MAX);
slowprint("\nUSER-MESSAGE: ", 8000); slowPrint("\nUSER-MESSAGE: ", 8000);
slowprint(receiveBuffer, 8000); slowPrint(receiveBuffer, 8000);
slowprint("\nCOMM-LINK (CONT.)> ", 8000); slowPrint("\nCOMM-LINK (CONT.)> ", 8000);
bzero(receiveBuffer, MAX); bzero(receiveBuffer, MAX);
} }
} }
@ -57,7 +63,7 @@ int main(int argc, char **argv)
} }
else else
{ {
slowprint("Socket successfully created.\n", 8000); slowPrint("Socket successfully created.\n", 8000);
} }
bzero(&servaddr, sizeof(servaddr)); bzero(&servaddr, sizeof(servaddr));
@ -76,12 +82,12 @@ int main(int argc, char **argv)
// Connect the server and client sockets, Kronk: // Connect the server and client sockets, Kronk:
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0)
{ {
slowprint("Connection with the Silverkin Industries Comm-Link Server Failed:\nPlease contact your service representative.\n", 8000); slowPrint("Connection with the Silverkin Industries Comm-Link Server Failed:\nPlease contact your service representative.\n", 8000);
exit(0); exit(0);
} }
else else
{ {
slowprint("Connected to the Silverkin Industries Comm-Link Server:\nHave a pleasant day.\n", 8000); slowPrint("Connected to the Silverkin Industries Comm-Link Server:\nHave a pleasant day.\n", 8000);
} }
// Run a thread to send messages, and use main to recieve. // Run a thread to send messages, and use main to recieve.

View File

@ -27,8 +27,8 @@ int main()
struct sockaddr_in serverAddress, clientAddress; struct sockaddr_in serverAddress, clientAddress;
// Give an intro: Display the Silverkin Industries logo and splash text. // Give an intro: Display the Silverkin Industries logo and splash text.
slowprint(logostring, 3000); slowPrint(logostring, 3000);
slowprint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK SERVER ====--\nVersion Alpha 0.1\n", 5000); slowPrint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK SERVER ====--\nVersion Alpha 0.1\n", 5000);
// Initialize the sockets to 0, so we don't crash. // Initialize the sockets to 0, so we don't crash.
for (int index = 0; index < maxClients; index++) for (int index = 0; index < maxClients; index++)
@ -46,7 +46,7 @@ int main()
else else
{ {
slowprint(" Socket creation is \033[32;40mGREEN.\033[0m\n", 5000); slowPrint(" Socket creation is \033[32;40mGREEN.\033[0m\n", 5000);
} }
bzero(&serverAddress, sizeof(serverAddress)); bzero(&serverAddress, sizeof(serverAddress));
@ -64,7 +64,7 @@ int main()
} }
else else
{ {
slowprint(" Socket binding is \033[32;40mGREEN.\033[0m\n", 5000); slowPrint(" Socket binding is \033[32;40mGREEN.\033[0m\n", 5000);
} }
// Let's start listening: // Let's start listening:
@ -75,7 +75,7 @@ int main()
} }
else else
{ {
slowprint(" Server listening is \033[32;40mGREEN.\033[0m\n", 5000); slowPrint(" Server listening is \033[32;40mGREEN.\033[0m\n", 5000);
} }
length = sizeof(clientAddress); length = sizeof(clientAddress);

16
src/misc/inputhandling.c Normal file
View File

@ -0,0 +1,16 @@
// inputhandling.c: Implementation of input handling library for SilverMUD.
// Barry Kane, 2021.
#include <ctype.h>
void userInputSanatize(char * inputString, int length)
{
for(int index = 0; index <= length; index++)
{
if(!isprint(inputString[index]))
{
inputString[index] = '\n';
inputString[index + 1] = '\0';
break;
}
}
}

10
src/misc/inputhandling.h Normal file
View File

@ -0,0 +1,10 @@
// inputhandling.h: Header file for the inputhandling library for SilverMUD.
// Barry Kane, 2021
#ifndef INPUTHANDLING_H
#define INPUTHANDLING_H
#include <ctype.h>
// Sanatize user input to ensure it's okay to send to the server:
void userInputSanatize(char * inputString, int length);
#endif

View File

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
void slowprint(char * stringToPrint, int delay) void slowPrint(char * stringToPrint, int delay)
{ {
int characterIndex = 0; int characterIndex = 0;
while(stringToPrint[characterIndex] != '\0') while(stringToPrint[characterIndex] != '\0')

View File

@ -1,9 +1,11 @@
// texteffects.h: Header file for the texteffects library for SilverMUD.
// Barry Kane, 2021.
#ifndef TEXTEFFECTS_H_ #ifndef TEXTEFFECTS_H_
#define TEXTEFFECTS_H_ #define TEXTEFFECTS_H_
#include <stdio.h> #include <stdio.h>
// A fancy, character by character print. Similar to a serial terminal with lower baud rate. // A fancy, character by character print. Similar to a serial terminal with lower baud rate.
void slowprint(char * stringToPrint, int delay); void slowPrint(char * stringToPrint, int delay);
// A string containing an ASCII art version of the Silverkin Industries logo. // A string containing an ASCII art version of the Silverkin Industries logo.
char * logostring = " ///////\n //////////////////////////////////////////\n ///////////////////////////////////////////////////////////\n ////////// ////////////////////////////\n ### # # # # ##### ### # # # # # /////////////////\n ### # # # # ## # # ## # ## # //////////////\n ## # # # # # ### # # # # # # /////////\n #### # ### # ##### # # # # # # ## ///////\n # ## # ##### # # ### ### ### # ##### ### ////// \n # # # # # # # # ## # # # # ## ## ////\n # # # # # # # # ## # ### # # ## //\n # # ### ##### ##### ### # # # # #### ### //\n"; char * logostring = " ///////\n //////////////////////////////////////////\n ///////////////////////////////////////////////////////////\n ////////// ////////////////////////////\n ### # # # # ##### ### # # # # # /////////////////\n ### # # # # ## # # ## # ## # //////////////\n ## # # # # # ### # # # # # # /////////\n #### # ### # ##### # # # # # # ## ///////\n # ## # ##### # # ### ### ### # ##### ### ////// \n # # # # # # # # ## # # # # ## ## ////\n # # # # # # # # ## # ### # # ## //\n # # ### ##### ##### ### # # # # #### ### //\n";