137 lines
3.6 KiB
C
137 lines
3.6 KiB
C
#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 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) {
|
|
|
|
int 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, "enc_client", 10)) {
|
|
send(establishedConnectionFD, "Goood Client", 12, 0);
|
|
}
|
|
else {
|
|
send(establishedConnectionFD, "Wrong Client", 12, 0);
|
|
close(establishedConnectionFD);
|
|
exit(0);
|
|
}
|
|
|
|
memset(buffer1, '\0', 70001);
|
|
charsRead = recv(establishedConnectionFD, buffer1, 70001, MSG_WAITALL);
|
|
if (charsRead < 0) error("ERROR reading from socket");
|
|
printf("%d\n", charsRead);
|
|
|
|
memset(buffer2, '\0', 70001);
|
|
charsRead = recv(establishedConnectionFD, buffer2, 70001, MSG_WAITALL);
|
|
if (charsRead < 0) error("ERROR reading from socket");
|
|
printf("%d\n", charsRead);
|
|
|
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer1);
|
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer2);
|
|
printf("strlen: %ld\n", strlen(buffer1));
|
|
|
|
// do enc
|
|
char* cyphertext = malloc(70001);
|
|
memset(cyphertext, '\0', strlen(buffer1)+1);
|
|
for(int i = 0; i < strlen(buffer1); i++) {
|
|
cyphertext[i] = from_c((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);
|
|
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;
|
|
}
|