142 lines
3.7 KiB
C
142 lines
3.7 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[70000];
|
||
|
|
||
|
if (argc < 4) { fprintf(stderr,"USAGE: %s hostname port\n", argv[0]); exit(0); }
|
||
|
|
||
|
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, "enc_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);
|
||
|
}
|
||
|
|
||
|
// Send plaintext to server
|
||
|
charsWritten = send(socketFD, plain, 70001, 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("%d\n",charsWritten);
|
||
|
|
||
|
// 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("%d\n",charsWritten);
|
||
|
|
||
|
// Get Cyphertext from server
|
||
|
memset(buffer, '\0', sizeof(buffer));
|
||
|
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0);
|
||
|
//printf("%d\n", charsRead);
|
||
|
|
||
|
if (charsRead < 0) error("CLIENT: ERROR reading from socket");
|
||
|
printf("%s\n", buffer);
|
||
|
close(socketFD);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
char* toString(char* file) {
|
||
|
|
||
|
int charsWritten = 0;
|
||
|
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);
|
||
|
buffer = malloc (sizeof(char) * 70001);
|
||
|
memset(buffer, '\0', 70001);
|
||
|
if (buffer)
|
||
|
{
|
||
|
fread (buffer, 1, length, f);
|
||
|
buffer[length-1] = '\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); 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);
|
||
|
}
|
||
|
}
|
||
|
}
|