-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCtrlLinkTbl.c
290 lines (139 loc) · 3 KB
/
CtrlLinkTbl.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
/* -- $Id: CtrlLinkTbl.cpp,v 1.12.2.2.4.2 2004/07/22 11:14:36 yuzhe Exp $ -- */
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Debuger.h"
#include "CtrlLinkTbl.h"
#include "SysConfExt.h"
CTRL_LINK_DESC CtrlLinkTbl[MAX_CONN_NUM]; // ¿ØÖÆÁ´½ÓÃèÊö±í
int
InitCtrlLinkTbl ()
{
int i;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
pcld->ipaddr = 0;
memset (&pcld->username, '\0', MAX_USRNAME_LEN);
pcld->socket_fd = 0;
pcld->ctrl_tid = 0;
pthread_mutex_init (&pcld->mutex, NULL);
pcld->notinformcnt = 0;
pcld->head = -1;
pcld->tail = -1;
}
return 0;
}
int
DeinitCtrlLinkTbl ()
{
int i;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
pthread_mutex_destroy (&pcld->mutex);
memset (pcld, '\0', sizeof (CTRL_LINK_DESC));
}
return 0;
}
PCTRL_LINK_DESC GetCtrlLink (int sock_fd)
{
int i;
int isOk = 0;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld->socket_fd == sock_fd)
{
isOk = 1;
break;
}
}
if (isOk == 1)
return pcld;
else
return NULL;
}
int
GetLinkUser (int sock_fd, char *username, int len)
{
if (len < MAX_USRNAME_LEN)
return -1;
int i;
int isOk = 0;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld->socket_fd == sock_fd)
{
memcpy (username, pcld->username, MAX_USRNAME_LEN);
username[MAX_USRNAME_LEN - 1] = '\0';
isOk = 1;
break;
}
}
if (isOk == 1)
return 0;
else
return -1;
}
int
DelFromLinkTbl (int sock_fd)
{
PCTRL_LINK_DESC pctrllink;
pctrllink = GetCtrlLink (sock_fd);
if (pctrllink == 0)
return -1;
int i;
int isOk = 0;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld != pctrllink)
continue;
pthread_mutex_lock (&pcld->mutex);
pcld->ipaddr = 0;
memset (&pcld->username, '\0', MAX_USRNAME_LEN);
pcld->socket_fd = 0;
pcld->ctrl_tid = 0;
pcld->notinformcnt = 0;
pcld->head = -1;
pcld->tail = -1;
pthread_mutex_unlock (&pcld->mutex);
isOk = 1;
break;
}
if (isOk == 1)
return 0;
else
return -1;
}
#ifdef DEBUG
int
TestCtrlLinkTbl ()
{
printf
(" #################### LINK_CTRL TABLE ################################ \n");
printf
(" ipaddr username sock_fd ctrl_tid notinformcnt buflen head tail \n");
printf
(" ------------- -------- ------- -------- ------------ ------ ---- ---- \n");
int i;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
printf (" %13s %8s %7d %8ld %12d %6d %4d %4d\n",
inet_ntoa (*(struct in_addr *) &pcld->ipaddr),
pcld->username, pcld->socket_fd, pcld->ctrl_tid,
pcld->notinformcnt, pcld->unhandled_buf.len, pcld->head,
pcld->tail);
} printf ("\n\n");
return 0;
}
#endif /* */