📄 inetlib.c
字号:
int netAddr, /* network part of the inet address */ int hostAddr, /* host part of the inet address */ struct in_addr *pInetAddr /* where to return the inet address */ ) { register u_long addr; if (netAddr < 128) addr = (netAddr << IN_CLASSA_NSHIFT) | (hostAddr & IN_CLASSA_HOST); else if (netAddr < 65536) addr = (netAddr << IN_CLASSB_NSHIFT) | (hostAddr & IN_CLASSB_HOST); else if (netAddr < 16777216) addr = (netAddr << IN_CLASSC_NSHIFT) | (hostAddr & IN_CLASSC_HOST); else addr = (netAddr << IN_CLASSD_NSHIFT) | (hostAddr & IN_CLASSD_HOST); pInetAddr->s_addr = htonl (addr); }/********************************************************************************* inet_makeaddr - form an Internet address from network and host numbers** This routine constructs the Internet address from the network number and* local host address.** WARNING* This routine is supplied for UNIX compatibility only. Each time this* routine is called, four bytes are allocated from memory. Use* inet_makeaddr_b() instead.** EXAMPLE* The following example returns the address 0x5a000002 to the structure * `in_addr':* .CS* inet_makeaddr (0x5a, 2);* .CE** RETURNS: The network address in an `in_addr' structure.** SEE ALSO: inet_makeaddr_b()*/struct in_addr inet_makeaddr ( int netAddr, /* network part of the address */ int hostAddr /* host part of the address */ ) { struct in_addr *pAddr = (struct in_addr *) malloc (sizeof (struct in_addr)); if (pAddr != NULL) inet_makeaddr_b (netAddr, hostAddr, pAddr); return (*pAddr); }/********************************************************************************* inet_netof - return the network number from an Internet address** This routine extracts the network portion of an Internet address.** EXAMPLE* The following example returns 0x5a:* .CS* inet_netof (0x5a000002);* .CE** RETURNS: The network portion of <inetAddress>.*/int inet_netof ( struct in_addr inetAddress /* inet address */ ) { register u_long i = ntohl ((u_long) inetAddress.s_addr); if (IN_CLASSA (i)) return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); else if (IN_CLASSB (i)) return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); else if (IN_CLASSC (i)) return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); else return (((i)&IN_CLASSD_NET) >> IN_CLASSD_NSHIFT); }/********************************************************************************* inet_netof_string - extract the network address in dot notation** This routine extracts the network Internet address from a host Internet* address (specified in dotted decimal notation). The routine handles * class A, B, and C network addresses. The buffer <netString> should * be INET_ADDR_LEN bytes long.** NOTE* This is the only routine in inetLib that handles subnet masks correctly.** EXAMPLE* The following example copies "90.0.0.0" to <netString>:* .CS* inet_netof_string ("90.0.0.2", netString);* .CE** RETURNS: N/A*/void inet_netof_string ( char *inetString, /* inet addr to extract local portion from */ char *netString /* net inet address to return */ ) { struct in_addr inaddrHost; struct in_addr inaddrNet; /* convert string to u_long */ inaddrHost.s_addr = inet_addr (inetString); inaddrNet.s_addr = htonl ((in_netof (inaddrHost))); /* convert network portion to dot notation */ inet_ntoa_b (inaddrNet, netString); }/********************************************************************************* inet_network - convert an Internet network number from string to address** This routine forms a network address from an ASCII string containing* an Internet network number.** EXAMPLE* The following example returns 0x5a:* .CS* inet_network ("90");* .CE** RETURNS: The Internet address for an ASCII string, or ERROR if invalid.*/u_long inet_network ( register char *inetString /* string version of inet addr */ ) { register u_long val, base, n; register char c; u_long parts[4], *pp = parts; register int i;again: val = 0; base = 10; if (*inetString == '0') base = 8, inetString++; if (*inetString == 'x' || *inetString == 'X') base = 16, inetString++; while ((c = *inetString)) { if (isdigit ((int) c)) { val = (val * base) + (c - '0'); inetString++; continue; } if (base == 16 && isxdigit ((int) c)) { val = (val << 4) + (c + 10 - (islower ((int) c) ? 'a' : 'A')); inetString++; continue; } break; } if (*inetString == '.') { if (pp >= parts + 4) { (void) errnoSet (S_inetLib_ILLEGAL_NETWORK_NUMBER); return (ERROR); } *pp++ = val, inetString++; goto again; } if (*inetString && !isspace ((int) *inetString)) { (void) errnoSet (S_inetLib_ILLEGAL_NETWORK_NUMBER); return (ERROR); } *pp++ = val; n = pp - parts; if (n > 4) { (void) errnoSet (S_inetLib_ILLEGAL_NETWORK_NUMBER); return (ERROR); } for (val = 0, i = 0; i < n; i++) { val <<= 8; val |= parts[i] & 0xff; } return (val); }/********************************************************************************* inet_ntoa_b - convert an network address to dot notation, store it in a buffer** This routine converts an Internet address in network format to dotted* decimal notation.** This routine is identical to the UNIX inet_ntoa() routine* except that you must provide a buffer of size INET_ADDR_LEN.** EXAMPLE* The following example copies the string "90.0.0.2" to <pString>:* .CS* struct in_addr iaddr;* ...* iaddr.s_addr = 0x5a000002;* ...* inet_ntoa_b (iaddr, pString);* .CE** RETURNS: N/A*/void inet_ntoa_b ( struct in_addr inetAddress, /* inet address */ char *pString /* where to return ASCII string */ ) { register char *p = (char *)&inetAddress;#define UC(b) (((int)b)&0xff) (void) sprintf (pString, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); }/********************************************************************************* inet_ntoa - convert a network address to dotted decimal notation** This routine converts an Internet address in network format to dotted* decimal notation.** WARNING* This routine is supplied for UNIX compatibility only. Each time this* routine is called, 18 bytes are allocated from memory. Use inet_ntoa_b()* instead.** EXAMPLE* The following example returns a pointer to the string "90.0.0.2":* .CS* struct in_addr iaddr;* ...* iaddr.s_addr = 0x5a000002;* ...* inet_ntoa (iaddr);* .CE** RETURNS: A pointer to the string version of an Internet address.** SEE ALSO: inet_ntoa_b()*/char *inet_ntoa ( struct in_addr inetAddress /* inet address */ ) { FAST char *buf = (char *) malloc (INET_ADDR_LEN); if (buf != NULL) inet_ntoa_b (inetAddress, buf); return (buf); }/********************************************************************************* inet_aton - convert a network address from dot notation, store in a structure** This routine interprets an Internet address. All the network library* routines call this routine to interpret entries in the data bases* that are expected to be an address. The value returned is stored in* network byte order in the structure provided.** EXAMPLE* The following example returns 0x5a000002 in the 's_addr' member of the * structure pointed to by <pinetAddr>:* .CS* inet_aton ("90.0.0.2", pinetAddr);* .CE** RETURNS: OK, or ERROR if address is invalid.*/STATUS inet_aton ( char * pString, /* string containing address, dot notation */ struct in_addr * inetAddress /* struct in which to store address */ ) { u_long rtnAddress; int oldError; /* * Since the conversion routine returns the equivalent of ERROR for * the string "255.255.255.255", this routine detects problems by * a change in the global error number value. Save the original * setting in case no error occurs. */ oldError = errno; errno = 0; rtnAddress = inet_addr (pString); if (errno != S_inetLib_ILLEGAL_INTERNET_ADDRESS) { inetAddress->s_addr = rtnAddress; /* Valid address, even 0xffffffff */ errno = oldError; return (OK); } return (ERROR); }#endif /* STANDALONE_AGENT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -