-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp_dec_d.c
334 lines (316 loc) · 10 KB
/
otp_dec_d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
* otp_dec_d.c
*
* Author: Gregory Mankes
* Takes a file sent over a socket, sends it back to the client decrypted
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
/*******************************************************************************
* struct addrinfo * create_address_info(char*)
*
* creates a pointer to an address info linked list with port
* Args: two strings: the address and port number
* Returns: An address info linked list
******************************************************************************/
struct addrinfo * create_address_info(char * port){
int status;
struct addrinfo hints;
struct addrinfo * res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((status = getaddrinfo(NULL, port, &hints, &res)) != 0){
fprintf(stderr,
"getaddrinfo error: %s\nDid you enter the correct IP/Port?\n",
gai_strerror(status));
exit(1);
}
return res;
}
/*******************************************************************************
* int create_socket(struct addrinfo *)
*
* Creates a socket from an address info linked list
* Args: The address info linked list
* Returns: a socket file descriptor
******************************************************************************/
int create_socket(struct addrinfo * res){
int sockfd;
if ((sockfd = socket((struct addrinfo *)(res)->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
fprintf(stderr, "Error in creating socket\n");
exit(1);
}
return sockfd;
}
/*******************************************************************************
* void connect_socket(int, struct addrinfo *)
*
* Connects the socket to the address specified in the address info linked list
* Args: a socket file descriptor and an address info linked list
******************************************************************************/
void connect_socket(int sockfd, struct addrinfo * res){
int status;
if ((status = connect(sockfd, res->ai_addr, res->ai_addrlen)) == -1){
fprintf(stderr, "Error in connecting socket\n");
exit(1);
}
}
/*******************************************************************************
* void bind_socket(int, struct addrinfo *)
*
* Binds the socket to a port
* Args: a socket file descriptor and an address info linked list
******************************************************************************/
void bind_socket(int sockfd, struct addrinfo * res){
if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) {
close(sockfd);
fprintf(stderr, "Error in binding socket\n");
exit(1);
}
}
/*******************************************************************************
* void listen_socket(int)
*
* listens on the bound port
* Args: a socket file descriptor
******************************************************************************/
void listen_socket(int sockfd){
if(listen(sockfd, 5) == -1){
close(sockfd);
fprintf(stderr, "Error in listening on socket\n");
exit(1);
}
}
/*******************************************************************************
* void send_file(int, char *, int)
*
* Sends a file over a socket
* Args: a socket file descriptor, a string and the length of the string
******************************************************************************/
void send_file(int new_fd, const char * message, int message_length){
// keep track of the loop var and the bytes wrote
int nwrote = 0;
int i = 0;
// begin sending the file back
for (; i < message_length; i+=nwrote){
nwrote = write(new_fd, message, message + i);
if(nwrote < 0){
fprintf(stderr, "Error in writing to socket\n");
_Exit(2);
}
}
// receive the done response
char buff[20];
memset(buff, 0, sizeof(buff));
recv(new_fd, buff, sizeof(buff), 0);
}
/*******************************************************************************
* int handshake(int)
*
* Completes a handshake with a client of the same type
* Args: a socket file descriptor
******************************************************************************/
int handshake(int new_fd){
char buffer[20];
memset(buffer, 0, sizeof(buffer));
// receive the name of the client
recv(new_fd, buffer, sizeof(buffer),0);
// accept clients of the same type
if(strcmp(buffer, "opt_dec") == 0){
return 1;
}
return 0;
}
/*******************************************************************************
* char * recv_file(int, int)
*
* Receives a file of a specified size and returns its contents in a string
* Args: a socket file descriptor and a message length
******************************************************************************/
char * recv_file(int new_fd, int message_length){
// keep track of the loop var and bytes read
int nread = 0;
int i = 0;
// allocate a string for the file
char * to_receive = malloc(message_length * sizeof(char));
memset(to_receive, '\0', sizeof(to_receive));
// begin receiving the file
for(; i< message_length; i+= nread){
nread = read(new_fd, to_receive + i, message_length -i);
if(nread < 0){
fprintf(stderr, "Error in receiving file\n");
_Exit(2);
}
}
// echo finished response
char * finished = "opt_enc_d f";
send(new_fd, finished, strlen(finished),0);
return to_receive;
}
/*******************************************************************************
* void decrypt_message(char *, char *, int)
*
* decrypts a file with a specified key
* Args: the file as a string, the key, and the message length
******************************************************************************/
void decrypt_message(char * message, char * key, int message_length){
int i = 0;
int message_num;
int key_num;
int result_num;
int alphabet = 27; // because of newline
for (; i < message_length; i++){
// ignore newlines
if (message[i] != '\n'){
// convert the characters of key[i] and message[i]
// to the range of 0 to 26
if(message[i] == ' '){
message_num = 26;
}
else{
message_num = message[i] -'A';
}
if(key[i] == ' '){
key_num = 26;
}
else{
key_num = key[i] - 'A';
}
// once converted, add them and mod by the alphabet
result_num = (message_num - key_num + alphabet) % alphabet;
// place the result in the string.
if(result_num == 26){
message[i] = ' ';
}
else{
message[i] = 'A' + (char)result_num;
}
}
}
}
/*******************************************************************************
* void handle_request(int)
*
* Handles the request from the client
* Args: the newly created socket from the request
******************************************************************************/
void handle_request(int new_fd){
int correct_client = handshake(new_fd);
if (!correct_client){
fprintf(stderr, "Invalid Client\n");
char invalid[] = "Invalid";
send(new_fd, invalid, strlen(invalid),0);
_Exit(2);
}
char valid[] = "Valid";
send(new_fd, valid, strlen(valid), 0);
// get the length of how long the file is
char buffer[10];
memset(buffer, 0, sizeof(buffer));
recv(new_fd, buffer, sizeof(buffer), 0);
int message_length = atoi(buffer);
// send the length of the file back
send(new_fd, buffer, strlen(buffer),0);
// get the length of the key
memset(buffer, 0, sizeof(buffer));
recv(new_fd, buffer, sizeof(buffer), 0);
int key_length = atoi(buffer);
// send the length of the key back
send(new_fd, buffer, strlen(buffer),0);
// get the message
char * message = recv_file(new_fd, message_length);
// get the key
char * key = recv_file(new_fd, key_length);
decrypt_message(message, key, message_length);
// send back the file
send_file(new_fd, message, message_length);
// free the key and message
free(message);
free(key);
exit(0);
}
/*******************************************************************************
* void wait_for_connection
*
* waits for a new connection to the server
* Args: the file descriptor to wait on
******************************************************************************/
void wait_for_connection(int sockfd){
// create a container for the connection
struct sockaddr_storage their_addr;
// create a size for the connection
socklen_t addr_size;
// create a new file descriptor for the connection
int new_fd;
// status variable
int status;
// pid variable;
pid_t pid;
// run forever
while(1){
// get the address size
addr_size = sizeof(their_addr);
// accept a new client
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
// if there is no new client keep waiting
if(new_fd == -1){
fprintf(stderr, "Error in accepting connection\n");
continue;
}
// fork to let a new process handle the new socket
pid = fork();
// if there was an error, say so
if(pid == -1){
fprintf(stderr, "Error in fork\n");
}
else if(pid == 0){
// child process
close(sockfd);
handle_request(new_fd);
close(new_fd);
}
else{
// parent process
close(new_fd);
while (pid > 0){
pid = waitpid(-1, &status, WNOHANG);
}
}
}
}
/*******************************************************************************
* int main(int, char*)
*
* main method. sets up server socket, command line args and calls
* wait_for_connection
* Args: the command lin args
******************************************************************************/
int main(int argc, char *argv[]){
if(argc != 2){
fprintf(stderr, "Invalid number of arguments\n");
exit(1);
}
printf("Server open on port %s\n", argv[1]);
// create address info with the port number
struct addrinfo * res = create_address_info(argv[1]);
// create socket with this address info
int sockfd = create_socket(res);
// bind this socket to the port
bind_socket(sockfd, res);
// listen on the port
listen_socket(sockfd);
// wait for up to 5 incoming connections
wait_for_connection(sockfd);
// clean up
freeaddrinfo(res);
}