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 196435 | Differences between
and this patch

Collapse All | Expand All

(-)src-native-new/src/shared/TPTPUtil/TPTUtil.def (+3 lines)
Lines 101-104 Link Here
101
	getExecutableName
101
	getExecutableName
102
	readStringFromBuffer
102
	readStringFromBuffer
103
	vrfusrpwd
103
	vrfusrpwd
104
	validateDirectory
105
	isAbsolutePath
106
	getTempDir	
104
	
107
	
(-)src-native-new/src/shared/TPTPUtil/TPTPUtil.cpp (+39 lines)
Lines 846-851 Link Here
846
	return statBuf.st_size;
846
	return statBuf.st_size;
847
}
847
}
848
848
849
int validateDirectory(char* dirName) {
850
	struct STAT statBuf;
851
852
	if (STAT(dirName, &statBuf) != 0) {
853
		return 0;
854
	}
855
	
856
	return (S_ISDIR(statBuf.st_mode)) ? 1 : 0;
857
}
858
859
int isAbsolutePath(const char* path) {
860
	int n;
861
	
862
	if (path == NULL) return 0;
863
	
864
	n = strlen(path);
865
	if (n == 0) return 0;
866
	
867
#ifdef _WIN32
868
	if(n >= 3 && isalpha(*path) && *(path+1)==':' && *(path+2)=='\\') return 1;
869
	return (*path == '\\') ? 1 : 0;
870
#else
871
	return (*path == '/') ? 1 : 0;
872
#endif	
873
}
874
875
char* getTempDir() {
876
	char* dir;
877
	
878
	dir = getenv("TEMP");
879
	if(dir != NULL) return dir;
880
	
881
#ifdef _WIN32
882
	return "C:\\tmp";
883
#else
884
	return "/tmp";
885
#endif
886
}
887
849
int getConfigElementName( const char* cmdStr, char** elemName)
888
int getConfigElementName( const char* cmdStr, char** elemName)
850
{
889
{
851
	int i=0;
890
	int i=0;
(-)src-native-new/src/transport/TPTPClientCompTL/TPTPClientCompTL.dsp (+8 lines)
Lines 118-123 Link Here
118
# End Source File
118
# End Source File
119
# Begin Source File
119
# Begin Source File
120
120
121
SOURCE=.\nativeFileServer.c
122
# End Source File
123
# Begin Source File
124
121
SOURCE=.\RACClientSupport.c
125
SOURCE=.\RACClientSupport.c
122
# End Source File
126
# End Source File
123
# Begin Source File
127
# Begin Source File
Lines 166-171 Link Here
166
# End Source File
170
# End Source File
167
# Begin Source File
171
# Begin Source File
168
172
173
SOURCE=.\nativeFileServer.h
174
# End Source File
175
# Begin Source File
176
169
SOURCE=.\RACClientSupport.h
177
SOURCE=.\RACClientSupport.h
170
# End Source File
178
# End Source File
171
# Begin Source File
179
# Begin Source File
(-)src-native-new/src/transport/TPTPClientCompTL/ClientCTL.h (-1 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2006 Intel Corporation.
2
 * Copyright (c) 2005, 2008 Intel Corporation.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 35-40 Link Here
35
	BOOL            isDataMultiplexed;
35
	BOOL            isDataMultiplexed;
36
	BOOL            isPolling;
36
	BOOL            isPolling;
37
	TID             pollingThread;
37
	TID             pollingThread;
38
	BOOL            isJavaUnsecuredFileServer;
38
39
39
	/* Peer monitoring related fields */
40
	/* Peer monitoring related fields */
40
	int             usePeerAttachPort;
41
	int             usePeerAttachPort;
(-)src-native-new/src/transport/TPTPClientCompTL/ClientCTL.c (-18 / +56 lines)
Lines 17-22 Link Here
17
	#include <sys/socket.h>
17
	#include <sys/socket.h>
18
#endif
18
#endif
19
19
20
#include <string.h>
20
#include "tptp/TPTPTypes.h"
21
#include "tptp/TPTPTypes.h"
21
#include "tptp/TPTPSupportTypes.h"
22
#include "tptp/TPTPSupportTypes.h"
22
#include "tptp/TransportSupport.h"
23
#include "tptp/TransportSupport.h"
Lines 39-44 Link Here
39
extern void *processPollingThread(void *args);
40
extern void *processPollingThread(void *args);
40
#endif
41
#endif
41
42
43
THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args);
44
extern int stopNativeFileServer();
45
42
/**
46
/**
43
 *********************************************************
47
 *********************************************************
44
 *
48
 *
Lines 59-64 Link Here
59
	tptp_string *isDataMultiplexed;
63
	tptp_string *isDataMultiplexed;
60
	tptp_string *processPolling;
64
	tptp_string *processPolling;
61
	tptp_string *securityEnabled;
65
	tptp_string *securityEnabled;
66
	tptp_string *fileServer;
62
67
63
	rc = baseTL_createTransportListener( cmo, pTransportData, tlo, "Client Compatibility TL" );
68
	rc = baseTL_createTransportListener( cmo, pTransportData, tlo, "Client Compatibility TL" );
64
	if ( ( rc == 0 ) && (tlo != NULL) && (tlo->data != NULL) )
69
	if ( ( rc == 0 ) && (tlo != NULL) && (tlo->data != NULL) )
Lines 191-208 Link Here
191
			cctlData->isPolling = TRUE;
196
			cctlData->isPolling = TRUE;
192
		}
197
		}
193
198
194
		
199
		rc = getConfigurationString(pTransportData->configurationData, "JavaUnsecuredFileServer", &fileServer);
200
		if (rc ==0 ) {
201
#ifdef _WIN32
202
#ifdef _WIN64
203
			if (0 == strcasecmp(processPolling, "true"))
204
#else
205
			if (0 == strcmpi(processPolling, "true"))
206
#endif
207
#else
208
			if (0 == strcasecmp(processPolling, "true"))
209
#endif
210
			{
211
				cctlData->isJavaUnsecuredFileServer = TRUE;
212
			}
213
			else {
214
				cctlData->isJavaUnsecuredFileServer = FALSE;
215
			}
216
217
			tptp_free(fileServer);
218
		}
219
		else {
220
				cctlData->isJavaUnsecuredFileServer = FALSE;
221
		}
222
195
		/* Assume NO Security for now !!! */
223
		/* Assume NO Security for now !!! */
196
		/******************************/
224
197
		/* Create JVM */
225
		/* Create JVM */
198
		rc = tptpCreateJavaVitualMachine(stateData, NULL);
226
		if (cctlData->isJavaUnsecuredFileServer || cctlData->securityEnabled) {
199
		if (rc == 0) { 
227
			if (tptpCreateJavaVitualMachine(stateData, NULL) == 0) {
200
			jvmCreated = TRUE;
228
				jvmCreated = TRUE;
201
		}	
229
			}		
202
		else
230
			else {
203
		{
231
				jvmCreated = FALSE;
204
			TPTP_LOG_WARNING_MSG( stateData, "Cannot create JVM and therefore cannot create file server" );
232
				TPTP_LOG_ERROR_MSG(stateData, "Cannot create JVM");
205
			jvmCreated = FALSE;
233
			}
234
		}
235
		else {
236
			jvmCreated = FALSE;		// skip jvm
206
		}
237
		}
207
238
208
		/* Start createFileServer */
239
		/* Start createFileServer */
Lines 242-266 Link Here
242
				}
273
				}
