Compare commits

..

No commits in common. "master" and "test" have entirely different histories.
master ... test

24 changed files with 125 additions and 1260 deletions

3
.gitignore vendored
View file

@ -52,6 +52,3 @@ Module.symvers
Mkfile.old
dkms.conf
.ccls-cache
*temp*
.gdb_history

View file

@ -1,151 +0,0 @@
#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);
}
}
}

View file

@ -1,158 +0,0 @@
#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");
//printf("char1: %ld\n", charsRead);
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);
//printf("strlen: %ld\n", strlen(buffer1));
// 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;
}

View file

@ -1,141 +0,0 @@
#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, 70001, MSG_WAITALL);
//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);
}
}
}

View file

@ -1,137 +0,0 @@
#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;
}

View file

@ -1,39 +0,0 @@
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
int main(int arg, char** args) {
if(arg < 2) {
printf("Not enough Args\n");
return 1;
}
int num_chars = atoi(args[1]);
char* crypto = malloc(sizeof(char) * (num_chars + 10));
srand(time(NULL));
char* tempstr = malloc(sizeof(char) * 2);
while(num_chars) {
char temp = 'A' + (rand() % 26);
sprintf(tempstr, "%c", temp);
strncat(crypto, tempstr, 1);
num_chars--;
}
printf("%s\n", crypto);
return 0;
}

View file

@ -1,28 +0,0 @@
CC=gcc --std=gnu99 -g
all: keygen enc_client enc_server dec_client dec_server
echo Everything Made
keygen: keygen.c
$(CC) keygen.c -o keygen
enc_client: enc_client.c
$(CC) enc_client.c -o enc_client
enc_server: enc_server.c
$(CC) enc_server.c -o enc_server
dec_client: dec_client.c
$(CC) dec_client.c -o dec_client
dec_server: dec_server.c
$(CC) dec_server.c -o dec_server
ec: all
./enc_client plaintext1 key1 11111
dc: all
./dec_client cyphertext1 key1 22222
clean: all
rm -fr *.o vgcore.* keygen enc_client enc_server dec_client dec_server

View file

