OS1/pads/dec_client.c
2023-12-14 03:17:58 +00:00

151 lines
4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
void error(const char *msg) { perror(msg); exit(0); }
void sendFile(int, char*);
void checkLen(char*, char*);
char* toString(char*);
void checkChars(char*, char*);
int main(int argc, char *argv[]) {
int socketFD, portNumber, charsWritten, charsRead;
struct sockaddr_in serverAddress;
struct hostent* serverHostInfo;
char* buffer = malloc(sizeof(char) * 70001);
if (argc < 4) { perror("Failed to launch correctly"); exit(1); }
checkLen(argv[1], argv[2]);
char* plain = toString(argv[1]);
char* key = toString(argv[2]);
checkChars(plain, "plain");
checkChars(key, "key");
// Set up the server address struct
memset((char*)&serverAddress, '\0', sizeof(serverAddress));
portNumber = atoi(argv[3]);
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(portNumber);
serverHostInfo = gethostbyname("localhost");
if (serverHostInfo == NULL) { fprintf(stderr, "CLIENT: ERROR, no such host\n"); exit(0); }
memcpy((char*)&serverAddress.sin_addr.s_addr, (char*)serverHostInfo->h_addr, serverHostInfo->h_length);
// Set up the socket
socketFD = socket(AF_INET, SOCK_STREAM, 0);
if (socketFD < 0) error("CLIENT: ERROR opening socket");
// Connect to server
if (connect(socketFD, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) // Connect socket to addy
fprintf(stderr, "Cannot connect to Server: %d\n", atoi(argv[1]));
// Check Correct Server
send(socketFD, "dec_client", 10, 0);
charsWritten = recv(socketFD, buffer, 12, 0);
if(!strncmp(buffer, "Wrong Client", 12)) {
fprintf(stderr, "Server Has Rejected the Client: %d\n", atoi(argv[3]));
exit(1);
}
char* final = malloc(sizeof(char) * 140002);
memcpy(final, plain, 70001);
memcpy(final+70001, key, 70001);
// Send plaintext to server
charsWritten = send(socketFD, final, 140002, 0);
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
if (charsWritten < strlen(plain)) printf("CLIENT: WARNING: Not all data written to socket!\n");
//printf("strlen1: %ld\n", strlen(plain) );
//printf("chars1: %d\n", charsWritten);
//charsWritten = recv(socketFD, buffer, 3, 0);
/*
// Send key to server
charsWritten = send(socketFD, key, 70001, 0);
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
if (charsWritten < strlen(key)) printf("CLIENT: WARNING: Not all data written to socket!\n");
printf("strlen2: %ld\n", strlen(key) );
printf("chars2: %d\n", charsWritten);
*/
// Get Cyphertext from server
memset(buffer, '\0', 70001);
charsRead = recv(socketFD, buffer, 70001, MSG_WAITALL);
if (charsRead < 0) error("CLIENT: ERROR reading from socket");
//printf("strlen3: %ld\n", strlen(buffer) );
//printf("chars3: %d\n", charsRead);
printf("%s\n", buffer);
close(socketFD);
return 0;
}
char* toString(char* file) {
char* buffer;
size_t length;
FILE * f = fopen (file, "rb");
if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
//buffer = malloc (length + 1);
buffer = malloc (70001);
memset(buffer, '\0', 70001);
if (buffer)
{
fread (buffer, 1, length, f);
buffer[length] = '\0';
}
fclose (f);
}
return buffer;
}
void checkLen(char* plain, char* key) {
FILE *p = fopen(plain, "r");
if(errno !=0) {
perror(plain);
exit(1);
}
FILE *k = fopen(key, "r");
if(errno != 0) {
perror(key);
exit(1);
}
fseek(p, 0, SEEK_END);
fseek(k, 0, SEEK_END);
if(ftell(k) < ftell(p)) {
fprintf(stderr, "Keyfile Too Small: exiting...\n");
exit(1);
}
}
void checkChars(char* str, char* type) {
for(int i = 0; i < strlen(str)-1; i++) {
if(str[i] == ' ');
else if(str[i] > 64 && str[i] < 91);
else {
fprintf(stderr, "Invalid Character in %s file: Exiting...\n", type);
exit(1);
}
}
}