243
274
244
				if(isValid) {
275
				if(isValid) {
245
					/* Start secure file server... this needs to be done after vairiables are read from config */
276
					/* Start secure java file server... this needs to be done after vairiables are read from config */
246
					startSecuredFileServer(stateData, &_fileServerTID);
277
					startSecuredFileServer(stateData, &_fileServerTID);
247
					/* Start the secured file server */
278
					/* Start the secured file server */
248
					startSecuredServer(stateData, &_secsvrtid);
279
					startSecuredServer(stateData, &_secsvrtid);
249
				}
280
				}
250
				else {
281
				else {
251
					TPTP_LOG_ERROR_MSG( stateData, "Cannot start secured server. Some required parameters are missing from the configuration file" );
282
					TPTP_LOG_ERROR_MSG( stateData, "Cannot start secured server. Some required parameters are missing from the configuration file" );
252
				}
283
				}
253
			}
284
			}
254
			else {
285
			else {
255
				/* Start the insecured file server */
286
				/* Start the insecured java file server */
256
				startFileServer(stateData, &_fileServerTID);
287
				startFileServer(stateData, &_fileServerTID);
257
			}
288
			}
258
		}
289
		}
259
		else {
260
			//for linux we have already warned about not having
261
			//File transfer.
262
			TPTP_LOG_ERROR_MSG( stateData, "Cannot create JVM" );
263
		}
