-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
263 lines (217 loc) · 5.75 KB
/
server.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/******************************************************************************
*
* FILENAME:
* server.c
*
* DESCRIPTION:
* A server to transmit file via datagram socket(UDP).
*
* REVISION(MM/DD/YYYY):
* 05/06/2015 Shengkui Leng ([email protected])
* - Initial version
*
******************************************************************************/
#include <unistd.h>
#include <libgen.h>
#include <signal.h>
#include "dsc.h"
volatile sig_atomic_t loop_flag = 1;
static uint32_t g_sequence_number = 0;
static FILE *g_fp = NULL;
/*
* Save data to file
*/
dsc_command_t *cmd_save_data(dsc_command_t *req)
{
dsc_command_t *res;
dsc_request_send_data_t *req_data = (dsc_request_send_data_t *)req;
int rc = STATUS_SUCCESS;
//printf("CMD_SEND_DATA\n");
//printf("SEQ number: %d\n", req_data->common.seq_no);
do {
if (g_sequence_number != req_data->common.seq_no) {
printf("Invalid SEQ number\n");
rc = STATUS_INVALID_SEQ_NO;
break;
}
int len = req_data->common.data_len;
if (len == 0) {
/* The end of file, close it. */
printf("File saved\n");
fclose(g_fp);
g_fp = NULL;
break;
}
int bytes = fwrite(req_data->data, 1, len, g_fp);
if (bytes < len) {
perror("write file error");
rc = STATUS_WRITE_FILE_ERROR;
break;
}
printf("Write data: %d\n", bytes);
g_sequence_number++;
} while (0);
res = (dsc_command_t *)malloc(sizeof(dsc_command_t));
if (res != NULL) {
res->status = rc;
res->seq_no = req_data->common.seq_no;
res->data_len = 0;
}
return (dsc_command_t *)res;
}
/*
* Start a new file transmition
*/
dsc_command_t *cmd_start(dsc_command_t *req)
{
dsc_command_t *res;
dsc_request_start_t *req_data = (dsc_request_start_t *)req;
int rc = STATUS_SUCCESS;
//printf("CMD_START\n");
printf("File: %s\n", basename(req_data->filename));
if (g_fp) {
fclose(g_fp);
g_fp = NULL;
}
g_fp = fopen(basename(req_data->filename), "w");
if (NULL == g_fp) {
perror("open file error");
rc = STATUS_OPEN_FILE_ERROR;
}
g_sequence_number = req_data->common.seq_no + 1;
res = (dsc_command_t *)malloc(sizeof(dsc_command_t));
if (res != NULL) {
res->status = rc;
res->seq_no = req_data->common.seq_no;
res->data_len = 0;
}
return (dsc_command_t *)res;
}
/*
* Unknown request type
*/
dsc_command_t *cmd_unknown(dsc_command_t *req)
{
dsc_command_t *res;
printf("Unknown request type\n");
res = (dsc_command_t *)malloc(sizeof(dsc_command_t));
if (res != NULL) {
res->status = STATUS_INVALID_COMMAND;
res->data_len = 0;
}
return res;
}
/*
* The handler to process all requests from client
*/
dsc_command_t *my_request_handler(dsc_command_t *req)
{
dsc_command_t *resp = NULL;
switch (req->command) {
case CMD_SEND_DATA:
resp = cmd_save_data(req);
break;
case CMD_START:
resp = cmd_start(req);
break;
default:
resp = cmd_unknown(req);
break;
}
return resp;
}
/*
* When user press CTRL+C, quit the server process.
*/
void handler_sigint(int sig)
{
loop_flag = 0;
}
void install_sig_handler()
{
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = handler_sigint;
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);
}
/******************************************************************************
* NAME:
* print_usage
*
* DESCRIPTION:
* Print usage information and exit the program.
*
* PARAMETERS:
* pname - The name of the program.
*
* RETURN:
* None
******************************************************************************/
void print_usage(char *pname)
{
printf("\n"
"===================================================\n"
" Server to transmit file via datagram socket \n"
" v%d.%d \n"
"===================================================\n"
"\n"
"Usage: %s [-p port_number]\n"
"\n"
"Options:\n"
" -p port_number The port number of server, default: %d\n"
"\n"
"Example:\n"
" %s -p 9000\n"
"\n",
VERSION_MAJOR, VERSION_MINOR,
pname, SERVER_PORT, pname
);
exit(STATUS_ERROR);
}
int main(int argc, char *argv[])
{
dsc_server_t *s;
char *pname = argv[0];
int serv_port = SERVER_PORT;
int opt;
while ((opt = getopt(argc, argv, ":hp:")) != -1) {
switch (opt) {
case 'p':
serv_port = strtol(optarg, NULL, 10);
if (serv_port <= 0) {
printf("Error: invalid port number!\n");
print_usage(pname);
}
break;
case 'h':
print_usage(pname);
break;
case ':':
printf("Error: option '-%c' needs a value\n", optopt);
print_usage(pname);
break;
case '?':
default:
printf("Error: invalid option '-%c'\n", optopt);
print_usage(pname);
break;
}
}
if (optind < argc) {
printf("Error: invalid argument '%s'\n", argv[optind]);
print_usage(pname);
}
printf("Server listening on port %d\n", serv_port);
s = server_init(&my_request_handler, serv_port);
if (s == NULL) {
printf("Error: server init error\n");
return STATUS_INIT_ERROR;
}
install_sig_handler();
while (loop_flag) {
server_accept_request(s);
}
server_close(s);
return STATUS_SUCCESS;
}