-
Notifications
You must be signed in to change notification settings - Fork 84
/
ACInterface.c
901 lines (719 loc) · 30.5 KB
/
ACInterface.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*******************************************************************************************
* Copyright (c) 2006-2009 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. *
* *
* In addition, as a special exception, the copyright holders give permission to link the *
* code of portions of this program with the OpenSSL library under certain conditions as *
* described in each individual source file, and distribute linked combinations including *
* the two. You must obey the GNU General Public License in all respects for all of the *
* code used other than OpenSSL. If you modify file(s) with this exception, you may *
* extend this exception to your version of the file(s), but you are not obligated to do *
* so. If you do not wish to do so, delete this exception statement from your version. *
* If you delete this exception statement from all source files in the program, then also *
* delete it here. *
* *
* --------------------------------------------------------------------------------------- *
* Project: Capwap *
* *
* Authors : Antonio Davoli ([email protected]) *
* Donato Capitella ([email protected]) *
*******************************************************************************************/
#include "CWAC.h"
#include "ACAppsProtocol.h"
#include "CWVendorPayloads.h"
#include "WUM.h"
#define LIST_CMD "LIST"
#define SCAN_CMD "SCAN"
#define QUIT_CMD "QUIT"
#define LISTEN_PORT 1235
#define COMMAND_BUFFER_SIZE 5120
#define WTP_LIST_BUFFER_SIZE 1024
#define CMD_TYPE_SIZE sizeof(unsigned char)
int is_valid_wtp_index(int index);
int Readn(int sock, void *buf, size_t n);
/********************************************************************
* Now the only parameter need by the application thread manager *
* is the index of socket. *
********************************************************************/
typedef struct {
int index;
} CWInterfaceThreadArg;
/************************************************************************
* CWOFDMSetValues provide to set the command values (type, parameters, *
* output socket) on the correct wtp structure. *
************************************************************************/
int CWOFDMSetValues(int selection, int socketIndex, OFDMControlValues* ofdmValues) {
CWThreadMutexLock(&(gWTPs[selection].interfaceMutex));
gWTPs[selection].ofdmValues = ofdmValues;
gWTPs[selection].interfaceCommand = OFDM_CONTROL_CMD;
gWTPs[selection].applicationIndex = socketIndex;
CWSignalThreadCondition(&gWTPs[selection].interfaceWait);
CWWaitThreadCondition(&gWTPs[selection].interfaceComplete, &gWTPs[selection].interfaceMutex);
CWThreadMutexUnlock(&(gWTPs[selection].interfaceMutex));
return 0;
}
int CWVendorSetValues(int selection, int socketIndex, CWProtocolVendorSpecificValues* vendorValues) {
CWThreadMutexLock(&(gWTPs[selection].interfaceMutex));
gWTPs[selection].vendorValues = vendorValues;
gWTPs[selection].interfaceCommand = UCI_CONTROL_CMD;
gWTPs[selection].applicationIndex = socketIndex;
CWSignalThreadCondition(&gWTPs[selection].interfaceWait);
CWWaitThreadCondition(&gWTPs[selection].interfaceComplete, &gWTPs[selection].interfaceMutex);
CWThreadMutexUnlock(&(gWTPs[selection].interfaceMutex));
return 0;
}
int CWWumSetValues(int selection, int socketIndex, CWProtocolVendorSpecificValues* vendorValues) {
CWThreadMutexLock(&(gWTPs[selection].interfaceMutex));
gWTPs[selection].vendorValues = vendorValues;
gWTPs[selection].interfaceCommand = WTP_UPDATE_CMD;
gWTPs[selection].applicationIndex = socketIndex;
CWSignalThreadCondition(&gWTPs[selection].interfaceWait);
CWWaitThreadCondition(&gWTPs[selection].interfaceComplete, &gWTPs[selection].interfaceMutex);
CWThreadMutexUnlock(&(gWTPs[selection].interfaceMutex));
return 0;
}
/*
* Elena Agostini - 09/2014: IEEE 802.11 Binding + WUM
*/
int CWWLANSetValues(int selection, int socketIndex, WUMWLANCmdParameters * cmdWLAN) {
CWThreadMutexLock(&(gWTPs[selection].interfaceMutex));
gWTPs[selection].cmdWLAN = cmdWLAN;
gWTPs[selection].interfaceCommand = IEEE_WLAN_CONFIGURATION_CMD;
gWTPs[selection].applicationIndex = socketIndex;
CWSignalThreadCondition(&gWTPs[selection].interfaceWait);
CWWaitThreadCondition(&gWTPs[selection].interfaceComplete, &gWTPs[selection].interfaceMutex);
CWThreadMutexUnlock(&(gWTPs[selection].interfaceMutex));
return 0;
}
/************************************************************************
* CWManageApplication is the function that provide the management of *
* interaction with a single application. *
* -------------------------------------------------------------------- *
* The messages used are defined in ACAppsProtocol.h *
************************************************************************/
CW_THREAD_RETURN_TYPE CWManageApplication(void* arg) {
int socketIndex = ((CWInterfaceThreadArg*)arg)->index;
CWSocket sock = appsManager.appSocket[socketIndex];
int n, connected= htonl(CONNECTION_OK), gActiveWTPsTemp;
char commandBuffer[COMMAND_BUFFER_SIZE];
char wtpListBuffer[WTP_LIST_BUFFER_SIZE];
int payload_size;
int i, nameLength, numActiveWTPs=0, wtpIndex;
int iTosend, nLtoSend;
unsigned char cmd_msg, msg_elem;
OFDMControlValues* ofdmValues;
CWProtocolVendorSpecificValues* vendorValues;
CWVendorUciValues* uciValues;
CWVendorWumValues* wumValues;
/********************************************************************************
* Write on application socket that connection setting is happened correctly. *
********************************************************************************/
if ( Writen(sock, &connected, sizeof(int) ) < 0 ) {
CWLog("Error on writing on application socket ");
return NULL;
}
/*
* Before starting, make sure to detach the thread because the parent
* doesn't do a join and we risk resource leaks.
*
* ref -> BUG-TRL01
* 15/10/2009 - Donato Capitella
*/
pthread_detach(pthread_self()); // no need here to check return value
/************************
* Thread Main Loop *
************************/
CW_REPEAT_FOREVER {
memset(commandBuffer, 0, COMMAND_BUFFER_SIZE);
/****************************************
* Waiting for client commands *
****************************************/
if ( ( n = Readn(sock, &cmd_msg, CMD_TYPE_SIZE) ) > 0 ) {
if ( cmd_msg == QUIT_MSG ) {
/****************************************************
* Quit Message: Clean socket information *
****************************************************/
goto quit_manage;
}
if ( cmd_msg == LIST_MSG ) {
/****************************************
* Manage LIST command *
* ------------------------------------ *
* 1. Get Number of active WTPs, *
* 2. Create Message Answer, *
* 3. Send To client socket. *
****************************************/
if(!CWErr(CWThreadMutexLock(&gActiveWTPsMutex))) {
CWLog("Error locking the mutex");
return NULL;
}
gActiveWTPsTemp = gActiveWTPs;
CWThreadMutexUnlock(&gActiveWTPsMutex);
if( gActiveWTPsTemp > 0 )
for(i=0; i<gMaxWTPs; i++)
if(gWTPs[i].isNotFree)
if (! gWTPs[i].WTPProtocolManager.name)
gActiveWTPsTemp -= 1;
numActiveWTPs = htonl (gActiveWTPsTemp );
memset(wtpListBuffer, 0, sizeof(wtpListBuffer));
payload_size = 0;
memcpy(wtpListBuffer, &numActiveWTPs, sizeof(int));
payload_size = sizeof(int);
if( gActiveWTPsTemp > 0 ) {
for(i=0; i<gMaxWTPs; i++) {
if(gWTPs[i].isNotFree) {
if (! gWTPs[i].WTPProtocolManager.name)
continue;
nameLength = strlen(gWTPs[i].WTPProtocolManager.name);
iTosend = htonl(i);
nLtoSend = htonl(nameLength);
memcpy(wtpListBuffer+payload_size, &iTosend, sizeof(int));
payload_size += sizeof(int);
memcpy(wtpListBuffer+payload_size, &nLtoSend, sizeof(int));
payload_size += sizeof(int);
memcpy(wtpListBuffer+payload_size, gWTPs[i].WTPProtocolManager.name, strlen(gWTPs[i].WTPProtocolManager.name));
payload_size += strlen(gWTPs[i].WTPProtocolManager.name);
}
}
}
if ( Writen(sock, wtpListBuffer, payload_size) < 0 )
CWLog("Error on write message on application socket");
}
if ( cmd_msg == CONF_UPDATE_MSG ) {
/****************************************
* Manage CONF command *
* ------------------------------------ *
* 1. Select the type of CONF_UPDATE, *
* 2. Get Index of WTP, *
* 3. Manage request. *
****************************************/
if ( (n = Readn(sock, commandBuffer, sizeof(msg_elem) + sizeof(wtpIndex))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
memcpy(&msg_elem, commandBuffer, sizeof(unsigned char)); /* CONF_UPDATE type */
memcpy(&wtpIndex, commandBuffer + sizeof(unsigned char), sizeof(int)); /* WTP Index */
wtpIndex = ntohl(wtpIndex);
/* Check if WTP Index is valid */
if (!is_valid_wtp_index(wtpIndex)) {
CWLog("WTP Index non valid!");
goto quit_manage;
}
switch (msg_elem) {
case MSG_ELEMENT_TYPE_OFDM:
{
/* Antonio Case */
CW_CREATE_OBJECT_ERR(ofdmValues, OFDMControlValues, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
if ( (n = Readn(sock, ofdmValues, sizeof(OFDMControlValues))) < 0) {
CWLog("Error while reading from socket");
goto quit_manage;
}
/****************************************************
* Two behaviors availables: *
* - One message element For All WTPs Active *
* - One message for a specific WTP *
****************************************************/
if ( wtpIndex == ALL_ACTIVE_WTPS ) { /* All wpts case */
if(!CWErr(CWThreadMutexLock(&gActiveWTPsMutex))) {
CWLog("Error locking the mutex");
return NULL;
}
numActiveWTPs = gActiveWTPs;
CWThreadMutexUnlock(&gActiveWTPsMutex);
if(numActiveWTPs>0) {
for(i=0; i<gMaxWTPs; i++) {
if(gWTPs[i].isNotFree) {
CWOFDMSetValues(i, socketIndex, ofdmValues);
}
}
}
}
else /* One specific Wtp Case */
CWOFDMSetValues(wtpIndex, socketIndex, ofdmValues);
CW_FREE_OBJECT(ofdmValues);
break;
}
case MSG_ELEMENT_TYPE_VENDOR_UCI:
{
/* Matteo Case */
/*Do stuff to parse uci payload */
int commandLength = 0;
CW_CREATE_OBJECT_ERR(vendorValues, CWProtocolVendorSpecificValues, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
CW_CREATE_OBJECT_ERR(uciValues, CWVendorUciValues, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
vendorValues->vendorPayloadType = CW_MSG_ELEMENT_VENDOR_SPEC_PAYLOAD_UCI;
uciValues->response = NULL;
if ( (n = Readn(sock, commandBuffer, sizeof(unsigned char) + sizeof(int))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
memcpy(&(uciValues->command), commandBuffer, sizeof(unsigned char));
memcpy(&commandLength, commandBuffer + sizeof(unsigned char), sizeof(int));
commandLength = ntohl(commandLength);
if (commandLength > 0) {
if ( (n = Readn(sock, commandBuffer + sizeof(char) + sizeof(int), commandLength)) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
CW_CREATE_STRING_ERR(uciValues->commandArgs, commandLength, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
memcpy(uciValues->commandArgs, commandBuffer + sizeof(unsigned char) + sizeof(int), sizeof(char)*commandLength);
uciValues->commandArgs[commandLength] = '\0';
} else
uciValues->commandArgs = NULL;
vendorValues->payload = uciValues;
/****************************************************
* Two behaviors availables: *
* - One message element For All WTPs Active *
* - One message for a specific WTP *
****************************************************/
if ( wtpIndex == ALL_ACTIVE_WTPS ) { /* All wpts case */
if(!CWErr(CWThreadMutexLock(&gActiveWTPsMutex))) {
CWLog("Error locking the mutex");
return NULL;
}
numActiveWTPs = gActiveWTPs;
CWThreadMutexUnlock(&gActiveWTPsMutex);
if(numActiveWTPs>0) {
for(i=0; i<gMaxWTPs; i++) {
if(gWTPs[i].isNotFree) {
printf("Sending UCI Configuration Message to: %d - %s \n", i, gWTPs[i].WTPProtocolManager.name);
CWVendorSetValues(i, socketIndex, vendorValues);
}
}
}
}
else /* One specific Wtp Case */
CWVendorSetValues(wtpIndex, socketIndex, vendorValues);
break;
}
case MSG_ELEMENT_TYPE_VENDOR_WUM:
{
/* Donato's Case */
CW_CREATE_OBJECT_ERR(vendorValues, CWProtocolVendorSpecificValues, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
CW_CREATE_OBJECT_ERR(wumValues, CWVendorWumValues, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
vendorValues->vendorPayloadType = CW_MSG_ELEMENT_VENDOR_SPEC_PAYLOAD_WUM;
/*
* Read WTP Update Message fields into the wumValues structure
*/
if ( (n = Readn(sock, &(wumValues->type), sizeof(unsigned char))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
if (wumValues->type == WTP_UPDATE_REQUEST) {
if ( (n = Readn(sock, commandBuffer, 3*sizeof(unsigned char)+sizeof(int))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
memcpy(&(wumValues->_major_v_), commandBuffer, sizeof(unsigned char));
memcpy(&(wumValues->_minor_v_), commandBuffer + sizeof(unsigned char), sizeof(unsigned char));
memcpy(&(wumValues->_revision_v_), commandBuffer + 2*sizeof(unsigned char), sizeof(unsigned char));
memcpy(&(wumValues->_pack_size_), commandBuffer + 3*sizeof(unsigned char), sizeof(unsigned int));
wumValues->_pack_size_ = ntohl(wumValues->_pack_size_);
} else if (wumValues->type == WTP_CUP_FRAGMENT) {
int seqNum;
int fragSize;
/* Read sequence number and fragment size */
if ( (n = Readn(sock, commandBuffer, 2*sizeof(int))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
memcpy(&seqNum, commandBuffer, sizeof(int));
memcpy(&fragSize, commandBuffer + sizeof(int), sizeof(int));
seqNum = ntohl(seqNum);
fragSize = ntohl(fragSize);
if (seqNum < 0) {
CWLog("Update with sequence number < 0 not valid.");
goto quit_manage;
}
if (fragSize <= 0) {
CWLog("Update with fragment size <= 0 not valid.");
goto quit_manage;
}
/* Read update package into buffer */
char *buf = malloc(fragSize);
if (buf == NULL) {
CWLog("Can't allocate memory");
goto quit_manage;
}
if ( (n = Readn(sock, buf, fragSize)) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
wumValues->_cup_ = buf;
wumValues->_seq_num_ = seqNum;
wumValues->_cup_fragment_size_ = fragSize;
}
vendorValues->payload = wumValues;
/* Send Request */
if ( wtpIndex == ALL_ACTIVE_WTPS ) { /* All wpts case */
if(!CWErr(CWThreadMutexLock(&gActiveWTPsMutex))) {
CWLog("Error locking the mutex");
return NULL;
}
numActiveWTPs = gActiveWTPs;
CWThreadMutexUnlock(&gActiveWTPsMutex);
if(numActiveWTPs>0) {
for(i=0; i<gMaxWTPs; i++) {
if(gWTPs[i].isNotFree) {
printf("Sending WUM Configuration Message to: %d - %s \n", i, gWTPs[i].WTPProtocolManager.name);
CWWumSetValues(i, socketIndex, vendorValues);
}
}
}
}
else /* One specific Wtp Case */
CWWumSetValues(wtpIndex, socketIndex, vendorValues);
break;
}
/*
* Elena Agostini - 09/2014: IEEE 802.11 Binding + WUM
*/
case MSG_ELEMENT_TYPE_ADD_WLAN:
{
char typeRequest=0;
int msgLen=0;
char * payload;
char * ssid;
int radioID, wlanID, tunnel, countChar=0;
WUMWLANCmdParameters * cmdWLAN;
CW_CREATE_OBJECT_ERR(cmdWLAN, WUMWLANCmdParameters, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
CWLog("Received ADD WLAN command");
if ( (n = Readn(sock, &(typeRequest), sizeof(unsigned char))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
//CWLog("typeRequest : %d\n", typeRequest);
if ( (n = Readn(sock, &msgLen, sizeof(int))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
//CWLog("msgLen : %d\n", msgLen);
CW_CREATE_ARRAY_CALLOC_ERR(payload, msgLen+1, char, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
if ( (n = Readn(sock, payload, (msgLen*sizeof(unsigned char)))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
cmdWLAN->typeCmd = CW_OP_ADD_WLAN;
char * token;
token = strtok(payload, ":");
while(token != NULL)
{
switch(countChar)
{
case 0:
cmdWLAN->radioID = atoi(token);
if(cmdWLAN->radioID > 0)
cmdWLAN->radioID--;
break;
case 1:
cmdWLAN->wlanID = atoi(token);
if(cmdWLAN->wlanID > 0)
cmdWLAN->wlanID--;
break;
case 2:
CW_CREATE_STRING_FROM_STRING_ERR(cmdWLAN->ssid, token, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
break;
}
countChar++;
token = strtok(NULL, ":");
}
//RFC vieta radioID <= 0 e wlanID <= 0
if(cmdWLAN->radioID < 0 || cmdWLAN->wlanID < 0)
{
CWLog("ERROR: radioID or wlanID < 0");
CW_FREE_OBJECT(cmdWLAN);
break;
}
/****************************************************
* Two behaviors availables: *
* - One message element For All WTPs Active *
* - One message for a specific WTP *
****************************************************/
if ( wtpIndex == ALL_ACTIVE_WTPS ) { // All wpts case
if(!CWErr(CWThreadMutexLock(&gActiveWTPsMutex))) {
CWLog("Error locking the mutex");
return NULL;
}
numActiveWTPs = gActiveWTPs;
CWThreadMutexUnlock(&gActiveWTPsMutex);
if(numActiveWTPs>0) {
for(i=0; i<gMaxWTPs; i++) {
if(gWTPs[i].isNotFree) {
CWWLANSetValues(i, socketIndex, cmdWLAN);
}
}
}
}
else // One specific Wtp Case
CWWLANSetValues(wtpIndex, socketIndex, cmdWLAN);
CW_FREE_OBJECT(cmdWLAN->ssid);
CW_FREE_OBJECT(cmdWLAN);
break;
}
case MSG_ELEMENT_TYPE_DEL_WLAN:
{
char typeRequest=0;
int msgLen=0;
char * payload;
char * ssid;
int radioID, wlanID, countChar=0;
WUMWLANCmdParameters * cmdWLAN;
CW_CREATE_OBJECT_ERR(cmdWLAN, WUMWLANCmdParameters, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
CWLog("Received DEL WLAN command");
if ( (n = Readn(sock, &(typeRequest), sizeof(unsigned char))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
// CWLog("typeRequest : %d\n", typeRequest);
if ( (n = Readn(sock, &msgLen, sizeof(int))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
//CWLog("msgLen : %d\n", msgLen);
CW_CREATE_ARRAY_CALLOC_ERR(payload, msgLen+1, char, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
if ( (n = Readn(sock, payload, (msgLen*sizeof(unsigned char)))) < 0 ) {
CWLog("Error while reading from socket.");
goto quit_manage;
}
// CWLog("payload : %s", payload);
cmdWLAN->typeCmd = CW_OP_DEL_WLAN;
char * token;
token = strtok(payload, ":");
while(token != NULL)
{
switch(countChar)
{
case 0:
cmdWLAN->radioID = atoi(token);
break;
case 1:
cmdWLAN->wlanID = atoi(token);
break;
}
countChar++;
token = strtok(NULL, ":");
}
//RFC vieta radioID <= 0 e wlanID <= 0
/*if(cmdWLAN->radioID <= 0 || cmdWLAN->wlanID <= 0)
{
CWLog("ERROR: radioID or wlanID <= 0");
CW_FREE_OBJECT(cmdWLAN);
break;
}
*/
/****************************************************
* Two behaviors availables: *
* - One message element For All WTPs Active *
* - One message for a specific WTP *
****************************************************/
if ( wtpIndex == ALL_ACTIVE_WTPS ) { // All wpts case
if(!CWErr(CWThreadMutexLock(&gActiveWTPsMutex))) {
CWLog("Error locking the mutex");
return NULL;
}
numActiveWTPs = gActiveWTPs;
CWThreadMutexUnlock(&gActiveWTPsMutex);
if(numActiveWTPs>0) {
for(i=0; i<gMaxWTPs; i++) {
if(gWTPs[i].isNotFree) {
CWWLANSetValues(i, socketIndex, cmdWLAN);
}
}
}
}
else // One specific Wtp Case
CWWLANSetValues(wtpIndex, socketIndex, cmdWLAN);
CW_FREE_OBJECT(cmdWLAN);
break;
}
default:
/* Error Case: Not correct msg_elem type */
break;
}
}
}
else {
CWLog("Error on receive application command (read socket)");
goto quit_manage;
}
}
quit_manage:
if(!CWErr(CWThreadMutexLock(&appsManager.numSocketFreeMutex))) {
CWLog("Error locking numSocketFree Mutex");
return NULL;
}
appsManager.isFree[socketIndex] = CW_TRUE;
appsManager.numSocketFree++;
CWDestroyThreadMutex(&appsManager.socketMutex[socketIndex]);
CWThreadMutexUnlock(&appsManager.numSocketFreeMutex);
close(sock);
return NULL;
}
/****************************************************************************
* CWInterface is the function that provide the interaction between AC and *
* extern applications. Listen on main Inet familty socket and create a *
* CWManageApplication thread for every client connected. *
****************************************************************************/
CW_THREAD_RETURN_TYPE CWInterface(void* arg)
{
CWSocket listen_sock, conn_sock;
struct sockaddr_in servaddr;
CWInterfaceThreadArg *argPtr;
CWThread thread_id;
int i, clientFull = htonl(FULL_CLIENT_CONNECTED), optValue = 1;
/****************************************************
* Setup of Application Socket Management Structure *
****************************************************/
for ( i=0; i < MAX_APPS_CONNECTED_TO_AC; i++)
appsManager.isFree[i] = CW_TRUE;
appsManager.numSocketFree = MAX_APPS_CONNECTED_TO_AC;
if ( !CWErr(CWCreateThreadMutex(&appsManager.numSocketFreeMutex)) ) {
CWLog("Error on mutex creation on appManager structure");
return NULL;
}
/****************************************************
* Setup (Creation and filling) of main socket *
****************************************************/
if ( ( listen_sock = socket(AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {
CWLog("Error on socket creation on Interface");
return NULL;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* Not Extern: INADDR_ANY */
servaddr.sin_port = htons(LISTEN_PORT);
if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &optValue, sizeof(int)) == -1) {
CWLog("Error on socket creation on Interface");
return NULL;
}
/************************************
* Binding socket and Listen call *
************************************/
if ( bind(listen_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr_in)) < 0 ) {
CWLog("Error on Binding");
return NULL;
}
if ( listen(listen_sock, MAX_APPS_CONNECTED_TO_AC) < 0 ) {
CWLog("Error on LIsTsocket creation");
return NULL;
}
/********************************
* Main Loop *
********************************/
CW_REPEAT_FOREVER
{
if ( ( conn_sock = accept(listen_sock, (struct sockaddr *) NULL, NULL) ) > 0 ) {
/************************************************************************
* Check (with lock) the number of socket free at the moment of accept, *
* if this value is greater than 0 will be spawn a new Manage thread. *
************************************************************************/
if(!CWErr(CWThreadMutexLock(&appsManager.numSocketFreeMutex))) {
CWLog("Error locking numSocketFree Mutex");
return NULL;
}
if ( appsManager.numSocketFree > 0 ) {
CW_CREATE_OBJECT_ERR(argPtr, CWInterfaceThreadArg, {CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL); return 0;});
/************************************
* Search socket for application *
************************************/
for (i=0; i < MAX_APPS_CONNECTED_TO_AC; i++)
{
if ( appsManager.isFree[i] == CW_TRUE ) {
argPtr->index = i;
appsManager.isFree[i] = CW_FALSE;
appsManager.appSocket[i] = conn_sock;
break;
}
}
appsManager.numSocketFree--;
CWThreadMutexUnlock(&appsManager.numSocketFreeMutex);
if ( !CWErr(CWCreateThreadMutex(&appsManager.socketMutex[argPtr->index])) ) {
CWLog("Error on mutex creation on appManager structure");
return NULL;
}
if(!CWErr(CWCreateThread(&thread_id, CWManageApplication, argPtr))) {
CWLog("Error on thread CWManageApplication creation");
appsManager.isFree[argPtr->index] = CW_TRUE;
close(conn_sock);
CW_FREE_OBJECT(argPtr);
/*
* If we can't create the thread, we have to increment numSocketFree.
*
* ref -> BUG-LE01
* 15/10/2009 - Donato Capitella
*/
if(!CWErr(CWThreadMutexLock(&appsManager.numSocketFreeMutex))) {
CWLog("Error locking numSocketFree Mutex");
return NULL;
}
appsManager.numSocketFree++;
CWThreadMutexUnlock(&appsManager.numSocketFreeMutex);
}
}
else {
CWThreadMutexUnlock(&appsManager.numSocketFreeMutex);
/****************************************************************
* There isn't space for another client, send error answer. *
***************************************************************/
if ( Writen(conn_sock, &clientFull, sizeof(int) ) < 0 ) {
printf("Error on sending Error Message\n");
close(conn_sock);
}
}
}
else
CWLog("Error on accept (applications) system call");
}
CWDestroyThreadMutex(&appsManager.numSocketFreeMutex);
close(listen_sock);
}
int is_valid_wtp_index(int wtpIndex)
{
if (wtpIndex < gMaxWTPs && gWTPs[wtpIndex].isNotFree)
return CW_TRUE;
return CW_FALSE;
}
/*
* Steven's readn().
*/
int readn(int fd, void *vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nread = recv(fd, ptr, nleft, 0)) < 0) {
if (errno == EINTR)
nread = 0; /* and call read() again */
else
return(-1);
} else if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
return(n - nleft); /* return >= 0 */
}
int Readn(int fd, void *ptr, size_t nbytes)
{
int n;
if ( (n = readn(fd, ptr, nbytes)) < 0) {
CWLog("Error while reading data from socket.");
return -1;
}
return n;
}