-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
184 lines (156 loc) · 4.76 KB
/
client.c
File metadata and controls
184 lines (156 loc) · 4.76 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
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
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
void errorCheck(int n, char *message);
char *get_hash(char *string);
char *get_string1(char *hashstring);
char *get_string2(char *hashstring);
int main(int argc, char *argv[])
{
int sockfd, portno, n;
char downloadDir[256];
char username[25];
char currentMessage[10];
char *command = (char *) malloc(100);
char *message = (char *) malloc(100);
char *hash = (char *) malloc(100);
char *backup = (char *) malloc(100);
char filepath[256];
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc <4) {
fprintf(stderr,"usage %s hostname port work_directory\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
printf("--> Creating socket file descriptor\n");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
server = gethostbyname(argv[1]);
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;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
/* Now connect to the server */
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR connecting");
exit(1);
}
/* Now LOGIN <username> to server
*/
bzero(buffer, 256);
strcpy(username, "dhoni");
strcpy(buffer, "LOGIN ");
strcpy(currentMessage, buffer);
strcat(buffer, username);
/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer) * sizeof(char));
errorCheck(n, "Sending LOGIN to server");
printf("--> Message sent to server: %s\n", buffer);
/* Now read server response */
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
errorCheck(n, "Reading server response for LOGIN");
printf("--> Message received from server: %s\n", buffer);
strcpy(backup, buffer);
command = get_string1(buffer);
if (strcmp(command, "NEEDAUTH") != 0) {
printf("Invalid message from server, expecting NEEDAUTH\n");
exit(1);
}
message = get_string2(backup);
strcpy(backup, message);
hash = get_hash(message);
hash = get_string1(hash);
printf("--> Hashed password: %s, Plain password: %s\n", hash, backup);
/* Writing back to server */
bzero(buffer, 256);
strcpy(buffer, "AUTH ");
strcat(hash, backup);
strcat(buffer, hash);
n = write(sockfd, buffer, strlen(buffer));
errorCheck(n, "Writing hashed password to server");
printf("--> Message sent to server: %s\n", buffer);
/* Get confirmation from server */
bzero(buffer, 256);
n = read(sockfd, buffer, 32);
errorCheck(n, "Reading confirmation from server");
printf("--> Message received from server: %s\n", buffer);
/* Request file to server */
n = write(sockfd, "GETFILE details.txt", 32);
errorCheck(n, "Requesting file from server");
printf("--> Message sent server: %s\n", buffer);
/* Receive file from server */
bzero(buffer, 256);
n = read(sockfd, buffer, sizeof(buffer));
errorCheck(n, "Reading file contents from server");
printf("--> Message received from server: %s\n", buffer);
/* checking if the file present in server using server response*/
bzero(message, 32);
bzero(backup, strlen(buffer));
strcpy(backup, buffer);
message = get_string1(buffer);
if (strcmp(command, "DATA") != 0) {
printf("ERROR message from server, expecting DATA\n");
exit(1);
}
/* write data into file */
strcpy(filepath, argv[3]);
strcat(filepath, "/");
strcat(filepath, "details.txt");
FILE *fp = fopen(filepath, "w");
printf("--> Writing the received data into file, if any file with name, it will be replaced...\n");
fwrite(backup, sizeof(char), strlen(backup), fp);
printf("--> Write done into %s file\n", filepath);
fclose(fp);
return 0;
}
void errorCheck(int n, char *message){
if (n < 0)
{
printf("ERROR in %s \n", message);
exit(1);
}
}
char *get_hash(char *string){
char command[50];
char *s = (char *) malloc(10);
FILE *fp;
strcpy(command, "echo -n ");
strcat(command, string);
strcat(command, " | md5sum");
fp = popen(command, "r");
bzero(string, 32);
while(fgets(s, sizeof(s), fp) != 0)
{
strcat(string, s);
}
pclose(fp);
return string;
}
char *get_string1(char *string){
char *substring;
char *search = " ";
substring = strtok(string, search);
return substring;
}
char *get_string2(char *string){
char *substring;
char *search = " ";
substring = strtok(string, search);
substring = strtok(NULL, search);
return substring;
}