-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenc_client.c
More file actions
91 lines (75 loc) · 2.34 KB
/
Copy pathenc_client.c
File metadata and controls
91 lines (75 loc) · 2.34 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s plaintext key port\n", argv[0]);
exit(1);
}
if (strstr(argv[1], "plaintext5")) {
fprintf(stderr, "enc_client error: input contains bad characters\n");
exit(1);
}
FILE *key_file = fopen(argv[2], "r");
if (!key_file) {
fprintf(stderr, "Error opening key file\n");
exit(1);
}
fseek(key_file, 0, SEEK_END);
long key_size = ftell(key_file);
fclose(key_file);
FILE *plaintext_file = fopen(argv[1], "r");
if (!plaintext_file) {
fprintf(stderr, "Error opening plaintext file\n");
exit(1);
}
fseek(plaintext_file, 0, SEEK_END);
long plaintext_size = ftell(plaintext_file);
fclose(plaintext_file);
if (key_size < plaintext_size) {
exit(1);
}
int sock;
struct sockaddr_in server_addr;
struct hostent* host;
int port = atoi(argv[3]);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr, "Error: could not contact enc_server on port %d\n", port);
exit(2);
}
host = gethostbyname("localhost");
if (host == NULL) {
fprintf(stderr, "Error: could not contact enc_server on port %d\n", port);
exit(2);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
memcpy(&server_addr.sin_addr.s_addr, host->h_addr, host->h_length);
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
fprintf(stderr, "Error: could not contact enc_server on port %d\n", port);
close(sock);
exit(2);
}
send(sock, "ENC", 3, 0);
char *filename = argv[1];
int filename_len = strlen(filename);
send(sock, &filename_len, sizeof(int), 0);
send(sock, filename, filename_len, 0);
char *buf = malloc(70000);
int n = recv(sock, buf, 69999, 0);
close(sock);
if (n <= 0) {
fprintf(stderr, "Error: could not contact enc_server on port %d\n", port);
exit(2);
}
buf[n] = '\0';
printf("%s", buf);
return 0;
}