| 1 |
/* File: dceclientlib.c |
| 2 |
* Author: Richard Bruskiewich ([email protected]) |
| 3 |
* Copyright (C) J Thierry-Mieg and R Durbin, 1992-1997 |
| 4 |
*------------------------------------------------------------------- |
| 5 |
* This file is part of the ACEDB genome database package, written by |
| 6 |
* Richard Durbin (MRC LMB, UK) [email protected], and |
| 7 |
* Jean Thierry-Mieg (CRBM du CNRS, France) [email protected] |
| 8 |
* |
| 9 |
* Description: |
| 10 |
* Distributed Computing Environment (DCE) RPC - client side |
| 11 |
* Adapted from aceclientlib.c, version 1.9 9/5/96 |
| 12 |
* |
| 13 |
* Exported functions: |
| 14 |
openServer() |
| 15 |
closeServer() |
| 16 |
askServer() |
| 17 |
askServerBinary() |
| 18 |
* HISTORY: |
| 19 |
* Last edited: Dec 31 01:15 1997 (rbrusk): ace4.5e, continuing development |
| 20 |
* * Aug 8 00:37 1997 (rbrusk): implemented full protocol specs |
| 21 |
* * Jul 22 20:17 1997 (rbrusk) |
| 22 |
* * Mar 16 14:20 1997 (rbrusk): rewrote ace_server() interface (again) |
| 23 |
* * Feb 13 01:20 1997 (rbrusk): rewrote ace_server() interface |
| 24 |
* * Jan 9 13:35 1997 (rbrusk) |
| 25 |
* Created: Jan 9 13:35 1997 (rbrusk) |
| 26 |
*------------------------------------------------------------------- |
| 27 |
*/ |
| 28 |
|
| 29 |
/* %W% %G% */ |
| 30 |
|
| 31 |
#include "regular.h" |
| 32 |
#include <errno.h> |
| 33 |
#include "rpcace.h" |
| 34 |
|
| 35 |
/* The semantics of the "clnt" symbol in |
| 36 |
aceclient.h is really "svr" == pointer to SERVER */ |
| 37 |
#define clnt svr |
| 38 |
#include "aceclient.h" |
| 39 |
#include "WinAceError.h" |
| 40 |
#include "client2service.h" |
| 41 |
|
| 42 |
/* A global client_debug flag */ |
| 43 |
ACLNT_VAR_DEF BOOL client_debug = FALSE ; |
| 44 |
|
| 45 |
/************************************************************* |
| 46 |
* A Client Authentication routine? |
| 47 |
*************************************************************/ |
| 48 |
static int getMagic (int magic1, char *nm) |
| 49 |
{ int magic = 0, magic2 = 0, magic3 = 0 ; |
| 50 |
FILE *f ; |
| 51 |
int level ; |
| 52 |
char *cp ; |
| 53 |
|
| 54 |
if (magic1 < 0) magic1 = -magic1 ; /* old system */ |
| 55 |
if (!nm || !*nm) return 0 ; |
| 56 |
freeinit() ; |
| 57 |
level = freesettext(nm,0) ; |
| 58 |
if (!freecard(level)) |
| 59 |
goto fin ; |
| 60 |
|
| 61 |
cp = freeword () ; |
| 62 |
if (!cp) goto fin ; |
| 63 |
if (client_debug)fprintf(stderr,"Write: %s ", cp) ; |
| 64 |
if (strcmp(cp, "NON_WRITABLE")) |
| 65 |
{ |
| 66 |
f = fopen (cp, "r") ; |
| 67 |
if (!f) |
| 68 |
goto tryread ; |
| 69 |
if (fscanf(f, "%d", &magic3) != 1) |
| 70 |
magic3 = 0 ; |
| 71 |
fclose(f) ; |
| 72 |
} |
| 73 |
|
| 74 |
tryread: |
| 75 |
cp = freeword () ; |
| 76 |
if (magic3 || !cp) goto assumePublic ; |
| 77 |
if (client_debug)fprintf(stderr,"Read: %s ", cp) ; |
| 78 |
if (strcmp(cp, "PUBLIC") && strcmp(cp,"RESTRICTED")) |
| 79 |
{ |
| 80 |
|
| 81 |
f = fopen (cp, "r") ; |
| 82 |
if (!f) |
| 83 |
{ messout ("// Access to this database is restricted sorry\n") ; |
| 84 |
goto fin ; |
| 85 |
} |
| 86 |
if (fscanf(f, "%d", &magic2) != 1) |
| 87 |
messout ("// Access to this database is restricted sorry\n") ; |
| 88 |
fclose(f) ; |
| 89 |
} |
| 90 |
else if (strcmp(cp, "PUBLIC") && strcmp(cp,"RESTRICTED")) |
| 91 |
assumePublic: /* compatibility with older servers */ |
| 92 |
magic = magic1 ; |
| 93 |
if (magic2) |
| 94 |
magic = magic1 * magic2 % 73256171 ; |
| 95 |
if (magic3) |
| 96 |
magic = magic1 * magic3 % 43532334 ; |
| 97 |
|
| 98 |
fin: |
| 99 |
freeclose(level) ; |
| 100 |
if (client_debug)fprintf(stderr,"magic1=%d, magic2=%d, magic3=%d, magic=%d\n", |
| 101 |
magic1, magic2, magic3, magic) ; |
| 102 |
return magic ; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* This function returns a DCE/RPC server binding handle (in lpSvr object) |
| 107 |
*/ |
| 108 |
static LP_SERVER bindServer( unsigned char *szServerStringBinding, |
| 109 |
RPC_STATUS *status) |
| 110 |
{ |
| 111 |
RPC_BINDING_HANDLE hServerBinding ; |
| 112 |
LP_SERVER lpSvr = NULL ; |
| 113 |
|
| 114 |
if( szServerStringBinding == NULL || status == NULL ) { |
| 115 |
|
| 116 |
#if defined(WIN32) && defined(_DEBUG) |
| 117 |
AceASSERT(0) ; /* I want to trap this during debugging?! */ |
| 118 |
#endif |
| 119 |
*status = RPC_S_INVALID_ARG ; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
*status = RpcBindingFromStringBinding( szServerStringBinding, &hServerBinding ) ; |
| 124 |
|
| 125 |
if (*status == RPC_S_OK ) |
| 126 |
{ /* Assume valid hServerBinding found, so assign bindings? */ |
| 127 |
int bSize = strlen(szServerStringBinding) ; |
| 128 |
lpSvr = (LP_SERVER)messalloc( (mysize_t)sizeof(SERVER) ) ; |
| 129 |
lpSvr->lpszServerStringBinding = messalloc( (mysize_t)(bSize + 1) ) ; |
| 130 |
strcpy(lpSvr->lpszServerStringBinding,szServerStringBinding) ; |
| 131 |
lpSvr->hServerBinding = hServerBinding ; |
| 132 |
} |
| 133 |
} |
| 134 |
return lpSvr ; /* may be NULL */ |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* This function frees DCE server binding resources |
| 139 |
* If the function fails, the contents of lpSvr may be left indeterminate? |
| 140 |
*/ |
| 141 |
static RPC_STATUS freeServer( LP_SERVER lpSvr ) |
| 142 |
{ |
| 143 |
RPC_STATUS status ; |
| 144 |
|
| 145 |
if( lpSvr == NULL ) return RPC_S_INVALID_ARG ; |
| 146 |
|
| 147 |
if( !(lpSvr->lpszServerStringBinding == NULL || |
| 148 |
lpSvr->hServerBinding == NULL ) ) |
| 149 |
{ |
| 150 |
if( (status = RpcBindingFree( &(lpSvr->hServerBinding) )) |
| 151 |
!= RPC_S_OK ) return status ; |
| 152 |
} |
| 153 |
else |
| 154 |
return RPC_X_SS_CONTEXT_MISMATCH ; |
| 155 |
|
| 156 |
/*Otherwise, OK to attempt SERVER deletion */ |
| 157 |
if( messfree( lpSvr->lpszServerStringBinding ) && |
| 158 |
messfree( lpSvr ) ) |
| 159 |
return RPC_S_OK ; |
| 160 |
else |
| 161 |
return RPC_S_ACCESS_DENIED ; |
| 162 |
} |
| 163 |
|
| 164 |
RPC_STATUS freeReponse( unsigned char **reponse ) |
| 165 |
{ |
| 166 |
RPC_STATUS rpc_status = RPC_S_OK ; |
| 167 |
rpc_status = RpcSmFree( *reponse ) ; |
| 168 |
*reponse = NULL ; |
| 169 |
return rpc_status ; |
| 170 |
} |
| 171 |
|
| 172 |
RPC_STATUS copyReponse( unsigned char **pAnswer, long *pAnswerLength, unsigned char **pReponse ) |
| 173 |
{ |
| 174 |
RPC_STATUS rpc_status = RPC_S_OK ; |
| 175 |
|
| 176 |
if( *pReponse != NULL && **pReponse ) |
| 177 |
{ |
| 178 |
int reponseSize = strlen(*pReponse) ; |
| 179 |
#if !defined(WIN32) |
| 180 |
*pAnswer = malloc( (mysize_t)(reponseSize + 1) ) ; |
| 181 |
#else /* defined(WIN32) */ |
| 182 |
/* Is it OK to use messalloc here |
| 183 |
so the calling routine can use messfree() |
| 184 |
which operates upon the heap of the DLL |
| 185 |
not the application? May crash (never NULL) */ |
| 186 |
*pAnswer = messalloc( (mysize_t)(reponseSize + 1) ) ; |
| 187 |
#endif /* defined(WIN32) */ |
| 188 |
if( *pAnswer != NULL ) |
| 189 |
{ |
| 190 |
_mbscpy( *pAnswer, *pReponse ) ; |
| 191 |
*pAnswerLength = reponseSize ; |
| 192 |
rpc_status = freeReponse( pReponse ) ; |
| 193 |
} |
| 194 |
else /* Note: never executed if pAnswer is messalloc()'d */ |
| 195 |
{ |
| 196 |
rpc_status = RPC_S_OUT_OF_MEMORY ; |
| 197 |
*pAnswer = NULL; |
| 198 |
*pAnswerLength = 0; |
| 199 |
} |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
*pAnswer = NULL; |
| 204 |
*pAnswerLength = 0; |
| 205 |
} |
| 206 |
return rpc_status ; |
| 207 |
} |
| 208 |
|
| 209 |
unsigned char *defaultProtocol() |
| 210 |
{ |
| 211 |
static unsigned char pStr[] = "ncacn_ip_tcp" ; |
| 212 |
return pStr ; |
| 213 |
} |
| 214 |
|
| 215 |
unsigned char *defaultEndpoint(unsigned char *service, unsigned char *protocol) |
| 216 |
{ |
| 217 |
static unsigned char epStr[256] ; |
| 218 |
protocol = checkProtocol(protocol) ; |
| 219 |
|
| 220 |
if ( !strcmp("ncacn_np", protocol) ) |
| 221 |
{ |
| 222 |
if( !service || !*service) |
| 223 |
strcpy(epStr, "\\pipe\\aceserver") ; |
| 224 |
else |
| 225 |
{ |
| 226 |
strcpy(epStr, "\\pipe\\") ; |
| 227 |
strncat(epStr, service, 255) ; |
| 228 |
} |
| 229 |
} |
| 230 |
else if ( !strcmp("ncacn_nb_nb", protocol) ) |
| 231 |
strcpy(epStr, "32") ; |
| 232 |
else |
| 233 |
strcpy(epStr, "11000") ; /* a TCP/IP or other port #? */ |
| 234 |
|
| 235 |
epStr[255] = '\0' ; |
| 236 |
strlwr(epStr) ; |
| 237 |
|
| 238 |
return epStr ; |
| 239 |
} |
| 240 |
|
| 241 |
/* checkProtocol permits the user to use generic terms such as |
| 242 |
PIPE and TCP/IP for protocols on the commandline, |
| 243 |
rather than "ncacn_*" notation, which is more esoteric? |
| 244 |
The user CAN use that notation, however, but this function |
| 245 |
does not otherwise (currently) validate the protocol strings |
| 246 |
*/ |
| 247 |
unsigned char *checkProtocol(unsigned char *protocol) |
| 248 |
{ |
| 249 |
static unsigned char pStr[32] ; |
| 250 |
|
| 251 |
if(!protocol || !*protocol) |
| 252 |
return defaultProtocol() ; |
| 253 |
|
| 254 |
if( !stricmp("pipe", protocol) ) /* the default anyhow...*/ |
| 255 |
strcpy((char *)pStr, "ncacn_np" ) ; |
| 256 |
else if ( !stricmp("tcp/ip", protocol) || |
| 257 |
!stricmp("tcpip", protocol) || |
| 258 |
!stricmp("tcp_ip", protocol)) |
| 259 |
strcpy((char *)pStr, "ncacn_ip_tcp" ) ; |
| 260 |
else if ( !stricmp("netbeui", protocol)) |
| 261 |
strcpy((char *)pStr, "ncacn_nb_nb" ) ; |
| 262 |
else |
| 263 |
/* otherwise, should be "ncacn_*" notation; WARNING: Not validated! */ |
| 264 |
strncpy((char *)pStr, protocol, 31) ; |
| 265 |
|
| 266 |
pStr[31] = '\0' ; |
| 267 |
strlwr(pStr) ; |
| 268 |
|
| 269 |
return pStr ; |
| 270 |
} |
| 271 |
|
| 272 |
unsigned char *checkEndpoint(unsigned char *service, |
| 273 |
unsigned char *protocol, |
| 274 |
unsigned char *endpoint ) |
| 275 |
{ |
| 276 |
static unsigned char epStr[256] ; |
| 277 |
|
| 278 |
if(!endpoint || !*endpoint) |
| 279 |
return defaultEndpoint(service, protocol) ; |
| 280 |
|
| 281 |
/* not really reentrant, but guaranteed to terminate? */ |
| 282 |
if(!protocol || !*protocol) |
| 283 |
return checkEndpoint(service, defaultProtocol(), endpoint) ; |
| 284 |
|
| 285 |
/* This implementation doesn't do any further checking? */ |
| 286 |
strncpy(epStr, endpoint, 255) ; |
| 287 |
epStr[255] = '\0' ; |
| 288 |
strlwr(epStr) ; |
| 289 |
|
| 290 |
return epStr ; |
| 291 |
} |
| 292 |
|
| 293 |
/************************************************************* |
| 294 |
Open RPC connection to server |
| 295 |
|
| 296 |
INPUT (Note: The defaults should eventually be user configurable?) |
| 297 |
|
| 298 |
unsigned char *service server service name |
| 299 |
Default: AceServer |
| 300 |
|
| 301 |
unsigned char *host hostname running server |
| 302 |
Default: local host |
| 303 |
|
| 304 |
unsigned char *protocol communication protocol code |
| 305 |
pipe or "ncacn_np" == connection oriented named pipe |
| 306 |
tcp/ip or "ncacn_ip_tcp" == connection oriented TCP/IP |
| 307 |
Default: "pipe" |
| 308 |
|
| 309 |
unsigned char *endpoint protocol dependent: port number (e.g. for TCP/IP) |
| 310 |
or name ("\\pipe\\pipename" for pipes) |
| 311 |
Default: AceServer |
| 312 |
|
| 313 |
int timeOut nn_seconds maximum period to wait for service startup and answer |
| 314 |
|
| 315 |
OUTPUT |
| 316 |
return value: |
| 317 |
ACE_HANDLE * pointer to structure containing open connection |
| 318 |
and client identification information |
| 319 |
*/ |
| 320 |
|
| 321 |
#define serverHandle (lpSvr->hServerBinding) |
| 322 |
|
| 323 |
/* Server Name extra characters for length of null terminated |
| 324 |
server name string of format "\\<host>:<rpc_port>\AceServer" */ |
| 325 |
#define SN_LEN 14 |
| 326 |
|
| 327 |
static DWORD serverTimeOut = 0 ; // in seconds |
| 328 |
#define MAXTRIES 3 |
| 329 |
|
| 330 |
static void ACE_SERVER( handle_t IDL_handle, |
| 331 |
LP_ACE_DATA ace_data, |
| 332 |
error_status_t __RPC_FAR *ace_rpc_status) |
| 333 |
{ |
| 334 |
int nRetry = MAXTRIES ; // 3 strikes and you're out! |
| 335 |
while(nRetry--) |
| 336 |
{ |
| 337 |
ace_data->reponse = NULL ; |
| 338 |
|
| 339 |
ace_server( IDL_handle, ace_data, ace_rpc_status) ; |
| 340 |
|
| 341 |
if( *ace_rpc_status == RPC_S_OK ) break ; |
| 342 |
|
| 343 |
ErrorMsg("ACE_SERVER() timeout", *ace_rpc_status) ; |
| 344 |
|
| 345 |
// Wait locally first, then... |
| 346 |
Sleep(serverTimeOut) ; |
| 347 |
|
| 348 |
/* ...set a longer RPC comm timeout */ |
| 349 |
RpcMgmtSetComTimeout(IDL_handle, RPC_C_BINDING_MAX_TIMEOUT) ; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
ACLNT_FUNC_DEF ACE_HANDLE *openServer( unsigned char *szDatabaseName, |
| 354 |
unsigned char *szServiceName, |
| 355 |
unsigned char *szHostName, |
| 356 |
unsigned char *szProtocol, |
| 357 |
unsigned char *szEndpoint, |
| 358 |
DWORD dwTimeOut // in seconds |
| 359 |
) |
| 360 |
{ |
| 361 |
int length, |
| 362 |
clientId = 0, n, |
| 363 |
magic1, magic3; |
| 364 |
ACE_DATA ace_data ; |
| 365 |
unsigned char *answer ; |
| 366 |
long answerLength ; |
| 367 |
ACE_HANDLE *handle ; |
| 368 |
LP_SERVER lpSvr = NULL ; |
| 369 |
RPC_STATUS rpc_status = RPC_S_OK ; |
| 370 |
unsigned long eCode ; |
| 371 |
error_status_t rpcace_error ; |
| 372 |
|
| 373 |
unsigned char *sztheDatabaseName, |
| 374 |
*sztheServiceName, |
| 375 |
*sztheHostName, |
| 376 |
*sztheProtocol, |
| 377 |
*sztheEndpoint ; |
| 378 |
|
| 379 |
unsigned char *szServerStringBinding = 0 ; |
| 380 |
unsigned char szDefaultDatabaseName[] = { "ACEDB" } ; |
| 381 |
unsigned char szDefaultServiceName[] = { "aceserver" } ; |
| 382 |
|
| 383 |
// Save the timeout for future reference |
| 384 |
serverTimeOut = dwTimeOut ; |
| 385 |
|
| 386 |
if( szHostName && *szHostName ) |
| 387 |
sztheHostName = szHostName ; |
| 388 |
else |
| 389 |
sztheHostName = NULL ; // localhost is default */ |
| 390 |
|
| 391 |
if( szDatabaseName && *szDatabaseName ) |
| 392 |
sztheDatabaseName = szDatabaseName ; |
| 393 |
else |
| 394 |
sztheDatabaseName = szDefaultDatabaseName ; |
| 395 |
|
| 396 |
if( szServiceName && *szServiceName ) |
| 397 |
sztheServiceName = szServiceName ; |
| 398 |
else |
| 399 |
sztheServiceName = szDefaultServiceName ; |
| 400 |
|
| 401 |
if(!client_debug ) |
| 402 |
/* don't run aceserver as service if in client_debug mode? |
| 403 |
Attempt to start the service on the specified host |
| 404 |
Should also succeed if already open! Fail otherwise */ |
| 405 |
if( !startServer(sztheHostName, sztheDatabaseName, sztheServiceName, serverTimeOut) ) |
| 406 |
return NULL ; |
| 407 |
|
| 408 |
if( szProtocol && *szProtocol ) |
| 409 |
sztheProtocol = checkProtocol(szProtocol) ; |
| 410 |
else |
| 411 |
/* use the default... |
| 412 |
rbrusk: should default eventually be user configurable? */ |
| 413 |
sztheProtocol = defaultProtocol() ; /* named pipe */ |
| 414 |
|
| 415 |
/* named pipe -- compatibility with given protocol NOT verified */ |
| 416 |
if( szEndpoint && *szEndpoint ) |
| 417 |
sztheEndpoint = checkEndpoint( sztheServiceName, sztheProtocol, szEndpoint ) ; |
| 418 |
else |
| 419 |
sztheEndpoint = defaultEndpoint(sztheServiceName, sztheProtocol) ; |
| 420 |
|
| 421 |
RpcTryExcept |
| 422 |
{ |
| 423 |
/* rpcgen code: |
| 424 |
clnt = clnt_create (host, RPC_ACE, RPC_ACE_VERS, "tcp"); |
| 425 |
if (!clnt) return NULL ; |
| 426 |
*/ |
| 427 |
|
| 428 |
/* Construct DCE server network address */ |
| 429 |
|
| 430 |
RPC_IF_ID interfaceID ; |
| 431 |
unsigned char *ObjectUUID ; |
| 432 |
|
| 433 |
rpc_status = RpcIfInqId( RPC_ACE_v1_2_c_ifspec, &interfaceID ) ; |
| 434 |
if (rpc_status != RPC_S_OK) |
| 435 |
{ |
| 436 |
ErrorMsg("DCE openServer()@RpcIfInqId()", rpc_status) ; |
| 437 |
return NULL ; |
| 438 |
} |
| 439 |
rpc_status = UuidToString( &interfaceID.Uuid, &ObjectUUID ); |
| 440 |
if (rpc_status != RPC_S_OK) |
| 441 |
{ |
| 442 |
ErrorMsg("DCE openServer()@UuidToString()", rpc_status) ; |
| 443 |
return NULL ; |
| 444 |
} |
| 445 |
rpc_status = |
| 446 |
RpcStringBindingCompose(ObjectUUID, |
| 447 |
sztheProtocol, |
| 448 |
sztheHostName, |
| 449 |
sztheEndpoint, |
| 450 |
NULL, |
| 451 |
&szServerStringBinding ) ; |
| 452 |
RpcStringFree(&ObjectUUID) ; |
| 453 |
if (rpc_status != RPC_S_OK) |
| 454 |
{ |
| 455 |
ErrorMsg("DCE openServer(%d)@RpcStringBindingCompose()", rpc_status) ; |
| 456 |
return NULL ; |
| 457 |
} |
| 458 |
|
| 459 |
/* The main operation: attempt to establish contact with the server... */ |
| 460 |
lpSvr = bindServer(szServerStringBinding, &rpc_status ) ; |
| 461 |
|
| 462 |
RpcStringFree(&szServerStringBinding) ; |
| 463 |
|
| 464 |
if(rpc_status) |
| 465 |
{ |
| 466 |
ErrorMsg("DCE openServer()@bindServer()", rpc_status) ; |
| 467 |
return NULL ; /* Non zero binding_status => error */ |
| 468 |
} |
| 469 |
|
| 470 |
/* authenticate */ |
| 471 |
ace_data.question = "" ; |
| 472 |
ace_data.clientId = 0; |
| 473 |
ace_data.magic = 0; |
| 474 |
ace_data.aceError = 0; |
| 475 |
ace_data.encore = 0; |
| 476 |
ace_data.kBytes = 0 ; |
| 477 |
|
| 478 |
ACE_SERVER(serverHandle, &ace_data, &rpcace_error) ; |
| 479 |
|
| 480 |
if (rpcace_error != RPC_S_OK) |
| 481 |
{ |
| 482 |
ErrorMsg("openServer()@ace_server_1()", rpcace_error) ; |
| 483 |
freeServer(lpSvr); |
| 484 |
return NULL ; |
| 485 |
} |
| 486 |
|
| 487 |
rpc_status = copyReponse( &answer, &answerLength, &ace_data.reponse ) ; |
| 488 |
if (rpc_status != RPC_S_OK) |
| 489 |
{ |
| 490 |
ErrorMsg("openServer()@copyReponse_1()", rpc_status ) ; |
| 491 |
freeServer(lpSvr); |
| 492 |
return NULL ; |
| 493 |
} |
| 494 |
} |
| 495 |
RpcExcept(1) { |
| 496 |
eCode = RpcExceptionCode(); |
| 497 |
ErrorMsg("openServer()@exception_1()", eCode ) ; |
| 498 |
printf("Exception reported during aceserver binding: 0x%lx = %ld\n", eCode, eCode); |
| 499 |
freeServer(lpSvr); |
| 500 |
return NULL ; |
| 501 |
} |
| 502 |
RpcEndExcept /* bind server*/ |
| 503 |
|
| 504 |
clientId = ace_data.clientId ; |
| 505 |
magic1 = ace_data.magic ; |
| 506 |
|
| 507 |
if (!clientId) |
| 508 |
{ |
| 509 |
ErrorMsg("openServer(): clientId_1 is NULL?", -1 ) ; |
| 510 |
freeServer(lpSvr); |
| 511 |
return NULL ; |
| 512 |
} |
| 513 |
|
| 514 |
if (ace_data.aceError) |
| 515 |
{ |
| 516 |
ErrorMsg("openServer(): ace_data.aceError_1", ace_data.aceError ) ; |
| 517 |
freeServer(lpSvr); |
| 518 |
return NULL ; |
| 519 |
} |
| 520 |
|
| 521 |
length = answerLength ; |
| 522 |
|
| 523 |
/* aceserver authentication */ |
| 524 |
RpcTryExcept { |
| 525 |
if ( answer && length ) |
| 526 |
{ |
| 527 |
magic3 = getMagic(magic1, answer) ; |
| 528 |
|
| 529 |
/* confirm magic by reaccessing client */ |
| 530 |
ace_data.question = ""; |
| 531 |
ace_data.reponse = NULL ; |
| 532 |
ace_data.clientId = clientId ; |
| 533 |
ace_data.magic = magic3 ; |
| 534 |
ace_data.aceError = 0 ; |
| 535 |
ace_data.encore = 0 ; |
| 536 |
|
| 537 |
ACE_SERVER(serverHandle, &ace_data, &rpcace_error) ; |
| 538 |
if (rpcace_error != RPC_S_OK) |
| 539 |
{ |
| 540 |
ErrorMsg("openServer()@ace_server_2() authentication error", |
| 541 |
rpcace_error) ; |
| 542 |
freeServer(lpSvr); |
| 543 |
return NULL ; |
| 544 |
} |
| 545 |
rpc_status = freeReponse( &ace_data.reponse ) ; |
| 546 |
if ( rpc_status != RPC_S_OK ) |
| 547 |
{ |
| 548 |
ErrorMsg("openServer()@freeReponse_2()", |
| 549 |
rpc_status) ; |
| 550 |
freeServer(lpSvr) ; |
| 551 |
return NULL ; |
| 552 |
} |
| 553 |
n = ace_data.clientId; |
| 554 |
} |
| 555 |
else |
| 556 |
n = clientId + 1 ; /* so we fail */ |
| 557 |
|
| 558 |
if (ace_data.aceError) |
| 559 |
{ |
| 560 |
ErrorMsg("openServer(): ace_data.aceError_2", ace_data.aceError ) ; |
| 561 |
freeServer(lpSvr) ; |
| 562 |
return NULL ; |
| 563 |
} |
| 564 |
if (n != clientId) |
| 565 |
{ |
| 566 |
/* authentication failed */ |
| 567 |
ErrorMsg("openServer(): clientId_2", n ) ; |
| 568 |
freeServer(lpSvr) ; |
| 569 |
return NULL ; |
| 570 |
} |
| 571 |
|
| 572 |
/* create mem for handle - malloc OK because DLL in control of free() */ |
| 573 |
if ((handle = (ACE_HANDLE *)malloc(sizeof(ACE_HANDLE))) == NULL) { |
| 574 |
ace_data.question = "Quit"; |
| 575 |
ace_data.reponse = NULL ; |
| 576 |
ace_data.clientId = clientId ; |
| 577 |
ace_data.magic = magic3 ; |
| 578 |
ace_data.aceError = 0 ; |
| 579 |
ace_data.encore = 0 ; |
| 580 |
|
| 581 |
ACE_SERVER(serverHandle, &ace_data, &rpcace_error) ; |
| 582 |
if (rpcace_error == RPC_S_OK) |
| 583 |
{ |
| 584 |
freeReponse( &ace_data.reponse ) ; |
| 585 |
} |
| 586 |
else |
| 587 |
{ |
| 588 |
ErrorMsg("openServer()@ace_server_3()", |
| 589 |
rpcace_error) ; |
| 590 |
} |
| 591 |
freeServer(lpSvr) ; |
| 592 |
return NULL ; |
| 593 |
} |
| 594 |
} |
| 595 |
RpcExcept(1) { |
| 596 |
eCode = RpcExceptionCode(); |
| 597 |
ErrorMsg("openServer()@exception_2()", eCode ) ; |
| 598 |
printf("Exception reported during aceserver authentication: 0x%lx = %ld\n", eCode, eCode); |
| 599 |
} |
| 600 |
RpcEndExcept /* Server authentication */ |
| 601 |
|
| 602 |
handle->clientId = clientId; |
| 603 |
handle->magic = magic3; |
| 604 |
handle->svr = (void *)lpSvr; |
| 605 |
|
| 606 |
return handle ; |
| 607 |
} |
| 608 |
|
| 609 |
/************************************************************* |
| 610 |
notify server of intent to close connection |
| 611 |
close connection |
| 612 |
free structures |
| 613 |
|
| 614 |
INPUT |
| 615 |
ACE_HANDLE * pointer to structure containing open connection |
| 616 |
and client identification information |
| 617 |
OUTPUT |
| 618 |
none |
| 619 |
*/ |
| 620 |
#define aceServerHandle (((LP_SERVER)(handle->svr))->hServerBinding) |
| 621 |
|
| 622 |
ACLNT_FUNC_DEF void closeServer(ACE_HANDLE *handle) |
| 623 |
{ |
| 624 |
ACE_DATA ace_data ; |
| 625 |
error_status_t rpcace_error ; |
| 626 |
unsigned long eCode ; |
| 627 |
|
| 628 |
/* aceserver closure */ |
| 629 |
RpcTryExcept |
| 630 |
{ |
| 631 |
if (handle != NULL) |
| 632 |
{ |
| 633 |
if ( (LP_SERVER)handle->svr != NULL ) |
| 634 |
{ |
| 635 |
ace_data.question = "Quit"; |
| 636 |
ace_data.reponse = NULL ; |
| 637 |
ace_data.clientId = handle->clientId ; |
| 638 |
ace_data.magic = handle->magic ; |
| 639 |
ace_data.aceError = 0 ; |
| 640 |
ace_data.encore = 0 ; |
| 641 |
ace_data.kBytes = 0; |
| 642 |
|
| 643 |
ACE_SERVER(aceServerHandle, &ace_data, &rpcace_error) ; |
| 644 |
if (rpcace_error == RPC_S_OK) |
| 645 |
{ |
| 646 |
freeReponse( &ace_data.reponse ) ; |
| 647 |
} |
| 648 |
else |
| 649 |
{ |
| 650 |
ErrorMsg("closeServer()@ace_server()\n", |
| 651 |
rpcace_error) ; |
| 652 |
} |
| 653 |
freeServer((LP_SERVER)handle->svr) ; |
| 654 |
} |
| 655 |
free((char *)handle); |
| 656 |
} |
| 657 |
} |
| 658 |
RpcExcept(1) |
| 659 |
{ |
| 660 |
eCode = RpcExceptionCode(); |
| 661 |
ErrorMsg("closeServer()@exception_1()", eCode ) ; |
| 662 |
printf("Exception reported during aceserver closure: 0x%lx = %ld\n", eCode, eCode); |
| 663 |
} |
| 664 |
RpcEndExcept /* Server closure */ |
| 665 |
|
| 666 |
} |
| 667 |
|
| 668 |
/************************************************************* |
| 669 |
transfer request to server, and wait for binary answer |
| 670 |
INPUT |
| 671 |
char * request string containing request |
| 672 |
unsigned char ** answer ptr to char ptr, that has to be filled with answer |
| 673 |
ACE_HANDLE * pointer to structure containing open connection |
| 674 |
and client identification information |
| 675 |
int chunkSize desired size (in kBytes) of returned data-block |
| 676 |
This is only a hint. The server can return more. |
| 677 |
The server splits on ace boundaries |
| 678 |
|
| 679 |
a chunkSize of 0 allocates a default reponse buffer (64KB in size) |
| 680 |
|
| 681 |
OUTPUT |
| 682 |
unsigned char ** answer ptr to char ptr. Pointing to allocated memory containing |
| 683 |
answer string. This memory will be filled with the |
| 684 |
unmodified data handled as binary bytes. |
| 685 |
return value: |
| 686 |
int error condition |
| 687 |
ESUCCESS (0) no error. |
| 688 |
EIO (5) no response received from server. |
| 689 |
ENOMEM (12) no memory available to store answer. |
| 690 |
or a server generated error |
| 691 |
|
| 692 |
JC if the server can return both an encore and an aceError at the same time |
| 693 |
I'm in trouble. I use only one int return value for both |
| 694 |
*/ |
| 695 |
|
| 696 |
#define DEFACLNTT_CHUNK_SIZE 64 |
| 697 |
|
| 698 |
ACLNT_FUNC_DEF int askServerBinary(ACE_HANDLE *handle, char *request, unsigned char **answerPtr, |
| 699 |
int *answerLength, int *encorep, int chunkSize) |
| 700 |
{ |
| 701 |
long newBytes ; |
| 702 |
static ACE_DATA ace_data ; |
| 703 |
RPC_STATUS rpc_status = RPC_S_OK ; |
| 704 |
unsigned long eCode ; |
| 705 |
error_status_t rpcace_error ; |
| 706 |
int aceError, encore = 0 ; |
| 707 |
|
| 708 |
if ( handle == NULL || (LP_SERVER)handle->svr == NULL ) |
| 709 |
return (int)(RPC_X_SS_CONTEXT_MISMATCH) ; |
| 710 |
|
| 711 |
ace_data.question = "" ; |
| 712 |
ace_data.clientId = handle->clientId ; |
| 713 |
ace_data.magic = handle->magic ; |
| 714 |
ace_data.aceError = 0 ; |
| 715 |
ace_data.kBytes = chunkSize ? chunkSize : DEFACLNTT_CHUNK_SIZE ; |
| 716 |
|
| 717 |
/* check if request contains a local command */ |
| 718 |
if (!strncasecmp(request,"encore",6)) |
| 719 |
{ |
| 720 |
/* encore request */ |
| 721 |
ace_data.question = ""; |
| 722 |
ace_data.encore = WANT_ENCORE; |
| 723 |
} |
| 724 |
else if (!strncasecmp(request,"noencore",8)) |
| 725 |
{ |
| 726 |
/* encore request */ |
| 727 |
ace_data.question = ""; |
| 728 |
ace_data.encore = DROP_ENCORE; |
| 729 |
} |
| 730 |
else if (!strncasecmp(request,"quit",4)) |
| 731 |
{ /* ignore quit request. Must go through closeServer routine */ |
| 732 |
*answerLength = 0; |
| 733 |
*answerPtr = NULL; |
| 734 |
return 0; |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
ace_data.encore = 0; |
| 739 |
ace_data.question = request; |
| 740 |
} |
| 741 |
|
| 742 |
if (*encorep == 3) ace_data.encore = -3 ; |
| 743 |
|
| 744 |
/* aceserver RPC query call */ |
| 745 |
RpcTryExcept { |
| 746 |
|
| 747 |
ace_data.reponse = NULL ; |
| 748 |
ACE_SERVER(aceServerHandle, &ace_data, &rpcace_error) ; |
| 749 |
if ( rpcace_error != RPC_S_OK ) |
| 750 |
{ |
| 751 |
ErrorMsg("askServerBinary()@ace_server()", |
| 752 |
rpcace_error) ; |
| 753 |
freeServer((LP_SERVER)handle->svr) ; /* fatal error: release the server? */ |
| 754 |
return (int)(rpcace_error) ; /* and return the error number? */ |
| 755 |
} |
| 756 |
rpc_status = copyReponse( answerPtr, answerLength, &ace_data.reponse ) ; |
| 757 |
|
| 758 |
/* validity checking of reponse */ |
| 759 |
/* no data was received, return error */ |
| 760 |
if ( (rpcace_error = (error_status_t)rpc_status) != RPC_S_OK ) |
| 761 |
{ |
| 762 |
ErrorMsg("askServerBinary()@copyReponse()", |
| 763 |
rpcace_error) ; |
| 764 |
freeServer((LP_SERVER)handle->svr) ; /* fatal error: release the server? */ |
| 765 |
return (int)(rpcace_error) ; /* and return the error number? */ |
| 766 |
} |
| 767 |
|
| 768 |
/* no answer was received or error, return NULL answer |
| 769 |
leave checking for NULL reponse to upper layer */ |
| 770 |
/* store server returned error status. Give this to the client */ |
| 771 |
if( !*answerPtr || !answerLength || ace_data.aceError ) |
| 772 |
{ |
| 773 |
ErrorMsg("askServerBinary(): no answer", |
| 774 |
ace_data.aceError) ; |
| 775 |
return ace_data.aceError ; |
| 776 |
} |
| 777 |
} |
| 778 |
RpcExcept(1) { |
| 779 |
eCode = RpcExceptionCode(); |
| 780 |
ErrorMsg("askServerBinary()@exception_1()", eCode ) ; |
| 781 |
printf("Exception reported during aceserver RPC query call: 0x%lx = %ld\n", eCode, eCode); |
| 782 |
} |
| 783 |
RpcEndExcept /* aceserver RPC query call */ |
| 784 |
|
| 785 |
*encorep = ace_data.encore ; |
| 786 |
return ace_data.aceError ? ace_data.aceError : - encore ; /* surcharge pour JD */ |
| 787 |
} |
| 788 |
|
| 789 |
/*************************************************************** |
| 790 |
transfer request to server, and wait for binary answer. Convert answer |
| 791 |
to ASCII string |
| 792 |
|
| 793 |
INPUT |
| 794 |
char * request string containing request |
| 795 |
char ** answer ptr to char ptr, that has to be filled with answer |
| 796 |
ACE_HANDLE * pointer to structure containing open connection |
| 797 |
and client identification information |
| 798 |
int chunkSize desired size (in kBytes) of returned data-block |
| 799 |
This is only a hint. The server can return more. |
| 800 |
The server splits on ace boundaries |
| 801 |
a chunkSize of 0 indicates a request for unbuffered answers |
| 802 |
|
| 803 |
OUTPUT |
| 804 |
char ** answer ptr to char ptr. Pointing to allocated memory containing |
| 805 |
answer string. |
| 806 |
return value: |
| 807 |
int error condition |
| 808 |
ESUCCESS (0) no error. |
| 809 |
EIO (5) no response received from server. |
| 810 |
ENOMEM (12) no memory available to store answer. |
| 811 |
or a server generated error |
| 812 |
*/ |
| 813 |
|
| 814 |
ACLNT_FUNC_DEF int askServer(ACE_HANDLE *handle, char *request, char **answerPtr, int chunkSize) |
| 815 |
{ int length, i, encore ; |
| 816 |
int returnValue; |
| 817 |
unsigned char *binaryAnswer; |
| 818 |
char *answer; |
| 819 |
char *loop; |
| 820 |
|
| 821 |
returnValue = askServerBinary(handle, request, &binaryAnswer, &length, &encore, chunkSize) ; |
| 822 |
if (returnValue <= 0) |
| 823 |
{ /* allocate memory for return string */ |
| 824 |
/* if memory is more important than speed, we could run |
| 825 |
through the string first and count the number of '\0''s |
| 826 |
and substract this from the memoryblock we allocate */ |
| 827 |
if (!length ) /* empty string */ |
| 828 |
{ *answerPtr = 0; |
| 829 |
return returnValue; |
| 830 |
} |
| 831 |
#if !defined(WIN32) |
| 832 |
if ((answer = (char *)malloc(length+1)) == NULL) |
| 833 |
{ free(binaryAnswer); |
| 834 |
return(ENOMEM); |
| 835 |
} |
| 836 |
#else /* defined(WIN32) */ |
| 837 |
/* Use messalloc, so invoking application can use messfree()? |
| 838 |
Crashes if out of memory (no error code returned)? */ |
| 839 |
answer = (char *)messalloc(length+1) ; |
| 840 |
#endif /* defined(WIN32) */ |
| 841 |
/* initial step of the copy process */ |
| 842 |
loop = (char *)binaryAnswer; |
| 843 |
strcpy(answer,loop); |
| 844 |
i = *loop ? strlen(loop): 0 ; |
| 845 |
loop += i; |
| 846 |
for (;(*loop == '\0')&&(i<length) ;loop++,i++); |
| 847 |
|
| 848 |
for (;i<length;) |
| 849 |
{ strcat(answer,loop); |
| 850 |
i += strlen(loop); |
| 851 |
loop += strlen(loop); |
| 852 |
for (;(*loop == '\0')&&(i<length) ;loop++,i++); |
| 853 |
} |
| 854 |
*(answer+i) = '\0'; /* for safety, make sure the string is terminated */ |
| 855 |
free((char *)binaryAnswer); |
| 856 |
*answerPtr = answer; |
| 857 |
} |
| 858 |
return returnValue; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
/************** end of file **************/ |