-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbdgmsg.h
217 lines (174 loc) · 5.9 KB
/
bdgmsg.h
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
/*
* This file is part of the tknet project.
* which be used under the terms of the GNU General Public
* License version 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included
* in the packaging of this file. Please review the following
* information to ensure the GNU General Public License
* version 3.0 requirements will be met:
* http://www.gnu.org/copyleft/gpl.html
*
* Copyright (C) 2012 Zhong Wei <[email protected]> .
*/
#define PEER_NAME_ID_LEN 16
#define TK_NET_DATA_LEN 128
#define TK_NET_BDG_MSG_FLAG 0
#define BDG_READ_OPT_ANY 0
#define BDG_READ_OPT_ADDR_FILTER 1
#define BDG_READ_OPT_MSG_FILTER 2
#define BRIDGE_MSG_INFO_UNKNOWN 0
#define BRIDGE_MSG_INFO_HELLO_BDG 1
#define BRIDGE_MSG_INFO_IAM_HERE 2
#define BRIDGE_MSG_INFO_REGISTER 3
#define BRIDGE_MSG_ERR_NAMEID_EXIST 4
#define BRIDGE_MSG_INFO_RGST_OK 5
#define BRIDGE_MSG_INFO_WAITING 6
#define BRIDGE_MSG_INFO_ECHO 7
#define BRIDGE_MSG_INFO_CONNECT 8
#define BRIDGE_MSG_INFO_CONNECT_BEGIN 9
#define BRIDGE_MSG_ERR_NO_NAMEID 10
#define BRIDGE_MSG_INFO_PUNCHING 11
#define BRIDGE_MSG_INFO_PUNCHING_FINISH 12
#define BRIDGE_MSG_INFO_CONNECT_ADDR 13
#define BRIDGE_MSG_ERR_CONNECT_ADDR 14
#define BRIDGE_MSG_INFO_HELLO 15
#define BRIDGE_MSG_INFO_ESTABLISHED 16
#define BRIDGE_MSG_ERR_NO_SEED_TO_RELAY 17
#define BRIDGE_MSG_ERR_ERROR 18
#define BRIDGE_MSG_INFO_ACKNOWLEDGE 19
#define BRIDGE_MSG_INFO_WAIT_RELAY 20
#define CONNECT_DECISION_FLAG_BEGIN 1
#define CONNECT_DECISION_FLAG_DIRECT 2
#define CONNECT_DECISION_FLAG_ERR 3
#define CONNECT_DECISION_FLAG_A_SIDE_RELAY 4
#define CONNECT_DECISION_FLAG_B_SIDE_RELAY 5
struct BridgeHelloStepPa
{
BOOL res;
struct NetAddr addr;
};
struct BridgeClientProcPa
{
char *pMyNameID;
const char **ppTargetNameID;
struct NetAddr DirectConnectAddr;
BOOL ifSkipRegister;
BOOL ifFastSendWait;//everytime client go to send "waiting"
//message letting server begain the connection decision,
//it is too slow. We use this flag to let client send immediately
//after he returns to the wait step.
BOOL ifConnected;//if server client are disconnected for some reasons,
//we set this flag to zero, then the main loop will know this situation,
//and may try to connect another server.
};
struct Peer
{
char NameID[PEER_NAME_ID_LEN];
struct NetAddr addr;
uchar NATType;
};
struct BridgeProc
{
struct PeerData *pPeerDataRoot;
struct Iterator *pSeedPeerCache;
struct Process proc;
struct Sock *pSock;
struct ProcessingList *pProcList;
struct Peer s,a,b,sx;
uint DecisionRelayID;
uchar DecisionFlag;
struct NetAddr DecisionPunAddr,DecisionConAddr;
char PunAddrNameID[PEER_NAME_ID_LEN];
char ConAddrNameID[PEER_NAME_ID_LEN];
struct NetAddr addr;
struct NetAddr MultiSendTo;
uchar MultiSendInfo;
uchar ErrCode;
void *Else;
};
struct BridgeMsg
{
uchar info;
struct NetAddr addr;
uint RelayID;
char NameID[PEER_NAME_ID_LEN];
uchar NATType;
uchar Relays;
uchar ErrCode;
};
struct TkNetMsg
{
uchar flag;
ushort UsrDatLen;//Only used when msg union stands for UsrDat
//(flag is not TK_NET_BDG_MSG_FLAG).
union
{
struct BridgeMsg BdgMsg;
char UsrDat[TK_NET_DATA_LEN];
}msg;
};
DECLARATION_STRUCT_CONSTRUCTOR( BridgeProc )
DECLARATION_STRUCT_CONSTRUCTOR( Peer )
void
ConsAndStartBridgeServer(struct BridgeProc * , struct PeerData * ,
struct ProcessingList * , struct Sock *,struct Iterator *);
void
FreeBridgeServer(struct BridgeProc *);
void
FreeSubBridgeServerTemplate();
void
BridgeClientTryBdgServerProc(struct BridgeProc *,struct BridgeHelloStepPa * , struct Sock *);
struct BridgeClientProcPa*
BridgeMakeClientProc(struct BridgeProc *, struct Sock *,struct ProcessingList *,
struct NetAddr *, char * ,uchar , const char** ,BOOL);
void
FreeBdgClientProc(struct BridgeProc *);
void
SetPeerByPeerData(struct Peer *,struct PeerData *);
#define BDG_ADDR( _name , _p_bdg_proc ) \
&( _p_bdg_proc-> _name .addr )
#define IF_NEAD_RELAY( _NAT0 , _NAT1 ) ( _NAT0 + _NAT1 >= 5)
struct BridgeMsg*
BdgMsgRead(struct Process * , uchar , uchar , struct NetAddr *);
void
BdgMsgWrite(struct Process * ,struct BridgeMsg * , struct NetAddr *);
struct PeerData*
NewPeerDataWithBdgProc(struct NetAddr ,uchar ,char *,struct BridgeProc *);
void
BdgSubServerProcInit();
EXTERN_STEP( BdgClientTryBdgServer )
EXTERN_STEP( BridgeMain )
EXTERN_STEP( BdgBeginSubServer )
EXTERN_STEP( BdgConnectRequireServer )
EXTERN_STEP( BdgConnectRequireReply )
EXTERN_STEP( BdgConnectDecision )
EXTERN_STEP( BdgPunchingServer )
EXTERN_STEP( BdgConnectAddrServer )
EXTERN_STEP( BdgErrReturnServer )
EXTERN_STEP( BdgClientRegister )
EXTERN_STEP( BdgClientWait )
EXTERN_STEP( BdgClientConnectRequire )
EXTERN_STEP( BdgClientDoConnectAddr )
EXTERN_STEP( BdgClientMultiSendNotify )
typedef void (*CONNECT_CALLBK)(struct NetAddr,struct Sock*,struct ProcessingList*,
struct Iterator*,struct Iterator*);
extern CONNECT_CALLBK g_ConnectionNotify;
#define ON_CONNECT() \
OnConnect(struct NetAddr pa_addr,struct Sock *pa_pSock,struct ProcessingList *pa_pProcList \
,struct Iterator* pa_pINow,struct Iterator *pa_pIForward)
static __inline void
PeerTrace( struct Peer *pa_pPeer )
{
char AddrText[32];
GetAddrText(&pa_pPeer->addr,AddrText);
PROMPT(Usual,"%s(%s)",pa_pPeer->NameID,AddrText);
}
static __inline void
BetweenMacro( struct Peer *pa_pPeer0, struct Peer *pa_pPeer1 )
{
PROMPT(Usual,"between ");
PeerTrace( pa_pPeer0 );
PROMPT(Usual," and ");
PeerTrace( pa_pPeer1 );
PROMPT(Usual," . \n");
}