264
	}
290
	}
265
291
266
	return 0;
292
	return 0;
Lines 515-520 Link Here
515
	struct sockaddr_in  myServerSockAddr ;
541
	struct sockaddr_in  myServerSockAddr ;
516
	tl_state_data_t*    stateData;
542
	tl_state_data_t*    stateData;
517
	cctl_state_data_t*  cctlData;
543
	cctl_state_data_t*  cctlData;
544
	TID                 threadId;
545
	HANDLE              threadHandle;
518
546
519
	if ( !isValidTPTPBlock(tlo, TPTP_BASETL_MAKE_OBJECT_ID(tlo)) )
547
	if ( !isValidTPTPBlock(tlo, TPTP_BASETL_MAKE_OBJECT_ID(tlo)) )
520
		return TPTP_ERROR_INVALID_PARAMETER;
548
		return TPTP_ERROR_INVALID_PARAMETER;
Lines 549-554 Link Here
549
		return -1; // TODO: Use a more specific error
577
		return -1; // TODO: Use a more specific error
550
	}
578
	}
551
579
580
	if ((!cctlData->isJavaUnsecuredFileServer) && !cctlData->securityEnabled) {
581
		rc = tptpStartThread(startNativeFileServer, (LPVOID)stateData, &threadId, &threadHandle);
582
		if (rc == 0) {
583
			TPTP_LOG_INFO_MSG1(stateData, "Native file server started listening on port %d", cctlData->filePort);
584
		}
585
		else {
586
			TPTP_LOG_ERROR_MSG(stateData, "Native file server could not start");
587
		}
588
	}			
589
552
	return baseTL_startTransportListener( tlo, serveRequests );
590
	return baseTL_startTransportListener( tlo, serveRequests );