@ -1,149 +0,0 @@
#!/bin/bash
# FYI, this command removes file abc if it is empty: [ -s abc ] || rm -f abc
usage="usage: $0 encryptionport decryptionport"
#use the standard version of echo
echo=/bin/echo
#Make sure we have the right number of arguments
if test $# -gt 2 -o $# -lt 2
then
${echo} $usage 1>&2
exit 1
fi
#Clean up any previous runs
${echo} '#Initializing - Cleaning up - ignore Operation Not Permitted errors'
${echo} '#Note: this script requires that the current directory (.) be in your PATH in ~/.bashrc'
killall -q -u $USER dec_client
killall -q -u $USER dec_server
killall -q -u $USER enc_client
killall -q -u $USER enc_server
rm -f ciphertext*
rm -f plaintext*_*
rm -f key20
rm -f key70000
#Record the ports passed in
encport=$1
decport=$2
#Run the daemons
./enc_server $encport &
./dec_server $decport &
sleep 5
${echo}
${echo} '#-----------------------------------------'
${echo} '#START OF GRADING SCRIPT'
${echo} '#keygen 20 > key20'
./keygen 20 > key20
${echo} "#5 POINTS: key20 must exist"
[ -s key20 ] || rm -f key20
if [ -f key20 ]; then ${echo} 'key20 exists!'; else ${echo} 'key20 DOES NOT EXIST'; fi
${echo}
${echo} "#-----------------------------------------"
${echo} "#5 POINTS: Number of characters in key20, should be 21:"
wc -m key20
${echo}
${echo} "#-----------------------------------------"
${echo} '#keygen 70000 > key70000'
./keygen 70000 > key70000
${echo} "#5 POINTS: Number of characters in key70000, should be 70001:"
[ -s key70000 ] || rm -f key70000
wc -m key70000
${echo}
${echo} "#-----------------------------------------"
${echo} '#enc_client plaintext1 key20 $encport'
${echo} "#10 POINTS: Should return error about too-short key"
./enc_client plaintext1 key20 $encport
${echo}
${echo} "#-----------------------------------------"
${echo} '#enc_client plaintext1 key70000 $encport'
${echo} "#20 POINTS: Should return encrypted version of plaintext1"
./enc_client plaintext1 key70000 $encport
${echo}
${echo} '#-----------------------------------------'
${echo} '#enc_client plaintext1 key70000 $encport > ciphertext1'
./enc_client plaintext1 key70000 $encport > ciphertext1
${echo} "#10 POINTS: ciphertext1 must exist"
[ -s ciphertext1 ] || rm -f ciphertext1
if [ -f ciphertext1 ]; then ${echo} 'ciphertext1 exists!'; else ${echo} 'ciphertext1 DOES NOT EXIST'; fi
${echo}
${echo} '#-----------------------------------------'
${echo} '#10 POINTS: ciphertext1 must be same number of chars as source'
${echo} '#wc -m plaintext1'
wc -m plaintext1
${echo} '#Should be same: wc -m ciphertext1'
wc -m ciphertext1
${echo}
${echo} '#-----------------------------------------'
${echo} '#5 POINTS: ciphertext1 should look encrypted'
cat ciphertext1
${echo}
${echo} '#-----------------------------------------'
${echo} '#dec_client ciphertext1 key70000 $encport'
${echo} '#5 POINTS: Should fail giving error that dec_client cannot use enc_server'
./dec_client ciphertext1 key70000 $encport
${echo}
${echo} '#-----------------------------------------'
${echo} '#20 POINTS: should return decrypted ciphertext1 that matches source'
${echo} '#cat plaintext1'
cat plaintext1
${echo} '#dec_client ciphertext1 key70000 $decport'
./dec_client ciphertext1 key70000 $decport
${echo}
${echo} '#-----------------------------------------'
${echo} '#dec_client ciphertext1 key70000 $decport > plaintext1_a'
./dec_client ciphertext1 key70000 $decport > plaintext1_a
${echo} "#10 POINTS: plaintext1_a must exist"
[ -s plaintext1_a ] || rm -f plaintext1_a
if [ -f plaintext1_a ]; then ${echo} 'plaintext1_a exists!'; else ${echo} 'plaintext1_a DOES NOT EXIST'; fi
${echo}
${echo} '#-----------------------------------------'
${echo} '#cmp plaintext1 plaintext1_a'
${echo} '#5 POINTS: plaintext1 must be the same as plaintext1_a:'
${echo} '#echo $? should be == 0, which means the cmp succeeded!'
cmp plaintext1 plaintext1_a
echo $?
${echo}
${echo} '#-----------------------------------------'
${echo} '#20 POINTS: concurrent test of encryption - look for 4 properly-sized ciphertext# files, or 5 where the 5th is 0 bytes'
${echo} '#5 POINTS: Should be only one error about plaintext5 being bad'
rm -f ciphertext*
rm -f plaintext*_*
./enc_client plaintext1 key70000 $encport > ciphertext1 &
./enc_client plaintext2 key70000 $encport > ciphertext2 &
./enc_client plaintext3 key70000 $encport > ciphertext3 &
./enc_client plaintext4 key70000 $encport > ciphertext4 &
./enc_client plaintext5 key70000 $encport > ciphertext5 &
${echo} 'Ten second sleep, your program must complete in this time'
sleep 10
ls -pla
${echo}
${echo} '#-----------------------------------------'
${echo} '#15 POINTS: concurrent test of decryption - look for 4 plaintext#_a files that match the plaintext# files'
./dec_client ciphertext1 key70000 $decport > plaintext1_a &
./dec_client ciphertext2 key70000 $decport > plaintext2_a &
./dec_client ciphertext3 key70000 $decport > plaintext3_a &
./dec_client ciphertext4 key70000 $decport > plaintext4_a &
${echo} '#Ten second sleep, your program must complete in this time'
sleep 10
ls -pla
#Clean up
${echo}
${echo} '#-----------------------------------------'
${echo} '#Cleaning up - ignore Operation Not Permitted errors'
killall -q -u $USER dec_client
killall -q -u $USER dec_server
killall -q -u $USER enc_client
killall -q -u $USER enc_server
rm -f ciphertext*
rm -f plaintext*_*
rm -f key20
rm -f key70000
${echo}
${echo} '#SCRIPT COMPLETE'

