|
Lines 21-44
Link Here
|
| 21 |
|
21 |
|
| 22 |
#include "tptp/NoLog.h" |
22 |
#include "tptp/NoLog.h" |
| 23 |
|
23 |
|
|
|
24 |
// The following if-else was added for IPv6 support |
| 25 |
#ifdef _WIN32 |
| 26 |
#include <ws2tcpip.h> |
| 27 |
#else |
| 28 |
#define INVALID_SOCKET -1 |
| 29 |
#define SOCKET_ERROR -1 |
| 30 |
#endif |
| 31 |
|
| 32 |
static void convertAddrToIpString(struct sockaddr_storage * addr, char **hostname); |
| 33 |
static SOCKET getSocketFromAddrList(struct addrinfo *AddrInfo, struct addrinfo **sockAddrOut); |
| 24 |
|
34 |
|
| 25 |
/** |
35 |
/** |
| 26 |
********************************************************* |
36 |
********************************************************* |
| 27 |
* |
37 |
* |
| 28 |
* @brief |
38 |
* @brief |
| 29 |
* get the localhost socket connection |
39 |
* get the localhost (server) socket connection |
| 30 |
* |
40 |
* |
| 31 |
* @return |
41 |
* @param portNum The port number of the local host to listen on. |
| 32 |
* the raw unconnected socket |
42 |
* @param outResult A pointer to an array of SOCKETS to be listened on |
|
|
43 |
* @param outNumSockets The integer that is passed in as a pointer to this function is set to the number of sockets of the outResult array |
| 44 |
*********************************************************/ |
| 45 |
int getTheSocket(int portNum, SOCKET *outResult, int *outNumSockets) { |
| 46 |
int Family = PF_UNSPEC; |
| 47 |
int SocketType = SOCK_STREAM; |
| 48 |
char PortStr[8]; |
| 49 |
char *Address = NULL; |
| 50 |
int i, RetVal; |
| 51 |
|
| 52 |
struct addrinfo Hints, *AddrInfo, *AI; |
| 53 |
SOCKET* sockArr; |
| 54 |
|
| 55 |
SOCKET serverSocket; |
| 56 |
|
| 57 |
int ServSockSize = 0; |
| 58 |
|
| 59 |
sprintf(PortStr, "%d", portNum); |
| 60 |
|
| 61 |
sockArr = outResult; |
| 62 |
|
| 63 |
// MS' description of the following call: |
| 64 |
// By setting the AI_PASSIVE flag in the hints to getaddrinfo, we're |
| 65 |
// indicating that we intend to use the resulting address(es) to bind |
| 66 |
// to a socket(s) for accepting incoming connections. This means that |
| 67 |
// when the Address parameter is NULL, getaddrinfo will return one |
| 68 |
// entry per allowed protocol family containing the unspecified address |
| 69 |
// for that family. |
| 70 |
// |
| 71 |
memset(&Hints, 0, sizeof (Hints)); |
| 72 |
Hints.ai_family = Family; |
| 73 |
Hints.ai_socktype = SocketType; |
| 74 |
Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; |
| 75 |
RetVal = getaddrinfo(Address, PortStr, &Hints, &AddrInfo); |
| 76 |
if (RetVal != 0) { |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
// First check for IPv6 connections |
| 81 |
for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next) { |
| 82 |
|
| 83 |
// Highly unlikely, but check anyway. |
| 84 |
if (i == FD_SETSIZE) { |
| 85 |
TPTP_LOG_ERROR_MSG("getaddrinfo exceeded the number of available addrs"); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
|
| 89 |
// PF_INET6 only |
| 90 |
if (!(AI->ai_family == PF_INET6)) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
serverSocket = bindAndListen(AI); |
| 95 |
if(serverSocket >= 0) { |
| 96 |
sockArr[ServSockSize++] = serverSocket; |
| 97 |
} |
| 98 |
i++; |
| 99 |
} |
| 100 |
|
| 101 |
// Then check IPv4 |
| 102 |
for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next) { |
| 103 |
|
| 104 |
// Highly unlikely, but check anyway. |
| 105 |
if (i == FD_SETSIZE) { |
| 106 |
TPTP_LOG_ERROR_MSG("getaddrinfo returned more addresses than we could use."); |
| 107 |
return -1; |
| 108 |
} |
| 109 |
|
| 110 |
if (!(AI->ai_family == PF_INET)) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
// jwest: On our Linux machines, listening on IPv6 also listened on IPv4. So if we get an error here, "Address already in Use", we can ignore it. |
| 115 |
serverSocket = bindAndListen(AI); |
| 116 |
if(serverSocket >= 0) { |
| 117 |
sockArr[ServSockSize++] = serverSocket; |
| 118 |
} |
| 119 |
|
| 120 |
i++; |
| 121 |
} |
| 122 |
|
| 123 |
freeaddrinfo(AddrInfo); |
| 124 |
|
| 125 |
*outNumSockets = ServSockSize; |
| 126 |
|
| 127 |
if (ServSockSize == 0) { |
| 128 |
TPTP_LOG_ERROR_MSG("Fatal error: unable to serve on any address.") ; |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
********************************************************* |
| 137 |
* |
| 138 |
* @brief |
| 139 |
* bind and prepare the server socket |
| 33 |
* |
140 |
* |
|
|
141 |
* @return |
| 142 |
* 0 - Success |
| 143 |
* nonzero - Error. |
| 34 |
*********************************************************/ |
144 |
*********************************************************/ |
| 35 |
SOCKET getTheSocket(int portNum, struct sockaddr_in *pAddr) |
145 |
SOCKET bindAndListen(struct addrinfo *AI) { |
| 36 |
{ |
146 |
|
| 37 |
pAddr->sin_family = AF_INET; |
147 |
SOCKET ServSock; |
| 38 |
pAddr->sin_port = htons((u_short) portNum); |
148 |
|
| 39 |
pAddr->sin_addr.s_addr = htonl(INADDR_ANY); |
149 |
// Open a socket with the correct address family for this address. |
|
|
150 |
ServSock = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol); |
| 151 |
if (ServSock == INVALID_SOCKET) { |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
if (bind(ServSock, AI->ai_addr, (int)AI->ai_addrlen) == SOCKET_ERROR) { |
| 156 |
closeThisSocket(ServSock); |
| 157 |
return (-1); |
| 158 |
} |
| 159 |
|
| 160 |
if (listen(ServSock, 5) == SOCKET_ERROR) { |
| 161 |
closeThisSocket(ServSock); |
| 162 |
return (-1); |
| 163 |
} |
| 164 |
|
| 165 |
return (ServSock); |
| 40 |
|
166 |
|
| 41 |
return socket(AF_INET, SOCK_STREAM, 0) ; |
|
|
| 42 |
} |
167 |
} |
| 43 |
|
168 |
|
| 44 |
/** |
169 |
/** |
|
Lines 49-55
Link Here
|
| 49 |
* |
174 |
* |
| 50 |
* @return |
175 |
* @return |
| 51 |
* the port number |
176 |
* the port number |
| 52 |
* |
177 |
* IPV4-ONLY |
| 53 |
*********************************************************/ |
178 |
*********************************************************/ |
| 54 |
int getSocketPort(SOCKET sock) |
179 |
int getSocketPort(SOCKET sock) |
| 55 |
{ |
180 |
{ |
|
Lines 66-77
Link Here
|
| 66 |
********************************************************* |
191 |
********************************************************* |
| 67 |
* |
192 |
* |
| 68 |
* @brief |
193 |
* @brief |
| 69 |
* get the localhost info |
194 |
* Gets the localhost info. |
| 70 |
* |
195 |
* |
|
|
196 |
* Note: When resolving the addresses of the local host, this function will always return the |
| 197 |
* first in the list of addresses given to it by the operating system. For the actual list of local addresses, |
| 198 |
* use a different function. |
| 199 |
* |
| 200 |
* Returns NULL if no address could be obtained. |
| 201 |
* |
| 71 |
* @return |
202 |
* @return |
| 72 |
* host info data structure |
203 |
* host info data structure |
| 73 |
*********************************************************/ |
204 |
*********************************************************/ |
| 74 |
struct hostent * getHostInfo() |
205 |
struct sockaddr_storage* getHostInfo() |
|
|
206 |
{ |
| 207 |
int RetVal; |
| 208 |
char *Address = NULL; |
| 209 |
char *Port = "10002"; // Hardcoded because we need a port |
| 210 |
|
| 211 |
struct addrinfo Hints, *AddrInfo; |
| 212 |
struct sockaddr_storage *result = NULL; |
| 213 |
|
| 214 |
|
| 215 |
int Family = PF_UNSPEC; // Give us both the IPv4 and IPv6 addresses |
| 216 |
int SocketType = SOCK_STREAM; |
| 217 |
|
| 218 |
memset(&Hints, 0, sizeof (Hints)); |
| 219 |
Hints.ai_family = Family; |
| 220 |
Hints.ai_socktype = SocketType; |
| 221 |
Hints.ai_flags = AI_NUMERICHOST; |
| 222 |
|
| 223 |
RetVal = getaddrinfo(Address, Port, &Hints, &AddrInfo); |
| 224 |
|
| 225 |
if(RetVal == 0) { |
| 226 |
|
| 227 |
result = cloneSockAddr((struct sockaddr_storage *)AddrInfo->ai_addr); |
| 228 |
|
| 229 |
freeaddrinfo(AddrInfo); |
| 230 |
} |
| 231 |
|
| 232 |
return (result); |
| 233 |
} |
| 234 |
|
| 235 |
struct hostent * getHostInfoIPv4M() |
| 75 |
{ |
236 |
{ |
| 76 |
char localhost[MAX_HOST_NAME_LENGTH+1]; |
237 |
char localhost[MAX_HOST_NAME_LENGTH+1]; |
| 77 |
|
238 |
|
|
Lines 92-98
Link Here
|
| 92 |
* 0 success |
253 |
* 0 success |
| 93 |
* non-zero failure |
254 |
* non-zero failure |
| 94 |
*********************************************************/ |
255 |
*********************************************************/ |
| 95 |
int convertIPAddressStringToUlong( char *addrStr, unsigned long* addr ) |
256 |
int convertIPAddressStringToUlongIPv4M( char *addrStr, unsigned long* addr ) |
| 96 |
{ |
257 |
{ |
| 97 |
*addr = inet_addr( addrStr ); |
258 |
*addr = inet_addr( addrStr ); |
| 98 |
if ( *addr == INADDR_NONE ) |
259 |
if ( *addr == INADDR_NONE ) |
|
Lines 123-135
Link Here
|
| 123 |
*********************************************************/ |
284 |
*********************************************************/ |
| 124 |
int getSocketIPString( SOCKET sock, char** ipAddrStr ) |
285 |
int getSocketIPString( SOCKET sock, char** ipAddrStr ) |
| 125 |
{ |
286 |
{ |
|
|
287 |
struct sockaddr_storage *sockAddrPtr = NULL; |
| 288 |
|
| 289 |
if ( sock == (SOCKET)NULL ) |
| 290 |
{ |
| 291 |
sockAddrPtr = getHostInfo(); |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
sockAddrPtr = getSelfAddrOfSocket(sock); |
| 296 |
} |
| 297 |
|
| 298 |
if (sockAddrPtr == NULL) return -1; |
| 299 |
|
| 300 |
convertAddrToIpString(sockAddrPtr, ipAddrStr); |
| 301 |
if ( *ipAddrStr == NULL ) |
| 302 |
{ |
| 303 |
return TPTP_SYS_NO_MEM; |
| 304 |
} |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
int getSocketIPStringIPv4M( SOCKET sock, char** ipAddrStr ) |
| 309 |
{ |
| 126 |
char* temp; |
310 |
char* temp; |
| 127 |
|
311 |
|
| 128 |
if ( sock == (SOCKET)NULL ) |
312 |
if ( sock == (SOCKET)NULL ) |
| 129 |
{ |
313 |
{ |
| 130 |
struct hostent* hinfo; |
314 |
struct hostent* hinfo; |
| 131 |
|
315 |
|
| 132 |
hinfo = getHostInfo(); |
316 |
hinfo = getHostInfoIPv4M(); |
| 133 |
if (hinfo == NULL) return -1; |
317 |
if (hinfo == NULL) return -1; |
| 134 |
|
318 |
|
| 135 |
/* The socket code owns 'temp' as returned below, so we need to copy it */ |
319 |
/* The socket code owns 'temp' as returned below, so we need to copy it */ |
|
Lines 164-169
Link Here
|
| 164 |
return 0; |
348 |
return 0; |
| 165 |
} |
349 |
} |
| 166 |
|
350 |
|
|
|
351 |
|
| 167 |
/** |
352 |
/** |
| 168 |
********************************************************* |
353 |
********************************************************* |
| 169 |
* |
354 |
* |
|
Lines 183-235
Link Here
|
| 183 |
*********************************************************/ |
368 |
*********************************************************/ |
| 184 |
int getPeerIPString( SOCKET sock, char** ipAddrStr ) |
369 |
int getPeerIPString( SOCKET sock, char** ipAddrStr ) |
| 185 |
{ |
370 |
{ |
| 186 |
char* temp; |
371 |
struct sockaddr_storage *sockAddrPtr = NULL; |
| 187 |
|
372 |
|
| 188 |
if ( sock == (SOCKET)NULL ) |
373 |
if ( sock == (SOCKET)NULL ) |
| 189 |
{ |
374 |
{ |
| 190 |
return -1; |
375 |
return -1; |
| 191 |
} |
376 |
} |
| 192 |
else |
|
|
| 193 |
{ |
| 194 |
int saSize; |
| 195 |
struct sockaddr_in saddr; |
| 196 |
|
377 |
|
| 197 |
saddr.sin_family = AF_INET; |
378 |
sockAddrPtr = getPeerAddrOfSocket(sock); |
| 198 |
saddr.sin_port = htons(0); |
|
|
| 199 |
saddr.sin_addr.s_addr = INADDR_ANY; |
| 200 |
BZERO(&saddr.sin_zero,8); |
| 201 |
|
379 |
|
| 202 |
saSize = sizeof(struct sockaddr); |
380 |
if(sockAddrPtr == NULL) { |
| 203 |
//getsockname returns the local address to get the address |
381 |
TPTP_LOG_ERROR_MSG("Error: Unable to get the address information for the remote peer.") ; |
| 204 |
//of the clinet we need to call getpeername. |
|
|
| 205 |
getpeername(sock,(struct sockaddr *)&saddr,&saSize); |
| 206 |
|
| 207 |
/* The socket code owns 'temp' as returned below, so we need to copy it */ |
| 208 |
temp = inet_ntoa(saddr.sin_addr); |
| 209 |
} |
382 |
} |
| 210 |
|
383 |
|
| 211 |
//Need to add 1 to strlen so free doesn't crash! |
384 |
convertAddrToIpString(sockAddrPtr, ipAddrStr); |
| 212 |
*ipAddrStr = (char *)tptp_malloc( strlen(temp) +1); |
385 |
tptp_free(sockAddrPtr); |
| 213 |
if ( *ipAddrStr == NULL ) |
386 |
if ( *ipAddrStr == NULL ) |
| 214 |
{ |
387 |
{ |
| 215 |
return TPTP_SYS_NO_MEM; |
388 |
return TPTP_SYS_NO_MEM; |
| 216 |
} |
389 |
} |
| 217 |
|
390 |
|
| 218 |
strcpy( *ipAddrStr, temp ); |
|
|
| 219 |
|
| 220 |
return 0; |
391 |
return 0; |
| 221 |
} |
392 |
} |
| 222 |
|
393 |
|
| 223 |
/** |
394 |
|
| 224 |
********************************************************* |
395 |
struct hostent * getTargetHostInfoIPv4M(char* hostname) |
| 225 |
* |
|
|
| 226 |
* @brief |
| 227 |
* get the localhost info |
| 228 |
* |
| 229 |
* @return |
| 230 |
* host info data structure |
| 231 |
*********************************************************/ |
| 232 |
struct hostent * getTargetHostInfo(char* hostname) |
| 233 |
{ |
396 |
{ |
| 234 |
struct hostent *pInfo = NULL ; |
397 |
struct hostent *pInfo = NULL ; |
| 235 |
|
398 |
|
|
Lines 239-294
Link Here
|
| 239 |
return ( pInfo ); |
402 |
return ( pInfo ); |
| 240 |
} |
403 |
} |
| 241 |
|
404 |
|
| 242 |
|
|
|
| 243 |
/** |
405 |
/** |
| 244 |
********************************************************* |
406 |
********************************************************* |
| 245 |
* |
407 |
* |
| 246 |
* @brief |
408 |
* @brief |
| 247 |
* bind and prepare the server socket |
409 |
* get the localhost info |
| 248 |
* |
410 |
* |
| 249 |
* @return |
411 |
* @return |
| 250 |
* 0 - Success |
412 |
* host info data structure |
| 251 |
* nonzero - Error. |
|
|
| 252 |
*********************************************************/ |
413 |
*********************************************************/ |
| 253 |
int bindAndListen(SOCKET sock, struct sockaddr *pAddr) |
414 |
int convertHostnameToAddrs(char *hostname, struct sockaddr_storage *** outResult, int *outNumResults) { |
| 254 |
{ |
415 |
int RetVal; |
| 255 |
int rc = 0 ; |
416 |
int i; |
| 256 |
|
417 |
|
| 257 |
setsockopt(sock, |
418 |
struct addrinfo Hints, *AddrInfo, *AI; |
| 258 |
SOL_SOCKET, |
|
|
| 259 |
SO_REUSEADDR, |
| 260 |
0, |
| 261 |
0); |
| 262 |
|
419 |
|
|
|
420 |
int Family = PF_UNSPEC; // Give us both the IPv4 and IPv6 addresses |
| 421 |
int SocketType = SOCK_STREAM; // TCP only |
| 263 |
|
422 |
|
| 264 |
rc = bind(sock, pAddr, sizeof(struct sockaddr)) ; |
423 |
struct sockaddr_storage ** result; |
| 265 |
|
424 |
|
| 266 |
if (rc < 0) |
425 |
// malloc an array of pointers to struct sockaddr_storages |
| 267 |
{ |
426 |
result = (struct sockaddr_storage **)tptp_malloc(sizeof(struct sockaddr_storage *) * 128); |
| 268 |
TPTP_LOG_ERROR_MSG("Error: cannot bind to the socket.") ; |
427 |
|
| 269 |
//printCurrentSysError() ; |
428 |
memset(&Hints, 0, sizeof (Hints)); |
|
|
429 |
Hints.ai_family = Family; |
| 430 |
Hints.ai_socktype = SocketType; |
| 431 |
|
| 432 |
// This was originally in the code, but it prevents lookups of host names, e.g. www.google.com. It only works with numeric hosts. |
| 433 |
// Hints.ai_flags = AI_NUMERICHOST; |
| 434 |
|
| 435 |
RetVal = getaddrinfo(hostname, NULL, &Hints, &AddrInfo); |
| 436 |
|
| 437 |
for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next, i++) { |
| 438 |
result[i] = cloneSockAddr((struct sockaddr_storage *)AI->ai_addr); |
| 270 |
} |
439 |
} |
| 271 |
else |
440 |
freeaddrinfo(AddrInfo); |
| 272 |
{ |
|
|
| 273 |
int result; |
| 274 |
|
441 |
|
| 275 |
result = setHandleInherited((HANDLE)sock) ; |
442 |
*outResult = result; |
| 276 |
if ( result == 0 ) |
443 |
*outNumResults = i; |
| 277 |
{ |
444 |
|
| 278 |
TPTP_LOG_ERROR_MSG("Error: unable to set handle info.") ; |
445 |
return RetVal; |
| 279 |
rc = - 1; |
446 |
} |
| 280 |
} |
447 |
|
| 281 |
else |
448 |
|
| 282 |
{ |
449 |
/** |
| 283 |
rc = listen(sock, 100) ; |
450 |
********************************************************* |
| 284 |
if (rc < 0) |
451 |
* |
| 285 |
{ |
452 |
* @brief |
| 286 |
TPTP_LOG_ERROR_MSG("Error: unable to listen.") ; |
453 |
* frees acquired addresses list |
| 287 |
} |
454 |
* |
| 288 |
} |
455 |
* @return |
|
|
456 |
* host info data structure |
| 457 |
*********************************************************/ |
| 458 |
void freeAddressList(struct sockaddr_storage ** arrOfAddr, int numAddr) { |
| 459 |
int x = 0; |
| 460 |
|
| 461 |
for(x = 0; x < numAddr; x++) { |
| 462 |
tptp_free(arrOfAddr[x]); |
| 289 |
} |
463 |
} |
| 290 |
|
464 |
|
| 291 |
return ( rc ) ; |
|
|
| 292 |
} |
465 |
} |
| 293 |
|
466 |
|
| 294 |
/** |
467 |
/** |
|
Lines 300-305
Link Here
|
| 300 |
* @return |
473 |
* @return |
| 301 |
* 0 - Success |
474 |
* 0 - Success |
| 302 |
* nonzero - Error. |
475 |
* nonzero - Error. |
|
|
476 |
* IPV4-ONLY |
| 303 |
*********************************************************/ |
477 |
*********************************************************/ |
| 304 |
int getPeerName(SOCKET sock, unsigned char* pAddr) |
478 |
int getPeerName(SOCKET sock, unsigned char* pAddr) |
| 305 |
{ |
479 |
{ |
|
Lines 329-335
Link Here
|
| 329 |
SOCKET acceptSocketConnection(SOCKET serverSock) |
503 |
SOCKET acceptSocketConnection(SOCKET serverSock) |
| 330 |
{ |
504 |
{ |
| 331 |
SOCKET clientSock ; |
505 |
SOCKET clientSock ; |
| 332 |
struct sockaddr_in clientAddress; |
506 |
struct sockaddr_storage clientAddress; |
| 333 |
size_t clientLen=sizeof(clientAddress); |
507 |
size_t clientLen=sizeof(clientAddress); |
| 334 |
int rc = 0 ; |
508 |
int rc = 0 ; |
| 335 |
int enable=1; |
509 |
int enable=1; |
|
Lines 385-391
Link Here
|
| 385 |
* 0 - Success and the socket representing this connection |
559 |
* 0 - Success and the socket representing this connection |
| 386 |
* nonzero - Error. |
560 |
* nonzero - Error. |
| 387 |
*********************************************************/ |
561 |
*********************************************************/ |
| 388 |
int connectToTCPServer(unsigned long ipAddr, |
562 |
int connectToTCPServer(char * hostname, int port, SOCKET *pSock) |
|
|
563 |
{ |
| 564 |
int rc = 0 ; |
| 565 |
SOCKET sock ; |
| 566 |
struct addrinfo *AI = NULL; |
| 567 |
|
| 568 |
TPTP_LOG_DEBUG_MSG("connectToTCPServer...") ; |
| 569 |
|
| 570 |
sock = connectToHostWithHostname(hostname, port, &AI); |
| 571 |
|
| 572 |
freeaddrinfo(AI); |
| 573 |
|
| 574 |
*pSock = sock ; |
| 575 |
|
| 576 |
return ( rc ) ; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
int connectToTCPServerIPv4M(unsigned long ipAddr, |
| 389 |
unsigned short portNum, |
581 |
unsigned short portNum, |
| 390 |
SOCKET *pSock) |
582 |
SOCKET *pSock) |
| 391 |
{ |
583 |
{ |
|
Lines 446-451
Link Here
|
| 446 |
********************************************************* |
638 |
********************************************************* |
| 447 |
* |
639 |
* |
| 448 |
* @brief |
640 |
* @brief |
|
|
641 |
* connect to the given system |
| 642 |
* |
| 643 |
* @return |
| 644 |
* 0 - Success and the socket representing this connection |
| 645 |
* nonzero - Error. |
| 646 |
*********************************************************/ |
| 647 |
int connectToTCPServerAddr(struct sockaddr_storage * addr, int port, SOCKET *pSock) |
| 648 |
{ |
| 649 |
int rc = 0 ; |
| 650 |
SOCKET sock ; |
| 651 |
|
| 652 |
TPTP_LOG_DEBUG_MSG("connectToTCPServerAddr...") ; |
| 653 |
|
| 654 |
setSockaddrStoragePort(addr, port); |
| 655 |
|
| 656 |
sock = connectToHostWithAddr(addr); |
| 657 |
|
| 658 |
*pSock = sock ; |
| 659 |
|
| 660 |
return ( rc ) ; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
/** |
| 667 |
********************************************************* |
| 668 |
* |
| 669 |
* @brief |
| 449 |
* common interface to terminate the given connection |
670 |
* common interface to terminate the given connection |
| 450 |
* (socket). |
671 |
* (socket). |
| 451 |
* |
672 |
* |
|
Lines 480-482
Link Here
|
| 480 |
return closeThisSocket(socket); |
701 |
return closeThisSocket(socket); |
| 481 |
} |
702 |
} |
| 482 |
#endif |
703 |
#endif |
|
|
704 |
|
| 705 |
struct sockaddr_storage * cloneSockAddr(struct sockaddr_storage * addr) { |
| 706 |
struct sockaddr_storage* result; |
| 707 |
|
| 708 |
if(addr->ss_family == PF_INET){ |
| 709 |
result = (struct sockaddr_storage *)tptp_malloc(sizeof(struct sockaddr_in)); |
| 710 |
memcpy(result, (struct sockaddr_in *)addr,sizeof(struct sockaddr_in)); |
| 711 |
|
| 712 |
} else if(addr->ss_family == PF_INET6) { |
| 713 |
result = (struct sockaddr_storage *) tptp_malloc(sizeof(struct sockaddr_in6)); |
| 714 |
memcpy(result, (struct sockaddr_in6 *)addr, sizeof(struct sockaddr_in6)); |
| 715 |
} else { |
| 716 |
result = (struct sockaddr_storage *) NULL; |
| 717 |
} |
| 718 |
|
| 719 |
return result; |
| 720 |
} |
| 721 |
|
| 722 |
static void convertAddrToIpString(struct sockaddr_storage * addr, char **hostname) { |
| 723 |
int r; |
| 724 |
|
| 725 |
*hostname = (char *)tptp_malloc(sizeof(char) * NI_MAXHOST); |
| 726 |
if(*hostname == NULL) return; |
| 727 |
|
| 728 |
memset(*hostname, 0, sizeof(char) * NI_MAXHOST); |
| 729 |
// hostname[0] = 0; |
| 730 |
|
| 731 |
r = getnameinfo((struct sockaddr *)addr, sizeof(struct sockaddr_storage), *hostname, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); |
| 732 |
} |
| 733 |
|
| 734 |
/** SOCKET -> struct sockaddr_storage * for the peer. */ |
| 735 |
struct sockaddr_storage * getPeerAddrOfSocket(SOCKET ConnSocket) { |
| 736 |
int result = 0; |
| 737 |
int addrLen = 0; |
| 738 |
struct sockaddr_storage * addr; |
| 739 |
|
| 740 |
addrLen = sizeof(struct sockaddr_storage); |
| 741 |
addr = (struct sockaddr_storage *)tptp_malloc(sizeof(struct sockaddr_storage)); |
| 742 |
|
| 743 |
result = getpeername(ConnSocket, (struct sockaddr *) addr, &addrLen); |
| 744 |
|
| 745 |
if(result == SOCKET_ERROR) { |
| 746 |
TPTP_LOG_ERROR_MSG("getpeername call failed to get peer address") ; |
| 747 |
return NULL; |
| 748 |
} |
| 749 |
|
| 750 |
return addr; |
| 751 |
} |
| 752 |
|
| 753 |
/** SOCKET => struct sockaddr_storage *, of the host's side of the socket. For the peer's address, use getPeerAddrOfSocket. */ |
| 754 |
struct sockaddr_storage * getSelfAddrOfSocket(SOCKET ConnSocket) { |
| 755 |
struct sockaddr_storage Addr; |
| 756 |
struct sockaddr_storage *result; |
| 757 |
int r = 0; |
| 758 |
|
| 759 |
socklen_t AddrLen = 0; |
| 760 |
|
| 761 |
AddrLen = sizeof (Addr); |
| 762 |
|
| 763 |
r = getsockname(ConnSocket, (struct sockaddr *) & Addr, &AddrLen); |
| 764 |
|
| 765 |
if(r != 0) return NULL; |
| 766 |
|
| 767 |
result = (struct sockaddr_storage *)tptp_malloc(sizeof(struct sockaddr_storage)); |
| 768 |
|
| 769 |
memcpy(result, &Addr, sizeof(struct sockaddr_storage)); |
| 770 |
|
| 771 |
return result; |
| 772 |
} |
| 773 |
|
| 774 |
/** Creates a client socket connection to the given hostname/ip, on the given port. The sockAddrOut variable is set to the particular |
| 775 |
struct addrinfo that was used to create the connection. This allows you to determine the family, either IPV4 of IPV6 (if needed.) |
| 776 |
|
| 777 |
If a hostname is specified in 'hostnameOrIp', this method will try to connect on both the IPv4 and IPv6 addresses of the hostname, where available. It |
| 778 |
returns the first successful SOCKET connection as a result. |
| 779 |
|
| 780 |
If an IP address is specified in 'hostnameOrIp', this method will only connect on either the IPv4 (x.x.x.x) or IPv6 (xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx) |
| 781 |
address of that host (based on the format of the ip address that is based in). |
| 782 |
*/ |
| 783 |
SOCKET connectToHostWithHostname(char * hostnameOrIp, int port, struct addrinfo ** sockAddrOut ) { |
| 784 |
int Family = PF_UNSPEC; |
| 785 |
int SocketType = SOCK_STREAM; // TCP |
| 786 |
int RetVal; |
| 787 |
char portStr[8]; |
| 788 |
struct addrinfo Hints, *AddrInfo; |
| 789 |
SOCKET ConnSocket; |
| 790 |
|
| 791 |
// Convert the integer port to a string for getaddrinfo |
| 792 |
sprintf(portStr, "%d", port); |
| 793 |
|
| 794 |
// |
| 795 |
// By not setting the AI_PASSIVE flag in the hints to getaddrinfo, we're |
| 796 |
// indicating that we intend to use the resulting address(es) to connect |
| 797 |
// to a service. This means that when the Server parameter is NULL, |
| 798 |
// getaddrinfo will return one entry per allowed protocol family |
| 799 |
// containing the loopback address for that family. |
| 800 |
// |
| 801 |
|
| 802 |
memset(&Hints, 0, sizeof (Hints)); |
| 803 |
Hints.ai_family = Family; |
| 804 |
Hints.ai_socktype = SocketType; |
| 805 |
RetVal = getaddrinfo(hostnameOrIp, portStr, &Hints, &AddrInfo); |
| 806 |
|
| 807 |
if (RetVal != 0) { |
| 808 |
return -1; |
| 809 |
} |
| 810 |
|
| 811 |
if (AddrInfo == NULL) { |
| 812 |
return -1; |
| 813 |
} |
| 814 |
|
| 815 |
ConnSocket = getSocketFromAddrList(AddrInfo, sockAddrOut); |
| 816 |
|
| 817 |
return ConnSocket; |
| 818 |
} |
| 819 |
|
| 820 |
/** Connects to the given address, and returns a SOCKET for that conncetion. If a socket could not be created, |
| 821 |
then INVALID_SOCKET is returned. Note: The struct sockaddr_storage must have a port specified. */ |
| 822 |
SOCKET connectToHostWithAddr(struct sockaddr_storage *addr) { |
| 823 |
SOCKET sock; |
| 824 |
int enable = 1 ; |
| 825 |
BOOL nagle = FALSE; |
| 826 |
struct linger linger; |
| 827 |
int rc = 0 ; |
| 828 |
int loopcnt = 0; |
| 829 |
|
| 830 |
// Open a socket with the correct address family for this address. |
| 831 |
sock = socket(addr->ss_family, SOCK_STREAM, 0); |
| 832 |
|
| 833 |
setsockopt(sock, |
| 834 |
SOL_SOCKET, |
| 835 |
SO_KEEPALIVE, |
| 836 |
(const char*)&enable, |
| 837 |
sizeof(enable)); |
| 838 |
|
| 839 |
linger.l_onoff=1; |
| 840 |
linger.l_linger=3; |
| 841 |
setsockopt(sock, |
| 842 |
SOL_SOCKET, |
| 843 |
SO_LINGER, |
| 844 |
(const char*)&linger, |
| 845 |
sizeof(linger)); |
| 846 |
|
| 847 |
setsockopt(sock, |
| 848 |
IPPROTO_TCP, |
| 849 |
TCP_NODELAY, |
| 850 |
(const char*)&nagle, |
| 851 |
sizeof(nagle)); |
| 852 |
|
| 853 |
/* go make the connection */ |
| 854 |
#ifndef _WIN32 |
| 855 |
do { |
| 856 |
rc = connect(sock, (struct sockaddr*)addr, sizeof(struct sockaddr_storage)); |
| 857 |
loopcnt++; |
| 858 |
} while (rc == -1 && errno == EINTR && loopcnt < 50); |
| 859 |
#else |
| 860 |
rc = connect(sock, (struct sockaddr*)addr, sizeof(struct sockaddr_storage)) ; |
| 861 |
#endif |
| 862 |
|
| 863 |
/* |
| 864 |
if (connect(sock, (struct sockaddr *)addr, sizeof(struct sockaddr_storage)) == SOCKET_ERROR) { |
| 865 |
closeThisSocket(sock); |
| 866 |
return INVALID_SOCKET; |
| 867 |
}*/ |
| 868 |
|
| 869 |
return sock; |
| 870 |
} |
| 871 |
|
| 872 |
/** Sets the port of a struct sockaddr_storage. */ |
| 873 |
void setSockaddrStoragePort(struct sockaddr_storage *addr, int port) { |
| 874 |
if(addr->ss_family == PF_INET) { |
| 875 |
struct sockaddr_in *saddr = (struct sockaddr_in *)addr; |
| 876 |
saddr->sin_port = htons(port); |
| 877 |
|
| 878 |
} else if(addr->ss_family == PF_INET6) { |
| 879 |
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *) addr; |
| 880 |
|
| 881 |
saddr->sin6_port = htons(port); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
/** Given a list of addresses (AddrInfo), it returns a SOCKET for the first one it is able to successfully connect to. The socket is returned |
| 886 |
as the return value from this function, and the the address of that socket is returned in sockAddrOut. |
| 887 |
Note: If a connection cannot be made, INVALID_SOCKET is returned. */ |
| 888 |
static SOCKET getSocketFromAddrList(struct addrinfo *AddrInfo, struct addrinfo **sockAddrOut) { |
| 889 |
struct addrinfo *AI; |
| 890 |
SOCKET ConnSocket = INVALID_SOCKET; |
| 891 |
char AddrName[NI_MAXHOST]; |
| 892 |
|
| 893 |
if(sockAddrOut != NULL) { |
| 894 |
*sockAddrOut = 0; |
| 895 |
} |
| 896 |
|
| 897 |
// |
| 898 |
// Try each address getaddrinfo returned, until we find one to which |
| 899 |
// we can successfully connect. |
| 900 |
// |
| 901 |
for (AI = AddrInfo; AI != NULL; AI = AI->ai_next) { |
| 902 |
|
| 903 |
// Open a socket with the correct address family for this address. (on error -1 is returned) |
| 904 |
ConnSocket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol); |
| 905 |
|
| 906 |
if (ConnSocket == INVALID_SOCKET) { |
| 907 |
continue; |
| 908 |
} |
| 909 |
|
| 910 |
// printf("Attempting to connect to: %s\n", Server ? Server : "localhost"); |
| 911 |
if (connect(ConnSocket, AI->ai_addr, (int)AI->ai_addrlen) != SOCKET_ERROR) { |
| 912 |
if(sockAddrOut != NULL) { |
| 913 |
*sockAddrOut = AI; |
| 914 |
} |
| 915 |
break; |
| 916 |
} |
| 917 |
|
| 918 |
if (getnameinfo(AI->ai_addr, (socklen_t)AI->ai_addrlen, AddrName, sizeof (AddrName), NULL, 0, NI_NUMERICHOST) != 0) { |
| 919 |
} |
| 920 |
|
| 921 |
closeThisSocket(ConnSocket); |
| 922 |
ConnSocket = INVALID_SOCKET; |
| 923 |
} |
| 924 |
|
| 925 |
return ConnSocket; |
| 926 |
} |
| 927 |
|
| 928 |
/** struct sockaddr_storage * => IP address in byte format (e.g. in_addr / in6_addr) + family |
| 929 |
The bytes are stored in outAddr, and the family is stored in family.*/ |
| 930 |
void getByteFormatFromSockAddr(struct sockaddr_storage *addr, void ** outAddr, int *family) { |
| 931 |
|
| 932 |
if(addr->ss_family == PF_INET) { |
| 933 |
|
| 934 |
struct sockaddr_in * sin = (struct sockaddr_in *) addr; |
| 935 |
struct in_addr ia = sin->sin_addr; |
| 936 |
|
| 937 |
struct in_addr *result = (struct in_addr *)tptp_malloc(sizeof(struct in_addr)); |
| 938 |
memcpy(result, &ia, sizeof(ia)); |
| 939 |
|
| 940 |
*family = PF_INET; |
| 941 |
|
| 942 |
*outAddr = result; |
| 943 |
|
| 944 |
} else { // PF_INET6 |
| 945 |
|
| 946 |
struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) addr; |
| 947 |
struct in6_addr ia = sin6->sin6_addr; |
| 948 |
struct in6_addr *result = (struct in6_addr *)tptp_malloc(sizeof(struct in6_addr)); |
| 949 |
memset(result, 0, sizeof(struct in6_addr)); |
| 950 |
|
| 951 |
memcpy(result, &ia, sizeof(struct in6_addr)); |
| 952 |
*family = PF_INET6; |
| 953 |
|
| 954 |
*outAddr = result; |
| 955 |
|
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
/** Given the IP Address in byte format (e.g. in_addr/inaddr_6) and the family |
| 960 |
=> struct sockaddr_storage * (either sockaddr_in or sockaddr_in6) */ |
| 961 |
void getSockAddrFromByteFormat(void *inAddr, int family, struct sockaddr_storage **outResult) { |
| 962 |
|
| 963 |
if(family == PF_INET) { |
| 964 |
struct in_addr * ia = (struct in_addr *)inAddr; |
| 965 |
struct sockaddr_in * result = (struct sockaddr_in *)tptp_malloc(sizeof(struct sockaddr_in)); |
| 966 |
|
| 967 |
memcpy(&result->sin_addr, ia, sizeof(struct in_addr)); |
| 968 |
result->sin_family = family; |
| 969 |
result->sin_port = htons(0); |
| 970 |
memset(result->sin_zero, 0, 8); |
| 971 |
|
| 972 |
*outResult = (struct sockaddr_storage*)result; |
| 973 |
|
| 974 |
} else { // PF_INET6 |
| 975 |
|
| 976 |
struct in6_addr * ia = (struct in6_addr *)inAddr; |
| 977 |
struct sockaddr_in6 * result = (struct sockaddr_in6 *) tptp_malloc(sizeof(struct sockaddr_in6)); |
| 978 |
|
| 979 |
memcpy(&result->sin6_addr, ia, sizeof(struct in6_addr)); |
| 980 |
result->sin6_family = family; |
| 981 |
result->sin6_port = htons(0); |
| 982 |
result->sin6_flowinfo = 0; |
| 983 |
result->sin6_scope_id = 0; |
| 984 |
|
| 985 |
*outResult = (struct sockaddr_storage*)result; |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
void initSocketAccept(socket_accept_t * sat) { |
| 990 |
tptp_list_init(&(sat->serverSocketList)); |
| 991 |
tptp_list_init(&(sat->socketsToProcess)); |
| 992 |
|
| 993 |
tptp_initializeLock(&sat->sockProcLock); |
| 994 |
|
| 995 |
} |
| 996 |
|
| 997 |
typedef struct { |
| 998 |
socket_accept_t * sat; |
| 999 |
SOCKET * serverSocket; |
| 1000 |
|
| 1001 |
} listenOnSocket_parameters_t; |
| 1002 |
|
| 1003 |
/** Used by acceptSocketConnections. Parameter is a listenOnSocket_parameters_t pointer. */ |
| 1004 |
THREAD_USER_FUNC_RET_TYPE listenAcceptSocketConnections(LPVOID args) |
| 1005 |
{ |
| 1006 |
SOCKET *serverSock; |
| 1007 |
SOCKET clientSock; |
| 1008 |
SOCKET *outClientSock; |
| 1009 |
socket_accept_t * sat; |
| 1010 |
struct sockaddr_storage clientAddress; |
| 1011 |
size_t clientLen=sizeof(clientAddress); |
| 1012 |
|
| 1013 |
listenOnSocket_parameters_t *lpt; |
| 1014 |
|
| 1015 |
lpt = (listenOnSocket_parameters_t *) args; |
| 1016 |
|
| 1017 |
serverSock = lpt->serverSocket; |
| 1018 |
sat = lpt->sat; |
| 1019 |
|
| 1020 |
while(1) { |
| 1021 |
// Listen on the socket that we have been passed |
| 1022 |
clientSock = accept(*serverSock, (struct sockaddr * ) & clientAddress, (int *)&clientLen); |
| 1023 |
|
| 1024 |
if(clientSock == INVALID_SOCKET) { |
| 1025 |
TPTP_LOG_ERROR_MSG("An invalid socket was returned by an accept() call.") ; |
| 1026 |
continue; |
| 1027 |
} |
| 1028 |
|
| 1029 |
// Add the socket to the list of sockets |
| 1030 |
|
| 1031 |
tptp_getWriteLock(&(sat->sockProcLock)); |
| 1032 |
|
| 1033 |
outClientSock = (SOCKET *) tptp_malloc(sizeof(SOCKET)); |
| 1034 |
*outClientSock = clientSock; |
| 1035 |
|
| 1036 |
tptp_list_add(&(sat->socketsToProcess), outClientSock); |
| 1037 |
|
| 1038 |
tptp_releaseWriteLock(&(sat->sockProcLock)); |
| 1039 |
|
| 1040 |
} |
| 1041 |
|
| 1042 |
return 0; |
| 1043 |
|
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
/** |
| 1048 |
Spawns threads to accept() on a list of given sockets, and returns the SOCKET from those threads. |
| 1049 |
*/ |
| 1050 |
SOCKET acceptSocketConnections(SOCKET * sockets, int numSockets, socket_accept_t * sockAcceptState) { |
| 1051 |
int i = 0; |
| 1052 |
tptp_node_t * node; |
| 1053 |
tptp_list_t * socketsMonitored = &(sockAcceptState->serverSocketList); |
| 1054 |
SOCKET * s; |
| 1055 |
SOCKET result; |
| 1056 |
int matchFound = 0; |
| 1057 |
TID threadID; |
| 1058 |
HANDLE threadHandle; |
| 1059 |
int socketsWaiting = 0; |
| 1060 |
|
| 1061 |
listenOnSocket_parameters_t *params; |
| 1062 |
|
| 1063 |
// Given the list of sockets, scan through and see if we are being asked to listen on any new ones |
| 1064 |
for(i = 0; i < numSockets; i++) { |
| 1065 |
s = &(sockets[i]); |
| 1066 |
matchFound = 0; |
| 1067 |
|
| 1068 |
// Scan through the list of sockets that we're already monitoring: check if there are any new additions |
| 1069 |
node = socketsMonitored->head; |
| 1070 |
while(node != NULL) { |
| 1071 |
if(node->data == s) { |
| 1072 |
matchFound = 1; |
| 1073 |
break; |
| 1074 |
} |
| 1075 |
node = node->next; |
| 1076 |
} |
| 1077 |
|
| 1078 |
if(!matchFound) { |
| 1079 |
tptp_list_add(socketsMonitored, s); |
| 1080 |
params = (listenOnSocket_parameters_t*) tptp_malloc(sizeof(listenOnSocket_parameters_t)); |
| 1081 |
params->sat = sockAcceptState; |
| 1082 |
params->serverSocket = s; |
| 1083 |
|
| 1084 |
tptpStartThread(listenAcceptSocketConnections, params, &threadID, &threadHandle) ; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
do { |
| 1089 |
// Check if there are any client sockets waiting... |
| 1090 |
tptp_getReadLock(&(sockAcceptState->sockProcLock)); |
| 1091 |
|
| 1092 |
socketsWaiting = (sockAcceptState->socketsToProcess).count; |
| 1093 |
|
| 1094 |
tptp_releaseReadLock(&(sockAcceptState->sockProcLock)); |
| 1095 |
|
| 1096 |
if(!socketsWaiting) { |
| 1097 |
Sleep(10); |
| 1098 |
} |
| 1099 |
|
| 1100 |
} while(!socketsWaiting); // While (there are no new sockets) |
| 1101 |
|
| 1102 |
tptp_getWriteLock(&(sockAcceptState->sockProcLock)); |
| 1103 |
|
| 1104 |
// Get the value at the top of the list. |
| 1105 |
node = sockAcceptState->socketsToProcess.head; |
| 1106 |
|
| 1107 |
result = *((SOCKET *)node->data); |
| 1108 |
|
| 1109 |
s = (SOCKET*)(node->data); |
| 1110 |
tptp_list_remove(&(sockAcceptState->socketsToProcess), s); // This calls frees the malloced socket. |
| 1111 |
|
| 1112 |
tptp_releaseWriteLock(&(sockAcceptState->sockProcLock)); |
| 1113 |
|
| 1114 |
return result; |
| 1115 |
|
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
/** A (binary) comparison of the contents of two addresses. |
| 1120 |
Note, it does not compare the port value, or any of the other sockaddr_in/sockaddr_in6 fields, merely the address field. |
| 1121 |
Also note, if the family of the two addresses differs, then this function will return FALSE. If you're looking for a way to compare IPv4 and |
| 1122 |
IPv6 addresses together, try comparing their resolved hostnames. */ |
| 1123 |
int compareAddresses(struct sockaddr_storage * one, struct sockaddr_storage * two) { |
| 1124 |
struct sockaddr_in * a4; |
| 1125 |
struct sockaddr_in * b4; |
| 1126 |
|
| 1127 |
struct sockaddr_in6 *a6; |
| 1128 |
struct sockaddr_in6 *b6; |
| 1129 |
int x = 0; |
| 1130 |
|
| 1131 |
unsigned char a; |
| 1132 |
unsigned char b; |
| 1133 |
|
| 1134 |
if(one->ss_family != two->ss_family) return 0; |
| 1135 |
|
| 1136 |
if(one->ss_family == PF_INET) { |
| 1137 |
a4 = (struct sockaddr_in *)one; |
| 1138 |
b4 = (struct sockaddr_in *)two; |
| 1139 |
|
| 1140 |
if(sizeof(a4->sin_addr) != sizeof(b4->sin_addr)) return 0; |
| 1141 |
|
| 1142 |
for(x = 0; x < sizeof(a4->sin_addr); x++) { |
| 1143 |
a = ((unsigned char*)&a4->sin_addr)[x]; |
| 1144 |
b = ((unsigned char*)&b4->sin_addr)[x]; |
| 1145 |
|
| 1146 |
if(a != b) return 0; |
| 1147 |
} |
| 1148 |
|
| 1149 |
} else if(one->ss_family == PF_INET6) { |
| 1150 |
a6 = (struct sockaddr_in6 *)one; |
| 1151 |
b6 = (struct sockaddr_in6 *)two; |
| 1152 |
|
| 1153 |
if(sizeof(a6->sin6_addr) != sizeof(b6->sin6_addr)) return 0; |
| 1154 |
|
| 1155 |
for(x = 0; x < sizeof(a6->sin6_addr); x++) { |
| 1156 |
a = ((unsigned char*)&a6->sin6_addr)[x]; |
| 1157 |
b = ((unsigned char*)&b6->sin6_addr)[x]; |
| 1158 |
|
| 1159 |
if(a != b) return 0; |
| 1160 |
|
| 1161 |
} |
| 1162 |
|
| 1163 |
} else { |
| 1164 |
return 0; |
| 1165 |
} |
| 1166 |
|
| 1167 |
return 1; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/** sockaddr_storage -> "www.google.com" */ |
| 1171 |
char * convertAddrToHostname(struct sockaddr_storage *addr) { |
| 1172 |
char * hostname; |
| 1173 |
hostname = (char *)tptp_malloc(sizeof(char) * 260); |
| 1174 |
hostname[0] = 0; |
| 1175 |
|
| 1176 |
getnameinfo((struct sockaddr *)addr, sizeof(struct sockaddr_storage), hostname, 260, NULL, 0, 0); |
| 1177 |
|
| 1178 |
return hostname; |
| 1179 |
|
| 1180 |
} |
| 1181 |
|
| 1182 |
/** This method uses a number of tricks to determine whether or not the given address is local. */ |
| 1183 |
int isAddrLocal(struct sockaddr_storage * addr) { |
| 1184 |
int foundMatch = 0; |
| 1185 |
int x = 0; |
| 1186 |
struct sockaddr_storage ** addrList; |
| 1187 |
int addrListSize = 0; |
| 1188 |
|
| 1189 |
char *hostName; |
| 1190 |
char *localHostName = NULL; |
| 1191 |
|
| 1192 |
getLocalHostAddresses(&addrList, &addrListSize); |
| 1193 |
for(x = 0; x < addrListSize; x++) { |
| 1194 |
|
| 1195 |
if(!foundMatch) { |
| 1196 |
foundMatch = compareAddresses(addrList[x], addr); |
| 1197 |
|
| 1198 |
if(localHostName == NULL) { |
| 1199 |
localHostName = convertAddrToHostname(addrList[x]); |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
// If we couldn't match using binary comparison, resolve both hostnames and compare those, also try standard localhost addrs |
| 1205 |
if(!foundMatch) { |
| 1206 |
|
| 1207 |
hostName = convertAddrToHostname(addr); |
| 1208 |
|
| 1209 |
if(hostName != NULL && (strcmp(hostName, "::1") == 0 || strcmp(hostName, "127.0.0.1") == 0 || strcmp(hostName, "localhost") == 0)) { |
| 1210 |
foundMatch = 1; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if(localHostName != NULL && hostName != NULL && strcmp(localHostName, hostName) == 0 ) { |
| 1214 |
foundMatch = 1; |
| 1215 |
} |
| 1216 |
tptp_free(hostName); |
| 1217 |
} |
| 1218 |
|
| 1219 |
freeAddressList(addrList, addrListSize); |
| 1220 |
tptp_free(localHostName); |
| 1221 |
|
| 1222 |
return foundMatch; |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
/** Get localhost addresses in sockaddr_storage form. Note that sockaddr_storages received from this method use port |
| 1228 |
10002, and this will need to change if they are being listened on. */ |
| 1229 |
void getLocalHostAddresses(struct sockaddr_storage *** arrOfAddr, int * numAddr) { |
| 1230 |
int RetVal; |
| 1231 |
char *Address = NULL; |
| 1232 |
char *Port = "10002"; // One needs to specify a port, so I'm giving it this one. |
| 1233 |
int i = 0, size = 0, x = 0, y = 0; |
| 1234 |
struct sockaddr_storage ** addrs; |
| 1235 |
int matchFound = 0; |
| 1236 |
|
| 1237 |
char hostname[260]; |
| 1238 |
|
| 1239 |
struct sockaddr_storage ** resultArr = (struct sockaddr_storage **)tptp_malloc(sizeof(struct sockaddr_storage) * 64); |
| 1240 |
|
| 1241 |
struct addrinfo Hints, *AddrInfo = NULL, *AI; |
| 1242 |
|
| 1243 |
int Family = PF_UNSPEC; // Give us both the IPv4 and IPv6 addresses |
| 1244 |
int SocketType = SOCK_STREAM; |
| 1245 |
|
| 1246 |
*numAddr = 0; |
| 1247 |
|
| 1248 |
memset(&Hints, 0, sizeof (Hints)); |
| 1249 |
Hints.ai_family = Family; |
| 1250 |
Hints.ai_socktype = SocketType; |
| 1251 |
Hints.ai_flags = AI_NUMERICHOST; |
| 1252 |
|
| 1253 |
RetVal = getaddrinfo(Address, Port, &Hints, &AddrInfo); |
| 1254 |
|
| 1255 |
if(RetVal != 0) return; |
| 1256 |
|
| 1257 |
for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next) { |
| 1258 |
resultArr[i] = cloneSockAddr((struct sockaddr_storage *)AI->ai_addr); |
| 1259 |
|
| 1260 |
i++; |
| 1261 |
} |
| 1262 |
|
| 1263 |
freeaddrinfo(AddrInfo); |
| 1264 |
|
| 1265 |
// Grab the user's hostname |
| 1266 |
if(gethostname(hostname, 260) == 0) { |
| 1267 |
|
| 1268 |
// Resolve it to a list of IP addresses |
| 1269 |
convertHostnameToAddrs(hostname, &addrs, &size); |
| 1270 |
|
| 1271 |
// For each address... |
| 1272 |
for(x = 0; x< size; x++) { |
| 1273 |
matchFound = 0; |
| 1274 |
|
| 1275 |
// Verify that that address is not already in the list |
| 1276 |
for(y = 0; y < i; y++) { |
| 1277 |
if(compareAddresses(addrs[x], resultArr[y])) { |
| 1278 |
matchFound = 1; |
| 1279 |
break; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
// If it's not found, add it to the list |
| 1284 |
if(!matchFound) { |
| 1285 |
resultArr[i] = addrs[x]; |
| 1286 |
i++; |
| 1287 |
} else { |
| 1288 |
// Otherwise, that address is already in the list, so free it |
| 1289 |
tptp_free(addrs[x]); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
*numAddr = i; |
| 1297 |
*arrOfAddr = resultArr; |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
/** Given a range of bytes, print their int value. */ |
| 1302 |
void printChars(unsigned char *b, int l) { |
| 1303 |
int x = 0; |
| 1304 |
|
| 1305 |
for(x = 0; x < l; x++) { |
| 1306 |
printf("[%d]", (*b)); |
| 1307 |
b++; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
/** For debug purposes, prints the values of the fields of sockaddr_storage.*/ |
| 1312 |
void printSockAddrStorage(struct sockaddr_storage *inAddr) { |
| 1313 |
|
| 1314 |
if(inAddr->ss_family == PF_INET) { |
| 1315 |
struct sockaddr_in * ia = (struct sockaddr_in *)inAddr; |
| 1316 |
|
| 1317 |
printf("addr: "); |
| 1318 |
printChars((unsigned char*)&(ia->sin_addr), 4); |
| 1319 |
printf("\r\n"); |
| 1320 |
|
| 1321 |
printf("family: %d\r\n", ia->sin_family); |
| 1322 |
printf("port: %d\r\n", ia->sin_port); |
| 1323 |
printf("zero: %s\r\n", ia->sin_zero); |
| 1324 |
|
| 1325 |
|
| 1326 |
} else { // PF_INET6 |
| 1327 |
|
| 1328 |
struct sockaddr_in6 * ia = (struct sockaddr_in6 *)inAddr; |
| 1329 |
|
| 1330 |
printf("addr: "); |
| 1331 |
printChars((unsigned char*)&(ia->sin6_addr), 16); |
| 1332 |
printf("\r\n"); |
| 1333 |
|
| 1334 |
printf("family: %d\r\n", (int)ia->sin6_family); |
| 1335 |
printf("flowinfo: %d\r\n", (int)ia->sin6_flowinfo); |
| 1336 |
printf("port: %d\r\n", (int)ia->sin6_port); |
| 1337 |
printf("scope_id: %d\r\n", (int)ia->sin6_scope_id); |
| 1338 |
|
| 1339 |
} |
| 1340 |
|
| 1341 |
} |