-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
238 lines (199 loc) · 5.1 KB
/
client.c
File metadata and controls
238 lines (199 loc) · 5.1 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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#define BUFFER_SIZE 1024
#define ERROR 0
#define SUCCESS 1
#define AUTH 2
#define GET 3
#define PUT 4
#define EXIT 5
int port = 7777;
int sock_fd = -1;
char *line = NULL;
FILE *fp = NULL;
// Close file descriptors and exit
void cleanup(int n) {
if (sock_fd > 0) close(sock_fd);
if (line != NULL) free(line);
if (fp != NULL) fclose(fp);
exit(n);
}
void texit() {
// not authenticated
if (sock_fd < 0) return;
uint8_t type = EXIT;
write(sock_fd, &type, sizeof(type));
close(sock_fd);
sock_fd = -1;
}
void handler(int signum) {
if (signum == SIGINT ||
signum == SIGTERM ||
signum == SIGQUIT) {
texit();
cleanup(0);
}
}
void tconnect(char *addr_str, char *user, char *pass) {
uint8_t type;
uint32_t len;
int ret;
struct sockaddr_in server_addr;
// set server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
ret = inet_pton(AF_INET, addr_str, &server_addr.sin_addr);
if (ret == 0) return;
// create socket
sock_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sock_fd < 0) {
printf("error create socket %d <%s>\n", errno, strerror(errno));
cleanup(1);
}
ret = connect(sock_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
if (ret < 0) {
printf("error connect %d <%s>\n", errno, strerror(errno));
cleanup(1);
}
// auth
type = AUTH;
write(sock_fd, &type, sizeof(type));
len = strlen(user);
len = htonl(len);
write(sock_fd, &len, sizeof(len));
write(sock_fd, user, strlen(user));
len = strlen(pass);
len = htonl(len);
write(sock_fd, &len, sizeof(len));
write(sock_fd, pass, strlen(pass));
// response
read(sock_fd, &type, sizeof(type));
if (type != SUCCESS) texit();
}
void tget(char *fname) {
// not authenticated
if (sock_fd < 0) return;
char buffer[BUFFER_SIZE+1];
memset(buffer, 0, BUFFER_SIZE+1);
// send command
uint8_t type = GET;
uint32_t len = strlen(fname);
len = htonl(len);
write(sock_fd, &type, sizeof(type));
write(sock_fd, &len, sizeof(len));
write(sock_fd, fname, ntohl(len));
// get response
read(sock_fd, &type, sizeof(type));
if (type == ERROR) {
printf("error get file\n");
return;
}
read(sock_fd, &len, sizeof(len));
len = ntohl(len);
// open file
fp = fopen(fname, "wb");
if (fp == NULL) {
printf("error open file %d <%s>\n", errno, strerror(errno));
while (len > 0) {
int n = BUFFER_SIZE;
if (len < BUFFER_SIZE) n = len;
n = read(sock_fd, buffer, n);
if (n == -1) printf("error read %d <%s>\n", errno, strerror(errno));
len = len - n;
}
return;
}
// write to file
while (len > 0) {
int n = BUFFER_SIZE;
if (len < BUFFER_SIZE) n = len;
n = read(sock_fd, buffer, n);
if (n == -1) printf("error read %d <%s>\n", errno, strerror(errno));
fwrite(buffer, 1, n, fp);
len = len - n;
}
fclose(fp);
fp = NULL;
}
void tput(char *fname) {
// not authenticated
if (sock_fd < 0) return;
uint8_t type;
uint32_t len, fsize;
// open file
fp = fopen(fname, "rb");
if (fp == NULL) {
printf("error open file %d <%s>\n", errno, strerror(errno));
return;
}
// get file size
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
if (fsize < 0) return;
fseek(fp, 0, SEEK_SET);
// send command
type = PUT;
len = strlen(fname);
len = htonl(len);
write(sock_fd, &type, sizeof(type));
write(sock_fd, &len, sizeof(len));
write(sock_fd, fname, ntohl(len));
// send file
len = htonl(fsize);
write(sock_fd, &len, sizeof(len));
len = fsize;
char buffer[BUFFER_SIZE+1];
memset(buffer, 0, BUFFER_SIZE+1);
while (len > 0) {
int n = BUFFER_SIZE;
if (len < BUFFER_SIZE) n = len;
fread(buffer, 1, n, fp);
int j = write(sock_fd, buffer, n);
if (j == -1) printf("error write %d <%s>\n", errno, strerror(errno));
fseek(fp, j-n, SEEK_CUR);
len = len - j;
}
fclose(fp);
fp = NULL;
}
int main(int argc, char *argv[]) {
// Set up signal handler
struct sigaction act;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGINT, &act, NULL) < 0)
printf("error sigaction SIGINT %d <%s>\n", errno, strerror(errno));
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, stdin)) != -1) {
line[--nread] = 0;
int i;
int args = 0;
int offsets[3];
for (i = 0; i < nread; i++) {
if (line[i] == ' ') {
line[i] = 0;
offsets[args++] = i+1;
if (args >= 3) break;
}
}
if (strcmp(line, "connect") == 0 && args == 3) tconnect(line+offsets[0], line+offsets[1], line+offsets[2]);
else if (strcmp(line, "get") == 0 && args == 1) tget(line+offsets[0]);
else if (strcmp(line, "put") == 0 && args == 1) tput(line+offsets[0]);
else if (strcmp(line, "exit") == 0) {
if (sock_fd > 0) texit();
else break;
}
}
cleanup(0);
return 0;
}