服务端登录验证

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
#include "Server.h"
//#include <shadow.h>
//#include <crypt.h>
//#include <syslog.h>
int IsLoading(int netfd){
if(send(netfd, "请输入用户名和密码", sizeof("请输入用户名和密码"), 0) <= 0){
printf("error recv loadmsg\n");
return -1;
}
char load[2][PATHLEN];
char *username = load[0];
if(recv(netfd, load, 2 * PATHLEN, 0) <= 0){
printf("error recv loadmsg\n");
return -1;
}

struct spwd *info = getspnam(username);
int i = strlen(info->sp_pwdp) - 1;
for(; i >= 0; --i){
if(info->sp_pwdp[i] == '$'){
break;
}
}
char *salt = (char *)malloc(i);
strncpy(salt, info->sp_pwdp, i);
salt[i] = '\0';
char *password = crypt(load[1], salt);
printf("%s\n%s\n", salt, password);
if(strcmp(info->sp_pwdp, password) == 0){
openlog("Server", LOG_PID | LOG_PERROR, LOG_USER);
//syslog(LOG_ERR, "错误信息");
//syslog(LOG_WORNING, "错误信息");
return 0;
}else{
return -1;
}
}


服务端队列的初始化

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

void InitQueue(Queue_t *queue){
queue->head = NULL;
queue->tail = NULL;
queue->count = 0;
}

void EnQueue(Queue_t *queue, int netfd){
Node_t *new_node = (Node_t *)malloc(sizeof(Node_t));
ERROR_CHECK(new_node, NULL, "malloc new_node");
new_node->netfd = netfd;
new_node->depth = -1; //下标,默认目录,目前所在目录层
new_node->next = NULL;

if (queue->count == 0) { // 队列为空时,头和尾都指向新节点
queue->head = new_node;
queue->tail = new_node;
} else {
queue->tail->next = new_node;
queue->tail = new_node;
}
queue->count++;
}
void DeQueue(Queue_t *queue){
queue->head = queue->head->next;
if (queue->head == NULL) {
queue->tail = NULL;
}
-- queue->count;
}

服务端线程

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

void *Worker(void *arg) {
Resource_t *res = (Resource_t *)arg;
while(1){
pthread_mutex_lock(&res->mutex);
// 等待队列中有任务
while(res->queue->count < 1){
pthread_cond_wait(&res->cond, &res->mutex);
}
// 从队列头部取出任务(先进先出)
Node_t *p = res->queue->head;
if(p != NULL){
DeQueue(res->queue);
-- res->freepid;
}
pthread_mutex_unlock(&res->mutex);
while(1){
if(RecvCommand(p) == -1){
pthread_mutex_lock(&res->mutex);
closelog();
close(p->netfd);
free(p);
++ res->freepid;
pthread_cond_broadcast(&res->cond);
pthread_mutex_unlock(&res->mutex);
break;
}
}
}
pthread_exit(NULL);
}