Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 165409 | Differences between
and this patch

Collapse All | Expand All

(-)src-native-new/src/transport/TPTPClientCompTL/Connect2AC.c (-10 / +20 lines)
Lines 23-28 Link Here
23
#include "Connect2AC.h"
23
#include "Connect2AC.h"
24
#include "RACClientSupport.h"
24
#include "RACClientSupport.h"
25
25
26
#ifdef _WIN32
27
	#include "ws2tcpip.h"
28
#endif
29
26
#define CMD_BUFFER_LENGTH 8192 /* TODO: Use a universal definition */
30
#define CMD_BUFFER_LENGTH 8192 /* TODO: Use a universal definition */
27
#define MAX_AGENT_NAME    1024 /* TODO: Use a universal definition */
31
#define MAX_AGENT_NAME    1024 /* TODO: Use a universal definition */
28
32
Lines 957-964 Link Here
957
	tptp_int32 flags = TPTP_DATA_PATH_TWOWAY;
961
	tptp_int32 flags = TPTP_DATA_PATH_TWOWAY;
958
	tptp_int32 dataConnectionID;
962
	tptp_int32 dataConnectionID;
959
	SOCKET     dataSock;
963
	SOCKET     dataSock;
960
	tptp_int32 clientIP;
964
	
961
	tptp_int32 clientIPLen;
965
	struct sockaddr_storage * addr;
966
962
	client_data_connection_block_t *dcb;
967
	client_data_connection_block_t *dcb;
963
	tptp_int32 rc;
968
	tptp_int32 rc;
964
	TID                 threadId;
969
	TID                 threadId;
Lines 971-979 Link Here
971
		status = getAgent(ccb->stateData, ccb, "ProcessController", &processControllerDestID);
976
		status = getAgent(ccb->stateData, ccb, "ProcessController", &processControllerDestID);
972
	}
977
	}
973
978
974
	getClientIP(ccb, (unsigned char*)&clientIP, &clientIPLen);
979
	getClientIPAddr(ccb, &addr);
975
	dataSock = openClientDataSocket(clientIP, remoteConsolePort);
980
981
	dataSock = openClientDataSocketAddr(addr, remoteConsolePort);
982
983
	tptp_free(addr);
