-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
194 lines (172 loc) · 5.01 KB
/
Copy pathclient.c
File metadata and controls
194 lines (172 loc) · 5.01 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
#include"header.h"
int sock_fd = -1;
/**
* Show usage of client file
*/
void usage(char*prog_name);
/**
* Find file in server
* @param hostname hostname/ip of server
* @param filename filename to find
*/
void sendFind(const char*hostname, const char*filename);
/**
* Send file to server
* @param hostname hostname/ip of server
* @param filepath filepath of file to send
*/
void sendFile(const char*hostname, const char*filepath);
/**
* Handle cmdline options
* @param argc Number of options
* @param argv Options
*/
void checkOpts(int argc, char**argv);
/**
* Extract filename from filepath
* @param filepath Filepath to extract from
* @param buf Buffer to place extracted filename
*/
void getFilename(const char*filepath, char*buf);
int main(int argc, char** argv) {
signal(SIGINT, cleanup);
checkOpts(argc, argv);
return (EXIT_SUCCESS);
}
void sendFile(const char*hostname, const char*filepath) {
char srv[BUF_MAX], msg[BUF_MAX];
memset(msg, '\0', sizeof (msg));
msg[0] = SAVE_FILE;
FILE*fd = fopen(filepath, "rb");
if (fd == NULL) {
fprintf(stderr, "file open error : ");
return;
}
char filename[FILENAME_MAX];
getFilename(filepath, filename);
strncat(msg, filename, FILENAME_MAX);
strcat(msg, "\n");
int rc = 0;
while (((rc = fgetc(fd)) != EOF) && (strlen(msg) < BUF_MAX - 3)) {
msg[(int) strlen(msg)] = (char) rc;
}
fclose(fd);
if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket creation error");
return;
}
struct sockaddr_in inet_socket;
inet_socket.sin_family = AF_INET;
inet_socket.sin_port = htons(PORT);
inet_pton(AF_INET, hostname, &inet_socket.sin_addr);
if (connect(sock_fd, (struct sockaddr*) &inet_socket, sizeof (inet_socket)) != 0) {
perror("Error connecting to server");
return;
}
if (send(sock_fd, msg, strlen(msg), 0) < 0) {
perror("Send error");
}
memset(msg, '\0', sizeof (msg));
if (recv(sock_fd, srv, sizeof (srv) - 1, 0) <= 0) {
perror("Receive error");
} else {
printf("%s\n", srv);
memset(srv, '\0', sizeof (srv));
}
}
void sendFind(const char*hostname, const char*filename) {
if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket creation error");
return;
}
struct sockaddr_in inet_socket;
inet_socket.sin_family = AF_INET;
inet_socket.sin_port = htons(PORT);
inet_pton(AF_INET, hostname, &inet_socket.sin_addr);
if (connect(sock_fd, (struct sockaddr*) &inet_socket, sizeof (inet_socket)) != 0) {
perror("Error connecting to server");
return;
}
char srv[BUF_MAX], msg[BUF_MAX];
msg[0] = CHECK_FIND;
strcat(msg, filename);
if (send(sock_fd, msg, strlen(msg), 0) < 0) {
perror("Send error");
}
memset(msg, '\0', sizeof (msg));
if (recv(sock_fd, srv, sizeof (srv) - 1, 0) <= 0) {
perror("Receive error");
} else {
if (strcasecmp(srv, FILE_NOT_FOUND) == 0) {
printf("%s: file not found\n", srv);
} else {
printf("%s\n", srv);
}
memset(srv, '\0', sizeof (srv));
}
}
void cleanup(int sig) {
printf("\n^C caught. Cleaning up...\n");
if (sock_fd > 0)close(sock_fd);
exit(EXIT_SUCCESS);
}
void checkOpts(int argc, char**argv) {
char*hostname = NULL;
char*find = NULL;
char*put = NULL;
while (TRUE) {
const char*short_options = "h:f:p:";
struct option long_options[] = {
{"hostname", required_argument, 0, 'h'},
{"find", required_argument, 0, 'f'},
{"put", required_argument, 0, 'p'},
{0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, short_options, long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'h':
hostname = optarg;
break;
case 'f':
find = optarg;
break;
case 'p':
put = optarg;
break;
default:
printf("Invalid option\n");
usage(argv[0]);
}
}
if (hostname == NULL) {
printf("server host not set\n");
usage(argv[0]);
}
if (find == NULL && put == NULL) {
printf("find / put not set\n");
usage(argv[0]);
}
if (find != NULL) {
sendFind(hostname, find);
} else if (put != NULL) {
sendFile(hostname, put);
}
}
void usage(char*prog_name) {
printf("Usage : %s hostname command [option]\n", prog_name);
printf("command : find => find shit\n");
printf("command : put => put shit\n");
exit(0);
}
void getFilename(const char*filepath, char*buf) {
char*last_sep = strrchr(filepath, FILE_SEPARATOR);
if (last_sep == NULL) {
strncpy(buf, filepath, MAX_FILNAME_SIZE - 2);
} else {
strncpy(buf, ++last_sep, MAX_FILNAME_SIZE - 2);
}
}