0%

简单服务器Demo3

基础信息

为了完成工程实践项目写的测试程序

目前已经完成了:目录切换,日志文件,MD5文件验证

客户端

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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<netinet/in.h>
#include "md5.h"

#define MAXLINE 4096
#define PORT 2345

char rev[0x1000];
unsigned char digest[16] = {0};
MD5_CTX context;

void showProgress(int size, int len){
if(size == 0){
size = 1;
}
if(len == 0){
len = 1;
}

for(int i=0;i<0x42;i++){
printf("\b");
}
printf("[");
for(int i=0;i<0x40;i++){
if(i <= (len*0x40/size)){
printf("#");
}
else if(i > (len*0x40/size)){
printf("-");
}
}
printf("]");
}

void doPull(char* filename, int sockfd, int size){
int n,len,i;
char rev[0x1000];
FILE* fp;
fp = fopen(filename,"w");
len = 0;
while(1){
memset(rev,0,sizeof(rev));
n = recv(sockfd, rev, sizeof(rev), 0);
if(n < 0){
break;
}
fwrite(rev, 1, n, fp);
len += sizeof(rev);
showProgress(size/0x1000,len/0x1000);
}
printf("\n");
fflush(fp);
fclose(fp);
}

void doPush(char* filename, int sockfd){
int n,i,size,len,key;
char sed[0x1000];
char rev[0x80];
char temp[0x80];
struct stat st;
char* data;
FILE* fp;

sprintf(sed,"./%s",filename);
if(stat(sed,&st)<0){
printf("File %s not find\n",filename);
if(send(sockfd, "File not find", 13, 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
return;
}
else{
printf("Find the flie %s\n",filename);
strcpy(sed,"Try to upload the flie");
if(send(sockfd, "Try to upload the flie", 22, 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}

fp = fopen(filename,"r");
size = st.st_size;
len = 0;
data = (char*)malloc(size);

if(fp){
memset(sed,0,sizeof(sed));
while(1){
for(i = 0;i<sizeof(sed);i++){
n = fread(&sed[i], 1, 1, fp);
if(n == 0){
break;
}
}
send(sockfd, sed, i, 0);
len += sizeof(sed);
showProgress(size/0x1000,len/0x1000);
if(n == 0){
break;
}
}
}
printf("\n");

MD5Update(&context, data, size);
MD5Final(&context, digest);
printf("md5: ");
for(i = 0; i < 16; i++){
printf("%02x", digest[i]);
sprintf(temp,"%s%02x", temp, digest[i]);
}
printf("\n");

sleep(1);
if(recv(sockfd, rev, sizeof(rev), 0) < 0) {
printf("Something error\n");
}

if(!strcmp(temp, rev)){
printf("The file has been uploaded without data errors\n");
strcpy(sed,"File md5 success");
if(send(sockfd, sed, sizeof(sed), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}
else{
printf("The uploaded file has data errors\n");
strcpy(sed,"File md5 error");
if(send(sockfd, sed, sizeof(sed), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}

free(data);
fflush(fp);
fclose(fp);
}

int main(int argc, char** argv)
{
int sockfd, n, size;
char cmd[0x100], temp[0x80], filename[0x40];
struct sockaddr_in servaddr;
struct sockaddr_in clieaddr;

if(argc != 2){
printf("usage: ./client <ipaddress>\n");
exit(0);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);

struct timeval tv_out;
tv_out.tv_sec = 1;
tv_out.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out));

if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
else{
printf("connect success: %s\n",argv[1]);
}
memset(&clieaddr, 0, sizeof(clieaddr));
clieaddr.sin_family = AF_INET;
clieaddr.sin_port = htons(PORT);
clieaddr.sin_addr.s_addr = htons(INADDR_ANY);

MD5Init(&context);

while(1){
write(1,"$ ",0x2);
n = read(0,cmd,0x100);
cmd[n-1] = '\0';
if(strlen(cmd) == 0){
continue;
}
if(send(sockfd, cmd, strlen(cmd), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(rev,0,sizeof(rev));
if(recv(sockfd, rev, sizeof(rev), 0) < 0) {
continue;
}
write(1,rev,strlen(rev));
if(!strcmp("The connection has been closed\n",rev)){
puts("Client closed");
break;
}
else if(!strncmp("Start download file: ",rev,20)){
strcpy(filename,&rev[21]);
strtok(filename,"-");
size = atoi(strtok(NULL,"-"));
printf("Target file: %s(%d)\n",filename,size);
doPull(filename,sockfd,size);
printf("The file has been downloaded\n");
}
else if(!strncmp("Try to upload file: ",rev,19)){
strcpy(filename,&rev[20]);
filename[strlen(filename)-1] = 0;
printf("Target file: %s\n",filename);
doPush(filename,sockfd);
}
}

close(sockfd);
exit(0);
}

服务端

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<dirent.h>
#include<pthread.h>
#include<time.h>
#include<signal.h>
#include "md5.h"

#define MAXLINE 4096
#define PORT 6666
#define LISTENQ 1024

MD5_CTX context;
unsigned char digest[16] = {0};

struct info {
pthread_t tidp;
int sockfd;
struct sockaddr_in clieaddr;
};

struct info *clieinfos[0x20];
char rootdir[0x80];
char current[0x80];
char logMsg[0x100];
FILE* lfp;

enum CMD{LS,PULL,PUSH,CD,PWD,HELP,EXIT,NUL};

int printLog(char * msg){
printf(msg);
fprintf(lfp,msg);
}

int getTime(char *buf){
char YMD[15] = {0};
char HMS[10] = {0};
time_t current_time;
struct tm* now_time;
char *cur_time = (char *)malloc(0x20);

time(&current_time);
now_time = localtime(&current_time);

strftime(YMD, sizeof(YMD), "%F ", now_time);
strftime(HMS, sizeof(HMS), "%T", now_time);

memset(cur_time,0,sizeof(cur_time));
strncat(cur_time, YMD, 11);
strncat(cur_time, HMS, 8);
strcpy(buf,cur_time);

free(cur_time);
}

long getFileSize(FILE *stream)
{
long file_size = -1;
long cur_offset = ftell(stream);
if (cur_offset == -1) {
sprintf(logMsg,"ftell failed :%s\n", strerror(errno));
printLog(logMsg);
return -1;
}
if (fseek(stream, 0, SEEK_END) != 0) {
sprintf(logMsg,"fseek failed: %s\n", strerror(errno));
printLog(logMsg);
return -1;
}
file_size = ftell(stream);
if (file_size == -1) {
sprintf(logMsg,"ftell failed :%s\n", strerror(errno));
printLog(logMsg);
}
if (fseek(stream, cur_offset, SEEK_SET) != 0) {
sprintf(logMsg,"fseek failed: %s\n", strerror(errno));
printLog(logMsg);
return -1;
}
return file_size;
}

int cmdChange(char* buf){
if(!strncmp("ls",buf,2) || !strncmp("LS",buf,2)){
return LS;
}else if(!strncmp("pull",buf,4) || !strncmp("PULL",buf,4)){
return PULL;
}else if(!strncmp("push",buf,4) || !strncmp("PUSH",buf,4)){
return PUSH;
}else if(!strncmp("cd",buf,2) || !strncmp("CD",buf,2)){
return CD;
}else if(!strncmp("pwd",buf,3) || !strncmp("PWD",buf,3)){
return PWD;
}else if(!strncmp("help",buf,4) || !strncmp("HELP",buf,4)){
return HELP;
}else if(!strncmp("exit",buf,4) || !strncmp("EXIT",buf,4)){
return EXIT;
}else{
return NUL;
}
}

int cmdHander(int index){
int n,i,cmd,size;
int connfd;
FILE* fp;
char buf[0x80], info[0x1000], temp[0xa0], time[0x20], dir[0x80];
char* filename;
char* dirname;
char* dirpart;
char* data;
struct stat st;
struct sockaddr_in clieaddr;

connfd = clieinfos[index]->sockfd;
memcpy(&clieaddr, &clieinfos[index]->clieaddr,sizeof(clieaddr));
getTime(time);
sprintf(logMsg,"connect time: %s\n", time);
printLog(logMsg);

while(1){
memset(buf,0,sizeof(buf));
memset(info,0,sizeof(info));
memset(temp,0,sizeof(temp));
if((n = recv(connfd, buf, MAXLINE, 0)) < 0){
continue;
}
buf[n] = '\0';
sprintf(logMsg,"client cmd:%s\n", buf);
printLog(logMsg);
cmd = cmdChange(buf);

switch(cmd){
case LS:
sprintf(temp,"ls %s -l",current);
fp = popen(temp,"r");
if(fp == NULL){
sprintf(logMsg,"popen error\n");
printLog(logMsg);
}
while(fgets(temp, sizeof(temp), fp) > 0){
strcat(info,temp);
}
if(strlen(info) == 0){
sprintf(info,"(null)\n");
}
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return fclose(fp);
}
fclose(fp);
break;
case PULL:
strtok(buf," ");
filename = strtok(NULL," ");
if(filename == NULL){
sprintf(info,"CMD[PULL] need parameter <filename>\n");
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}
snprintf(temp,sizeof(temp),"%s/%s",current,filename);
if(stat(temp,&st)<0){
sprintf(info,"File %s not find\n",filename);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}
size = st.st_size;
sprintf(info,"Start download file: %s-%d\n",filename,size);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
fp = fopen(temp,"r");
while(1){
memset(info,0,sizeof(info));
n = fread(info, 1, sizeof(info), fp);
send(connfd, info, n, 0);
if(n == 0){
break;
}
}
sprintf(logMsg,"client %s:%d had downloaded file %s\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port),filename);
printLog(logMsg);
fclose(fp);
break;
case PUSH:
strtok(buf," ");
filename = strtok(NULL," ");
if(filename == NULL){
sprintf(info,"CMD[PUSH] need parameter <filename>\n");
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}
sprintf(info,"Try to upload file: %s\n",filename);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
memset(info,0,sizeof(info));
if(recv(connfd, info, sizeof(info), 0) < 0) {
sprintf(logMsg,"recv msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
}
if(!strcmp("Try to upload the flie",info)){
snprintf(temp,sizeof(temp),"%s/%s",current,filename);
fp = fopen(temp,"w");
if(!fp){
sprintf(logMsg,"Something error\n");
printLog(logMsg);
}
while(1){
n = recv(connfd, info, sizeof(info), 0);
if(n < 0){
break;
}
fwrite(info, 1, n, fp);
}
fflush(fp);

data = (char*)malloc(getFileSize(fp));
MD5Update(&context, data, getFileSize(fp));
MD5Final(&context, digest);

sprintf(logMsg,"md5: ");
printLog(logMsg);
memset(info,0,sizeof(info));
for(i = 0; i < 16; i++){
sprintf(logMsg,"%02x", digest[i]);
sprintf(info,"%s%02x", info, digest[i]);
printLog(logMsg);
}
sprintf(logMsg,"\n");
printLog(logMsg);

if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}

if(recv(connfd, info, sizeof(info), 0) < 0) {
sprintf(logMsg,"recv msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
}

if(!strcmp(info,"File md5 error")){
sprintf(logMsg,"client %s:%d fail to upload file %s\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port),filename);
printLog(logMsg);
}
else if(!strcmp(info,"File md5 success")){
sprintf(logMsg,"client %s:%d success to upload file %s\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port),filename);
printLog(logMsg);
}

free(data);
fclose(fp);

}
else if(!strcmp("File not find",info)){
sprintf(logMsg,"client %s:%d failed to upload file\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port));
printLog(logMsg);
}
break;
case CD:
strtok(buf," ");
dirname = strtok(NULL," ");
if(dirname == NULL){
sprintf(info,"CMD[CD] need parameter <dirname>\n");
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}

dirpart = strtok(dirname,"/");
n = 0;
memset(dir,0,sizeof(dir));
while(dirpart != NULL){
if(strcmp(dirpart,"..")==0){n++;}
else if(strcmp(dirpart,".")==0){}
else{
snprintf(dir,sizeof(dir),"%s/%s",dir,dirpart);
}
dirpart = strtok(NULL,"/");
}

snprintf(temp,sizeof(temp),"%s%s",current,dir);
while(n > 0){
if(strcmp(temp,rootdir) > 0){
i = strlen(temp);
while(1){
if(temp[i]=='/'){
temp[i--]=0;
break;
}
temp[i--]=0;
}
}
n--;
}

if(stat(temp,&st)<0){
sprintf(info,"Dir %s not find\n",dirname);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}
if(st.st_mode & S_IFREG){
sprintf(info,"The %s is file not dir\n",dirname);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}

strcpy(current,temp);
sprintf(info,"Current dir is %s\n",current);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
case PWD:
sprintf(info,"%s\n",current);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
case HELP:
sprintf(info,"<---------------------------------------->\n");
strcat(info,"1.LS: Show the file you can PULL\n");
strcat(info,"2.PULL <filename>: Download <filename> from the server\n");
strcat(info,"3.PUSH <filename>: Upload <filename> to the server\n");
strcat(info,"4.CD <dirname>: Enter the specified path\n");
strcat(info,"5.PWD: Displays the current path\n");
strcat(info,"6.EXIT: Close the connection to the server\n");
strcat(info,"<---------------------------------------->\n");
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
case EXIT:
sprintf(logMsg,"client %s:%d is closed\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port));
printLog(logMsg);
sprintf(info,"The connection has been closed\n");
getTime(time);
sprintf(logMsg,"closed time: %s\n", time);
printLog(logMsg);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
free(clieinfos[index]);
clieinfos[index] = NULL;
fflush(lfp);
return close(connfd);
case NUL:
sprintf(info,"CMD[%s] is not find, please use CMD[help]\n",buf);
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
default:
sprintf(info,"Something error\n");
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
return -1;
}
break;
}
}
}

int initRoot(){
struct stat st;

getcwd(rootdir, sizeof(rootdir));
strcat(rootdir,"/root");
if(stat(rootdir,&st)<0){
printf("The rootdir %s not find\n",rootdir);
printf("Creating %s now\n",rootdir);
mkdir(rootdir,0777);
}
else{
printf("The rootdir is %s\n",rootdir);
}
strcpy(current,rootdir);
}

int initLog(){
char buf[0x40];
char time[0x20];
char logname[0x100];

getTime(time);
sprintf(buf,"Current time: %s\n", time);
time[10] = 0;
sprintf(logname,"%s/log.%s",rootdir,time);

lfp = fopen(logname,"a+");
if(lfp == NULL){
printf("create log error\n");
exit(0);
}
printLog(buf);
}

int getID(){
for(int i=0;i<0x20;i++){
if(clieinfos[i] == NULL){
clieinfos[i] = (struct info*)malloc(sizeof(struct info));
return i;
}
}
return -1;
}

int main(int argc, char** argv)
{
int listenfd, connfd, i;
struct sockaddr_in servaddr;
struct sockaddr_in listendAddr;
struct sigaction sa;
char ipAddr[INET_ADDRSTRLEN];
char info[0x100];
int len;

initRoot();
initLog();
MD5Init(&context);

sa.sa_handler = SIG_IGN;
signal(SIGCHLD,SIG_IGN);
sigaction(SIGPIPE, &sa, 0);

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
sprintf(logMsg,"create socket error: %s(errno: %d)\n",strerror(errno),errno);
printLog(logMsg);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

struct timeval tv_out;
tv_out.tv_sec = 1;
tv_out.tv_usec = 0;
setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out));

int opt = 1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
{
sprintf(logMsg,"bind socket error: %s(errno: %d)\n",strerror(errno),errno);
printLog(logMsg);
exit(0);
}
if(listen(listenfd, LISTENQ) == -1)
{
sprintf(logMsg,"listen socket error: %s(errno: %d)\n",strerror(errno),errno);
printLog(logMsg);
exit(0);
}

len = sizeof(listendAddr);
if(getsockname(listenfd, (struct sockaddr *)&listendAddr, &len) == -1){
sprintf(logMsg,"getsockname error\n");
printLog(logMsg);
exit(0);
}
sprintf(logMsg,"listen address = %s:%d\n", inet_ntoa(listendAddr.sin_addr), ntohs(listendAddr.sin_port));
printLog(logMsg);

while(1)
{
if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
continue;
}
if(i = getID() < 0){
sprintf(info,"The thread is full, please wait\n");
if(send(connfd, info, strlen(info), 0) < 0) {
sprintf(logMsg,"send msg error: %s(errno: %d)\n", strerror(errno), errno);
printLog(logMsg);
}
}
clieinfos[i]->sockfd = connfd;
len = sizeof(clieinfos[i]->clieaddr);
if(getpeername(connfd, (struct sockaddr *)&(clieinfos[i]->clieaddr), &len) == -1){
sprintf(logMsg,"getpeername error");
printLog(logMsg);
exit(0);
}
sprintf(logMsg,"client %s:%d try to connect\n", inet_ntop(AF_INET, &clieinfos[i]->clieaddr.sin_addr, ipAddr, sizeof(ipAddr)), ntohs(clieinfos[i]->clieaddr.sin_port));
printLog(logMsg);
if (pthread_create(&(clieinfos[i]->tidp) , NULL, cmdHander, i)){
sprintf(logMsg,"pthread_create error!\n");
printLog(logMsg);
return -1;
}
}
close(listenfd);
fclose(lfp);
}

MD5

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
#include <string.h>
#include "md5.h"


unsigned char PADDING[]={0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void MD5Init(MD5_CTX *context)
{
context->count[0] = 0;
context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
}
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen)
{
unsigned int i = 0,index = 0,partlen = 0;
index = (context->count[0] >> 3) & 0x3F;
partlen = 64 - index;
context->count[0] += inputlen << 3;
if(context->count[0] < (inputlen << 3))
context->count[1]++;
context->count[1] += inputlen >> 29;

if(inputlen >= partlen)
{
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i = partlen;i+64 <= inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index = 0;
}
else
{
i = 0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
void MD5Final(MD5_CTX *context,unsigned char digest[16])
{
unsigned int index = 0,padlen = 0;
unsigned char bits[8];
index = (context->count[0] >> 3) & 0x3F;
padlen = (index < 56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[j] = input[i] & 0xFF;
output[j+1] = (input[i] >> 8) & 0xFF;
output[j+2] = (input[i] >> 16) & 0xFF;
output[j+3] = (input[i] >> 24) & 0xFF;
i++;
j+=4;
}
}
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[i] = (input[j]) |
(input[j+1] << 8) |
(input[j+2] << 16) |
(input[j+3] << 24);
i++;
j+=4;
}
}
void MD5Transform(unsigned int state[4],unsigned char block[64])
{
unsigned int a = state[0];
unsigned int b = state[1];
unsigned int c = state[2];
unsigned int d = state[3];
unsigned int x[64];
MD5Decode(x,block,64);
FF(a, b, c, d, x[ 0], 7, 0xd76aa478);
FF(d, a, b, c, x[ 1], 12, 0xe8c7b756);
FF(c, d, a, b, x[ 2], 17, 0x242070db);
FF(b, c, d, a, x[ 3], 22, 0xc1bdceee);
FF(a, b, c, d, x[ 4], 7, 0xf57c0faf);
FF(d, a, b, c, x[ 5], 12, 0x4787c62a);
FF(c, d, a, b, x[ 6], 17, 0xa8304613);
FF(b, c, d, a, x[ 7], 22, 0xfd469501);
FF(a, b, c, d, x[ 8], 7, 0x698098d8);
FF(d, a, b, c, x[ 9], 12, 0x8b44f7af);
FF(c, d, a, b, x[10], 17, 0xffff5bb1);
FF(b, c, d, a, x[11], 22, 0x895cd7be);
FF(a, b, c, d, x[12], 7, 0x6b901122);
FF(d, a, b, c, x[13], 12, 0xfd987193);
FF(c, d, a, b, x[14], 17, 0xa679438e);
FF(b, c, d, a, x[15], 22, 0x49b40821);


GG(a, b, c, d, x[ 1], 5, 0xf61e2562);
GG(d, a, b, c, x[ 6], 9, 0xc040b340);
GG(c, d, a, b, x[11], 14, 0x265e5a51);
GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);
GG(a, b, c, d, x[ 5], 5, 0xd62f105d);
GG(d, a, b, c, x[10], 9, 0x2441453);
GG(c, d, a, b, x[15], 14, 0xd8a1e681);
GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);
GG(a, b, c, d, x[ 9], 5, 0x21e1cde6);
GG(d, a, b, c, x[14], 9, 0xc33707d6);
GG(c, d, a, b, x[ 3], 14, 0xf4d50d87);
GG(b, c, d, a, x[ 8], 20, 0x455a14ed);
GG(a, b, c, d, x[13], 5, 0xa9e3e905);
GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8);
GG(c, d, a, b, x[ 7], 14, 0x676f02d9);
GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);


HH(a, b, c, d, x[ 5], 4, 0xfffa3942);
HH(d, a, b, c, x[ 8], 11, 0x8771f681);
HH(c, d, a, b, x[11], 16, 0x6d9d6122);
HH(b, c, d, a, x[14], 23, 0xfde5380c);
HH(a, b, c, d, x[ 1], 4, 0xa4beea44);
HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9);
HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60);
HH(b, c, d, a, x[10], 23, 0xbebfbc70);
HH(a, b, c, d, x[13], 4, 0x289b7ec6);
HH(d, a, b, c, x[ 0], 11, 0xeaa127fa);
HH(c, d, a, b, x[ 3], 16, 0xd4ef3085);
HH(b, c, d, a, x[ 6], 23, 0x4881d05);
HH(a, b, c, d, x[ 9], 4, 0xd9d4d039);
HH(d, a, b, c, x[12], 11, 0xe6db99e5);
HH(c, d, a, b, x[15], 16, 0x1fa27cf8);
HH(b, c, d, a, x[ 2], 23, 0xc4ac5665);


II(a, b, c, d, x[ 0], 6, 0xf4292244);
II(d, a, b, c, x[ 7], 10, 0x432aff97);
II(c, d, a, b, x[14], 15, 0xab9423a7);
II(b, c, d, a, x[ 5], 21, 0xfc93a039);
II(a, b, c, d, x[12], 6, 0x655b59c3);
II(d, a, b, c, x[ 3], 10, 0x8f0ccc92);
II(c, d, a, b, x[10], 15, 0xffeff47d);
II(b, c, d, a, x[ 1], 21, 0x85845dd1);
II(a, b, c, d, x[ 8], 6, 0x6fa87e4f);
II(d, a, b, c, x[15], 10, 0xfe2ce6e0);
II(c, d, a, b, x[ 6], 15, 0xa3014314);
II(b, c, d, a, x[13], 21, 0x4e0811a1);
II(a, b, c, d, x[ 4], 6, 0xf7537e82);
II(d, a, b, c, x[11], 10, 0xbd3af235);
II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);
II(b, c, d, a, x[ 9], 21, 0xeb86d391);
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}