-
Notifications
You must be signed in to change notification settings - Fork 0
/
decryption_client.c
154 lines (121 loc) · 2.98 KB
/
decryption_client.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
#include "protocol.h"
#include "util.h"
struct timespec ts_start;
struct timespec ts_end;
double s_elapsed;
void perform_actions(int sock,char *input_file)
{
char buffer[169] = {0};
char output_file[300] = {0};
strcpy(output_file,input_file);
strcat(output_file,".decrypted");
//printf("Output_file: %s\n",output_file);
while(1)
{
memset(buffer,0,169);
int x = receive_msg(sock,buffer);
switch(x)
{
case HANDSHAKE:
{
//printf("Server handshake has arrived on client\n");
send_handshake_response(sock);
FILE *fd = fopen(input_file,"r");
FILE *foutput = fopen(output_file,"w");
// Here come all the requests.
while(read_line(fd, buffer))
{
//printf("%s\n",buffer);
send_decryption_request(sock,buffer,strlen(buffer));
x = receive_msg(sock,buffer);
if(x != RESPONSE_MESSAGE)
{
perror("Wrong server response");
break;
}
int msg_len = *((int *)(buffer+sizeof(int)));
for(int i = 0; i < msg_len; i++)
{
// printf("%c",buffer[i+2*sizeof(int)]);
fprintf(foutput,"%c",buffer[i+2*sizeof(int)]);
} fprintf(foutput,"\n");
}
fclose(foutput);
fclose(fd);
send_end_of_request(sock);
return;
break;
}
// case RESPONSE_MESSAGE:
// {
// printf("Server response message has arrived on client\n");
// dump_payload(buffer);
// break;
// }
case ERROR_MESSAGE:
{
printf("Error message has arrived on client\n");
dump_payload(buffer);
return;
break;
}
default:
{
printf("Unknown %d msg type has arrived on client\n",x);
char aux[] = "Unknown msg type has arrived on client";
send_error_message(sock,aux,strlen(aux));
return;
break;
}
}
} // End while
}
int main(int argc, char **argv)
{
if(argc < 4)
{
printf("Usage: ./decryption_client [ServerHost] [ServerPort] [FilePath]\n");
return 0;
}
//printf("argc %d\n",argc);
struct sockaddr_in serv_addr;
for(int k = 0; k < argc-3; k++)
{
int sockfd[argc-3], portno;
struct hostent *server;
char host[30];
strcpy(host,argv[1]);
portno = atoi(argv[2]);
char input_file[300];
strcpy(input_file,argv[3+k]);
sockfd[k] = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd[k] < 0)
{
char err_msg[30] = "ERROR opening socket";
perror(err_msg);
}
server = gethostbyname(host);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy
(
(char *)&serv_addr.sin_addr.s_addr,
(char *)server->h_addr,
server->h_length
);
serv_addr.sin_port = htons(portno);
int k = connect(sockfd[k],(struct sockaddr *)&serv_addr,sizeof(serv_addr));
if (k < 0)
{
char err_msg[30] = "ERROR connecting";
perror(err_msg);
}
perform_actions(sockfd[k],input_file);
close(sockfd[k]);
}
return 0;
}