553
}
591
}
554
592
(-)src-native-new/include/tptp/TPTPUtils.h (-1 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 Intel Corporation.
2
 * Copyright (c) 2005, 2008 Intel Corporation.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 236-241 Link Here
236
int tptp_postSemaphore(Semaphore_t *pSem);
236
int tptp_postSemaphore(Semaphore_t *pSem);
237
237
238
int getFileSize(char *fileName);
238
int getFileSize(char *fileName);
239
int validateDirectory(char* dirName);
240
int isAbsolutePath(const char* path);
241
char* getTempDir();
239
242
240
/* String utilities */
243
/* String utilities */
241
void tptp_strcopy(tptp_string* destination, const tptp_string* source);
244
void tptp_strcopy(tptp_string* destination, const tptp_string* source);
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.h (+101 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Intel Corporation.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    Intel - Initial API and Implementation
10
 *
11
 * $Id: nativeFileServer.h,v 1.0 2008/02/04 22:47:17 ialelekov Exp $
12
 *
13
 *******************************************************************************/ 
14
15
#ifndef _NATIVE_FILE_SERVER
16
#define _NATIVE_FILE_SERVER
17
18
#include <string.h>
19
20
#ifdef _WIN32
21
	#include <direct.h>
22
#else
23
	#include <sys/stat.h>
24
	#include <sys/types.h>
25
#endif
26
27
#include "tptp/TPTPTypes.h"
28
#include "tptp/TPTPSupportTypes.h"
29
#include "tptp/TransportSupport.h"
30
#include "tptp/TPTPBaseTL.h" 
31
#include "tptp/compatibility/RAComm.h" 
32
#include "ClientCTL.h"
33
34
#ifdef _WIN32
35
	#define  pathSeparatorCharacter '\\'
36
	#define  pathSeparator "\\"
37
#else
38
	#define  pathSeparatorCharacter '/'
39
	#define  pathSeparator "/"
40
#endif			
41
42
typedef struct {
43
	SOCKET      socket;
44
	char*		buffer;
45
	int 		bufferLength;
46
	tl_state_data_t* stateData;
47
} fs_connection_block_t;
48
49
#define DETERMINE_SERVER_REACH_COMMAND 1
50
#define DETERMINE_SERVER_REACH_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.DetermineServerReachCommand"
51
52
#define GET_FILE_COMMAND 2
53
#define GET_FILE_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.GetFileCommand"
54
55
#define PUT_FILE_COMMAND 3
56
#define PUT_FILE_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.PutFileCommand"
57
58
#define DELETE_FILE_COMMAND 4
59
#define DELETE_FILE_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.DeleteFileCommand"
60
61
#define DELETE_DIR_COMMAND 5
62
#define DELETE_DIR_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.DeleteDirectoryCommand"
63
64
#define VALIDATE_DIR_COMMAND 6
65
#define VALIDATE_DIR_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.ValidateDirectoryExistenceCommand"
66
67
#define QUERY_SERVER_STATUS_COMMAND 7
68
#define QUERY_SERVER_STATUS_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.QueryServerStatusCommand"
69
70
#define MDFY_PERMISSION_COMMAND 8
71
#define MDFY_PERMISSION_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.ModifyPermissionCommand"
72
73
#define LIST_CONTENT_COMMAND 9
74
#define LIST_CONTENT_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.ListContentCommandCommand"
75
76
#define OPEN_CONTROL_DIALOG "org.eclipse.hyades.execution.core.loader.ScopedChannelClassLoader$AbstractControlDirective$BeginControlDirective"
77
#define CLOSE_CONTROL_DIALOG "org.eclipse.hyades.execution.core.loader.ScopedChannelClassLoader$AbstractControlDirective$EndControlDirective"
78
79
#define FS_TRUE		127		// java.lang.Boolean.MAX_VALUE
80
#define	FS_FALSE	-128	// java.lang.Boolean.MIN_VALUE
81
82
extern int readInt (SOCKET socket, int* value);
83
extern char* readString(SOCKET socket);
84
85
extern int writeByte(SOCKET socket, int byte);
86
extern int writeBoolean(SOCKET socket, int d);
87
extern int writeInt (SOCKET socket, int v);
88
extern int writeString(SOCKET socket, char* buffer);
89
extern int writeStrings(SOCKET socket, char** array, int len);
90
91
extern char* readStringIntoBuffer(fs_connection_block_t* con);
92
extern int readFile(fs_connection_block_t* con, char* fileName);
93
94
extern int writeFile(fs_connection_block_t* con, char* fileName);
95
96
extern int openControlDialog(fs_connection_block_t* con);
97
extern int closeControlDialog(fs_connection_block_t* con);
98
99
extern THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args);
100
101
#endif
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.c (+556 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Intel Corporation.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    Intel - Initial API and Implementation
10
 *
11
 * $Id: nativeFileServer.c,v 1.0 2008/02/04 22:47:17 ialelekov Exp $
12
 *
13
 *******************************************************************************/ 
14
15
#include "nativeFileServer.h"
16
17
int readInt (SOCKET socket, int* value) {
18
	int bytesRead, bytesToRead, i, d, r;
19
	unsigned char* p;
20
	char buf[5];	// readFromSocket() appends terminating 0
21
22
	bytesToRead = 4;
23
	while(bytesToRead > 0) {
24
		if (readFromSocket(socket, buf+4-bytesToRead, bytesToRead, &bytesRead) <= 0) {
25
			return -1;
26
		}
27
		
28
		bytesToRead -= bytesRead;
29
	}
30
	
31
	for (i=0, p=(unsigned char*)buf, r=0; i<4; i++, p++) {
32
		d = *p;
33
		r = (r << 8) + d;
34
	}
35
	
36
	*value = r;
37
38
	return 0; 
39
}
40
41
char* readString(SOCKET socket) {
42
	int bytesRead;
43
	int stringLength, stringReadBytes;
44
	char* str;
45
46
	if (readInt(socket, &stringLength) < 0) {
47
		return NULL;
48
	}
49
	
50
	if (stringLength < 0) stringLength *= -1;	// negative means hyades dynamic command loading
51
	
52
	str = (char*) tptp_malloc(stringLength+1);
53
	if (str == NULL) return NULL;
54
	
55
	stringReadBytes = 0;
56
	
57
	while(stringReadBytes < stringLength) {
58
		if (readFromSocket(socket, str+stringReadBytes, stringLength-stringReadBytes, &bytesRead) <= 0) {
59
			tptp_free(str);
60
			return NULL;
61
		}
62
		
63
		stringReadBytes += bytesRead;
64
	}
65
	
66
	*(str + stringLength) = '\0';
67
	
68
	return str;
69
}
70
71
char* readStringIntoBuffer(fs_connection_block_t* con) {
72
	int bytesRead;
73
	int stringLength, stringReadBytes;
74
75
	if (readInt(con->socket, &stringLength) < 0) return NULL;
76
77
	if (stringLength < 0) stringLength *= -1;	// negative means hyades dynamic command loading
78
79
	if (stringLength > (con->bufferLength-1)) return NULL;		// including terminting 0
80
	
81
	stringReadBytes = 0;
82
	
83
	while(stringReadBytes < stringLength) {
84
		if (readFromSocket(con->socket, con->buffer+stringReadBytes, stringLength-stringReadBytes, &bytesRead) < 0) {
85
			return NULL;
86
		}
87
		
88
		stringReadBytes += bytesRead;
89
	}
90
	
91
	*(con->buffer + stringLength) = '\0';
92
	
93
	return con->buffer;
94
}
95
96
int writeByte(SOCKET socket, int byte) {
97
	char c = (char) (byte & 0xff);
98
	
99
	return writeToSocket(socket, &c, 1);
100
}
101
102
int writeBoolean(SOCKET socket, int d) {
103
	d = (d != 0) ? FS_TRUE : FS_FALSE;
104
	return writeByte(socket, d);
105
}
106
107
int writeInt (SOCKET socket, int v) {
108
	writeByte (socket, (v >> 24));
109
	writeByte (socket, (v >> 16));
110
	writeByte (socket, (v >> 8));
111
	return writeByte(socket, v);
112
}
113
114
int writeString(SOCKET socket, char* buffer) {
115
	int len = strlen(buffer);
116
	writeInt(socket, len);
117
	return writeToSocket(socket, buffer, len); 
118
}
119
120
int writeStrings(SOCKET socket, char** array, int len) {
121
	int i;
122
	
123
	writeInt(socket, len);
124
	for (i=0; i<len; i++) {
125
		writeString(socket, array[i]);
126
	}
127
	
128
	return 0; 
129
}
130
131
int mkDirs(char* path) {
132
	char *p;
133
	
134
	p = path;
135
	while(1) {
136
		p = strchr(p, pathSeparatorCharacter);
137
		if (p == NULL) break;
138
		
139
		*p = '\0';
140
		
141
#ifdef _WIN32
142
		_mkdir(path);
143
#else
144
		mkdir(path, 0777);
145
#endif
146
		
147
		*p++ = pathSeparatorCharacter;
148
	}
149
	
150
	return 0;
151
}
152
153
int readFile(fs_connection_block_t* con, char* fileName) {
154
	int contentLen;
155
	FILE* fp;
156
	int num;
157
	
158
	mkDirs(fileName);	
159
	
160
	fp = fopen(fileName, "wb");
161
	if (fp == NULL) {
162
		TPTP_LOG_ERROR_MSG1(con->stateData, "NFS: write file open error: %s", fileName);
163
		return -1;
164
	}
165
166
	readInt(con->socket, &contentLen);	// high 4 bytes
167
	readInt(con->socket, &contentLen);	
168
169
	while (contentLen > 0) {
170
		int bytesToRead = contentLen;
171
		if (bytesToRead > con->bufferLength) bytesToRead = con->bufferLength;
172
		
173
		if (readFromSocket(con->socket, con->buffer, bytesToRead, &num) < 0) break;
174
		if (num <= 0) break;
175
		
176
		fwrite(con->buffer, num, 1, fp);
177
		contentLen -= num;
178
	}
179
180
	fclose(fp);
181
182
	writeInt(con->socket, 0);	// high 4 bytes
183
	writeInt(con->socket, getFileSize(fileName));	
184
185
	return 0; 
186
}
187
188
int writeFile(fs_connection_block_t* con, char* fileName) {
189
	int contentLen;
190
	int gotLen;
191
	FILE* fp;
192
	int n;
193
	
194
	contentLen = getFileSize(fileName);
195
	if (contentLen < 0) return -1;
196
197
	fp = fopen(fileName, "rb");
198
	if (fp == NULL) {
199
		TPTP_LOG_ERROR_MSG1(con->stateData, "NFS: read file open error: %s", fileName);
200
		return -1;
201
	}
202
203
	writeInt(con->socket, 0);			// high 4 bytes of contentLength (64-bit)
204
	writeInt(con->socket, contentLen);
205
206
	while ((n = fread((void *) con->buffer, 1, con->bufferLength, fp)) > 0) {
207
		writeToSocket(con->socket, con->buffer, n); 
208
	}		
209
210
	fclose(fp);
211
				
212
	readInt(con->socket, &gotLen);	// high 4 bytes
213
	readInt(con->socket, &gotLen);
214
	
215
	return 0; 
216
}
217
218
int openControlDialog(fs_connection_block_t* con) {
219
	return writeString(con->socket, OPEN_CONTROL_DIALOG);
220
}
221
222
int closeControlDialog(fs_connection_block_t* con) {
223
	return writeString(con->socket, CLOSE_CONTROL_DIALOG);
224
}
225
226
int readFSCommand(fs_connection_block_t* con) {
227
	char* str;
228
229
	str = readStringIntoBuffer(con);
230
	if (str == NULL) {
231
		return -1;
232
	}
233
 
234
	if (!strcmp(str, DETERMINE_SERVER_REACH_COMMAND_STR)) return DETERMINE_SERVER_REACH_COMMAND; 
235
	if (!strcmp(str, GET_FILE_COMMAND_STR)) return GET_FILE_COMMAND;
236
	if (!strcmp(str, PUT_FILE_COMMAND_STR)) return PUT_FILE_COMMAND;
237
	if (!strcmp(str, DELETE_FILE_COMMAND_STR)) return DELETE_FILE_COMMAND;
238
	if (!strcmp(str, DELETE_DIR_COMMAND_STR)) return DELETE_DIR_COMMAND;
239
	if (!strcmp(str, VALIDATE_DIR_COMMAND_STR)) return VALIDATE_DIR_COMMAND;
240
	if (!strcmp(str, QUERY_SERVER_STATUS_COMMAND_STR)) return QUERY_SERVER_STATUS_COMMAND;
241
	if (!strcmp(str, MDFY_PERMISSION_COMMAND_STR)) return MDFY_PERMISSION_COMMAND;
242
	if (!strcmp(str, LIST_CONTENT_COMMAND_STR)) return LIST_CONTENT_COMMAND;
243
244
	TPTP_LOG_ERROR_MSG1(con->stateData, "NFS: unknown message: %s", str);
245
246
	return -1;		
247
}
248
249
int processServerReachCmd(fs_connection_block_t* con) {
250
	char* host;
251
	int port;
252
	int d, rc;
253
	SOCKET testSocket;
254
	unsigned long ipAddr;
255
	
256
	host = readStringIntoBuffer(con);
257
	if (host == NULL) return -1;
258
	
259
	readInt (con->socket, &port); 
260
	
261
	d = 0;
262
	rc = convertIPAddressStringToUlong(host, &ipAddr);	// reverse call
263
	if (rc == 0) {
264
		if (!connectToTCPServer(ipAddr, (unsigned short) port, &testSocket)) {
265
			d = 1;
266
			closeSocket(testSocket);
267
		}
268
	}
269
	
270
	writeByte(con->socket, d);	// "server is reachable" responce
271
	
272
	return 0;
273
}
274
275
int getFiles(fs_connection_block_t* con, char** names, int num) {
276
	int i;
277
	char* name;
278
279
	for(i=0; i<num; i++) {
280
		name = readString(con->socket);
281
		if (name == NULL) {
282
			return -1;		
283
		}
284
		
285
		names[i] = name;	
286
	}
287
288
	for (i=0; i<num; i++) {
289
		if (writeFile(con, names[i])) {
290
			return -1;
291
		}
292
	}
293
	
294
	return 0;	
295
}
296
297
void checkFileSeparators(char* path) {
298
	char  ips;
299
	char* p;
300
	
301
#ifdef _WIN32
302
	ips = '/';
303
#else
304
	ips = '\\';
305
#endif			
306
	
307
	p = path;
308
	while (1) {
309
		p = strchr(p, ips);
310
		if (p == NULL) break;
311
		
312
		*p = pathSeparatorCharacter;
313
	}
314
}
315
316
char* getAbsolutePath(char* path) {
317
	char *absPath, *tmpDir;
318
	int tmpDirLen;
319
	
320
	if (isAbsolutePath(path)) return NULL;
321
	
322
	tmpDir = getTempDir();
323
	tmpDirLen = strlen(tmpDir);
324
	
325
	absPath = (char*) tptp_malloc(strlen(tmpDir) + strlen(path) + 2);	// including trailing null and file separator
326
	strcpy(absPath, tmpDir);
327
	if (*(absPath+tmpDirLen-1) != pathSeparatorCharacter) strcat(absPath, pathSeparator);
328
	strcat(absPath, path);
329
330
	return absPath;
331
}
332
333
int putFiles(fs_connection_block_t* con, char** names, int num) {
334
	int i;
335
	char *name, *absName;
336
	
337
	for(i=0; i<num; i++) {
338
		name = readStringIntoBuffer(con);
339
		if (name == NULL) {
340
			return -1;		
341
		}
342
		
343
		checkFileSeparators(name);	
344
	
345
		absName = getAbsolutePath(name);
346
		if (absName != NULL) { 
347
			names[i] = absName;
348
		}
349
		else {
350
			names[i] = (char*) tptp_malloc(strlen(name)+1);
351
			strcpy(names[i], name);
352
		}	
353
	}
354
355
	for (i=0; i<num; i++) {
356
		if (readFile(con, names[i])) {
357
			return -1;
358
		}
359
	}	
360
361
	writeStrings(con->socket, names, num);	// files resolution
362
	
363
	return 0;
364
}
365
366
int processDeleteFileCmd(fs_connection_block_t* con) {
367
	int i, numFiles, num;
368
	char* name;
369
370
	readInt(con->socket, &numFiles);
371
	readInt(con->socket, &num);
372
	if (num <= 0) return -1;
373
	
374
	for (i=0; i<num; i++) {
375
		name = readStringIntoBuffer(con);
376
		if (name == NULL) return -1;
377
		
378
		remove(name);
379
	}
380
381
	return 0;
382
}
383
384
int validateDir(fs_connection_block_t* con, char** names, int num) {
385
	int i;
386
	char* name;
387
	
388
	for(i=0; i<num; i++) {
389
		name = readString(con->socket);
390
		if (name == NULL) {
391
			return -1;		
392
		}
393
		
394
		names[i] = name;	
395
	}
396
397
	writeInt(con->socket, num);			// reponse array size
398
	for (i=0; i<num; i++) {
399
		writeBoolean(con->socket, validateDirectory(names[i]));
400
	}
401
	
402
	return 0;	
403
}
404
405
int processQueryServerStatusCmd(fs_connection_block_t* con) {
406
	char* message;
407
	
408
	message = readStringIntoBuffer(con);
409
	if (message == NULL) return -1;
410
	
411
	writeString(con->socket, message);
412
	
413
	return 0;
414
}
415
416
int processDummyCmd(fs_connection_block_t* con) {
417
	return 0;
418
}
419
420
int processFileArrayCmd(fs_connection_block_t* con, int cmd) {
421
	int i, numFiles, num;
422
	char** names=NULL;
423
	int res=-1;
424
	
425
	readInt(con->socket, &numFiles);
426
	readInt(con->socket, &num);
427
	if (num <= 0) return -1;
428
	
429
	names = (char**) malloc(num*sizeof(char*));
430
	memset(names, 0, num*sizeof(char*));
431
432
	switch(cmd) {
433
	case GET_FILE_COMMAND:
434
		res = getFiles(con, names, num);
435
		break;
436
		 
437
	case PUT_FILE_COMMAND:
438
		res = putFiles(con, names, num);
439
		break;
440
441
	case VALIDATE_DIR_COMMAND:
442
		res = validateDir(con, names, num);
443
		break;
444
	}
445
446
	for (i=0; i<num; i++) {
447
		if (names[i] != NULL) tptp_free(names[i]);
448
	}
449
	
450
	tptp_free(names);
451
	
452
	return res;
453
}
454
455
int processFSCommand (fs_connection_block_t* con, int cmd) {
456
	openControlDialog(con);
457
	closeControlDialog(con);
458
459
	switch(cmd) {
460
	case DETERMINE_SERVER_REACH_COMMAND:
461
		return processServerReachCmd(con);
462
463
	case GET_FILE_COMMAND:
464
	case PUT_FILE_COMMAND:
465
	case VALIDATE_DIR_COMMAND:
466
		return processFileArrayCmd(con, cmd);
467
468
	case DELETE_FILE_COMMAND:
469
	case DELETE_DIR_COMMAND:
470
		return processDeleteFileCmd(con);
471
472
	case QUERY_SERVER_STATUS_COMMAND:
473
		return processQueryServerStatusCmd(con);
474
475
	case MDFY_PERMISSION_COMMAND:
476
	case LIST_CONTENT_COMMAND:
477
		return processDummyCmd(con);
478
	}
479
480
	
481
	return -1;
482
}
483
484
THREAD_USER_FUNC_RET_TYPE processFSRequest(LPVOID args){
485
	SOCKET socket;
486
	fs_connection_block_t* con;
487
	int cmd;
488
489
	con = (fs_connection_block_t*)args;
490
	socket = con->socket;
491
	
492
	while ((cmd = readFSCommand(con)) > 0) {
493
		if (processFSCommand(con, cmd)) break;
494
	}
495
496
	tptp_free(con->buffer);
497
	tptp_free(con);
498
	closeSocket(socket);
499
	
500
	return 0;
501
}
502
503
static SOCKET serverSocket;
504
505
THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args){
506
	int rc, port;
507
	struct sockaddr_in  fsSockAddr;
508
	SOCKET clientSock;
509
	fs_connection_block_t* con;
510
	tl_state_data_t*    stateData;
511
	TID                 threadId;
512
	HANDLE              threadHandle;
513
514
	stateData = (tl_state_data_t*) args;
515
	port = ((cctl_state_data_t*)stateData->implData)->filePort;
516
	if (port <= 0) port = RA_FILE_PORT_NUM_SERVER;
517
		
518
	serverSocket = getTheSocket(port, &fsSockAddr);
519
	if (serverSocket < 0) {
520
		TPTP_LOG_ERROR_MSG(stateData, "NFS: unable to create the file server socket.");
521
		return 0;
522
	}
523
524
	rc = bindAndListen(serverSocket, (struct sockaddr*)&fsSockAddr);
525
	if (rc != 0) {
526
		TPTP_LOG_ERROR_MSG(stateData, "NFS: cannot bind to server socket");
527
		return 0;
528
	}
529
530
	while(1) { 
531
		clientSock = acceptSocketConnection(serverSocket); 
532
	    if (clientSock < 0) break;
533
534
		con = (fs_connection_block_t*) tptp_malloc(sizeof(fs_connection_block_t));
535
		if (con == NULL) {
536
			closeSocket(clientSock);
537
			continue;
538
		}
539
			
540
		con->socket = clientSock;        
541
		con->buffer = (char*) tptp_malloc(TPTP_DEFAULT_BUFFER_LENGTH+1); // +1 since readFromSocket() adds terminating 0
542
		con->bufferLength = TPTP_DEFAULT_BUFFER_LENGTH;
543
		con->stateData = stateData;
544
	        
545
	    tptpStartThread(processFSRequest, (LPVOID)con, &threadId, &threadHandle) ;
546
	}
547
	
548
	closeSocket(serverSocket);
549
	
550
	return 0;
551
} 
552
553
int stopNativeFileServer() {
554
	closeSocket(serverSocket);
555
	return 0;
556
}

Return to bug 196435