📄 wtxrpc.c
字号:
/* count the number of ',' in the address list */ if ( (*ptr) == ',') nAddr++; ptr++; } /* allocate <nAddr> + 1 strings */ if ( (pSplitKey->ipAddrList = (char **) calloc (nAddr + 1, sizeof (char *))) == NULL) goto error2; keyStart = ipAddrList; ptr = ipAddrList; nAddr = 0; while ((ptr != NULL) && (*ptr != '\0')) { if ( (*ptr) == ',') { /* there is an IP address list separator, copy this address */ if ( (curIPAddr = (char *) calloc ( (int) (ptr - keyStart) + 1, sizeof (char))) == NULL) goto error3; strncpy (curIPAddr, keyStart, (int) (ptr - keyStart)); pSplitKey->ipAddrList [nAddr] = curIPAddr; nAddr ++; keyStart = ptr + 1; } ptr ++; } /* we need to also copy the last address form the key */ if ( (pSplitKey->ipAddrList [nAddr] = (char *) calloc ( (int) (ptr - keyStart) + 1, sizeof (char))) == NULL) goto error3; strcpy (pSplitKey->ipAddrList [nAddr], keyStart); status = WTX_OK;error3: if (status != WTX_OK) wtxRpcKeySplitFree (pSplitKey);error2: if (ipAddrList != NULL) free (ipAddrList);error1: return (status); }/********************************************************************************* wtxRpcKeyFromRpcKeySplitGet - get an RPC key from a splitted RPC key structure** This routines takes the input <pSplitKey> RPC splitted key structure, to* build an RPC key string.** RETURNS: The RPC key string or NULL on error** ERRORS: N/A** SEE ALSO: wtxRpcKey(), wtxRpcKeySplit(), wtxRpcKeyIPListUpdate()* wtxRpcKeySplitFree()** NOMANUAL*/LOCAL char * wtxRpcKeyFromRpcKeySplitGet ( WTX_RPC_SPLIT_KEY * pSplitKey /* splitted key to create key from */ ) { char * ipList = NULL; /* IP address list, ',' separated */ char * rpcKey = NULL; /* extracted RPC ey string */ char buf [256]; /* formatting buffer */ int addrNum = 0; /* address number in list */ int keyLen = 0; /* RPC key string length */ int ipListLen = 0; /* IP addresses list length */ /* first of all, check the routines' arguments */ if ( (pSplitKey == NULL) || (pSplitKey->host == NULL) || (pSplitKey->ipAddrList == NULL)) goto error1; /* calculate the RPC ey string length */ memset (&buf, 0, 256); keyLen += strlen (pSplitKey->host) + 1; sprintf (buf, "%d", pSplitKey->progNum); keyLen += strlen (buf) + 1; sprintf (buf, "%d", pSplitKey->version); keyLen += strlen (buf) + 1; /* add the rpc/ + the (udp)|(rpc)/? */ keyLen += 7; /* allocate and store the list of IP addresses */ addrNum = 0; while (pSplitKey->ipAddrList [addrNum] != NULL) { ipListLen += strlen (pSplitKey->ipAddrList [addrNum]) + 1; addrNum ++; } keyLen += ipListLen; /* we have the key cumulated IP list length, allocate the string for it */ if ( (ipList = (char *) calloc (ipListLen, sizeof (char))) == NULL) goto error1; addrNum = 0; while (pSplitKey->ipAddrList [addrNum] != NULL) { if (addrNum == 0) strcpy (ipList, pSplitKey->ipAddrList [addrNum]); else sprintf (ipList, "%s,%s", ipList, pSplitKey->ipAddrList [addrNum]); addrNum ++; } if (pSplitKey->port == 0) { /* allocate room for the new key string */ if ( (rpcKey = (char *) calloc (keyLen + 1, sizeof (char))) == NULL) goto error2; sprintf (rpcKey, "rpc/%s/%s/%d/%d/%s", pSplitKey->host, ipList, pSplitKey->progNum, pSplitKey->version, (pSplitKey->protocol == IPPROTO_TCP) ? "tcp" : "udp"); } else { sprintf (buf, "%u", pSplitKey->port); keyLen += strlen (buf) + 1; /* allocate room for the new key string */ if ( (rpcKey = (char *) calloc (keyLen + 1, sizeof (char))) == NULL) goto error2; sprintf (rpcKey, "rpc/%s/%s/%d/%d/%s/%u", pSplitKey->host, ipList, pSplitKey->progNum, pSplitKey->version, (pSplitKey->protocol == IPPROTO_TCP) ? "tcp" : "udp", pSplitKey->port); }error2: if (ipList != NULL) free (ipList);error1: return (rpcKey); }/********************************************************************************* wtxRpcExchangeCreate - create a new exchange client using the RPC transport** This routine takes a wpwr key string and creates a new exchange client* * RETURNS: WTX_OK, or WTX_ERROR** NOMANUAL*/STATUS wtxRpcExchangeCreate ( WTX_XID xid, /* Exchange handle */ const char * key, /* Key for server to connect to */ BOOL usePMap /* use port mapper to connect ? */ ) { struct sockaddr_in addr; /* socket address */ WTX_RPC_SPLIT_KEY split; /* split key */ CLIENT * pClient = NULL; /* rpc client handle */ int rpcSock = 0; /* rpc socket */#ifdef HOST struct hostent * pHostEnt = NULL; /* host entry */ char * registryHost = NULL; /* registry host name */#endif /* HOST */ if (key != NULL) { if (wtxRpcKeySplit (key, &split) != WTX_OK) WTX_EXCHANGE_RETURN (xid, WTX_ERR_EXCHANGE_BAD_KEY, WTX_ERROR); } else { /* * NULL key => contact the registry service so fill in the split key * with the Tornado registry particulars */#ifdef HOST if ((registryHost = wpwrGetEnv ("WIND_REGISTRY")) == NULL) registryHost = "127.0.0.1"; /* its probably local */ strncpy (split.host, registryHost, sizeof (split.host)); split.host [sizeof (split.host) - 1] = '\0';#endif if (usePMap) split.port = 0; else split.port = REGISTRY_PORT; /* * Set ipAddrList to NULL. That will cause the address lookup logic * below to use gethostbyname for us. */ split.ipAddrList = NULL; split.progNum = WTX_SVC_BASE; split.version = 1; split.protocol = IPPROTO_TCP; } /* * Get the internet address for the given host. Parse hostname * as host name or alternatively in the `xx.xx.xx.xx' notation. */ memset ((void *) &addr, 0, sizeof (addr)); /* * Get the IP address. First use the dotted decimal part of the * split key if that is comprehensible. Otherwise use * gethostbyname to look up the address. If that fails try to * parse the hostname itself as a dotted decimal. Otherwise * complain. */ if ( (split.ipAddrList != NULL) && (int) (addr.sin_addr.s_addr = inet_addr (split.ipAddrList [0])) != -1) ;#ifdef HOST# ifdef WIN32 else if ((addr.sin_addr.s_addr = inet_addr (split.host)) != INADDR_NONE) ; else if ((pHostEnt = (struct hostent *)gethostbyname (split.host)) != NULL) addr.sin_addr.s_addr = * ((u_long *) pHostEnt->h_addr_list[0]); else { wtxRpcKeySplitFree (&split); WTX_EXCHANGE_RETURN (xid, WTX_ERR_EXCHANGE_NO_SERVER, WTX_ERROR); }# else /* WIN32 */ else if ((pHostEnt = (struct hostent *)gethostbyname (split.host)) != NULL) addr.sin_addr.s_addr = * ((u_long *) pHostEnt->h_addr_list[0]); else if ( (int) (addr.sin_addr.s_addr = inet_addr (split.host)) == -1) { wtxRpcKeySplitFree (&split); WTX_EXCHANGE_RETURN (xid, WTX_ERR_EXCHANGE_NO_SERVER, WTX_ERROR); }# endif /* WIN32 */#else else addr.sin_addr.s_addr = syncRegistry;#endif /* HOST */ addr.sin_family = AF_INET; addr.sin_port = htons(split.port); /* given by registry/portmapper */ rpcSock = RPC_ANYSOCK; /* any old socket */ if (split.protocol == IPPROTO_TCP) pClient = clnttcp_create (&addr, split.progNum, split.version, &rpcSock, 0, 0); else pClient = clntudp_create (&addr, split.progNum, split.version, rpcDefaultUdpTimeout, &rpcSock); if (pClient == NULL) /* transport failed? */ { wtxRpcKeySplitFree (&split); WTX_EXCHANGE_RETURN (xid, WTX_ERR_EXCHANGE_NO_SERVER, WTX_ERROR); } /* Set up the authorization mechanism */#ifdef HOST# ifndef WIN32 pClient->cl_auth = authunix_create_default();# else pClient->cl_auth = authunix_create("WIN32", wpwrGetUid(), NULL, NULL, NULL);# endif /* WIN32 */#else /* authentication */ pClient->cl_auth = authunix_create ("", 0, 0, 0, NULL); /* set the real value computed on host and stored on target */ pClient->cl_auth->ah_cred.oa_flavor = authCred.oa_flavor; pClient->cl_auth->ah_cred.oa_base = authCred.oa_base; pClient->cl_auth->ah_cred.oa_length = authCred.oa_length;#endif /* HOST */ xid->transport = pClient;#ifndef HOST#if (CPU==SIMHPPA) /* * Set RPC receive buffer for HPSIM when using SLIP. This is needed for * target symbol synchronization, since the target server can send * messages larger than the target MTU when not communicating thru WDB. */ { WDB_AGENT_INFO agentInfo; int blen; wdbInfoGet (&agentInfo); blen = agentInfo.mtu; if ( blen < 1000 ) /* Must be using SLIP */ setsockopt (rpcSock, SOL_SOCKET, SO_RCVBUF, (void *) &blen, sizeof (blen)); }#endif /* CPU=SIMHPPA */#endif /* ! HOST */ wtxRpcKeySplitFree (&split); return WTX_OK; }/********************************************************************************* wtxRpcExchangeControl - perform a miscellaneous exchange control function.** This routine performs a miscellaneous control function on the exchange * handle <xid>. ** RETURNS: WTX_OK, or WTX_ERROR.** ERRORS: WTX_ERR_EXCHANGE_INVALID_HANDLE, WTX_ERR_EXCHANGE_INVALID_ARG** NOMANUAL*/STATUS wtxRpcExchangeControl ( WTX_XID xid, /* Exchange handle */ UINT32 request, /* Exchange control request number */ void * arg /* Pointer to request argument */ ) { CLIENT * pClient; pClient = (CLIENT *) xid->transport; switch (request) { case WTX_EXCHANGE_CTL_TIMEOUT_SET: xid->timeout = *(UINT32 *) arg; break; case WTX_EXCHANGE_CTL_TIMEOUT_GET: *(UINT32 *) arg = xid->timeout; break; default: WTX_EXCHANGE_RETURN (xid, WTX_ERR_EXCHANGE_REQUEST_UNSUPPORTED, WTX_ERROR); } return WTX_OK; }/********************************************************************************* wxRpcDelete - delete the exchange handle and any transport connection.** This routine deletes the exchange mechanism specified by the exchange * handle <xid>. <xid> should not be used again in any further exchange* calls as the underlying transport mechanism is closed also.** RETURNS: WTX_OK, or WTX_ERROR** NOMANUAL*/STATUS wtxRpcExchangeDelete ( WTX_XID xid /* Exchange handle */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -