-
Notifications
You must be signed in to change notification settings - Fork 39
/
WTPConfigureState.c
360 lines (294 loc) · 12.3 KB
/
WTPConfigureState.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
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
/*******************************************************************************************
* Copyright (c) 2006-7 Laboratorio di Sistemi di Elaborazione e Bioingegneria Informatica *
* Universita' Campus BioMedico - Italy *
* *
* This program is free software; you can redistribute it and/or modify it under the terms *
* of the GNU General Public License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with this *
* program; if not, write to the: *
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, *
* MA 02111-1307, USA. *
* *
* --------------------------------------------------------------------------------------- *
* Project: Capwap *
* *
* Author : Ludovico Rossi ([email protected]) *
* Del Moro Andrea ([email protected]) *
* Giovannini Federica ([email protected]) *
* Massimo Vellucci ([email protected]) *
* Mauro Bisson ([email protected]) *
*******************************************************************************************/
#include "CWWTP.h"
#ifdef DMALLOC
#include "../dmalloc-5.5.0/dmalloc.h"
#endif
/* void CWWTPResponseTimerExpired(void *arg, CWTimerID id); */
CWBool CWAssembleConfigureRequest(CWProtocolMessage ** messagesPtr,
int *fragmentsNumPtr, int PMTU, int seqNum, CWList msgElemList);
CWBool CWParseConfigureResponseMessage(unsigned char *msg, int len, int seqNum, CWProtocolConfigureResponseValues * valuesPtr);
CWBool CWSaveConfigureResponseMessage(CWProtocolConfigureResponseValues * configureResponse);
/*_________________________________________________________*/
/* *******************___FUNCTIONS___******************* */
/*
* Manage Configure State.
*/
CWStateTransition CWWTPEnterConfigure()
{
int seqNum;
CWProtocolConfigureResponseValues values;
CWLog("\n");
CWLog("######### Configure State #########");
/* send Configure Request */
seqNum = CWGetSeqNum();
if (!CWErr(CWWTPSendAcknowledgedPacket(seqNum,
NULL,
CWAssembleConfigureRequest,
(void *)CWParseConfigureResponseMessage,
(void *)CWSaveConfigureResponseMessage, &values))) {
CWNetworkCloseSocket(gWTPSocket);
#ifndef CW_NO_DTLS
CWSecurityDestroySession(gWTPSession);
CWSecurityDestroyContext(gWTPSecurityContext);
gWTPSecurityContext = NULL;
gWTPSession = NULL;
#endif
return CW_QUIT;
}
return CW_ENTER_DATA_CHECK;
}
/*
void CWWTPResponseTimerExpired(void *arg, CWTimerID id)
{
CWLog("WTP Response Configure Timer Expired");
CWNetworkCloseSocket(gWTPSocket);
}
*/
/*
* Send Configure Request on the active session.
*/
CWBool CWAssembleConfigureRequest(CWProtocolMessage ** messagesPtr,
int *fragmentsNumPtr, int PMTU, int seqNum, CWList msgElemList)
{
CWProtocolMessage *msgElems = NULL;
CWProtocolMessage *msgElemsBinding = NULL;
const int msgElemCount = 8;
const int msgElemBindingCount = 0;
int k = -1;
if (messagesPtr == NULL || fragmentsNumPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
CW_CREATE_PROTOCOL_MSG_ARRAY_ERR(msgElems, msgElemCount, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
CWDebugLog("Assembling Configure Request...");
/* Assemble Message Elements */
if ((!(CWAssembleMsgElemACName(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemACNameWithIndex(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemRadioAdminState(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemStatisticsTimer(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemWTPRebootStatistics(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemWTPRadioInformation(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemSupportedRates(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemMultiDomainCapability(&(msgElems[++k]))))) {
int i;
for (i = 0; i <= k; i++) {
CW_FREE_PROTOCOL_MESSAGE(msgElems[i]);
}
CW_FREE_OBJECT(msgElems);
/* error will be handled by the caller */
return CW_FALSE;
}
if (!(CWAssembleMessage(messagesPtr,
fragmentsNumPtr,
PMTU,
seqNum,
CW_MSG_TYPE_VALUE_CONFIGURE_REQUEST,
msgElems, msgElemCount, msgElemsBinding, msgElemBindingCount)))
return CW_FALSE;
CWDebugLog("Configure Request Assembled");
return CW_TRUE;
}
CWBool CWParseConfigureResponseMessage(unsigned char *msg, int len, int seqNum, CWProtocolConfigureResponseValues * valuesPtr)
{
CWControlHeaderValues controlVal;
CWProtocolMessage completeMsg;
CWBool bindingMsgElemFound = CW_FALSE;
int offsetTillMessages;
int i = 0;
int j = 0;
if (msg == NULL || valuesPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
CWDebugLog("Parsing Configure Response...");
completeMsg.msg = msg;
completeMsg.offset = 0;
valuesPtr->echoRequestTimer = 0;
valuesPtr->radioOperationalInfoCount = 0;
valuesPtr->radiosDecryptErrorPeriod.radiosCount = 0;
valuesPtr->bindingValues = NULL;
valuesPtr->ACIPv4ListInfo.ACIPv4ListCount = 0;
valuesPtr->ACIPv4ListInfo.ACIPv4List = NULL;
valuesPtr->ACIPv6ListInfo.ACIPv6ListCount = 0;
valuesPtr->ACIPv6ListInfo.ACIPv6List = NULL;
/* error will be handled by the caller */
if (!(CWParseControlHeader(&completeMsg, &controlVal)))
return CW_FALSE;
/* different type */
if (controlVal.messageTypeValue != CW_MSG_TYPE_VALUE_CONFIGURE_RESPONSE)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Message is not Configure Response as Expected");
if (controlVal.seqNum != seqNum)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Different Sequence Number");
/* skip timestamp */
controlVal.msgElemsLen -= CW_CONTROL_HEADER_OFFSET_FOR_MSG_ELEMS;
offsetTillMessages = completeMsg.offset;
/* parse message elements */
while ((completeMsg.offset - offsetTillMessages) < controlVal.msgElemsLen) {
unsigned short int type = 0; /* = CWProtocolRetrieve32(&completeMsg); */
unsigned short int len = 0; /* = CWProtocolRetrieve16(&completeMsg); */
CWParseFormatMsgElem(&completeMsg, &type, &len);
/* CWDebugLog("Parsing Message Element: %u, len: %u", type, len); */
if (CWBindingCheckType(type)) {
bindingMsgElemFound = CW_TRUE;
completeMsg.offset += len;
continue;
}
switch (type) {
case CW_MSG_ELEMENT_AC_IPV4_LIST_CW_TYPE:
if (!(CWParseACIPv4List(&completeMsg, len, &(valuesPtr->ACIPv4ListInfo))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_AC_IPV6_LIST_CW_TYPE:
if (!(CWParseACIPv6List(&completeMsg, len, &(valuesPtr->ACIPv6ListInfo))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_CW_TIMERS_CW_TYPE:
if (!(CWParseCWTimers(&completeMsg, len, valuesPtr)))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_RADIO_OPERAT_STATE_CW_TYPE:
/*
* just count how many radios we have, so we
* can allocate the array
*/
valuesPtr->radioOperationalInfoCount++;
completeMsg.offset += len;
break;
case CW_MSG_ELEMENT_CW_DECRYPT_ER_REPORT_PERIOD_CW_TYPE:
valuesPtr->radiosDecryptErrorPeriod.radiosCount++;
completeMsg.offset += len;
break;
case CW_MSG_ELEMENT_IDLE_TIMEOUT_CW_TYPE:
if (!(CWParseIdleTimeout(&completeMsg, len, valuesPtr)))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_WTP_FALLBACK_CW_TYPE:
if (!(CWParseWTPFallback(&completeMsg, len, valuesPtr)))
return CW_FALSE;
break;
default:
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Unrecognized Message Element");
}
}
if (completeMsg.offset != len)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Garbage at the End of the Message");
CW_CREATE_ARRAY_ERR((*valuesPtr).radioOperationalInfo,
(*valuesPtr).radioOperationalInfoCount,
CWRadioOperationalInfoValues, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
CW_CREATE_ARRAY_ERR((*valuesPtr).radiosDecryptErrorPeriod.radios,
(*valuesPtr).radiosDecryptErrorPeriod.radiosCount,
WTPDecryptErrorReportValues, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
completeMsg.offset = offsetTillMessages;
while (completeMsg.offset - offsetTillMessages < controlVal.msgElemsLen) {
unsigned short int type = 0; /* = CWProtocolRetrieve32(&completeMsg); */
unsigned short int len = 0; /* = CWProtocolRetrieve16(&completeMsg); */
CWParseFormatMsgElem(&completeMsg, &type, &len);
switch (type) {
case CW_MSG_ELEMENT_RADIO_OPERAT_STATE_CW_TYPE:
/* will be handled by the caller */
if (!(CWParseWTPRadioOperationalState(&completeMsg,
len, &(valuesPtr->radioOperationalInfo[i]))))
return CW_FALSE;
i++;
break;
case CW_MSG_ELEMENT_CW_DECRYPT_ER_REPORT_PERIOD_CW_TYPE:
if (!(CWParseDecryptErrorReportPeriod(&completeMsg,
len, &(valuesPtr->radiosDecryptErrorPeriod.radios[j]))))
return CW_FALSE;
j++;
break;
default:
completeMsg.offset += len;
break;
}
}
if (bindingMsgElemFound) {
if (!CWBindingParseConfigureResponse(msg + offsetTillMessages,
len - offsetTillMessages, &(valuesPtr->bindingValues))) {
return CW_FALSE;
}
}
CWDebugLog("Configure Response Parsed");
return CW_TRUE;
}
CWBool CWSaveConfigureResponseMessage(CWProtocolConfigureResponseValues * configureResponse)
{
if (configureResponse == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
if (gACInfoPtr == NULL)
return CWErrorRaise(CW_ERROR_NEED_RESOURCE, NULL);
CWDebugLog("Saving Configure Response...");
CWDebugLog("###A");
CWDebugLog("###Count:%d", (configureResponse->ACIPv4ListInfo).ACIPv4ListCount);
if ((gACInfoPtr->ACIPv4ListInfo).ACIPv4List == NULL) {
CWDebugLog("###NULL");
}
if ((configureResponse->ACIPv4ListInfo).ACIPv4ListCount > 0) {
//CW_FREE_OBJECT((gACInfoPtr->ACIPv4ListInfo).ACIPv4List);
(gACInfoPtr->ACIPv4ListInfo).ACIPv4ListCount = (configureResponse->ACIPv4ListInfo).ACIPv4ListCount;
(gACInfoPtr->ACIPv4ListInfo).ACIPv4List = (configureResponse->ACIPv4ListInfo).ACIPv4List;
}
CWDebugLog("###B");
if ((configureResponse->ACIPv6ListInfo).ACIPv6ListCount > 0) {
//CW_FREE_OBJECT((gACInfoPtr->ACIPv6ListInfo).ACIPv6List);
(gACInfoPtr->ACIPv6ListInfo).ACIPv6ListCount = (configureResponse->ACIPv6ListInfo).ACIPv6ListCount;
(gACInfoPtr->ACIPv6ListInfo).ACIPv6List = (configureResponse->ACIPv6ListInfo).ACIPv6List;
}
if (configureResponse->bindingValues != NULL) {
CWProtocolResultCode resultCode;
if (!CWBindingSaveConfigureResponse(configureResponse->bindingValues, &resultCode)) {
CW_FREE_OBJECT(configureResponse->bindingValues);
return CW_FALSE;
}
}
if (configureResponse->echoRequestTimer > 0) {
gEchoInterval = configureResponse->echoRequestTimer;
}
/*
""need to be added""
int discoveryTimer;
int radioOperationalInfoCount;
CWRadioOperationalInfoValues *radioOperationalInfo;
WTPDecryptErrorReport radiosDecryptErrorPeriod;
int idleTimeout;
int fallback;
*/
/*
* It is not clear to me what the original developers intended to
* accomplish. One thing's for sure: radioOperationalInfo, radiosDecryptErrorPeriod.radios,
* and bidingValues get allocated and are never freed,
* so we do it here...
*
* BUGs ML02-ML04-ML05
* 16/10/2009 - Donato Capitella
*/
//CW_FREE_OBJECT(configureResponse->radioOperationalInfo);
//CW_FREE_OBJECT(configureResponse->radiosDecryptErrorPeriod.radios);
//CW_FREE_OBJECT(configureResponse->bindingValues);
CWDebugLog("Configure Response Saved");
return CW_TRUE;
}