2023-12-13 19:08:11 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
void error(const char *msg) { perror(msg); exit(1); }
|
|
|
|
void onClient(int);
|
|
|
|
void clean_children();
|
|
|
|
int to_c(int);
|
|
|
|
int from_c(int);
|
|
|
|
int neg_mod(int, int);
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
int listenSocketFD, establishedConnectionFD, portNumber;
|
|
|
|
struct sockaddr_in serverAddress;
|
|
|
|
|
|
|
|
if (argc < 2) { fprintf(stderr,"USAGE: %s port\n", argv[0]); exit(1); }
|
|
|
|
|
|
|
|
// Set up the address struct for this process (the server)
|
|
|
|
memset((char *)&serverAddress, '\0', sizeof(serverAddress)); // Clear out the address struct
|
|
|
|
portNumber = atoi(argv[1]);
|
|
|
|
serverAddress.sin_family = AF_INET;
|
|
|
|
serverAddress.sin_port = htons(portNumber);
|
|
|
|
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
// Set up the socket
|
|
|
|
listenSocketFD = socket(AF_INET, SOCK_STREAM, 0); // Create the socket
|
|
|
|
if (listenSocketFD < 0) error("ERROR opening socket");
|
|
|
|
|
|
|
|
// Enable the socket to begin listening
|
|
|
|
if (bind(listenSocketFD, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0)
|
|
|
|
error("ERROR on binding");
|
|
|
|
|
|
|
|
listen(listenSocketFD, 5);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
clean_children();
|
|
|
|
onClient(listenSocketFD);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(listenSocketFD);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onClient(int listenSocketFD) {
|
|
|
|
|
|
|
|
// Accept a connection, blocking if one is not available until one connects
|
|
|
|
struct sockaddr_in clientAddress;
|
|
|
|
socklen_t sizeOfClientInfo = sizeof(clientAddress);
|
|
|
|
int establishedConnectionFD = accept(listenSocketFD, (struct sockaddr *)&clientAddress, &sizeOfClientInfo);
|
|
|
|
|
|
|
|
if (establishedConnectionFD < 0) error("ERROR on accept");
|
|
|
|
|
|
|
|
int f = fork();
|
|
|
|
|
|
|
|
if(!f) {
|
|
|
|
|
|
|
|
size_t charsRead;
|
|
|
|
char* buffer1 = malloc(sizeof(char) * 70001);
|
|
|
|
char* buffer2 = malloc(sizeof(char) * 70001);
|
|
|
|
|
|
|
|
charsRead = recv(establishedConnectionFD, buffer1, 10, 0);
|
|
|
|
// check for proper client
|
|
|
|
if(!strncmp(buffer1, "dec_client", 10)) {
|
|
|
|
send(establishedConnectionFD, "Goood Client", 12, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
send(establishedConnectionFD, "Wrong Client", 12, 0);
|
|
|
|
close(establishedConnectionFD);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* final = malloc(sizeof(char) * 140002);
|
|
|
|
|
|
|
|
memset(final, '\0', 140002);
|
|
|
|
charsRead = recv(establishedConnectionFD, final, 140002, MSG_WAITALL);
|
|
|
|
if (charsRead < 0) error("ERROR reading from socket");
|
2023-12-13 23:04:17 -08:00
|
|
|
//printf("char1: %ld\n", charsRead);
|
2023-12-13 19:08:11 -08:00
|
|
|
|
|
|
|
memcpy(buffer1, final, 70001);
|
|
|
|
memcpy(buffer2, final + 70001, 70001);
|
|
|
|
|
|
|
|
//charsRead = send(establishedConnectionFD, "ack", 3, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
memset(buffer2, '\0', 70001);
|
|
|
|
charsRead = recv(establishedConnectionFD, buffer2, 70001, 0);
|
|
|
|
if (charsRead < 0) error("ERROR reading from socket");
|
|
|
|
printf("char2: %ld\n", charsRead);
|
|
|
|
*/
|
|
|
|
|
|
|
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer1);
|
|
|
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer2);
|
|
|
|
|
2023-12-13 23:04:17 -08:00
|
|
|
//printf("strlen: %ld\n", strlen(buffer1));
|
2023-12-13 19:08:11 -08:00
|
|
|
// do enc
|
|
|
|
char* cyphertext = malloc(sizeof(char) * 70001);
|
|
|
|
memset(cyphertext, '\0', strlen(buffer1)+1);
|
|
|
|
for(int i = 0; i < strlen(buffer1)-1; i++) {
|
|
|
|
cyphertext[i] = from_c(neg_mod((to_c(buffer1[i]) - to_c(buffer2[i])), 27));
|
|
|
|
}
|
|
|
|
//printf("Cyphertext: %s\n", cyphertext);
|
|
|
|
|
|
|
|
// Send a Success message back to the client
|
|
|
|
charsRead = send(establishedConnectionFD, cyphertext, 70001, 0);
|
|
|
|
|
|
|
|
if (charsRead < 0) error("ERROR writing to socket");
|
|
|
|
close(establishedConnectionFD);
|
|
|
|
free(buffer1);
|
|
|
|
free(buffer2);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clean_children() {
|
|
|
|
|
|
|
|
// Clean up any child process that has not been cleaned
|
|
|
|
int pid = 43110;
|
|
|
|
while(pid >0) {
|
|
|
|
int status;
|
|
|
|
pid = waitpid(0, &status, WNOHANG | WUNTRACED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int to_c(int c) {
|
|
|
|
int ret;
|
|
|
|
if(c == ' ')
|
|
|
|
ret = 26;
|
|
|
|
else
|
|
|
|
ret = c - 'A';
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int from_c(int c) {
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if(c == 26)
|
|
|
|
ret = 32;
|
|
|
|
else
|
|
|
|
ret = c + 'A';
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int neg_mod(int num, int op) {
|
|
|
|
|
|
|
|
if(num > -1)
|
|
|
|
return num % op;
|
|
|
|
else
|
|
|
|
return num + op;
|
|
|
|
|
|
|
|
}
|