View file

@ -1 +0,0 @@
THE RED GOOSE FLIES AT MIDNIGHT STOP

View file

@ -1 +0,0 @@
CAVERNA POWER GRID SPACE CADETS STARSHIP TROOPERS DIXIT DOMINION ARKHAM HORROR MAGIC FALLING BANE ECLIPSE ACQUIRE CORE WORLDS REVOLUTION LORDS OF WATERDEEP MICE AND MYSTICS PATHFINDER ACG MUNCHKIN SMASHUP TWILIGHT STRUGGLE PUERTO RICO SMALL WORLD PANDEMIC SEVEN WONDERS ROBORALLY DIPLOMACY DEADWOOD KILL DOCTOR LUCKY

View file

@ -1 +0,0 @@
IN THE BEGINNING

File diff suppressed because one or more lines are too long

View file

@ -1 +0,0 @@
$*!(#*djs8301these-are-all-bad-characters

View file

@ -1,63 +0,0 @@
# pads
Pads is an implementation of a simple one time pad algorithm.
There are 5 programs, two pairs of server client programs and
one program to generate random keys.
The client validates the input and sends it to the server in
a chunk of 70001 chars. The server receives that; one for the
text and one for the key. The server encrypts the text with
the super special algorithm and then returns the encrypted or
decrypted text in another 70001 byte TCP stream.
To handle multiple connections, the process forks, does the transfer
encrypts the message, sends the text, and then kills the process.
The main process waits for the dead processes after receiving new
connections.
## Compiling
It is highly recommended to use the make command to compile the
5 required programs.
Simply type:
```
make
```
## Running
### servers
dec and enc server's both require the port to be specified as a
launch argument:
```
./enc_server 69
./dec_server 420
```
consider running them in the background with '&' and choosing ports
greater than 1024.
### clients
The clients communicate with their respective servers **only**. To
connect, you specify the password file, key file, and port as launch args:
```
./enc_client plaintext_file key_file 69
```
### keygen
The keygen program takes the number of characters as a launch argument:
```
./keygen 69
```
consider redirecting output with `> somefile`

View file

@ -2,15 +2,19 @@
volatile sig_atomic_t stop_flag = 0;
int stop_msg = 0;
int at_prompt = 0;
int main() {
// Signal Handling
/*
struct sigaction sa;
sa.sa_handler = sigstop_handle;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
*/
signal(SIGINT, sigint_handle);
signal(SIGTSTP, sigstop_handle);
// Reusable Variables
int status = 0;
size_t input_size = 2048;
size_t getline_len;
@ -18,10 +22,9 @@ int main() {
while(1) {
// Check and reap children that have exited
clean_children();
// Handle Switching Between Stop Mode
//signal handling
if(stop_msg) {
if(stop_flag)
printf("Entering Stop Mode: Background Processes Do Not Work (&)\n");
@ -31,15 +34,11 @@ int main() {
}
// Shell Value Grabbing
at_prompt = 1;
printf(": ");
fflush(stdout);
getline_len = getline(&input, &input_size, stdin);
input[getline_len - 1] = '\0';
at_prompt = 0;
// Fill in PID for '$$'
expand_dollar(input, input_size);
// Ignore whitespace or comments
@ -48,7 +47,8 @@ int main() {
continue;
}
// Format Line as Array
char*** array = malloc(sizeof(char**));
int num_strings = input_format(input, input_size, array);
@ -68,7 +68,6 @@ int main() {
}
}
// Build the array with each argument out of line
int input_format(char* input, size_t input_size, char*** array) {
const int max_args = 512;
@ -100,6 +99,11 @@ void inbuilt_cd(char* args) {
int run_command(char*** array, int num_array) {
char* comb_string = malloc(sizeof(char) * 100);
sprintf(comb_string, "/usr/bin/");
strcat(comb_string, (*array)[0]);
//Check to see if the process is ran in the background
int background = 0;
if(!strncmp("&", (*array)[num_array - 1], 1)) {
@ -127,7 +131,6 @@ int run_command(char*** array, int num_array) {
}
}
// Make array not have nullspace between non-null values
sterilize_array(array, num_array);
// Check to see if the process has input redirected
@ -149,22 +152,17 @@ int run_command(char*** array, int num_array) {
}
}
// Make array not have nullspace between non-null values
sterilize_array(array, num_array);
// Forking/Child processes
int f = fork();
pid_t pid;
if(!f) {
signal(SIGTSTP, sigstop_handle);
// Handle Redirected output
if(redirect_o) {
int fd = open(redirect_o_str, O_WRONLY | O_CREAT, 0666);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
}
// Or direct to null if in background
else if(background) {
int fd = open("/dev/null", O_WRONLY, 0666);
//Direct to dev null
@ -172,37 +170,32 @@ int run_command(char*** array, int num_array) {
dup2(fd, STDERR_FILENO);
}
// Handle Redirected input
if(redirect_i) {
int fd = open(redirect_i_str, O_RDONLY);
dup2(fd, STDIN_FILENO);
}
// Or direct to null if in background
else if(background) {
int fd = open("/dev/null", O_RDONLY);
dup2(fd, STDIN_FILENO);
}
// Spawn new Process
pid = execvp((*array)[0], (*array));
pid = execv(comb_string, (*array));
// Handle error on spawning process
perror((*array)[0]);
perror(comb_string);
exit(1);
}
// If in background, tell user the pid that was spawned
else if(background){
printf("background pid is %d\n", f);
}
// Wait on the process if it is running in forground
int status;
if(!background)
waitpid(f, &status, 0);
free(comb_string);
return status;
}
@ -214,7 +207,6 @@ void expand_dollar(char* input, size_t input_size) {
char* dollar;
// Check for two '$$'s and exchange them to the pid if they exist
dollar = strstr(input, "$$");
while(dollar != NULL) {
@ -254,19 +246,9 @@ void sterilize_array(char*** array, size_t num_array) {
void sigint_handle() {
}
void sigstop_handle() {
// Negate flags so that it handles both starting and stopping
void sigstop_handle(int n) {
stop_flag = !stop_flag;
stop_msg = !stop_msg;
if(at_prompt) {
if(stop_flag)
puts("\nEntering Stop Mode: Background Processes Do Not Work (&)\n:");
else
puts("\nExiting Stop Mode: Background Processes Now Work (&)\n:");
stop_msg = 0;
}
}
void clean_children() {
@ -275,14 +257,9 @@ void clean_children() {
int pid = 43110;
while(pid >0) {
int status;
pid = waitpid(0, &status, WNOHANG | WUNTRACED );
pid = waitpid(0, &status, WNOHANG);
// Handle terminated with status or signal
if(pid > 0) {
if(WIFSIGNALED(status))
printf("\nbackground pid %d is done: terminated with signal %d\n", pid, WTERMSIG(status));
else
if(pid > 0)
printf("\nbackground pid %d is done: exit value %d\n", pid, WEXITSTATUS(status));
}
}
}

View file

@ -1,11 +1,17 @@
CC=gcc --std=gnu99 -g
output=smallsh
all: main.c main.h
$(CC) main.c -o $(output)
all: main.c main.h node.o input.o
$(CC) main.c node.o input.o -o $(output)
node.o: node.c node.h
$(CC) -c node.c -o node.o
input.o: input.h input.c
$(CC) -c input.c -o input.o
test: all
./p3testscript 2>&1
./p3testscript
clean:
rm -fr *.o vgcore.* junk* $(output)
rm -fr *.o vgcore.* $(output)

62
smallsh/node.c Normal file
View file

@ -0,0 +1,62 @@
#include "node.h"
void append_node(struct node* head, struct node* node_app) {
if(!head) {
head = node_app;
return;
}
struct node* temp = head;
while(temp->node != NULL) {
temp = temp->node;
}
temp->node = node_app;
}
struct node* appendv_node(struct node* head, void* data) {
struct node* node = malloc(sizeof(struct node));
node->data = data;
node->node = NULL;
append_node(head, node);
return node;
}
int count_nodes(struct node* head) {
int num = 1;
if(!head)
return -1;
struct node* temp = head;
while(temp->node) {
temp = temp->node;
num++;
}
return num;
}
void iterate_nodes(struct node* head, void *func (void*, void*), void* val) {
if(!head)
return;
func(head->data, val);
struct node* temp = head;
while(temp->node) {
temp = temp->node;
func(temp->data, val);
}
}

24
smallsh/node.h Normal file
View file

@ -0,0 +1,24 @@
#include "stdlib.h"
#ifndef NODES
#define NODES
struct node {
struct node* node;
void* data;
};
// Appends Node to list
void append_node(struct node*, struct node*);
// Creates node with value and appends
// Returns node that was created
struct node* appendv_node(struct node*, void*);
// Counts the number of nodes in a list
int count_nodes(struct node*);
// Iterates over every node in a list and runs the given function
void iterate_nodes(struct node*, void *func(void*, void*), void*);
#endif

View file

@ -1,110 +0,0 @@
#!/bin/bash
echo "PRE-SCRIPT INFO"
echo " Grading Script PID: $$"
echo ' Note: your smallsh will report a different PID when evaluating $$'
./smallsh <<'___EOF___'
echo BEGINNING TEST SCRIPT
echo
echo --------------------
echo Using comment (5 points if only next prompt is displayed next)
#THIS COMMENT SHOULD DO NOTHING
echo
echo
echo --------------------
echo ls (10 points for returning dir contents)
ls
echo
echo
echo --------------------
echo ls out junk
ls > junk
echo
echo
echo --------------------
echo cat junk (15 points for correctly returning contents of junk)
cat junk
echo
echo
echo --------------------
echo wc in junk (15 points for returning correct numbers from wc)
wc < junk
echo
echo
echo --------------------
echo wc in junk out junk2; cat junk2 (10 points for returning correct numbers from wc)
wc < junk > junk2
cat junk2
echo
echo
echo --------------------
echo test -f badfile (10 points for returning error value of 1, note extraneous &)
test -f badfile
status &
echo
echo
echo --------------------
echo wc in badfile (10 points for returning text error)
wc < badfile
echo
echo
echo --------------------
echo badfile (10 points for returning text error)
badfile
echo
echo
echo --------------------
echo sleep 100 background (10 points for returning process ID of sleeper)
sleep 100 &
echo
echo
echo --------------------
echo pkill -signal SIGTERM sleep (10 points for pid of killed process, 10 points for signal)
echo (Ignore message about Operation Not Permitted)
pkill sleep
echo
echo
echo --------------------
echo sleep 1 background (10 pts for pid of bg ps when done, 10 for exit value)
sleep 1 &
sleep 1
echo
echo
echo --------------------
echo pwd
pwd
echo
echo
echo --------------------
echo cd
cd
echo
echo
echo --------------------
echo pwd (10 points for being in the HOME dir)
pwd
echo
echo
echo --------------------
echo mkdir testdir$$
mkdir testdir$$
echo
echo
echo --------------------
echo cd testdir$$
cd testdir$$
echo
echo
echo --------------------
echo pwd (5 points for being in the newly created dir)
pwd
echo --------------------
echo Testing foreground-only mode (20 points for entry & exit text AND ~5 seconds between times)
kill -SIGTSTP $$
date
sleep 5 &
date
kill -SIGTSTP $$
exit
___EOF___

View file

@ -1,42 +0,0 @@
# smallsh
This is an interactive shell called smallsh! It should be similar
to other shells you have used, but fallowing a minimalist approach:
aka a ton of features are missing. You got comfortable with pipes and
other fancy time-saving features; but for smallsh, we believe those
take away from the pure nature of typing into your computer and that
we should go back to a world without.
## Compiling
You can compile this program by using the make command or by hand.
Simply type:
```
make
```
or, to manually compile, type:
```
gcc -std=c99 main.c -o movies_by_year
```
## Running
To run, type:
```
make run
```
or
```
./smallsh
```
to run manually

View file

@ -1,113 +0,0 @@
#include "main.h"
#define SIZE 1
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t myCond1, myCond2;
int myCount;
int isProduced = 0;
int main() {
printf("PROGRAM START\n");
fflush(stdout);
// Create the consumer thread
pthread_t consumerID;
int resultInt = pthread_create(&consumerID, NULL, consumer, NULL);
producer();
}
void producer() {
while (1) {
pthread_mutex_lock(&myMutex);
print_mutex("PRODUCER", "locked");
while (isProduced == SIZE) {
print_wait("PRODUCER", "myCond2");
pthread_cond_wait(&myCond2, &myMutex);
}
if(myCount == 10)
end_program();
myCount++;
isProduced++;
pthread_mutex_unlock(&myMutex);
print_mutex("PRODUCER", "unlocked");
pthread_cond_signal(&myCond1);
print_signal("PRODUCER", "myCond1");
}
}
void* consumer() {
printf("CONSUMER THREAD CREATED\n");
fflush(stdout);
while (1) {
pthread_mutex_lock(&myMutex);
print_mutex("CONSUMER", "locked");
while (isProduced == 0) {
print_wait("CONSUMER", "myCond1");
pthread_cond_wait(&myCond1, &myMutex);
}
print_count();
isProduced--;
pthread_mutex_unlock(&myMutex);
print_mutex("CONSUMER", "unlocked");
pthread_cond_signal(&myCond2);
print_signal("CONSUMER", "myCond1");
}
}
void end_program() {
printf("PROGRAM END\n");
fflush(stdout);
exit(0);
}
void print_mutex(char* type, char* lockstate) {
printf("%s: myMutex %s\n", type, lockstate);
fflush(stdout);
}
void print_count() {
printf("myCount: %d -> %d\n", myCount-1, myCount);
fflush(stdout);
}
void print_wait(char* type, char* var) {
printf("%s: waiting on %s\n", type, var);
fflush(stdout);
}
void print_signal(char* type, char* var) {
printf("%s: signaling %s\n", type, var);
fflush(stdout);
}

View file

@ -1,13 +0,0 @@
#include "pthread.h"
#include "stdio.h"
#include "stdlib.h"
void* consumer();
void producer();
void end_program();
void print_mutex(char*, char*);
void print_count();
void print_signal(char*, char*);
void print_wait(char*, char*);

View file

@ -1,11 +0,0 @@
CC=gcc --std=gnu99 -g
output=myCounter
all: main.c main.h
$(CC) main.c -lpthread -o $(output)
run: all
./$(output)
clean:
rm -fr *.o vgcore.* $(output)

View file

@ -1,41 +0,0 @@
# myCounter
My Counter is an implementation of a threaded producer consumer architecture.
In this implementation, the producer (main thread) increments the counter and
the consumer (spawned thread) prints out the changes to myCount. Some status
updates about the locking, waiting, and signaling of threads and mutexes are
printed to the output as well.
## Compiling
You can compile this program by using the make command or by hand.
Simply type:
```
make
```
or, to manually compile, type:
```
gcc -std=c99 main.c -lpthread -o myCounter
```
## Running
To run, type:
```
make run
```
or
```
./myCounter
```
to run manually