-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWNetwork.c
301 lines (241 loc) · 8.78 KB
/
CWNetwork.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
/*******************************************************************************************
* 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
CWNetworkLev3Service gNetworkPreferredFamily = CW_IPv4;
extern CWThreadMutex gSocketMutex;
/*
* Assume address is valid
*/
__inline__ int CWNetworkGetAddressSize(CWNetworkLev4Address *addrPtr) {
switch ( ((struct sockaddr*)(addrPtr))->sa_family ) {
#ifdef IPV6
/* IPv6 is defined in Stevens' library */
case AF_INET6:
return sizeof(struct sockaddr_in6);
break;
#endif
case AF_INET:
default:
return sizeof(struct sockaddr_in);
}
}
/*
* Send buf on an unconnected UDP socket. Unsafe means that we don't use DTLS.
*/
//not allow mutile thread send at same time
CWBool CWNetworkSendUnsafeUnconnected(CWSocket sock,
CWNetworkLev4Address *addrPtr,
const char *buf,
int len) {
int sendlen = -1;
if(buf == NULL || addrPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
CWUseSockNtop(addrPtr, CWDebugLog(str););
CWUseSockNtop(addrPtr, CWLog(str););
CWLog("CWNetworkSendUnsafeUnconnected send len = %d",len);
//no need gSocketMutex
#if 1
if(!CWThreadMutexLock(&gSocketMutex)) {
CWLog("Error Locking gSocketSendMutex, Fail !");
return CW_FALSE;
}
#endif
while((sendlen = sendto(sock, buf, len, 0, (struct sockaddr*)addrPtr, CWNetworkGetAddressSize(addrPtr))) <= 0) {
CWLog("CWNetworkSendUnsafeUnconnected <= 0 while, Fail !");
if(errno == EINTR) continue;
CWNetworkRaiseSystemError(CW_ERROR_SENDING);
return CW_FALSE;
}
if(sendlen != len)
{
CWLog("CWNetworkSendUnsafeUnconnected sendlen:%d, != len:%d, Fail !",sendlen,len);
CWNetworkRaiseSystemError(CW_ERROR_SENDING);
return CW_FALSE;
}
#if 1
CWThreadMutexUnlock(&gSocketMutex);
#endif
return CW_TRUE;
}
/*
* Send buf on a "connected" UDP socket. Unsafe means that we don't use DTLS.
*/
CWBool CWNetworkSendUnsafeConnected(CWSocket sock, const char *buf, int len) {
if(buf == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
while(send(sock, buf, len, 0) < 0) {
if(errno == EINTR) continue;
CWNetworkRaiseSystemError(CW_ERROR_SENDING);
}
return CW_TRUE;
}
/*
* Receive a datagram on an connected UDP socket (blocking).
* Unsafe means that we don't use DTLS.
*/
CWBool CWNetworkReceiveUnsafeConnected(CWSocket sock, char *buf, int len, int *readBytesPtr) {
if(buf == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
while((*readBytesPtr = recv(sock, buf, len, 0)) < 0) {
if(errno == EINTR) continue;
CWNetworkRaiseSystemError(CW_ERROR_RECEIVING);
}
return CW_TRUE;
}
/*
* Receive a datagram on an unconnected UDP socket (blocking).
* Unsafe means that we don't use DTLS.
*/
CWBool CWNetworkReceiveUnsafe(CWSocket sock,
char *buf,
int len,
int flags,
CWNetworkLev4Address *addrPtr,
int *readBytesPtr) {
socklen_t addrLen = sizeof(CWNetworkLev4Address);
if(buf == NULL || addrPtr == NULL || readBytesPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
//up down at sam time is ok
#if 0
if(!CWThreadMutexLock(&gSocketMutex)) {
CWLog("Error Locking gSocketSendMutex, Fail !");
return CW_FALSE;
}
#endif
while((*readBytesPtr = recvfrom(sock, buf, len, flags, (struct sockaddr*)addrPtr, &addrLen)) < 0) {
if(errno == EINTR) continue;
CWLog("CWNetworkReceiveUnsafe recvfrom Fail!");
CWNetworkRaiseSystemError(CW_ERROR_RECEIVING);
}
#if 0
CWThreadMutexUnlock(&gSocketMutex);
#endif
if(buf == NULL || *readBytesPtr == 0)
{
CWLog("CWNetworkReceiveUnsafe recvfrom buf == NULL || *readBytesPtr == 0,Fail!");
return CW_FALSE;
}
//CWLog("CWNetworkReceiveUnsafe *readBytesPtr = %d",*readBytesPtr);
return CW_TRUE;
}
/*
* Init network for client.
*/
CWBool CWNetworkInitSocketClient(CWSocket *sockPtr, CWNetworkLev4Address *addrPtr) {
int yes = 1;
/* NULL addrPtr means that we don't want to connect to a
* specific address
*/
if(sockPtr == NULL) return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
#ifdef IPv6
if(((*sockPtr)=socket((gNetworkPreferredFamily == CW_IPv4) ? AF_INET : AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
#else
if(((*sockPtr)=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
#endif
CWNetworkRaiseSystemError(CW_ERROR_CREATING);
}
if(addrPtr != NULL) {
CWUseSockNtop(((struct sockaddr*)addrPtr), CWDebugLog(str););
if(connect((*sockPtr), ((struct sockaddr*)addrPtr), CWNetworkGetAddressSize(addrPtr)) < 0) {
CWNetworkRaiseSystemError(CW_ERROR_CREATING);
}
}
/* allow sending broadcast packets */
setsockopt(*sockPtr, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
return CW_TRUE;
}
/*
* Wrapper for select
*/
CWBool CWNetworkTimedPollRead(CWSocket sock, struct timeval *timeout) {
int r;
fd_set fset;
if(timeout == NULL) return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
FD_ZERO(&fset);
FD_SET(sock, &fset);
if((r = select(sock+1, &fset, NULL, NULL, timeout)) == 0) {
CWDebugLog("Select Time Expired");
return CWErrorRaise(CW_ERROR_TIME_EXPIRED, NULL);
} else
if (r < 0) {
CWDebugLog("Select Error");
if(errno == EINTR){
CWDebugLog("Select Interrupted by signal");
return CWErrorRaise(CW_ERROR_INTERRUPTED, NULL);
}
CWNetworkRaiseSystemError(CW_ERROR_GENERAL);
}
return CW_TRUE;
}
/*
* Given an host int the form of C string (e.g. "192.168.1.2" or "localhost"),
* returns the address.
*/
CWBool CWNetworkGetAddressForHost(char *host, CWNetworkLev4Address *addrPtr) {
struct addrinfo hints, *res, *ressave;
char serviceName[5];
CWSocket sock;
if(host == NULL || addrPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
CW_ZERO_MEMORY(&hints, sizeof(struct addrinfo));
#ifdef IPv6
if(gNetworkPreferredFamily == CW_IPv6) {
hints.ai_family = AF_INET6;
hints.ai_flags = AI_V4MAPPED;
} else {
hints.ai_family = AF_INET;
}
#else
hints.ai_family = AF_INET;
#endif
hints.ai_socktype = SOCK_DGRAM;
/* endianness will be handled by getaddrinfo */
snprintf(serviceName, 5, "%d", CW_CONTROL_PORT);
if (getaddrinfo(host, serviceName, &hints, &res) !=0 ) {
return CWErrorRaise(CW_ERROR_GENERAL, "Can't resolve hostname");
}
ressave = res;
do {
if((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
/* try next address */
continue;
}
/* success */
break;
} while ( (res = res->ai_next) != NULL);
close(sock);
if(res == NULL) {
/* error on last iteration */
CWNetworkRaiseSystemError(CW_ERROR_CREATING);
}
CW_COPY_NET_ADDR_PTR(addrPtr, (res->ai_addr));
freeaddrinfo(ressave);
return CW_TRUE;
}