📄 wtxrpc.c
字号:
for (len = ifc.ifc_len; len; len -= sizeof (ifReq)) { /* first take to get the length of the ip addr list length */ ifReq = *ifr; if (ioctl (s, SIOCGIFFLAGS, (char *)&ifReq) < 0) goto error2; if ( (ifReq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET)) { memcpy (&addr, &ifr->ifr_addr, sizeof (struct sockaddr_in));#ifdef PARISC_HPUX10 strcpy (ipAddr, inet_ntoa (addr.sin_addr));#else sprintf (ipAddr, "%i.%i.%i.%i", addr.sin_addr.S_un.S_un_b.s_b1, addr.sin_addr.S_un.S_un_b.s_b2, addr.sin_addr.S_un.S_un_b.s_b3, addr.sin_addr.S_un.S_un_b.s_b4);#endif /* PARISC_HPUX10 */ addrLen = strlen (ipAddr); if ( (addrLen >= 4) && (strncmp (ipAddr, "127.", 4) == 0)) { /* this is the local host address 127.0.0.1, don't add it */ } else { inetLen += addrLen + 1; } } ifr++; } close (s); /* do that again */ if ( (s = socket (AF_INET, SOCK_DGRAM, 0)) < 0) goto error1; ifc.ifc_len = sizeof (ifBuf); ifc.ifc_buf = ifBuf; /* get the if config */ if (ioctl (s, SIOCGIFCONF, (char *)&ifc) < 0) goto error2; ifr = ifc.ifc_req; /* get the list of all the IP addresses */ /* allocate the string for the IP addresses list */ if ( (ipAddrStr = (char *) calloc (inetLen, sizeof (char))) == NULL) goto error2; for (len = ifc.ifc_len; len; len -= sizeof (ifReq)) { /* first take to get the length of the ip addr list length */ ifReq = *ifr; if (ioctl (s, SIOCGIFFLAGS, (char *)&ifReq) < 0) goto error2; if ( (ifReq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET)) { memcpy (&addr, &ifr->ifr_addr, sizeof (struct sockaddr_in));#ifdef PARISC_HPUX10 strcpy (ipAddr, inet_ntoa (addr.sin_addr));#else sprintf (ipAddr, "%i.%i.%i.%i", addr.sin_addr.S_un.S_un_b.s_b1, addr.sin_addr.S_un.S_un_b.s_b2, addr.sin_addr.S_un.S_un_b.s_b3, addr.sin_addr.S_un.S_un_b.s_b4);#endif /* PARISC_HPUX10 */ addrLen = strlen (ipAddr); if (firstAddress) { if ( (addrLen >= 4) && (strncmp (ipAddr, "127.", 4) == 0)) { /* this is the local host address 127.0.01, do not add it */ } else { strcpy (ipAddrStr, ipAddr); firstAddress = FALSE; } } else { addrLen = strlen (ipAddr); if ( (addrLen >= 4) && (strncmp (ipAddr, "127.", 4) == 0)) { /* this is the local host address 127.0.0.1, don't add it */ } else { sprintf (ipAddrStr, "%s,%s", ipAddrStr, ipAddr); } } } ifr++; } #endif /* WIN32 */#else /* HOST */ ina.s_addr = hostGetByName (hostName); strcpy (ipAddrStr, inet_ntoa (ina));#endif /* HOST */ /* calculate the new key length */ newKeyLen = strlen (hostName) + strlen (ipAddrStr) + 10; sprintf (buf, "%d", progNum); newKeyLen += strlen (buf) + 1; sprintf (buf, "%d", version); newKeyLen += strlen (buf) + 1; /* If we cannot get the port used, don't write it. */ if(ppNewXprt == NULL) { /* allocate room for the new key string */ if ( (newKey = (char *) calloc (newKeyLen + 1, sizeof (char))) == NULL) goto error2; sprintf (newKey, "rpc/%s/%s/%d/%d/%s", hostName, ipAddrStr, progNum, version, (protocol == IPPROTO_TCP) ? "tcp" : "udp"); } else { sprintf (buf, "%d", (*ppNewXprt)->xp_port); newKeyLen += strlen (buf) + 1; /* allocate room for the new key string */ if ( (newKey = (char *) calloc (newKeyLen + 1, sizeof (char))) == NULL) goto error2; sprintf (newKey, "rpc/%s/%s/%d/%d/%s/%d", hostName, ipAddrStr, progNum, version, (protocol == IPPROTO_TCP) ? "tcp" : "udp", (*ppNewXprt)->xp_port); } /* free the ip address string now */error2: if (ipAddrStr != NULL) free (ipAddrStr);#if (defined HOST) && (!defined WIN32) close (s); #endif /* ! WIN32 */error1: return (newKey); /* return key */ }/********************************************************************************* wtxRpcKeyIPListUpdate - updates IP address list of the given RPC key** This routine uses the given <rpcKeyStr> RPC key string to update the* IP address list with the given <ipNum>. It takes the IP address <ipNum> in the* <rpcKeyStr> (if there are several), and set it first in the IP address list.** This allows to put the given <ipNum> as the first address in the list* of addresses to use to connect to the target server** If <ipNum> is negative, or if the <rpcKeyStr> is NULL, the routine will exit,* returning NULL.** RETURNS: a string with updated RPC key, or NULL on error.** ERRORS:* \ibs* \ibe** SEE ALSO: wtxRpcKey(), wtxRpcKeySplit()*/char * wtxRpcKeyIPListUpdate ( const char * rpcKeyStr, /* rpc Key string to update with IP */ int ipNum /* IP address num to set as default */ ) { WTX_RPC_SPLIT_KEY splitKey; /* splitted key output struct */ char * newKey = NULL; /* new updated RPC key string */ char * tmpIP = NULL; /* temp address IP holder */ int nAddr = 0; /* address number in list */ /* first of all, check the imput parameters */ if ((rpcKeyStr == NULL) || (ipNum < 0)) goto error1; /* split the key and look for <withIP> in this key */ memset (&splitKey, 0, sizeof (WTX_RPC_SPLIT_KEY)); if (wtxRpcKeySplit (rpcKeyStr, &splitKey) != WTX_OK) goto error1; if ( ( (nAddr = wtxRpcKeyIPListSizeGet (&splitKey)) == 0) || (nAddr < ipNum)) goto error2; tmpIP = splitKey.ipAddrList [0]; splitKey.ipAddrList [0] = splitKey.ipAddrList [ipNum]; splitKey.ipAddrList [ipNum] = tmpIP; /* we have an updated split key structure. Build new key string with it */ newKey = wtxRpcKeyFromRpcKeySplitGet (&splitKey);error2: wtxRpcKeySplitFree (&splitKey);error1: return (newKey); }/********************************************************************************* wtxRpcKeyIPListSizeGet - get the number of IP addresses of a splitted RPC key** This routine returns the number of IP addresses from a splitted RPC key* structure, where the WTX_RPC_SPLIT_KEY structure has the following* description :** EXPAND ../../../include/wtxrpc.h WTX_RPC_SPLIT_KEY** The <ipAddrList> contains the list of several IP adapters for the given* address** RETURNS: the number of IP adapters or 0 if there was no adapters** ERRROS: N/A** SEE ALSO: wtxRpcKeySplit(), wtxRpcKey()** NOMANUAL*/LOCAL int wtxRpcKeyIPListSizeGet ( WTX_RPC_SPLIT_KEY * pSplitKey /* splitted RPC key desc */ ) { int nIpAddr = 0; /* number of adapters for this IP */ if ( (pSplitKey == 0) || (pSplitKey->ipAddrList == NULL)) goto error1; while (pSplitKey->ipAddrList [nIpAddr] != NULL) nIpAddr++;error1: return (nIpAddr); }/********************************************************************************* wtxRpcKeySplitFree - free the freeable members of an RPC key structure** This routine frees the freeable members of an RPC splitted structure which* definition is :** EXPAND ../../../include/wtxrpc.h WTX_RPC_SPLIT_KEY** The RPC key structure itself is not freed, and it is up to the user to know* if he has to free it calling free() or not.** RETURNS: WTX_OK** ERRORS: N/A** SEE ALSO: wtxRpcKeySplit(), wtxRpcKeyIPListUpdate()*/STATUS wtxRpcKeySplitFree ( WTX_RPC_SPLIT_KEY * pRpcSplitKey /* RPC key to free members of */ ) { int addrNum = 0; /* address number in address list */ /* first of all, check the input parameters */ if ( (pRpcSplitKey == NULL) || (pRpcSplitKey->ipAddrList == NULL)) goto error1; /* free all the IP addressed from the IP address list */ while (pRpcSplitKey->ipAddrList [addrNum] != NULL) { free (pRpcSplitKey->ipAddrList [addrNum]); addrNum ++; } free (pRpcSplitKey->ipAddrList);error1: return (WTX_OK); }/******************************************************************************** wtxRpcKeySplit - split an RPC key string into its component parts** The given RPC key string is decoded and the various fields are placed* into the WTX_RPC_SPLIT_KEY structure provided (by reference).** RETURNS:* WTX_OK, if the key was split successfully and pSplitKey is filled, or* WTX_ERROR if the key could not be decoded.*/STATUS wtxRpcKeySplit ( const char * rpcKey, /* rpc key string to split */ WTX_RPC_SPLIT_KEY * pSplitKey /* splitted key output structure */ ) { STATUS status = WTX_ERROR; /* routine returned status */ char * ipAddrList = NULL; /* list of IP addressed for key */ char * keyStart = NULL; /* key start in string */ char * ptr = NULL; /* address list cursor */ char * curIPAddr = NULL; /* current IP address string */ char progNum[10]; /* program number in string format */ char version[10]; /* transport version in string format */ char protocol[10]; /* tcp or udp */ char portNum[10] = " 0"; /* port number in string format */ char keyPat[] = "rpc/%[^/]/%[.,0-9a-fA-F]/%[0-9a-fA-F]/%[0-9a-fA-F]/%[tcpudp]/%[0-9a-fA-F]"; int nPatternsToMatch = 6; /* number of patterns to match */ int nMatchedPatterns; /* number of matched patterns */ int nAddr = 1; /* number of addresses in key */ /* check arguments */ if ( (rpcKey == NULL) || (pSplitKey == NULL)) goto error1; /* * allocate memory for the IP adresses list, default it to the length of the * RPC key ... the list of IP addresses should not be longer */ if ( (ipAddrList = (char *) calloc (strlen (rpcKey) + 1, sizeof (char))) == NULL) goto error1; /* type/host/ipaddr/prognum/vers/protocol/portnum */ memset (pSplitKey, 0, sizeof (*pSplitKey)); nMatchedPatterns = sscanf (rpcKey, keyPat, pSplitKey->host, ipAddrList, progNum, version, protocol, portNum); /* * The port number may not be present in the key. Lets' examine if we got * all the elements or not. */ if (nMatchedPatterns == nPatternsToMatch) { pSplitKey->port = (unsigned short) atoi (portNum); } else { if (nMatchedPatterns == (nPatternsToMatch-1)) { /* port number not present. Use the portmapper */ pSplitKey->port = 0; /* use portmapper */ } else /* something wrong in the key! */ goto error2; } pSplitKey->progNum = atoi (progNum); pSplitKey->version = atoi (version); if (! strcmp (protocol, "udp")) pSplitKey->protocol = IPPROTO_UDP; else if (! strcmp (protocol, "tcp")) pSplitKey->protocol = IPPROTO_TCP; else goto error2; /* allocate and store the list of IP addresses */ ptr = ipAddrList; nAddr = 1; while ( (ptr != NULL) && (*ptr != '\0')) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -