forked from iosifpeterfi/openCAPWAP-OpenWRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CWOpenSSLBio.c
253 lines (205 loc) · 6.28 KB
/
CWOpenSSLBio.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
/*******************************************************************************************
* 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 "CWCommon.h"
#ifdef DMALLOC
#include "../dmalloc-5.5.0/dmalloc.h"
#endif
static int memory_write(BIO * h, const char *buf, int num);
static int memory_read(BIO * h, char *buf, int size);
static int memory_puts(BIO * h, const char *str);
static long memory_ctrl(BIO * h, int cmd, long arg1, void *arg2);
static int memory_new(BIO * h);
static int memory_free(BIO * data);
#ifndef IP_MTU
#define IP_MTU 14
#endif
typedef struct {
CWSocket sock;
CWNetworkLev4Address sendAddress;
CWSafeList *pRecvAddress;
unsigned int nMtu;
} BIO_memory_data;
static BIO_METHOD methods_memory = {
BIO_TYPE_DGRAM,
"memory packet",
memory_write,
memory_read,
memory_puts,
NULL, /* dgram_gets, */
memory_ctrl,
memory_new,
memory_free,
NULL,
};
BIO_METHOD *BIO_s_memory(void)
{
return (&methods_memory);
}
BIO *BIO_new_memory(CWSocket sock, CWNetworkLev4Address * pSendAddress, CWSafeList * pRecvAddress)
{
BIO *ret;
BIO_memory_data *pData;
ret = BIO_new(BIO_s_memory());
if (ret == NULL)
return NULL;
//
pData = (BIO_memory_data *) ret->ptr;
pData->sock = sock;
memcpy(&pData->sendAddress, pSendAddress, sizeof(CWNetworkLev4Address));
pData->pRecvAddress = pRecvAddress;
return ret;
}
static int memory_new(BIO * bi)
{
bi->init = 1;
bi->num = 0;
bi->flags = 0;
bi->ptr = (char *)malloc(sizeof(BIO_memory_data));
return 1;
}
static int memory_free(BIO * a)
{
if (a == NULL)
return 0;
free(a->ptr);
return 1;
}
static int memory_read(BIO * b, char *out, int outl)
{
int ret = -1;
char *buf;
int size;
BIO_memory_data *pData = (BIO_memory_data *) b->ptr;
//
//BIO_clear_retry_flags(b);
//
CWLockSafeList(pData->pRecvAddress);
// Used only in DTLS handshake
while (CWGetCountElementFromSafeList(pData->pRecvAddress) == 0) {
CWWaitElementFromSafeList(pData->pRecvAddress);
}
buf = (char *)CWRemoveHeadElementFromSafeList(pData->pRecvAddress, &size);
CWUnlockSafeList(pData->pRecvAddress);
if ((buf == NULL) || (size <= 0))
CWLog("Warning empty buffer");
else {
ret = ((size < outl) ? size : outl) - 4;
memcpy(out, buf + 4, ret);
CW_FREE_OBJECT(buf);
}
return ret;
}
static int memory_write(BIO * b, const char *in, int inl)
{
int ret = -1;
char strBuffer[MAX_UDP_PACKET_SIZE];
BIO_memory_data *pData = (BIO_memory_data *) b->ptr;
//
strBuffer[0] = (char)(CW_PROTOCOL_VERSION << 4) | (char)(CW_PACKET_CRYPT);
strBuffer[1] = strBuffer[2] = strBuffer[3] = 0;
//
memcpy(&strBuffer[4], in, inl);
//
errno = 0;
ret =
sendto(pData->sock, strBuffer, inl + 4, 0, (struct sockaddr *)&pData->sendAddress,
sizeof(struct sockaddr_storage));
//BIO_clear_retry_flags(b);
if (ret <= 0) {
if (errno == EINTR)
BIO_set_retry_write(b);
} else {
ret -= 4;
}
return ret;
}
static long memory_ctrl(BIO * b, int cmd, long num, void *ptr)
{
long ret = 1;
long sockopt_val = 0;
unsigned int sockopt_len = 0;
BIO_memory_data *pData = (BIO_memory_data *) b->ptr;
switch (cmd) {
case BIO_CTRL_RESET:
ret = 0;
break;
case BIO_CTRL_EOF:
ret = 0;
break;
case BIO_CTRL_INFO:
ret = 0;
break;
case BIO_CTRL_GET_CLOSE:
ret = 0;
break;
case BIO_CTRL_SET_CLOSE:
break;
case BIO_CTRL_WPENDING:
ret = 0;
break;
case BIO_CTRL_PENDING:
ret = 0;
break;
case BIO_CTRL_DUP:
ret = 1;
break;
case BIO_CTRL_FLUSH:
ret = 1;
break;
case BIO_CTRL_PUSH:
ret = 0;
break;
case BIO_CTRL_POP:
ret = 0;
case BIO_CTRL_DGRAM_QUERY_MTU:{
sockopt_len = sizeof(sockopt_val);
if ((ret = getsockopt(pData->sock, IPPROTO_IP, IP_MTU, (void *)&sockopt_val, &sockopt_len)) < 0
|| sockopt_val < 0) {
ret = 0;
} else {
pData->nMtu = sockopt_val;
ret = sockopt_val;
}
break;
}
case BIO_CTRL_DGRAM_GET_MTU:
ret = pData->nMtu;
break;
case BIO_CTRL_DGRAM_SET_MTU:
pData->nMtu = num;
ret = num;
break;
default:
ret = 0;
break;
}
return ret;
}
static int memory_puts(BIO * bp, const char *str)
{
return memory_write(bp, str, strlen(str));
}