984
976
	if (dataSock != -1) {
985
	if (dataSock != -1) {
986
977
		dcb = (client_data_connection_block_t*)tptp_malloc( sizeof(client_data_connection_block_t) );
987
		dcb = (client_data_connection_block_t*)tptp_malloc( sizeof(client_data_connection_block_t) );
978
		if ( dcb == NULL ) {
988
		if ( dcb == NULL ) {
979
			return FALSE;
989
			return FALSE;
Lines 1470-1481 Link Here
1470
	{
1480
	{
1471
		char* localIPStr;
1481
		char* localIPStr;
1472
1482
1473
		if (getSocketIPString( ccb->sock, &localIPStr) < 0) {
1483
		if (getSocketIPStringIPv4M( ccb->sock, &localIPStr) < 0) {
1474
			TPTP_LOG_ERROR_MSG(stateData, "Unable to get local IP address");
1484
			TPTP_LOG_ERROR_MSG(stateData, "Unable to get local IP address");
1475
			return -1;
1485
			return -1;
1476
		}
1486
		}
1477
		
1487
		
1478
		convertIPAddressStringToArray( localIPStr, localAddr);
1488
		convertIPAddressStringToArrayIPv4M( localIPStr, localAddr);
1479
		ra_createRASTRING3( &command->info.controller_request_monitor_port.node, localAddr, 4);
1489
		ra_createRASTRING3( &command->info.controller_request_monitor_port.node, localAddr, 4);
1480
1490
1481
		/* We're done with this now, so let's free it */
1491
		/* We're done with this now, so let's free it */
Lines 1487-1493 Link Here
1487
	command->info.controller_request_monitor_port.port = strtoul(peerPort, 0, 10);
1497
	command->info.controller_request_monitor_port.port = strtoul(peerPort, 0, 10);
1488
1498
1489
	/* Convert the peer address */
1499
	/* Convert the peer address */
1490
	convertIPAddressStringToArray( peerHost, peerAddr );
1500
	convertIPAddressStringToArrayIPv4M( peerHost, peerAddr );
1491
	ra_createRASTRING3( &command->info.controller_request_monitor_port.peerNode, peerAddr, 4 );
1501
	ra_createRASTRING3( &command->info.controller_request_monitor_port.peerNode, peerAddr, 4 );
1492
1502
1493
	/* Fill in the source agent's information */
1503
	/* Fill in the source agent's information */
Lines 1559-1570 Link Here
1559
	{
1569
	{
1560
		char* localIPStr;
1570
		char* localIPStr;
1561
1571
1562
		if (getSocketIPString( ccb->sock, &localIPStr) < 0) {
1572
		if (getSocketIPStringIPv4M( ccb->sock, &localIPStr) < 0) {
1563
			TPTP_LOG_ERROR_MSG(stateData, "Unable to get local IP address");
1573
			TPTP_LOG_ERROR_MSG(stateData, "Unable to get local IP address");
1564
			return -1;
1574
			return -1;
1565
		}
1575
		}
1566
		
1576
		
1567
		convertIPAddressStringToArray( localIPStr, localAddr);
1577
		convertIPAddressStringToArrayIPv4M( localIPStr, localAddr);
1568
		ra_createRASTRING3( &command->info.controller_request_monitor_port.node, localAddr, 4);
1578
		ra_createRASTRING3( &command->info.controller_request_monitor_port.node, localAddr, 4);
1569
1579
1570
		/* We're done with this now, so let's free it */
1580
		/* We're done with this now, so let's free it */
Lines 1576-1582 Link Here
1576
	command->info.controller_request_monitor_port.port = strtoul(peerPort, 0, 10);
1586
	command->info.controller_request_monitor_port.port = strtoul(peerPort, 0, 10);
1577
1587
1578
	/* Convert the peer address */
1588
	/* Convert the peer address */
1579
	convertIPAddressStringToArray( peerHost, command->info.controller_request_monitor_port.peerNode.data );
1589
	convertIPAddressStringToArrayIPv4M( peerHost, command->info.controller_request_monitor_port.peerNode.data );
1580
	command->info.controller_request_monitor_port.peerNode.length = 4;
1590
	command->info.controller_request_monitor_port.peerNode.length = 4;
1581
1591
1582
	/* Fill in the peer agent's information */
1592
	/* Fill in the peer agent's information */
(-)src-native-new/src/transport/TPTPClientCompTL/RACClientSupport.c (-4 / +102 lines)
Lines 34-39 Link Here
34
#include "secureServer.h"
34
#include "secureServer.h"
35
#include "Connect2AC.h"
35
#include "Connect2AC.h"
36
36
37
// Added for IPv6 support
38
#ifdef _WIN32
39
	#include <winsock2.h>
40
	#include <ws2tcpip.h>
41
#endif
42
 
43
37
#ifndef _WIN32
44
#ifndef _WIN32
38
#include <signal.h>  // Used by ra_killProcess
45
#include <signal.h>  // Used by ra_killProcess
39
#endif
46
#endif
Lines 740-751 Link Here
740
	}
747
	}
741
}
748
}
742
749
750
751
int getClientIPAddr(client_connection_block_t* ccb, struct sockaddr_storage ** addr) {
752
	
753
    if (ccb->connectionType == CCTL_NATIVE_SOCKET) {
754
    	*addr = getPeerAddrOfSocket(ccb->sock);
755
    	
756
    }
757
    else if(ccb->connectionType == CCTL_JAVA_SOCKET) {
758
        JNIEnv *env;
759
760
        if(!ATTACH_THREAD(env)) {
761
            jclass clazz = fndclsid(env, INTERFACE_CLIENT_HANDLER);
762
            if(clazz) {
763
                jmethodID method = getmthdid(env, clazz, "getRemoteAddress", "()[B");
764
                if(method) {
765
                    jbyteArray result=(jbyteArray)ENV(env)->CallObjectMethod(ENVPARM(env) ccb->javaObj, method);
766
                    if(result) {
767
                        jbyte *elements;
768
                        jsize size = ENV(env)->GetArrayLength(ENVPARM(env) result);
769
                        elements = ENV(env)->GetByteArrayElements(ENVPARM(env) result, JNI_FALSE);
770
#ifdef _LP64
771
                        assert(size<=INT_MAX); 
772
#endif                  
773
                        getSockAddrFromByteFormat(elements, size == 4 ? PF_INET : PF_INET6, addr );                        
774
                        
775
                    }
776
                }
777
			}
778
			DETACH_THREAD();
779
		}
780
	}
781
782
    return 0;
783
}
784
743
int getClientIP(client_connection_block_t* ccb, unsigned char *buffer, int *length) {
785
int getClientIP(client_connection_block_t* ccb, unsigned char *buffer, int *length) {
744
    //if(client->connection->type==RA_SOCKET_CONNECTION) { kevin we need to store this type info.
786
    //if(client->connection->type==RA_SOCKET_CONNECTION) { kevin we need to store this type info.
745
    if (ccb->connectionType == CCTL_NATIVE_SOCKET) {
787
    if (ccb->connectionType == CCTL_NATIVE_SOCKET) {
746
	    /* Retrieve the IP address of the remote client */
788
    	
747
		getPeerName(ccb->sock, buffer);
789
    	struct sockaddr_storage * addr;
748
        *length=4;
790
    	unsigned char *value;
791
		int family = 0;
792
    	addr = getPeerAddrOfSocket(ccb->sock);
793
    	
794
    	if(addr == NULL) {
795
    		*length = 0;
796
    		return -1;
797
    	}
798
    	
799
    	getByteFormatFromSockAddr(addr, (void **)&value, &family);
800
    	
801
    	if(addr->ss_family == PF_INET) {
802
    		memcpy(buffer, value, sizeof(struct in_addr));
803
    		*length = sizeof(struct in_addr);
804
    		tptp_free(value);
805
    	} else  if(addr->ss_family == PF_INET6) {
806
    		memcpy(buffer, value, sizeof(struct in6_addr));
807
    		*length = sizeof(struct in6_addr);
808
    		tptp_free(value);
809
    	} else {
810
    		tptp_free(addr);
811
    		*length = 0;
812
    		return -1;
813
    	}
814
    	
815
    	tptp_free(addr);
816
    	
749
    }
817
    }
750
    else if(ccb->connectionType == CCTL_JAVA_SOCKET) {
818
    else if(ccb->connectionType == CCTL_JAVA_SOCKET) {
751
        JNIEnv *env;
819
        JNIEnv *env;
Lines 774-787 Link Here
774
842
775
    return 0;
843
    return 0;
776
}
844
}
845
#ifndef _WIN32
846
	#define INVALID_SOCKET -1
847
#endif
848
849
SOCKET openClientDataSocketAddr(struct sockaddr_storage *addr, int port) {
850
	char hostname[260];
851
	tptp_int32 rc = -1;
852
	SOCKET outSocket;
853
	
854
	hostname[0] = 0;
855
856
	// Resolve the host name
857
	rc = getnameinfo((struct sockaddr *)addr, sizeof(struct sockaddr_storage), hostname, 260, NULL, 0, 0);
858
	if(rc == 0) {
859
		
860
		// Attempt to connect to the server by resolving the various addreses of the hostname
861
		connectToTCPServer(hostname, port, &outSocket);
862
863
		if(outSocket != INVALID_SOCKET) {
864
			return outSocket;
865
		} 
866
	} 
867
	
868
	// If we weren't able to resolve the host name, or couldn't succeed, try the IP address itself
869
	connectToTCPServerAddr(addr, port, &outSocket);
870
	
871
	return outSocket;
872
873
}
777
874
875
/** IPV4-ONLY */
778
SOCKET openClientDataSocket(tptp_int32 ip, tptp_int32 port)
876
SOCKET openClientDataSocket(tptp_int32 ip, tptp_int32 port)
779
{
877
{
780
878
781
	SOCKET sock = -1 ;
879
	SOCKET sock = -1 ;
782
	tptp_int32 rc = -1;
880
	tptp_int32 rc = -1;
783
881
784
	rc = connectToTCPServer(ip, (unsigned short) port, &sock);
882
	rc = connectToTCPServerIPv4M(ip, (unsigned short) port, &sock);
785
883
786
	if (rc == -1) {
884
	if (rc == -1) {
787
		return -1;
885
		return -1;
(-)src-native-new/src/transport/TPTPClientCompTL/RACClientSupport.h (+5 lines)
Lines 147-152 Link Here
147
/* computes a sockets ip address using getpeername */
147
/* computes a sockets ip address using getpeername */
148
int getClientIP(client_connection_block_t* ccb, unsigned char *buffer, int *length);
148
int getClientIP(client_connection_block_t* ccb, unsigned char *buffer, int *length);
149
149
150
151
int getClientIPAddr(client_connection_block_t* ccb, struct sockaddr_storage ** addr);
152
150
SOCKET openClientDataSocket(tptp_int32 ip, tptp_int32 port);
153
SOCKET openClientDataSocket(tptp_int32 ip, tptp_int32 port);
151
154
155
SOCKET openClientDataSocketAddr(struct sockaddr_storage *addr, int port);
156
152
#endif /* __RACPROCESSLIST_H__ */
157
#endif /* __RACPROCESSLIST_H__ */
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.c (-20 / +24 lines)
Lines 252-270 Link Here
252
	int d, rc;
252
	int d, rc;
253
	SOCKET testSocket;
253
	SOCKET testSocket;
254
	unsigned long ipAddr;
254
	unsigned long ipAddr;
255
	
255
	struct addrinfo *AI = NULL;
256
256
	host = readStringIntoBuffer(con);
257
	host = readStringIntoBuffer(con);
257
	if (host == NULL) return -1;
258
	if (host == NULL) return -1;
258
	
259
	
259
	readInt (con->socket, &port); 
260
	readInt (con->socket, &port); 
260
	
261
262
	testSocket = connectToHostWithHostname(host, port, &AI);
263
261
	d = 0;
264
	d = 0;
262
	rc = convertIPAddressStringToUlong(host, &ipAddr);	// reverse call
265
	
263
	if (rc == 0) {
266
	if(testSocket != INVALID_SOCKET) {
264
		if (!connectToTCPServer(ipAddr, (unsigned short) port, &testSocket)) {
267
		d = 1;
265
			d = 1;
268
		closeSocket(testSocket);
266
			closeSocket(testSocket);
267
		}
268
	}
269
	}
269
	
270
	
270
	writeByte(con->socket, d);	// "server is reachable" responce
271
	writeByte(con->socket, d);	// "server is reachable" responce
Lines 502-534 Link Here
502
503
503
static SOCKET serverSocket;
504
static SOCKET serverSocket;
504
505
506
static SOCKET * serverSockets;
507
static int numServerSockets;
508
static socket_accept_t socketAccept;
509
505
THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args){
510
THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args){
506
	int rc, port;
511
	int rc, port;
507
	struct sockaddr_in  fsSockAddr;
508
	SOCKET clientSock;
512
	SOCKET clientSock;
509
	fs_connection_block_t* con;
513
	fs_connection_block_t* con;
510
	tl_state_data_t*    stateData;
514
	tl_state_data_t*    stateData;
511
	TID                 threadId;
515
	TID                 threadId;
512
	HANDLE              threadHandle;
516
	HANDLE              threadHandle;
517
	
518
	serverSockets = (SOCKET *) tptp_malloc(sizeof(SOCKET) * FD_SETSIZE);
519
	
520
	initSocketAccept(&socketAccept);
513
521
514
	stateData = (tl_state_data_t*) args;
522
	stateData = (tl_state_data_t*) args;
515
	port = ((cctl_state_data_t*)stateData->implData)->filePort;
523
	port = ((cctl_state_data_t*)stateData->implData)->filePort;
516
	if (port <= 0) port = RA_FILE_PORT_NUM_SERVER;
524
	if (port <= 0) port = RA_FILE_PORT_NUM_SERVER;
517
		
525
	
518
	serverSocket = getTheSocket(port, &fsSockAddr);
526
	rc = getTheSocket(port, serverSockets, &numServerSockets); 
519
	if (serverSocket < 0) {
520
		TPTP_LOG_ERROR_MSG(stateData, "NFS: unable to create the file server socket.");
521
		return 0;
522
	}
523
527
524
	rc = bindAndListen(serverSocket, (struct sockaddr*)&fsSockAddr);
528
	//serverSocket = getTheSocket(port, &fsSockAddr);
525
	if (rc != 0) {
529
	if (rc < 0 || numServerSockets == 0) {
526
		TPTP_LOG_ERROR_MSG(stateData, "NFS: cannot bind to server socket");
530
		TPTP_LOG_ERROR_MSG(stateData, "NFS: unable to create the file server socket.");
527
		return 0;
531
		return 0;
528
	}
532
	}
529
533
	
530
	while(1) { 
534
	while(1) { 
531
		clientSock = acceptSocketConnection(serverSocket); 
535
		clientSock = acceptSocketConnections(serverSockets, numServerSockets, &socketAccept); 
532
	    if (clientSock < 0) break;
536
	    if (clientSock < 0) break;
533
537
534
		con = (fs_connection_block_t*) tptp_malloc(sizeof(fs_connection_block_t));
538
		con = (fs_connection_block_t*) tptp_malloc(sizeof(fs_connection_block_t));
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.h (+4 lines)
Lines 24-29 Link Here
24
	#include <sys/types.h>
24
	#include <sys/types.h>
25
#endif
25
#endif
26
26
27
#ifndef _WIN32
28
	#define INVALID_SOCKET (-1)
29
#endif
30
27
#include "tptp/TPTPTypes.h"
31
#include "tptp/TPTPTypes.h"
28
#include "tptp/TPTPSupportTypes.h"
32
#include "tptp/TPTPSupportTypes.h"
29
#include "tptp/TransportSupport.h"
33
#include "tptp/TransportSupport.h"
(-)src-native-new/src/transport/TPTPClientCompTL/ClientCTL.h (+9 lines)
Lines 22-30 Link Here
22
#include "secureServer.h"
22
#include "secureServer.h"
23
#include "tptp/TPTPUtils.h"
23
#include "tptp/TPTPUtils.h"
24
24
25
#include "tptp/TransportSupport.h" 
26
// I added the above header file for access to IPv4/IPv6 functions - jwest
27
25
typedef struct
28
typedef struct
26
{
29
{
30
	// IPV4-ONLY
27
	SOCKET			sock;
31
	SOCKET			sock;
32
	
33
	SOCKET *		sockets; // List of servers sockets we are listening on
34
	tptp_uint32		numSockets; // Number of sockets we are listening on
35
	socket_accept_t sockAccept; // State of server socket listening struct 
36
	
28
	tptp_uint32		port;
37
	tptp_uint32		port;
29
	tptp_uint32		filePort;
38
	tptp_uint32		filePort;
30
	tptp_uint32		acProtocolPort;
39
	tptp_uint32		acProtocolPort;
(-)src-native-new/src/transport/TPTPClientCompTL/RACmdHandlers.c (-11 / +7 lines)
Lines 185-192 Link Here
185
	PID real_pid;
185
	PID real_pid;
186
	ra_array_t finalEnvironment;
186
	ra_array_t finalEnvironment;
187
187
188
	tptp_int32 buffer;
189
	int bufferLen=6;
190
	SOCKET consoleSock; /* 9707 */
188
	SOCKET consoleSock; /* 9707 */
191
	ra_string_t uuid;
189
	ra_string_t uuid;
192
190
Lines 203-221 Link Here
203
	 *   In addition to launching the process, we need to look for any agents that
201
	 *   In addition to launching the process, we need to look for any agents that
204
	 *   the client expects to be associated with this process and attach to them
202
	 *   the client expects to be associated with this process and attach to them
205
	 */
203
	 */
206
204
	
207
    getClientIP(ccb, (unsigned char*)&buffer, &bufferLen);
208
209
	//ra_logServiceMessage(__FILE__, __LINE__, RA_DEBUG, "Console publish 0x%x, desired 0x%x  socket=%d", command->info.launch_process.consoleIP, buffer, client->connection->handle.socket);
205
	//ra_logServiceMessage(__FILE__, __LINE__, RA_DEBUG, "Console publish 0x%x, desired 0x%x  socket=%d", command->info.launch_process.consoleIP, buffer, client->connection->handle.socket);
210
206
211
	real_pid=0;
207
	real_pid=0;
212
208
	
213
	/* Launch Process */
209
	/* Launch Process */
214
	real_pid=startProcess(ccb, &command->info.launch_process.executable, 
210
	real_pid=startProcess(ccb, &command->info.launch_process.executable, 
215
							 &command->info.launch_process.arguments,
211
							 &command->info.launch_process.arguments,
216
							 &command->info.launch_process.location,
212
							 &command->info.launch_process.location,
217
							 &command->info.launch_process.environment,
213
							 &command->info.launch_process.environment,
218
							 buffer,
214
							 0 /* This value isn't used. */,
219
							 (unsigned short)command->info.launch_process.consolePort,
215
							 (unsigned short)command->info.launch_process.consolePort,
220
							 &finalEnvironment,
216
							 &finalEnvironment,
221
							 &consoleSock,
217
							 &consoleSock,
Lines 739-746 Link Here
739
	tptp_int32 rc = -1;
735
	tptp_int32 rc = -1;
740
	tptp_int32 flags;
736
	tptp_int32 flags;
741
	tptp_int32 dataConnectionID;
737
	tptp_int32 dataConnectionID;
742
	tptp_int32 clientIP;
738
	struct sockaddr_storage * addr;
743
	tptp_int32 clientIPLen;
744
	client_data_connection_block_t *dcb;
739
	client_data_connection_block_t *dcb;
745
	SOCKET dataSock;
740
	SOCKET dataSock;
746
741
Lines 796-804 Link Here
796
		flags = TPTP_DATA_PATH_RECEIVE | TPTP_DATA_PATH_DIRECT_SOCKET;
791
		flags = TPTP_DATA_PATH_RECEIVE | TPTP_DATA_PATH_DIRECT_SOCKET;
797
792
798
		if (agent->IPCBufCreated == FALSE) {
793
		if (agent->IPCBufCreated == FALSE) {
799
			getClientIP(ccb, (unsigned char*)&clientIP, &clientIPLen);
800
			dataSock = openClientDataSocket(clientIP, command->info.start_monitor_remote.port);
801
794
795
			getClientIPAddr(ccb, &addr);
796
			dataSock = openClientDataSocketAddr(addr, command->info.start_monitor_remote.port);
797
			
802
			if (dataSock == -1) {
798
			if (dataSock == -1) {
803
				sendErrorMessage("RAC009",
799
				sendErrorMessage("RAC009",
804
				 "Start Monitoring failed due to data channel socket connection failure.", 
800
				 "Start Monitoring failed due to data channel socket connection failure.", 
(-)src-native-new/src/transport/TPTPClientCompTL/ClientCTL.c (-17 / +18 lines)
Lines 79-84 Link Here
79
			return TPTP_ERROR_OUT_OF_MEMORY;
79
			return TPTP_ERROR_OUT_OF_MEMORY;
80
		}
80
		}
81
		cctlData = (cctl_state_data_t*)stateData->implData;
81
		cctlData = (cctl_state_data_t*)stateData->implData;
82
		
83
		initSocketAccept(&(cctlData->sockAccept));
82
84
83
		/* Read the TL configuration */
85
		/* Read the TL configuration */
84
		rc = getConfigurationString(pTransportData->configurationData, "IsDataMultiplexed", &isDataMultiplexed);
86
		rc = getConfigurationString(pTransportData->configurationData, "IsDataMultiplexed", &isDataMultiplexed);
Lines 538-549 Link Here
538
tptp_int32 startTransportListener(tptp_object* tlo)
540
tptp_int32 startTransportListener(tptp_object* tlo)
539
{
541
{
540
	int                 rc = 0;
542
	int                 rc = 0;
541
	struct sockaddr_in  myServerSockAddr ;
542
	tl_state_data_t*    stateData;
543
	tl_state_data_t*    stateData;
543
	cctl_state_data_t*  cctlData;
544
	cctl_state_data_t*  cctlData;
544
	TID                 threadId;
545
	TID                 threadId;
545
	HANDLE              threadHandle;
546
	HANDLE              threadHandle;
546
547
	SOCKET * socketList;
548
	
547
	if ( !isValidTPTPBlock(tlo, TPTP_BASETL_MAKE_OBJECT_ID(tlo)) )
549
	if ( !isValidTPTPBlock(tlo, TPTP_BASETL_MAKE_OBJECT_ID(tlo)) )
548
		return TPTP_ERROR_INVALID_PARAMETER;
550
		return TPTP_ERROR_INVALID_PARAMETER;
549
551
Lines 560-582 Link Here
560
	/* Create a pseudo-connection to monitor agent register events */
562
	/* Create a pseudo-connection to monitor agent register events */
561
	startAgentRegistrationListener( stateData );
563
	startAgentRegistrationListener( stateData );
562
564
565
	socketList = (SOCKET *)tptp_malloc(sizeof(SOCKET) * FD_SETSIZE);
566
	
567
	cctlData->sockets = socketList;
568
	
563
	/* create and initialize the server socket */
569
	/* create and initialize the server socket */
564
	cctlData->sock = getTheSocket( cctlData->port, &myServerSockAddr );
570
	// cctlData->sock = getTheSocket( cctlData->port, &myServerSockAddr );
565
571
566
	if ( cctlData->sock < 0 )
572
	/* create and initialize the server socket */
573
	rc = getTheSocket(cctlData->port, cctlData->sockets, &(cctlData->numSockets)); 
574
	
575
	if ( rc < 0 || cctlData->numSockets == 0 )
567
	{
576
	{
568
		TPTP_LOG_ERROR_MSG(stateData, "Error: unable to create the server socket.") ;
577
		TPTP_LOG_ERROR_MSG(stateData, "Error: unable to create the server socket.") ;
569
		return -1; // TODO: Use a more specific error
578
		return -1; // TODO: Use a more specific error
570
	}
579
	}
571
580
	
572
	rc = bindAndListen(cctlData->sock, (struct sockaddr*)&myServerSockAddr);
573
	if ( rc != 0 )
574
	{
575
		TPTP_LOG_SEVERE_MSG(stateData, "ERROR: Cannot start Agent Controller. ");
576
		TPTP_LOG_SEVERE_MSG1(stateData, "REASON: Cannot bind to socket port %d. Make sure the socket is free and no other Agent Controller is running.", cctlData->port);
577
		return -1; // TODO: Use a more specific error
578
	}
579
580
	if ((!cctlData->isJavaUnsecuredFileServer) && !cctlData->securityEnabled) {
581
	if ((!cctlData->isJavaUnsecuredFileServer) && !cctlData->securityEnabled) {
581
		rc = tptpStartThread(startNativeFileServer, (LPVOID)stateData, &threadId, &threadHandle);
582
		rc = tptpStartThread(startNativeFileServer, (LPVOID)stateData, &threadId, &threadHandle);
582
		CLOSE_THREAD_HANDLE(threadHandle);
583
		CLOSE_THREAD_HANDLE(threadHandle);
Lines 643-649 Link Here
643
	{
644
	{
644
		int ret;
645
		int ret;
645
		
646
		
646
		ret = convertIPAddressUlongToString( cctlData->peerAttachAddress, &ipAddrStr );
647
		ret = convertIPAddressUlongToStringIPv4M( cctlData->peerAttachAddress, &ipAddrStr );
647
		if ( ret != 0 )
648
		if ( ret != 0 )
648
		{
649
		{
649
			return ret;
650
			return ret;
Lines 653-659 Link Here
653
	{
654
	{
654
		/* NOTE: This has problems if there are multiple IP addresses, but in
655
		/* NOTE: This has problems if there are multiple IP addresses, but in
655
			     the case of compatibility mode, this isn't really used anyway */
656
			     the case of compatibility mode, this isn't really used anyway */
656
		if (getSocketIPString( (SOCKET)NULL, &ipAddrStr) < 0) {
657
		if (getSocketIPStringIPv4M( (SOCKET)NULL, &ipAddrStr) < 0) {
657
			TPTP_LOG_ERROR_MSG(stateData, "Unable to get local IP address");
658
			TPTP_LOG_ERROR_MSG(stateData, "Unable to get local IP address");
658
			return -1;
659
			return -1;
659
		}
660
		}
Lines 737-743 Link Here
737
		TPTP_LOG_ERROR_MSG1( stateData, "Unable to find host in sourceConnectionInfo: %s", sourceConnectionInfo );
738
		TPTP_LOG_ERROR_MSG1( stateData, "Unable to find host in sourceConnectionInfo: %s", sourceConnectionInfo );
738
		return rc;	
739
		return rc;	
739
	}
740
	}
740
	rc = convertIPAddressStringToUlong( ipAddrStr, &ipAddr );
741
	rc = convertIPAddressStringToUlongIPv4M( ipAddrStr, &ipAddr );
741
	if ( rc != 0 )
742
	if ( rc != 0 )
742
	{
743
	{
743
		TPTP_LOG_ERROR_MSG1( stateData, "Unable to convert IP address string to unsigned long: %s", ipAddrStr );
744
		TPTP_LOG_ERROR_MSG1( stateData, "Unable to convert IP address string to unsigned long: %s", ipAddrStr );
Lines 748-754 Link Here
748
	/* TODO: Check to see if this is local */
749
	/* TODO: Check to see if this is local */
749
750
750
	/* Connect to the remote AC */
751
	/* Connect to the remote AC */
751
	rc = connectToTCPServer( ipAddr, (unsigned short)port, &sock);
752
	rc = connectToTCPServerIPv4M( ipAddr, (unsigned short)port, &sock);
752
	if ( rc != 0 )
753
	if ( rc != 0 )
753
	{
754
	{
754
		TPTP_LOG_ERROR_MSG2( stateData, "Unable to connect to peer: %s, port %d", ipAddrStr, port );
755
		TPTP_LOG_ERROR_MSG2( stateData, "Unable to connect to peer: %s, port %d", ipAddrStr, port );
(-)src-native-new/src/transport/TPTPClientCompTL/CCTLServer.c (-4 / +10 lines)
Lines 465-471 Link Here
465
	/* accept and process one connection at a time */
465
	/* accept and process one connection at a time */
466
	while (stateData->processingThreadState == TPTP_TL_THREAD_RUNNING)
466
	while (stateData->processingThreadState == TPTP_TL_THREAD_RUNNING)
467
	{
467
	{
468
        clientSock = acceptSocketConnection(cctlData->sock); 
468
        // clientSock = acceptSocketConnection(cctlData->sock);
469
		clientSock = acceptSocketConnections(cctlData->sockets, cctlData->numSockets, &(cctlData->sockAccept));
469
470
470
		if (isSocketValid(clientSock) == 0)
471
		if (isSocketValid(clientSock) == 0)
471
		{
472
		{
Lines 478-485 Link Here
478
			ra_message_t*              message;
479
			ra_message_t*              message;
479
			ra_command_t*              command;
480
			ra_command_t*              command;
480
			char *ipAddr;
481
			char *ipAddr;
481
			unsigned char address[12];
482
			tptp_int32 rc;
482
			tptp_int32 rc;
483
			struct sockaddr_storage *sockAddr;
484
			
485
			sockAddr = getPeerAddrOfSocket(clientSock);
483
486
484
			rc = getPeerIPString(clientSock, &ipAddr);
487
			rc = getPeerIPString(clientSock, &ipAddr);
485
			if (rc != 0)
488
			if (rc != 0)
Lines 487-502 Link Here
487
				TPTP_LOG_ERROR_MSG(stateData, "Connection refused could not getPeerIPString");
490
				TPTP_LOG_ERROR_MSG(stateData, "Connection refused could not getPeerIPString");
488
				continue;
491
				continue;
489
			}
492
			}
490
			convertIPAddressStringToArray(ipAddr, address);
491
493
492
			if (!tptp_checkHost(cctlData->network_list, address)) {
494
			if (!tptp_checkHostIPv6(cctlData->network_list, sockAddr)) {
495
493
				TPTP_LOG_ERROR_MSG2(stateData, "Connection refused on socket %d on host %s", clientSock, ipAddr);
496
				TPTP_LOG_ERROR_MSG2(stateData, "Connection refused on socket %d on host %s", clientSock, ipAddr);
494
				//printf("Connection refused on socket %d on host %s", clientSock, ipAddr);
497
				//printf("Connection refused on socket %d on host %s", clientSock, ipAddr);
495
				tptp_free(ipAddr);
498
				tptp_free(ipAddr);
499
				tptp_free(sockAddr);
496
				closeThisSocket(clientSock);
500
				closeThisSocket(clientSock);
497
				continue;
501
				continue;
498
			}
502
			}
503
499
			tptp_free(ipAddr);
504
			tptp_free(ipAddr);
505
			tptp_free(sockAddr);
500
506
501
			setHandleInherited((HANDLE) clientSock) ;
507
			setHandleInherited((HANDLE) clientSock) ;
502
508
(-)src-native-new/include/tptp/MsgPipeline.h (+8 lines)
Lines 74-79 Link Here
74
	unsigned int  peerAttachAddress;	
74
	unsigned int  peerAttachAddress;	
75
} PeerAttachInfo_t;
75
} PeerAttachInfo_t;
76
76
77
/*
78
typedef struct {
79
	char * transportType;
80
	tptp_list_t hostList;
81
	int port;
82
} PeerIPv6Connection_t;
83
*/
84
77
/* Optional functions provided by TLs involved in peer monitoring */
85
/* Optional functions provided by TLs involved in peer monitoring */
78
typedef tptp_int32 (*setPeerAttachInfo_ptr_t)( tptp_object* tlo, PeerAttachInfo_t* peerInfo );
86
typedef tptp_int32 (*setPeerAttachInfo_ptr_t)( tptp_object* tlo, PeerAttachInfo_t* peerInfo );
79
typedef tptp_int32 (*getPeerConnectionInfo_ptr_t)( tptp_object* tlo, tptp_string* type, tptp_string** ci );
87
typedef tptp_int32 (*getPeerConnectionInfo_ptr_t)( tptp_object* tlo, tptp_string* type, tptp_string** ci );
(-)src-native-new/include/tptp/TransportSupportSocket.h (-13 / +68 lines)
Lines 40-46 Link Here
40
 *    non-zero   failure code
40
 *    non-zero   failure code
41
 *
41
 *
42
 *********************************************************/
42
 *********************************************************/
43
int convertIPAddressStringToArray( char *addrStr, unsigned char* addrArray );
43
int convertIPAddressStringToArrayIPv4M( char *addrStr, unsigned char* addrArray );
44
44
45
/**
45
/**
46
 *********************************************************
46
 *********************************************************
Lines 54-60 Link Here
54
 *    non-zero   failure code
54
 *    non-zero   failure code
55
 *
55
 *
56
 *********************************************************/
56
 *********************************************************/
57
int convertIPAddressStringToUlong( char *addrStr, unsigned long* addr );
57
int convertIPAddressStringToUlongIPv4M( char *addrStr, unsigned long* addr );
58
58
59
/**
59
/**
60
 *********************************************************
60
 *********************************************************
Lines 69-75 Link Here
69
 *    0          success
69
 *    0          success
70
 *    non-zero   failure
70
 *    non-zero   failure
71
 *********************************************************/
71
 *********************************************************/
72
int convertIPAddressUlongToString( unsigned long addr, char **addrStr );
72
int convertIPAddressUlongToStringIPv4M( unsigned long addr, char **addrStr );
73
73
74
/**
74
/**
75
 *********************************************************
75
 *********************************************************
Lines 88-94 Link Here
88
 *    non-zero   failure code
88
 *    non-zero   failure code
89
 *
89
 *
90
 *********************************************************/
90
 *********************************************************/
91
int getSocketIPString( SOCKET sock, char** ipAddrStr );
91
int getSocketIPStringIPv4M( SOCKET sock, char** ipAddrStr );
92
92
93
/**
93
/**
94
 *********************************************************
94
 *********************************************************
Lines 118-124 Link Here
118
 * @return
118
 * @return
119
 *    host info data structure
119
 *    host info data structure
120
 *********************************************************/
120
 *********************************************************/
121
struct hostent * getHostInfo() ;
121
struct sockaddr_storage* getHostInfo();
122
123
struct hostent * getHostInfoIPv4M();
122
124
123
/**
125
/**
124
 *********************************************************
126
 *********************************************************
Lines 129-135 Link Here
129
 * @return
131
 * @return
130
 *    host info data structure
132
 *    host info data structure
131
 *********************************************************/
133
 *********************************************************/
132
struct hostent * getTargetHostInfo(char* hostname) ;
134
struct hostent * getTargetHostInfoIPv4M(char* hostname);
133
135
134
/**
136
/**
135
 *********************************************************
137
 *********************************************************
Lines 141-147 Link Here
141
 *    the raw unconnected socket
143
 *    the raw unconnected socket
142
 *
144
 *
143
 *********************************************************/
145
 *********************************************************/
144
SOCKET getTheSocket(int portNum, struct sockaddr_in *pAddr) ;
146
int getTheSocket(int portNum, SOCKET *outResult, int *outNumSockets);
145
147
146
/**
148
/**
147
 *********************************************************
149
 *********************************************************
Lines 153-159 Link Here
153
 *    0 - Success
155
 *    0 - Success
154
 *    nonzero - Error.
156
 *    nonzero - Error.
155
 *********************************************************/
157
 *********************************************************/
156
int bindAndListen(SOCKET sock, struct sockaddr *pAddr) ;
158
SOCKET bindAndListen(struct addrinfo *AI);
157
159
158
/**
160
/**
159
 *********************************************************
161
 *********************************************************
Lines 173-184 Link Here
173
 *    connect to the given system
175
 *    connect to the given system
174
 *
176
 *
175
 * @return
177
 * @return
176
 *    0 - Success and the socket representing this connection
178
 *    If we were able to connect pSock contans a SOCKET, otherwise, it's INVALID_SOCKET.
177
 *    nonzero - Error.
179
 *********************************************************/
180
int connectToTCPServer(char * hostname, int port, SOCKET *pSock);
181
182
183
/**
184
 *********************************************************
185
 *
186
 * @brief
187
 *    connect to the given system
188
 *
189
 * @return
190
 *    If we were able to connect pSock contans a SOCKET, otherwise, it's INVALID_SOCKET.
178
 *********************************************************/
191
 *********************************************************/
179
int connectToTCPServer(unsigned long ipAddr,
192
int connectToTCPServerAddr(struct sockaddr_storage * addr, int port, SOCKET *pSock);
180
					   unsigned short portNum,
193
181
					   SOCKET *pSock);
194
195
int connectToTCPServerIPv4M(unsigned long ipAddr, unsigned short portNum, SOCKET *pSock);
182
196
183
/**
197
/**
184
 *********************************************************
198
 *********************************************************
Lines 198-203 Link Here
198
212
199
int closeSocket(int socket);
213
int closeSocket(int socket);
200
214
215
/** SOCKET ->  struct sockaddr_storage * for the peer. */
216
struct sockaddr_storage * getPeerAddrOfSocket(SOCKET ConnSocket);
217
218
/** struct sockaddr_storage * => IP address in byte format (e.g. in_addr / in6_addr) + family 
219
    The bytes are stored in outAddr, and the family is stored in family.*/
220
void getByteFormatFromSockAddr(struct sockaddr_storage *addr, void ** outAddr, int *family);
221
222
void getSockAddrFromByteFormat(void *inAddr, int family, struct sockaddr_storage **outResult);
223
224
void setSockaddrStoragePort(struct sockaddr_storage *addr, int port);
225
226
typedef struct {
227
	tptp_list_t serverSocketList;
228
229
	tptp_list_t socketsToProcess;
230
	Lock_t sockProcLock;
231
232
} socket_accept_t;
233
234
SOCKET acceptSocketConnections(SOCKET * sockets, int numSockets, socket_accept_t * sockAcceptState);
235
void initSocketAccept(socket_accept_t * sat);
236
237
struct sockaddr_storage * cloneSockAddr(struct sockaddr_storage * addr);
238
239
240
int isAddrLocal(struct sockaddr_storage * addr);
241
int compareAddresses(struct sockaddr_storage * one, struct sockaddr_storage * two);
242
char * convertAddrToHostname(struct sockaddr_storage *addr);
243
244
void getLocalHostAddresses(struct sockaddr_storage *** arrOfAddr, int * numAddr);
245
246
void freeAddressList(struct sockaddr_storage ** arrOfAddr, int numAddr);
247
248
int convertHostnameToAddrs(char *hostname, struct sockaddr_storage *** outResult, int *outNumResults);
249
250
struct sockaddr_storage * getSelfAddrOfSocket(SOCKET ConnSocket);
251
252
void printSockAddrStorage(struct sockaddr_storage *inAddr);
253
254
SOCKET connectToHostWithHostname(char * hostnameOrIp, int port, struct addrinfo ** sockAddrOut );
255
201
#ifdef __cplusplus
256
#ifdef __cplusplus
202
}
257
}
203
#endif
258
#endif
(-)src-native-new/include/tptp/TransportOSCalls.h (+2 lines)
Lines 77-82 Link Here
77
 *
77
 *
78
 * @return
78
 * @return
79
 *    0 - always
79
 *    0 - always
80
 * IPV4-ONLY
80
 *********************************************************/
81
 *********************************************************/
81
int printSocketRequest(SOCKET sock, struct sockaddr_in * pRequester) ;
82
int printSocketRequest(SOCKET sock, struct sockaddr_in * pRequester) ;
82
83
Lines 110-115 Link Here
110
 *********************************************************/
111
 *********************************************************/
111
int closeThisSocket(SOCKET sock) ;
112
int closeThisSocket(SOCKET sock) ;
112
113
114
/** IPV4-ONLY */
113
void extractAddressFromSockAddr(struct sockaddr_in *connection, unsigned char *address);
115
void extractAddressFromSockAddr(struct sockaddr_in *connection, unsigned char *address);
114
116
115
117
(-)src-native-new/include/tptp/TransportSupport.h (+4 lines)
Lines 95-100 Link Here
95
95
96
BOOL tptp_checkHost(network_list_t *list, unsigned char *address);
96
BOOL tptp_checkHost(network_list_t *list, unsigned char *address);
97
97
98
SOCKET connectToHostWithAddr(struct sockaddr_storage *addr);
99
100
BOOL tptp_checkHostIPv6(network_list_t *list, struct sockaddr_storage * addr);
101
98
#ifdef __cplusplus
102
#ifdef __cplusplus
99
}
103
}
100
#endif
104
#endif
(-)src-native-new/include/tptp/TPTPUtils.h (+7 lines)
Lines 98-103 Link Here
98
	unsigned char net[4];				/* The network address information in network byte order.  Note that this is IPV4 specific */
98
	unsigned char net[4];				/* The network address information in network byte order.  Note that this is IPV4 specific */
99
	unsigned char mask[4];				/* the subnet mask information in network byte order.  Note that this is IPV4 specific */
99
	unsigned char mask[4];				/* the subnet mask information in network byte order.  Note that this is IPV4 specific */
100
	char *hostname;
100
	char *hostname;
101
102
	unsigned char netipv6[16];
103
	unsigned char maskipv6[16];
104
105
	int netAndMaskAreIpv4;
106
101
}network_t;
107
}network_t;
102
108
103
typedef struct _network_list_node {
109
typedef struct _network_list_node {
Lines 234-239 Link Here
234
int tptp_deleteSemaphore(Semaphore_t *pSem);
240
int tptp_deleteSemaphore(Semaphore_t *pSem);
235
int tptp_waitSemaphore(Semaphore_t *pSem);
241
int tptp_waitSemaphore(Semaphore_t *pSem);
236
int tptp_postSemaphore(Semaphore_t *pSem);
242
int tptp_postSemaphore(Semaphore_t *pSem);
243
/*int tptp_waitSemaphoreTimeout(Semaphore_t *pSem, int timeoutInSecs);*/
237
244
238
int getFileSize(char *fileName);
245
int getFileSize(char *fileName);
239
int validateDirectory(char* dirName);
246
int validateDirectory(char* dirName);
(-)src-native-new/src/transport/RASocket/RASocket.c (-4 / +10 lines)
Lines 126-131 Link Here
126
  * @returns  0 - create server was successful.
126
  * @returns  0 - create server was successful.
127
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
127
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
128
  *               to get the failure details.
128
  *               to get the failure details.
129
  *  IPV4-ONLY
129
  */
130
  */
130
int ra_createBoundUDPServer(short portNum,
131
int ra_createBoundUDPServer(short portNum,
131
							ra_serverInfo_t *result) {
132
							ra_serverInfo_t *result) {
Lines 380-387 Link Here
380
  * @returns  0 - create server was successful.
381
  * @returns  0 - create server was successful.
381
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
382
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
382
  *               to get the failure details.
383
  *               to get the failure details.
384
  * IPV4-ONLY
383
  */
385
  */
384
385
int ra_createTCPServer(unsigned short portNum,
386
int ra_createTCPServer(unsigned short portNum,
386
					   ra_serverInfo_t *result) {
387
					   ra_serverInfo_t *result) {
387
388
Lines 660-665 Link Here
660
  * @returns   0 - connect was successful
661
  * @returns   0 - connect was successful
661
  *           <0 - connect failed, can ra_getLastErrorMajor/Minor
662
  *           <0 - connect failed, can ra_getLastErrorMajor/Minor
662
  *                to get the failure details.
663
  *                to get the failure details.
664
  * IPV4-ONLY
663
  */
665
  */
664
int ra_connectToTCPServer(ra_uint_t remoteIP,
666
int ra_connectToTCPServer(ra_uint_t remoteIP,
665
						  unsigned short remotePortNum,
667
						  unsigned short remotePortNum,
Lines 828-833 Link Here
828
 *           The number of characters written to the parameter buffer (excluding 
830
 *           The number of characters written to the parameter buffer (excluding 
829
 *           null terminator character.  Otherwise, -1 if a severe error occurred. 
831
 *           null terminator character.  Otherwise, -1 if a severe error occurred. 
830
 *           Call ra_getLastErrorMajor/Minor() to determine the error conditions.
832
 *           Call ra_getLastErrorMajor/Minor() to determine the error conditions.
833
 * IPV4-ONLY
831
 */
834
 */
832
short ra_getFormattedLocalIPAddress(char *ipAddressBuffer){
835
short ra_getFormattedLocalIPAddress(char *ipAddressBuffer){
833
	
836
	
Lines 886-891 Link Here
886
  *           -1 - severe error occured getting the IP address.  Call
889
  *           -1 - severe error occured getting the IP address.  Call
887
  *                ra_getLastErrorMajor/Minor to determine the
890
  *                ra_getLastErrorMajor/Minor to determine the
888
  *                error conditions.
891
  *                error conditions.
892
  * IPV4-ONLY
889
  */
893
  */
890
long ra_getLocalIPAddress() {
894
long ra_getLocalIPAddress() {
891
	char localhost[MAX_HOST_NAME_LENGTH];
895
	char localhost[MAX_HOST_NAME_LENGTH];
Lines 916-921 Link Here
916
920
917
	return result;
921
	return result;
918
}
922
}
923
 
919
924
920
/** GET_LOCAL_IP_ADDRESSES *******************************************
925
/** GET_LOCAL_IP_ADDRESSES *******************************************
921
  * Get all the IP addresses associated with this physical host.
926
  * Get all the IP addresses associated with this physical host.
Lines 927-932 Link Here
927
  *           -1 - severe error occured getting the IP address.  Call
932
  *           -1 - severe error occured getting the IP address.  Call
928
  *                ra_getLastErrorMajor/Minor to determine the
933
  *                ra_getLastErrorMajor/Minor to determine the
929
  *                error conditions.
934
  *                error conditions.
935
  *  IPV4-ONLY
930
  */
936
  */
931
short ra_getLocalIPAddresses(ra_IPAddresses_t *addr) {
937
short ra_getLocalIPAddresses(ra_IPAddresses_t *addr) {
932
	char localhost[MAX_HOST_NAME_LENGTH];
938
	char localhost[MAX_HOST_NAME_LENGTH];
Lines 1092-1098 Link Here
1092
	WORD wVersionRequested;
1098
	WORD wVersionRequested;
1093
	WSADATA wsaData;
1099
	WSADATA wsaData;
1094
	int err;
1100
	int err;
1095
	wVersionRequested = MAKEWORD(1, 1);
1101
	wVersionRequested = MAKEWORD(2, 2);
1096
1102
1097
	err = WSAStartup(wVersionRequested, &wsaData);
1103
	err = WSAStartup(wVersionRequested, &wsaData);
1098
1104
Lines 1109-1116 Link Here
1109
	/* 1.1 in wVersion since that is the version we */
1115
	/* 1.1 in wVersion since that is the version we */
1110
	/* requested. */
1116
	/* requested. */
1111
1117
1112
	if ( LOBYTE( wsaData.wVersion ) != 1 ||
1118
	if ( LOBYTE( wsaData.wVersion ) != 2 ||
1113
		HIBYTE( wsaData.wVersion ) != 1 ) {
1119
		HIBYTE( wsaData.wVersion ) != 2 ) {
1114
		/* Tell the user that we couldn't find a useable */
1120
		/* Tell the user that we couldn't find a useable */
1115
		/* winsock.dll. */
1121
		/* winsock.dll. */
1116
		ra_setLastError(SOCKET_INITIALIZE_FAILED, err);
1122
		ra_setLastError(SOCKET_INITIALIZE_FAILED, err);
(-)src-native-new/src/transport/RASocket/RASocket.h (+3 lines)
Lines 71-76 Link Here
71
  * @returns  0 - create server was successful.
71
  * @returns  0 - create server was successful.
72
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
72
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
73
  *               to get the failure details.
73
  *               to get the failure details.
74
  * IPV4-ONLY
74
  */
75
  */
75
extern int ra_createBoundUDPServer(short portNum,
76
extern int ra_createBoundUDPServer(short portNum,
76
								   ra_serverInfo_t *resultSet);
77
								   ra_serverInfo_t *resultSet);
Lines 89-94 Link Here
89
  * @returns  0 - create server was successful.
90
  * @returns  0 - create server was successful.
90
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
91
  *         <>0 - create failed, call ra_getlastErrorMajor/Minor
91
  *               to get the failure details.
92
  *               to get the failure details.
93
  * IPV4-ONLY
92
  */
94
  */
93
extern int ra_createTCPServer(unsigned short portNum,
95
extern int ra_createTCPServer(unsigned short portNum,
94
							  ra_serverInfo_t *resultSet);
96
							  ra_serverInfo_t *resultSet);
Lines 148-153 Link Here
148
 *           The number of characters written to the parameter buffer (excluding 
150
 *           The number of characters written to the parameter buffer (excluding 
149
 *           null terminator character.  Otherwise, -1 if a severe error occurred. 
151
 *           null terminator character.  Otherwise, -1 if a severe error occurred. 
150
 *           Call ra_getLastErrorMajor/Minor() to determine the error conditions.
152
 *           Call ra_getLastErrorMajor/Minor() to determine the error conditions.
153
 * IPV4-ONLY
151
 */
154
 */
152
extern short ra_getFormattedLocalIPAddress(char *ipAddressBuffer);
155
extern short ra_getFormattedLocalIPAddress(char *ipAddressBuffer);
153
156
(-)src-native-new/src/transport/transportSupport/TransportSupport.def (-5 / +23 lines)
Lines 25-31 Link Here
25
	ipcMemWrite  					  @22
25
	ipcMemWrite  					  @22
26
	ipcFlushToFunc					  @23
26
	ipcFlushToFunc					  @23
27
	ipcStopFlushing					  @24
27
	ipcStopFlushing					  @24
28
	getTargetHostInfo				  @25
28
	convertHostnameToAddrs			  @25
29
	setHandleInherited				  @26
29
	setHandleInherited				  @26
30
	isSocketValid					  @27
30
	isSocketValid					  @27
31
	destroyNamedPipe				  @28
31
	destroyNamedPipe				  @28
Lines 39-56 Link Here
39
	ipcMemWriteWithDIME				  @36
39
	ipcMemWriteWithDIME				  @36
40
	isValidTPTPBlock				  @37
40
	isValidTPTPBlock				  @37
41
	getPeerName				          @38
41
	getPeerName				          @38
42
	convertIPAddressStringToArray     @39
42
	convertIPAddressStringToArrayIPv4M @39
43
	convertIPAddressStringToUlong     @40
44
	getSocketIPString				  @41
43
	getSocketIPString				  @41
45
	ipcFlushToFD					  @42
44
	ipcFlushToFD					  @42
46
	tptp_checkHost					  @43
45
	tptp_checkHost					  @43
47
	openNonCriticalWriteOnlyNamedPipe @44
46
	openNonCriticalWriteOnlyNamedPipe @44
48
	getPeerIPString				      @45
47
	getPeerIPString				      @45
49
	convertIPAddressUlongToString     @46
48
	convertIPAddressUlongToStringIPv4M     @46
50
	getSocketPort                     @47
49
	getSocketPort                     @47
51
	ipcStopFlusher                    @48
50
	ipcStopFlusher                    @48
52
	ipcCloseMem                    	  @49
51
	ipcCloseMem                    	  @49
53
	isMemOpen						  @50
52
	isMemOpen						  @50
54
	ipcMemDetach					  @51
53
	ipcMemDetach					  @51
55
	closeSocket						  @52
54
	closeSocket						  @52
56
	
55
	getHostInfoIPv4M				  @53
56
	convertIPAddressStringToUlongIPv4M @54
57
	getSocketIPStringIPv4M			  @55
58
	freeAddressList					  @56
59
	getTargetHostInfoIPv4M			  @57
60
	getPeerAddrOfSocket				  @58
61
	getByteFormatFromSockAddr	      @59
62
	getSockAddrFromByteFormat		  @60
63
	connectToHostWithAddr			  @61
64
	setSockaddrStoragePort			  @62
65
	connectToTCPServerAddr			  @63
66
	connectToTCPServerIPv4M			  @64
67
	acceptSocketConnections			  @65
68
	initSocketAccept				  @66
69
	cloneSockAddr					  @67
70
	getLocalHostAddresses			  @68
71
	tptp_checkHostIPv6				  @69
72
	getSelfAddrOfSocket				  @70
73
	printSockAddrStorage			  @71
74
	connectToHostWithHostname		  @72
(-)src-native-new/src/transport/transportSupport/TransportSupportSocket.c (-82 / +941 lines)
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
}
(-)src-native-new/src/transport/transportSupport/TransportSupport.c (-6 / +137 lines)
Lines 323-329 Link Here
323
	return (isValid) ;
323
	return (isValid) ;
324
}
324
}
325
325
326
326
/* IPV4-ONLY */
327
long tptp_getLocalIPAddress() {
327
long tptp_getLocalIPAddress() {
328
	char localhost[MAX_HOST_NAME_LENGTH];
328
	char localhost[MAX_HOST_NAME_LENGTH];
329
	struct hostent *hpLocal;
329
	struct hostent *hpLocal;
Lines 352-358 Link Here
352
	return result;
352
	return result;
353
}
353
}
354
354
355
short tptp_getLocalIPAddresses(tptp_IPAddresses_t *addr) {
355
short tptp_getLocalIPAddressesIPv4M(tptp_IPAddresses_t *addr) {
356
	char localhost[MAX_HOST_NAME_LENGTH];
356
	char localhost[MAX_HOST_NAME_LENGTH];
357
	struct hostent *hpLocal;
357
	struct hostent *hpLocal;
358
	int count, i;
358
	int count, i;
Lines 399-405 Link Here
399
	return 0;
399
	return 0;
400
}
400
}
401
401
402
int isLocalHost(char* name) {
402
int isLocalHostIPv4M(char* name) {
403
	tptp_IPAddresses_t localAddresses;
403
	tptp_IPAddresses_t localAddresses;
404
	struct hostent *he;
404
	struct hostent *he;
405
	unsigned char *addr, *cur;
405
	unsigned char *addr, *cur;
Lines 412-418 Link Here
412
	
412
	
413
	addr = he->h_addr_list[0];
413
	addr = he->h_addr_list[0];
414
	
414
	
415
	if(tptp_getLocalIPAddresses(&localAddresses) != 0) return 0;
415
	if(tptp_getLocalIPAddressesIPv4M(&localAddresses) != 0) return 0;
416
	
416
	
417
	res = 0;
417
	res = 0;
418
	len = localAddresses.addressLength;
418
	len = localAddresses.addressLength;
Lines 432-437 Link Here
432
	return res;
432
	return res;
433
}
433
}
434
434
435
/** IPV4-ONLY */
435
BOOL tptp_checkHost(network_list_t *list, unsigned char *address) {
436
BOOL tptp_checkHost(network_list_t *list, unsigned char *address) {
436
	network_list_node_t *current;
437
	network_list_node_t *current;
437
438
Lines 464-470 Link Here
464
			/* We need to continuously recheck the IP address of the local machine as this can change
465
			/* We need to continuously recheck the IP address of the local machine as this can change
465
			   on modern operating systems without a reboot
466
			   on modern operating systems without a reboot
466
			*/
467
			*/
467
			if(tptp_getLocalIPAddresses(&localAddresses)==0) {
468
			if(tptp_getLocalIPAddressesIPv4M(&localAddresses)==0) {
468
				int len=localAddresses.addressLength;
469
				int len=localAddresses.addressLength;
469
				unsigned char *current;
470
				unsigned char *current;
470
				current=localAddresses.addresses;
471
				current=localAddresses.addresses;
Lines 545-551 Link Here
545
					aliases++;
546
					aliases++;
546
				}
547
				}
547
548
548
				if (isLocalHost(hostnameAllowed) && isLocalHost(hostnameEntry)) {
549
				if (isLocalHostIPv4M(hostnameAllowed) && isLocalHostIPv4M(hostnameEntry)) {
549
					return entry->allow ? TRUE : FALSE;
550
					return entry->allow ? TRUE : FALSE;
550
				}
551
				}
551
			}
552
			}
Lines 556-558 Link Here
556
557
557
	return FALSE;
558
	return FALSE;
558
}
559
}
560
561
BOOL tptp_checkHostIPv6(network_list_t *list, struct sockaddr_storage * addr) {
562
	network_list_node_t *current;
563
	int x = 0;
564
	unsigned char * ipaddr;
565
	int family = 0;
566
	int r = 0;
567
568
	//If the list or host address is not specified then return TRUE.
569
	//this means configuration is not specified.	
570
	if (list == NULL) {
571
		return TRUE;
572
	}
573
574
	if (addr == NULL) {
575
		return TRUE;
576
	}
577
578
	current = list->head;
579
580
	/* Look at the host list in order and find a matching rule */
581
	while(current) {
582
		network_t *entry=current->entry;
583
		/* Is this an allow rule */
584
585
		switch(entry->wildcard) {
586
587
		case TPTP_HW_ALL:
588
			if(entry->allow) {
589
				return TRUE;
590
			}
591
			return FALSE;
592
			break;
593
		case TPTP_HW_LOCAL:	
594
595
			if(isAddrLocal(addr)) {
596
				if(entry->allow) {
597
					return TRUE;
598
				}
599
				return FALSE;
600
			} 
601
			break;
602
		/* We are currently going to ignore UNKNOWN, and KNOWN posibilities */
603
		case TPTP_HW_UNKNOWN:
604
			break;
605
		case TPTP_HW_KNOWN:
606
			break;
607
		case TPTP_HW_MAX_VALUE:
608
			break;
609
		case TPTP_HW_NET:
610
			{
611
				getByteFormatFromSockAddr(addr, (void **)&ipaddr, &family);
612
				if(entry->netAndMaskAreIpv4 && addr->ss_family == PF_INET) {
613
					/* Check to see if the host is OK based upon net and mask.  If the mask and the remote address
614
					   and-ed together equal the net then it is a match */
615
					if((entry->mask[0] & ipaddr[0]) == entry->net[0]
616
						&& (entry->mask[1] & ipaddr[1]) == entry->net[1]
617
						&& (entry->mask[2] & ipaddr[2]) == entry->net[2]
618
						&& (entry->mask[3] & ipaddr[3]) == entry->net[3]) {
619
						if(entry->allow) {
620
							return TRUE;
621
						}
622
						return FALSE;
623
					}				
624
				} else if(!entry->netAndMaskAreIpv4 && addr->ss_family == PF_INET6){
625
626
					r = 1;
627
					for(x = 0; x < 16; x++) {
628
						if( (entry->maskipv6[x] & ipaddr[x]) == entry->netipv6[x]) {
629
							// No mismatch, this is good.
630
						} else {
631
							// We've found a mismatch. Set false and break.
632
							r = 0;
633
							break;
634
						}
635
					}
636
					if(r) {
637
						if(entry->allow) {
638
							return TRUE;
639
						}
640
						return FALSE;						
641
					}
642
				}
643
				
644
			}
645
			break;
646
		case TPTP_HW_NAMED:
647
			{
648
				char *hostname;
649
650
				struct sockaddr_storage ** addrs;
651
				int numAddrs = 0;
652
				int x = 0;
653
654
				// Convert the config host name to a list of addresses and compare them
655
				convertHostnameToAddrs(entry->hostname, &addrs, &numAddrs);
656
				for(x = 0; x < numAddrs; x++) {
657
					if(compareAddresses(addr, addrs[x])) {
658
						freeAddressList(addrs, numAddrs);
659
						
660
						if(entry->allow) {						
661
							return TRUE;
662
						}
663
						return FALSE;						
664
						
665
					}
666
				}
667
				freeAddressList(addrs, numAddrs);
668
				
669
				if(isAddrLocal(addr)) {
670
					return (entry->allow) ? TRUE : FALSE;					
671
				}
672
				
673
				hostname = convertAddrToHostname(addr);
674
				
675
				if(strcasecmp(hostname, entry->hostname) == 0) {
676
					tptp_free(hostname);
677
					return (entry->allow) ? TRUE : FALSE;					
678
				}
679
				
680
				tptp_free(hostname);
681
682
			}
683
			break;
684
		}
685
		current=current->next;
686
	}
687
688
	return FALSE;
689
}
(-)src-native-new/src/transport/transportSupport/TransportOSCalls_win.c (-4 / +6 lines)
Lines 33-45 Link Here
33
	WSADATA  wsaData;
33
	WSADATA  wsaData;
34
	int      rc = 0 ;
34
	int      rc = 0 ;
35
35
36
	wVersionRequested = MAKEWORD(1, 1);
36
	wVersionRequested = MAKEWORD(2, 2);
37
37
38
	rc = WSAStartup(wVersionRequested, &wsaData);
38
	rc = WSAStartup(wVersionRequested, &wsaData);
39
39
40
	if (rc == 0) 
40
	if (rc == 0) 
41
	{
41
	{
42
		if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) 
42
		if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) 
43
		{
43
		{
44
			WSACleanup();
44
			WSACleanup();
45
			rc = -1 ;
45
			rc = -1 ;
Lines 91-96 Link Here
91
 *
91
 *
92
 * @return
92
 * @return
93
 *    0 - always
93
 *    0 - always
94
 * IPV4-ONLY
94
 *********************************************************/
95
 *********************************************************/
95
int printSocketRequest(SOCKET sock, struct sockaddr_in *pRequester)
96
int printSocketRequest(SOCKET sock, struct sockaddr_in *pRequester)
96
{
97
{
Lines 174-180 Link Here
174
 *    0          success
175
 *    0          success
175
 *    non-zero   failure
176
 *    non-zero   failure
176
 *********************************************************/
177
 *********************************************************/
177
int convertIPAddressStringToArray( char *addrStr, unsigned char* addrArray )
178
int convertIPAddressStringToArrayIPv4M( char *addrStr, unsigned char* addrArray )
178
{
179
{
179
	struct in_addr addr;
180
	struct in_addr addr;
180
	
181
	
Lines 205-211 Link Here
205
 *    0          success
206
 *    0          success
206
 *    non-zero   failure
207
 *    non-zero   failure
207
 *********************************************************/
208
 *********************************************************/
208
int convertIPAddressUlongToString( unsigned long addr, char **addrStr )
209
int convertIPAddressUlongToStringIPv4M( unsigned long addr, char **addrStr )
209
{
210
{
210
	char* tempAddrStr;
211
	char* tempAddrStr;
211
	struct in_addr  saddr;
212
	struct in_addr  saddr;
Lines 230-235 Link Here
230
	return 0;
231
	return 0;
231
}
232
}
232
233
234
/** IPV4-ONLY */
233
void extractAddressFromSockAddr(struct sockaddr_in *connection, unsigned char *address)
235
void extractAddressFromSockAddr(struct sockaddr_in *connection, unsigned char *address)
234
{
236
{
235
	address[0]=connection->sin_addr.S_un.S_un_b.s_b1;
237
	address[0]=connection->sin_addr.S_un.S_un_b.s_b1;
(-)src-native-new/src/transport/transportSupport/TransportOSCalls_linux.c (-2 / +4 lines)
Lines 84-89 Link Here
84
 *
84
 *
85
 * @return
85
 * @return
86
 *    0 - always
86
 *    0 - always
87
 * IPV4-ONLY
87
 *********************************************************/
88
 *********************************************************/
88
int printSocketRequest(SOCKET sock, struct sockaddr_in *pRequester)
89
int printSocketRequest(SOCKET sock, struct sockaddr_in *pRequester)
89
{
90
{
Lines 169-175 Link Here
169
 *    0          success
170
 *    0          success
170
 *    non-zero   failure
171
 *    non-zero   failure
171
 *********************************************************/
172
 *********************************************************/
172
int convertIPAddressStringToArray( char *addrStr, unsigned char* addrArray )
173
int convertIPAddressStringToArrayIPv4M( char *addrStr, unsigned char* addrArray )
173
{
174
{
174
	struct in_addr addr;
175
	struct in_addr addr;
175
	int            rc;
176
	int            rc;
Lines 205-211 Link Here
205
 *    0          success
206
 *    0          success
206
 *    non-zero   failure
207
 *    non-zero   failure
207
 *********************************************************/
208
 *********************************************************/
208
int convertIPAddressUlongToString( unsigned long addr, char **addrStr )
209
int convertIPAddressUlongToStringIPv4M( unsigned long addr, char **addrStr )
209
{
210
{
210
	char* tempAddrStr;
211
	char* tempAddrStr;
211
	struct in_addr  saddr;
212
	struct in_addr  saddr;
Lines 230-235 Link Here
230
	return 0;
231
	return 0;
231
}
232
}
232
233
234
/** IPV4-ONLY */
233
void extractAddressFromSockAddr(struct sockaddr_in *connection, unsigned char *address)
235
void extractAddressFromSockAddr(struct sockaddr_in *connection, unsigned char *address)
234
{
236
{
235
	unsigned long tmpAddr;
237
	unsigned long tmpAddr;
(-)src-native-new/src/shared/TPTPUtil/TPTPSemaphore.c (+44 lines)
Lines 20-25 Link Here
20
#include <errno.h>
20
#include <errno.h>
21
#endif
21
#endif
22
22
23
/*
24
#ifndef _WIN32 
25
	#include <semaphore.h>
26
	#include <time.h> 
27
#endif
28
*/
23
/* Semaphore utilities */
29
/* Semaphore utilities */
24
30
25
/* 
31
/* 
Lines 114-116 Link Here
114
120
115
	return 0;
121
	return 0;
116
}
122
}
123
/*
124
int tptp_waitSemaphoreTimeout(Semaphore_t *pSem, int timeoutInSecs) {
125
	// Note this code is a placeholder that hasn't been tested. 
126
	// Keep this in mind when using it. - jwest
127
#ifdef _WIN32
128
	unsigned long rc;
129
	if (pSem->hSem == NULL)
130
		return -1;
131
132
	rc = WaitForSingleObject(pSem->hSem, timeoutInSecs*1000);
133
	if(rc == WAIT_TIMEOUT) {
134
		return -2;
135
	} else
136
	if (rc != WAIT_OBJECT_0)
137
		return -1;
138
139
#else
140
	struct timespec waitTime;
141
	if (clock_gettime(CLOCK_REALTIME, &waitTime) == -1) {
142
		return -1;
143
	}
144
	waitTime.tv_sec += timeoutInSecs;
145
	
146
	while (1) {
147
		int rc = sem_timedwait(&(pSem->sem), &waitTime);
148
		if (rc == 0) break;
149
150
		if(errno == ETIMEDOUT) {
151
			return -2;
152
		}
153
		if (errno != EINTR) return -1;
154
	}
155
#endif
156
157
	return 0;	
158
}
159
*/
160
(-)src-native-new/src/shared/TPTPUtil/TPTPUtil.cpp (-16 / +87 lines)
Lines 1685-1690 Link Here
1685
#define ELEMENT_TRANS_LYR_VARIABLE "TransportLayer"
1685
#define ELEMENT_TRANS_LYR_VARIABLE "TransportLayer"
1686
#define ELEMENT_CONFIGURATION_VARIABLE "Configuration"
1686
#define ELEMENT_CONFIGURATION_VARIABLE "Configuration"
1687
1687
1688
1689
/** Converts an ASCII hex character to an int. */
1690
int getcharhexval(char c) {
1691
    if(c >= '0' && c <=  '9') {
1692
         return c - '0';
1693
    }
1694
1695
    if(c >= 'A' && c <= 'F') {
1696
         return c - 'A' + 10;
1697
    }    
1698
    
1699
    if(c >= 'a' && c <= 'f') {
1700
         return c - 'a' + 10;         
1701
    }
1702
    
1703
    return -1;
1704
    
1705
}
1706
1707
void parseStringIntoBlock(char *str, unsigned char *result) {
1708
	unsigned int val = 0;
1709
1710
	val = getcharhexval(str[2])*16 + getcharhexval(str[3]);
1711
	result[1] = (unsigned char)val;
1712
	
1713
1714
	val = getcharhexval(str[0]) * 16 + getcharhexval(str[1]);
1715
	result[0] = (unsigned char)val;
1716
1717
}
1718
1719
void parseIPv6IP(char *str, unsigned char *result) {
1720
	
1721
	char *nameTemp = 0;
1722
	int i;
1723
	
1724
	nameTemp = (char *)tptp_malloc(strlen(str)+10);
1725
	strcpy(nameTemp, str);
1726
	
1727
	if(strcmp(str, "::1") == 0) {
1728
		for(int x = 0; x < 16; x++) {
1729
			result[x] = 0;
1730
		}
1731
		result[15] = 1;
1732
		
1733
		return;
1734
	}
1735
1736
	/* start at the back of the string and calculate the address*/
1737
	for(i=14; i>0; i-=2) {
1738
		char *current=strrchr(nameTemp, ':');
1739
		if(current == NULL) {
1740
			// The IP address is of an improper format, so bail.
1741
			tptp_free(nameTemp);
1742
			for(int x = 0; x < 16; x++) {
1743
				result[x] = 0;
1744
			}
1745
			return;
1746
		}
1747
		// result[i]=(char)atoi(current+1);
1748
		parseStringIntoBlock(current+1, &(result[i]));
1749
1750
		*current='\0';
1751
	}
1752
	parseStringIntoBlock(nameTemp, &(result[0]));
1753
	// result[0]=atoi(nameTemp);
1754
1755
	tptp_free(nameTemp);
1756
	
1757
}
1758
1688
void tptp_addNetwork(network_list_t *list, int allow, tptp_host_wildcard_t wildcard, char *name, char *mask)
1759
void tptp_addNetwork(network_list_t *list, int allow, tptp_host_wildcard_t wildcard, char *name, char *mask)
1689
{
1760
{
1690
	char *nameTemp = NULL;
1761
	char *nameTemp = NULL;
Lines 1699-1710 Link Here
1699
1770
1700
	/* set the type in the node */
1771
	/* set the type in the node */
1701
	network->allow=allow;
1772
	network->allow=allow;
1702
1773
	
1703
	/* copy name to a temporary buffer */
1774
	/* copy name to a temporary buffer */
1704
	if (name != NULL) {
1775
	if (name != NULL) {
1705
		nameTemp = (char *) tptp_malloc(strlen(name) +1);
1776
		nameTemp = (char *) tptp_malloc(strlen(name) +1);
1706
		strcpy(nameTemp, name);
1777
		strcpy(nameTemp, name);
1707
	}
1778
	}
1779
	
1708
	/* copy mask to a temporary buffer */
1780
	/* copy mask to a temporary buffer */
1709
	if (mask != NULL) {
1781
	if (mask != NULL) {
1710
		maskTemp = (char *) tptp_malloc(strlen(mask) +1);
1782
		maskTemp = (char *) tptp_malloc(strlen(mask) +1);
Lines 1713-1735 Link Here
1713
1785
1714
	/* If the wildcard in TPTP_HW_NAMED then we need to copy the address information */
1786
	/* If the wildcard in TPTP_HW_NAMED then we need to copy the address information */
1715
	if(wildcard==TPTP_HW_NAMED) {
1787
	if(wildcard==TPTP_HW_NAMED) {
1716
1788
		network->hostname = (char *) tptp_malloc(strlen(nameTemp) +1);
1717
		/* If the first character is a digit then we have a numeric address.  This
1789
		strcpy(network->hostname, nameTemp);
1718
		   is effectively the same as a TPTP_HW_NET with a mask of 255.255.255.255
1719
		*/
1720
		if(isdigit((int)*nameTemp)) {
1721
			maskTemp = (char*)tptp_malloc(sizeof(char) * 16); /* one extra char for null char */
1722
			strcpy(maskTemp, "255.255.255.255");
1723
			wildcard=TPTP_HW_NET;
1724
		}
1725
		else {
1726
			network->hostname = (char *) tptp_malloc(strlen(nameTemp) +1);
1727
			strcpy(network->hostname, nameTemp);
1728
		}
1729
	}
1790
	}
1730
1791
1731
	/* If this is a network then we need to copy the address information */
1792
	/* If this is a network then we need to copy the IPv4 address information */
1732
	if(wildcard==TPTP_HW_NET) {
1793
	if(wildcard==TPTP_HW_NET && strstr(name, ".") != NULL) {
1733
		int i;
1794
		int i;
1734
1795
1735
		/* start at the back of the string and calculate the address*/
1796
		/* start at the back of the string and calculate the address*/
Lines 1746-1751 Link Here
1746
			*current='\0';
1807
			*current='\0';
1747
		}
1808
		}
1748
		network->mask[0]=atoi(maskTemp);
1809
		network->mask[0]=atoi(maskTemp);
1810
		
1811
		network->netAndMaskAreIpv4 = 1;
1812
	
1813
	} // TPTP_HW_NET - IPv6 address
1814
	else if(wildcard==TPTP_HW_NET && strstr(name, ":") != NULL) {
1815
1816
		parseIPv6IP(name, network->netipv6);
1817
		parseIPv6IP(mask, network->maskipv6);
1818
		
1819
		network->netAndMaskAreIpv4 = 0;
1749
	}
1820
	}
1750
1821
1751
	if(nameTemp) tptp_free(nameTemp); nameTemp = NULL;
1822
	if(nameTemp) tptp_free(nameTemp); nameTemp = NULL;
(-)src-native-new/src/transport/socketTL/SocketListener.c (-14 / +42 lines)
Lines 115-120 Link Here
115
int setSSL(request_block_ptr_t pBlk);
115
int setSSL(request_block_ptr_t pBlk);
116
int handleAUTHENTICATE(request_block_ptr_t pBlk, char *pMsg);
116
int handleAUTHENTICATE(request_block_ptr_t pBlk, char *pMsg);
117
117
118
THREAD_USER_FUNC_RET_TYPE serveRequestThread(LPVOID args);
119
120
typedef struct { 
121
	SOCKET serverSock;
122
	server_block_ptr_t pServerData;
123
} serve_request_params_t;
124
125
118
/**
126
/**
119
 *********************************************************
127
 *********************************************************
120
 *
128
 *
Lines 658-663 Link Here
658
	return ( pRequestDataBlock ) ;
666
	return ( pRequestDataBlock ) ;
659
}
667
}
660
668
669
THREAD_USER_FUNC_RET_TYPE serveRequestThread(LPVOID args) {
670
	
671
	serve_request_params_t * params = (serve_request_params_t *)args;
672
	serveRequest(params->serverSock, params->pServerData);
673
	
674
	return 0;
675
}
676
661
677
662
/**
678
/**
663
 *********************************************************
679
 *********************************************************
Lines 678-683 Link Here
678
	TID threadId;
694
	TID threadId;
679
	HANDLE threadHandle ;
695
	HANDLE threadHandle ;
680
696
697
	// struct sockaddr_storage *selfaddr;
698
681
	/* initial status before the thread is running */
699
	/* initial status before the thread is running */
682
	pServerData->threadStatus = RUNNING ;
700
	pServerData->threadStatus = RUNNING ;
683
	pServerData->serverSock   = serverSock ;
701
	pServerData->serverSock   = serverSock ;
Lines 687-692 Link Here
687
	{
705
	{
688
        clientSock = acceptSocketConnection(serverSock); 
706
        clientSock = acceptSocketConnection(serverSock); 
689
707
708
//		selfaddr = getSelfAddrOfSocket(clientSock);
709
690
		if (isSocketValid(clientSock) == 0)
710
		if (isSocketValid(clientSock) == 0)
691
		{
711
		{
692
			TPTP_LOG_ERROR_MSG(pServerData, "Accept() receives invalid socket request. ") ;
712
			TPTP_LOG_ERROR_MSG(pServerData, "Accept() receives invalid socket request. ") ;
Lines 761-789 Link Here
761
THREAD_USER_FUNC_RET_TYPE doListening(LPVOID args) 
781
THREAD_USER_FUNC_RET_TYPE doListening(LPVOID args) 
762
{
782
{
763
	int      rc = 0;
783
	int      rc = 0;
764
	SOCKET   serverSock ;
784
	SOCKET   serverSockets[FD_SETSIZE];
785
	int socketNum = 0;
786
	int i;
787
 	unsigned long pthreadId;
788
	HANDLE pthreadHandle;
765
		
789
		
766
	struct sockaddr_in  myServerSockAddr ;
790
	serve_request_params_t * params;
767
791
	
768
	server_block_ptr_t  pParam = (server_block_ptr_t) args ;
792
	server_block_ptr_t  pParam = (server_block_ptr_t) args ;
769
793
770
	/* create and initialize the server socket */
794
	/* create and initialize the server socket */
771
	serverSock = getTheSocket(pParam->port, &myServerSockAddr) ;
795
	rc = getTheSocket(pParam->port, serverSockets, &socketNum) ;
772
796
773
	if (serverSock < 0)
797
	if (socketNum == 0)
774
	{
798
	{
775
		TPTP_LOG_ERROR_MSG(pParam, "Error: unable to create the server socket.") ;
799
		TPTP_LOG_ERROR_MSG(pParam, "Error: unable to create the server socket.") ;
776
	}
800
	}
777
801
778
	else
779
		rc = bindAndListen(serverSock, (struct sockaddr*)&myServerSockAddr) ;
780
781
782
	/* ready to accept incoming connection requests */
802
	/* ready to accept incoming connection requests */
783
	if (rc == 0)
803
	if (socketNum != 0 && rc == 0)
784
	{
804
	{
785
		TPTP_LOG_DEBUG_MSG1(pParam, "Socket server is running at port number of %d.", pParam->port) ;
805
		TPTP_LOG_DEBUG_MSG1(pParam, "Socket server is running at port number of %d.", pParam->port) ;
786
		serveRequest(serverSock, pParam) ;
806
		for (i=0; i < socketNum; ++i) {
807
			
808
			params = (serve_request_params_t *)tptp_malloc(sizeof(serve_request_params_t));
809
			params->pServerData = pParam;
810
			params->serverSock = serverSockets[i];
811
			
812
			tptpStartThread(serveRequestThread, params, &pthreadId, &pthreadHandle);
813
			
814
		}
787
	}
815
	}
788
816
789
	return ( 0 );
817
	return ( 0 );
Lines 1111-1117 Link Here
1111
	/* NOTE: This has problems if there are multiple IP addresses, but in
1139
	/* NOTE: This has problems if there are multiple IP addresses, but in
1112
	         the case of compatibility mode, this isn't really used anyway */
1140
	         the case of compatibility mode, this isn't really used anyway */
1113
	
1141
	
1114
	if (getSocketIPString( (SOCKET)NULL, &ipAddrStr) < 0) {
1142
	if (getSocketIPStringIPv4M( (SOCKET)NULL, &ipAddrStr) < 0) {
1115
		TPTP_LOG_ERROR_MSG(pServerData, "Unable to get local IP address");
1143
		TPTP_LOG_ERROR_MSG(pServerData, "Unable to get local IP address");
1116
		return -1;
1144
		return -1;
1117
	}
1145
	}
Lines 1169-1175 Link Here
1169
		TPTP_LOG_ERROR_MSG1( pServerData, "Unable to find host in connection info: %s", sourceConnectionInfo );
1197
		TPTP_LOG_ERROR_MSG1( pServerData, "Unable to find host in connection info: %s", sourceConnectionInfo );
1170
		return rc;	
1198
		return rc;	
1171
	}
1199
	}
1172
	rc = convertIPAddressStringToUlong( ipAddrStr, &ipAddr );
1200
	rc = convertIPAddressStringToUlongIPv4M( ipAddrStr, &ipAddr );
1173
	if ( rc != 0 )
1201
	if ( rc != 0 )
1174
	{
1202
	{
1175
		TPTP_LOG_ERROR_MSG1( pServerData, "Unable to convert IP address string to unsigned long: %s", ipAddrStr );
1203
		TPTP_LOG_ERROR_MSG1( pServerData, "Unable to convert IP address string to unsigned long: %s", ipAddrStr );
Lines 1180-1186 Link Here
1180
	/* TODO: Check to see if this is local */
1208
	/* TODO: Check to see if this is local */
1181
1209
1182
	/* Connect to the remote AC */
1210
	/* Connect to the remote AC */
1183
	rc = connectToTCPServer( ipAddr, (unsigned short)port, &sock);
1211
	rc = connectToTCPServerIPv4M( ipAddr, (unsigned short)port, &sock);
1184
	if ( rc != 0 )
1212
	if ( rc != 0 )
1185
	{
1213
	{
1186
		TPTP_LOG_ERROR_MSG2( pServerData, "Unable to connect to peer: %s, port %d", ipAddrStr, port );
1214
		TPTP_LOG_ERROR_MSG2( pServerData, "Unable to connect to peer: %s, port %d", ipAddrStr, port );
(-)src-native-new/src/shared/TPTPLogUtils/TPTPCBE.cpp (-11 / +2 lines)
Lines 155-179 Link Here
155
	if ( formattedLocalIPAddress[0] == '\0' ) 
155
	if ( formattedLocalIPAddress[0] == '\0' ) 
156
	{
156
	{
157
		char hostname[CBE_HOSTNAME_SIZE+1];
157
		char hostname[CBE_HOSTNAME_SIZE+1];
158
		struct hostent *host;
159
158
160
		// TODO: Make WSAStartup call on Windows
159
		// TODO: Make WSAStartup call on Windows
161
	
160
	
162
		if ( gethostname(hostname, CBE_HOSTNAME_SIZE) == 0 ) 
161
		if ( gethostname(hostname, CBE_HOSTNAME_SIZE) == 0 ) 
163
		{
162
		{
164
			host = gethostbyname(hostname);
163
			sprintf(formattedLocalIPAddress, "%s", hostname);
165
			if(host == NULL) 
166
			{
167
				sprintf(formattedLocalIPAddress, "%s", hostname);
168
			}
169
			else 
170
			{
171
				sprintf(formattedLocalIPAddress, "%s", host->h_name);
172
			}
173
		}
164
		}
174
		else 
165
		else 
175
		{
166
		{
176
			sprintf(formattedLocalIPAddress, "127.0.0.1");
167
			sprintf(formattedLocalIPAddress, "localhost");
177
		}
168
		}
178
	}
169
	}
179
170
(-)src-native-new/src/shared/TPTPLogUtils/TPTPLogUtils.cpp (-1 / +1 lines)
Lines 35-41 Link Here
35
			{
35
			{
36
				WSADATA  wsaData;
36
				WSADATA  wsaData;
37
37
38
				WSAStartup( MAKEWORD(1, 1), &wsaData);
38
				WSAStartup( MAKEWORD(2, 2), &wsaData);
39
			}
39
			}
40
			break;
40
			break;
41
41
(-)src-native-new/src/client/TCPDataServer.cpp (-4 / +66 lines)
Lines 576-581 Link Here
576
576
577
577
578
578
579
580
/**
581
 *********************************************************
582
 *                                                       
583
 * @brief                                                
584
 *    Given a hostname, get the current host IP address. An array of sockaddr_storages are stored in outResult, and the size
585
 *	  of that array is stored in outNumResults.
586
 * @return
587
 *    the IP address number
588
 *
589
 *********************************************************/
590
/*
591
void TCPDataServer::getIPAddresses(char *hostname, struct sockaddr_storage *** outResult, int *outNumResults) {
592
	int RetVal;
593
	int i;
594
595
	struct addrinfo Hints, *AddrInfo, *AI;
596
597
    int Family = PF_UNSPEC; // Give us both the IPv4 and IPv6 addresses
598
    int SocketType = SOCK_STREAM; // TCP only
599
600
	struct sockaddr_storage ** result;
601
602
	int    rc = 0 ;
603
604
	rc = initForSocketCalls();
605
	if(rc != 0) {
606
		*outNumResults = 0;
607
	}
608
	
609
	result = (struct sockaddr_storage **)tptp_malloc(sizeof(struct sockaddr_storage *) * 128);
610
	
611
	memset(&Hints, 0, sizeof (Hints));
612
    Hints.ai_family = Family;
613
    Hints.ai_socktype = SocketType;
614
	
615
	// This was originally in the code, but it prevents lookups of host names, e.g. www.google.com. It only works with numeric hosts.
616
    // Hints.ai_flags = AI_NUMERICHOST;  
617
618
    RetVal = getaddrinfo(hostname, NULL, &Hints, &AddrInfo);
619
620
	for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next, i++) {
621
		result[i] = cloneSockAddr((struct sockaddr_storage *)AI->ai_addr);
622
	}
623
	freeaddrinfo(AddrInfo);
624
625
	*outResult = result;
626
	*outNumResults = i;
627
628
	return RetVal;
629
}*/
630
631
579
/**
632
/**
580
 *********************************************************
633
 *********************************************************
581
 *                                                       
634
 *                                                       
Lines 586-592 Link Here
586
 *    the IP address number
639
 *    the IP address number
587
 *
640
 *
588
 *********************************************************/
641
 *********************************************************/
589
642
/*
590
unsigned long TCPDataServer::getLocalIPAddress() {
643
unsigned long TCPDataServer::getLocalIPAddress() {
591
	struct hostent *hpLocal = NULL;
644
	struct hostent *hpLocal = NULL;
592
	unsigned long   result = 0 ;
645
	unsigned long   result = 0 ;
Lines 595-601 Link Here
595
	rc = initForSocketCalls() ;
648
	rc = initForSocketCalls() ;
596
	if (rc == 0)
649
	if (rc == 0)
597
	{
650
	{
598
		hpLocal = getHostInfo() ;
651
		hpLocal = getHostInfoIPv4M() ;
599
652
600
		if (hpLocal != NULL)
653
		if (hpLocal != NULL)
601
			memcpy(&result, hpLocal->h_addr_list[0], sizeof(long));
654
			memcpy(&result, hpLocal->h_addr_list[0], sizeof(long));
Lines 605-612 Link Here
605
658
606
	return result;
659
	return result;
607
}
660
}
661
*/
608
662
609
663
#define MAX_HOST_NAME_LENGTH	128
610
664
611
void TCPDataServer::connect()
665
void TCPDataServer::connect()
612
{
666
{
Lines 614-622 Link Here
614
	char buffer[1024] ;
668
	char buffer[1024] ;
615
	int  bufferLength = 1024 ;
669
	int  bufferLength = 1024 ;
616
	int  bytesRead = 0 ;
670
	int  bytesRead = 0 ;
671
	
672
	char hostname[MAX_HOST_NAME_LENGTH];
673
	
674
	// Get the local host informantion 
675
	if (gethostname(hostname,MAX_HOST_NAME_LENGTH) < 0) {
676
		TPTP_LOG_DEBUG_MSG("Error: Unable to resolve local host name.");
677
		return;
678
	}
617
679
618
	/* go make connection */
680
	/* go make connection */
619
	rc = connectToTCPServer(getLocalIPAddress(), _port, &_sock) ;
681
	rc = connectToTCPServer(hostname, _port, &_sock) ;
620
682
621
	if (rc != 0)
683
	if (rc != 0)
622
	{
684
	{
(-)src-native-new/src/client/ConnectionImpl.h (-1 / +2 lines)
Lines 72-78 Link Here
72
72
73
				void init();
73
				void init();
74
				unsigned long getLocalIPAddress();
74
				unsigned long getLocalIPAddress();
75
				unsigned long getIPAddress(char* hostname);
75
				void getIPAddresses(char *hostname, struct sockaddr_storage *** outResult, int *outNumResults);
76
				unsigned long getIPAddressIPv4(char* hostname);
76
				int sendControlCommand(SOCKET sockconn, int commandflags);
77
				int sendControlCommand(SOCKET sockconn, int commandflags);
77
				int sendCONNECT_DATACommand(SOCKET datasock, int direction, int commandflags);
78
				int sendCONNECT_DATACommand(SOCKET datasock, int direction, int commandflags);
78
				int processControlMessage(char buffer[], int length);
79
				int processControlMessage(char buffer[], int length);
(-)src-native-new/src/client/ConnectionImpl.cpp (-11 / +61 lines)
Lines 32-38 Link Here
32
using namespace std;
32
using namespace std;
33
using namespace TPTP::Client;
33
using namespace TPTP::Client;
34
34
35
35
#ifdef _WIN32
36
	#include <Ws2tcpip.h>
37
#endif
36
38
37
ConnectionImpl::ConnectionImpl()
39
ConnectionImpl::ConnectionImpl()
38
{
40
{
Lines 219-226 Link Here
219
 * @return
221
 * @return
220
 *    the IP address number
222
 *    the IP address number
221
 *
223
 *
224
 *  IPV4-ONLY
222
 *********************************************************/
225
 *********************************************************/
223
224
unsigned long ConnectionImpl::getLocalIPAddress() {
226
unsigned long ConnectionImpl::getLocalIPAddress() {
225
	struct hostent *hpLocal = NULL;
227
	struct hostent *hpLocal = NULL;
226
	unsigned long   result = 0 ;
228
	unsigned long   result = 0 ;
Lines 229-235 Link Here
229
	rc = initForSocketCalls() ;
231
	rc = initForSocketCalls() ;
230
	if (rc == 0)
232
	if (rc == 0)
231
	{
233
	{
232
		hpLocal = getHostInfo() ;
234
		hpLocal = getHostInfoIPv4M() ; // TODO replace with portable impl
233
235
234
		if (hpLocal != NULL)
236
		if (hpLocal != NULL)
235
			memcpy(&result, hpLocal->h_addr_list[0], sizeof(long));
237
			memcpy(&result, hpLocal->h_addr_list[0], sizeof(long));
Lines 245-258 Link Here
245
 *********************************************************
247
 *********************************************************
246
 *                                                       
248
 *                                                       
247
 * @brief                                                
249
 * @brief                                                
248
 *    get the current host IP address
250
 *    Given a hostname, get the current host IP address. An array of sockaddr_storages are stored in outResult, and the size
249
 *
251
 *	  of that array is stored in outNumResults.
250
 * @return
252
 * @return
251
 *    the IP address number
253
 *    the IP address number
252
 *
254
 *
253
 *********************************************************/
255
 *********************************************************/
256
void ConnectionImpl::getIPAddresses(char *hostname, struct sockaddr_storage *** outResult, int *outNumResults) {
257
	int RetVal;
258
	int i;
259
260
	struct addrinfo Hints, *AddrInfo, *AI;
261
262
    int Family = PF_UNSPEC; // Give us both the IPv4 and IPv6 addresses
263
    int SocketType = SOCK_STREAM; // TCP only
264
265
	struct sockaddr_storage ** result;
266
267
	int    rc = 0 ;
268
269
	rc = initForSocketCalls();
270
	if(rc != 0) {
271
		*outNumResults = 0;
272
	}
273
	
274
	result = (struct sockaddr_storage **)tptp_malloc(sizeof(struct sockaddr_storage *) * 128);
275
	
276
	memset(&Hints, 0, sizeof (Hints));
277
    Hints.ai_family = Family;
278
    Hints.ai_socktype = SocketType;
279
	
280
	// This was originally in the code, but it prevents lookups of host names, e.g. www.google.com. It only works with numeric hosts.
281
    // Hints.ai_flags = AI_NUMERICHOST;  
282
283
    RetVal = getaddrinfo(hostname, NULL, &Hints, &AddrInfo);
254
284
255
unsigned long ConnectionImpl::getIPAddress(char* hostname) {
285
	for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next, i++) {
286
		result[i] = cloneSockAddr((struct sockaddr_storage *)AI->ai_addr);
287
	}
288
	freeaddrinfo(AddrInfo);
289
290
	if(i == 0) {
291
		tptp_free(result);
292
		result = NULL;
293
	}
294
295
	*outResult = result;
296
	*outNumResults = i;
297
298
}
299
300
301
302
303
304
unsigned long ConnectionImpl::getIPAddressIPv4(char* hostname) {
256
	struct hostent *hpHost = NULL;
305
	struct hostent *hpHost = NULL;
257
	unsigned long   result = 0 ;
306
	unsigned long   result = 0 ;
258
	int    rc = 0 ;
307
	int    rc = 0 ;
Lines 260-266 Link Here
260
	rc = initForSocketCalls() ;
309
	rc = initForSocketCalls() ;
261
	if (rc == 0)
310
	if (rc == 0)
262
	{
311
	{
263
		hpHost = getTargetHostInfo(hostname) ;
312
		hpHost = getTargetHostInfoIPv4M(hostname) ;
264
313
265
		if (hpHost != NULL)
314
		if (hpHost != NULL)
266
			memcpy(&result, hpHost->h_addr_list[0], sizeof(long));
315
			memcpy(&result, hpHost->h_addr_list[0], sizeof(long));
Lines 270-276 Link Here
270
}
319
}
271
320
272
321
273
274
int ConnectionImpl::connect(INode* node, int port)
322
int ConnectionImpl::connect(INode* node, int port)
275
{
323
{
276
	int rc = 0;
324
	int rc = 0;
Lines 279-285 Link Here
279
	_port = port;
327
	_port = port;
280
	/* go make connection */
328
	/* go make connection */
281
	init();
329
	init();
282
	rc = connectToTCPServer(getIPAddress(_node->getName()), port, &sock) ;
330
331
332
	rc = connectToTCPServer(_node->getName(), port, &sock) ;
283
333
284
	if (rc != 0)
334
	if (rc != 0)
285
	{
335
	{
Lines 307-313 Link Here
307
	_port = connInfo->getPortNum();
357
	_port = connInfo->getPortNum();
308
	/* go make connection */
358
	/* go make connection */
309
	init();
359
	init();
310
	rc = connectToTCPServer(getIPAddress(connInfo->getHostName()), _port, &sock) ;
360
	rc = connectToTCPServer(connInfo->getHostName(), _port, &sock) ;
311
361
312
	if (rc != 0)
362
	if (rc != 0)
313
	{
363
	{
Lines 513-519 Link Here
513
	SOCKET datasock = 0;
563
	SOCKET datasock = 0;
514
	int dataConnectionId;
564
	int dataConnectionId;
515
565
516
	rc = connectToTCPServer(getIPAddress(this->_node->getName()), _port, &datasock) ;
566
	rc = connectToTCPServer(this->_node->getName(), _port, &datasock) ;
517
567
518
	if (rc != 0)
568
	if (rc != 0)
519
	{
569
	{
(-)src-native-new/src/client/TPTP_INetAddress.cpp (-1 / +2 lines)
Lines 21-26 Link Here
21
21
22
#ifdef _WIN32
22
#ifdef _WIN32
23
23
24
/* IPV4-ONLY */
24
int TPTP_INetAddress::getAddress(char* hostname,char** inetaddr)
25
int TPTP_INetAddress::getAddress(char* hostname,char** inetaddr)
25
{
26
{
26
	hostent*  remote_hostname;
27
	hostent*  remote_hostname;
Lines 28-34 Link Here
28
	WSAData wsaData; 
29
	WSAData wsaData; 
29
	WSACleanup(); 
30
	WSACleanup(); 
30
31
31
	if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) 
32
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 
32
	{ 
33
	{ 
33
		return -1; 
34
		return -1; 
34
	} 
35
	} 

Return to bug 165409