-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkCtrl.c
139 lines (105 loc) · 2.84 KB
/
LinkCtrl.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
/* -- $Id: LinkCtrl.cpp,v 1.12.2.4.4.6 2004/08/13 00:56:27 yuzhe Exp $ -- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/socket.h>
#include "NetLog.h"
#include "Debuger.h"
#include "LinkCtrl.h"
#include "RecvOneFrame.h"
#include "Protocol.h"
#include "CtrlLinkTbl.h"
#include "UnknownRequest.h"
#include "SysCheckInCmd.h"
#include "HandShakeCmd.h"
#include "EvmsCmd.h"
#include "InfinibandCmd.h"
#include "IscsiCmd.h"
#include "SystemCmd.h"
#include "BondingCmd.h"
PktHandler g_PktHandler[MSG_TYPE_NUM] = {
GetUnknowCmd,
HandShakeCmd,
EvmsCmd,
InfinibandCmd,
IscsiCmd,
SystemCmd,
BondingCmd
};
static void Link_Ctrl_Cleanup(void *pinfo)
{
TRACE(12,
(" ####### Processing LINK_CTRL Cleanup Start! ######### \n"));
int sock_fd = *(int *) pinfo;
//停止所有关联数据线程
if (-1 == DelFromLinkTbl(sock_fd)) {
TRACE(12, (" Delete sock_fd from link table failed! "));
}
if (close(sock_fd) == -1) {
perror("close_mid");
}
TRACE(12,
(" ####### Processing NET_SERVER Cleanup Finished! ######### \n"));
return;
}
void print_frame_details(char * frame, int len)
{
int i = 0;
printf("LEN T S ODR RET CONTENT\n");
for(i = 0; i < len; i ++)
{
printf("%02x ", frame[i]);
}
printf("\n");
printf(" ");
for(i = 8; i < len; i ++)
{
printf("%c ", frame[i]);
}
printf("\n");
}
ssize_t my_send(int s, const void * buf, size_t len)
{
//TRACE(10, ("Responsed frame:\n"));
//print_frame_details((char *)buf, len);
return send(s, buf, len, 0);
}
/***************************************************************************************
* 客户控制连接处理函数,成功返回0,失败返回-1。
***************************************************************************************/
int Link_Ctrl(int socket_fd)
{
pthread_cleanup_push(Link_Ctrl_Cleanup, &socket_fd);
int len = 0;
TRACE(12, ("Enter Link_Ctrl()!\n"));
if (SysCheckInCmd(socket_fd) != 0) {
close(socket_fd);
return 0;
}
//开始处理应用报文。
PktHandler func;
char one_frame[MAX_FRAME_LEN];
memset(one_frame, 0, MAX_FRAME_LEN);
while ((len = RecvOneFrame(socket_fd, one_frame)) > 0) {
TRACE(10, ("Received frame:\n"));
print_frame_details(one_frame, ((PPACKET_HDR) one_frame)->len);
if (((PPACKET_HDR) one_frame)->type > 0
&& ((PPACKET_HDR) one_frame)->type <= MSG_TYPE_NUM) {
func = g_PktHandler[((PPACKET_HDR) one_frame)->type];
if (NULL != func) {
func(socket_fd, (char *) one_frame,
((PPACKET_HDR) one_frame)->len);
}
} else {
TRACE(10, ("Error Message Type!\n"));
}
memset(one_frame, 0, MAX_FRAME_LEN);
}
TRACE(2, ("Leave Link_Ctrl()!\n"));
pthread_cleanup_pop(1); /* Link_Ctrl_Cleanup(socket_fd) */
return 0;
}