FreeBSD/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c
/**************************************************************
 * InetAddress
 **************************************************************
 * INET-ADDRESS-MIB, REVISION     "200502040000Z"
 * InetAddress ::= TEXTUAL-CONVENTION
 *   STATUS      current
 *   DESCRIPTION
 *       "Denotes a generic Internet address.
 *
 *        An InetAddress value is always interpreted within the context
 *        of an InetAddressType value.  Every usage of the InetAddress
 *        textual convention is required to specify the InetAddressType
 *        object that provides the context.  It is suggested that the
 *        InetAddressType object be logically registered before the
 *        object(s) that use the InetAddress textual convention, if
 *        they appear in the same logical row.
 *
 *        The value of an InetAddress object must always be
 *        consistent with the value of the associated InetAddressType
 *        object.  Attempts to set an InetAddress object to a value
 *        inconsistent with the associated InetAddressType
 *        must fail with an inconsistentValue error.
 *
 *        When this textual convention is used as the syntax of an
 *        index object, there may be issues with the limit of 128
 *        sub-identifiers specified in SMIv2, STD 58.  In this case,
 *        the object definition MUST include a 'SIZE' clause to
 *        limit the number of potential instance sub-identifiers;
 *        otherwise the applicable constraints MUST be stated in
 *        the appropriate conceptual row DESCRIPTION clauses, or
 *        in the surrounding documentation if there is no single
 *        DESCRIPTION clause that is appropriate."
 *   SYNTAX       OCTET STRING (SIZE (0..255))
 **************************************************************
 * TODO: FIXME!!! syrinx: Since we do not support checking the
 * consistency of a varbinding based on the value of a previous
 * one, try to guess the type of address based on the 
 * OctetString SIZE - 4 for IPv4, 16 for IPv6, others currently
 * not supported.
 */


An InetAddress value is always interpreted within the context
of an InetAddressType value.  Every usage of the InetAddress
textual convention is required to specify the InetAddressType
object that provides the context.  It is suggested that the
InetAddressType object be logically registered before the
object(s) that use the InetAddress textual convention, if
they appear in the same logical row.

The value of an InetAddress object must always be
consistent with the value of the associated InetAddressType
object.  Attempts to set an InetAddress object to a value
inconsistent with the associated InetAddressType
must fail with an inconsistentValue error.

When this textual convention is used as the syntax of an
index object, there may be issues with the limit of 128
sub-identifiers specified in SMIv2, STD 58.  In this case,
the object definition MUST include a 'SIZE' clause to
limit the number of potential instance sub-identifiers;
otherwise the applicable constraints MUST be stated in
the appropriate conceptual row DESCRIPTION clauses, or
in the surrounding documentation if there is no single
DESCRIPTION clause that is appropriate."
SYNTAX       OCTET STRING (SIZE (0..255))


An InetAddress value is always interpreted within the context of an InetAddressType value . Every usage of the InetAddress textual convention is required to specify the InetAddressType object that provides the context . It is suggested that the InetAddressType object be logically registered before the object ( s ) that use the InetAddress textual convention if they appear in the same logical row . The value of an InetAddress object must always be consistent with the value of the associated InetAddressType object . Attempts to set an InetAddress object to a value inconsistent with the associated InetAddressType must fail with an inconsistentValue error . When this textual convention is used as the syntax of an index object there may be issues with the limit of 128 sub - identifiers specified in SMIv2 STD 58 . In this case the object definition MUST include a ' SIZE ' clause to limit the number of potential instance sub - identifiers ; otherwise the applicable constraints MUST be stated in the appropriate conceptual row DESCRIPTION clauses or in the surrounding documentation if there is no single DESCRIPTION clause that is appropriate . " SYNTAX OCTET STRING ( SIZE ( 0 .. 255 ))
static char *
snmp_oct2inetaddr(uint32_t len, char *octets, char *buf)
{
	int af;
	void *ip;
	struct in_addr	ipv4;
	struct in6_addr	ipv6;
	if (len > MAX_OCTSTRING_LEN || octets == NULL || buf == NULL)
		return (NULL);
	switch (len) {
		case 4:
			memcpy(&ipv4.s_addr, octets, sizeof(ipv4.s_addr));
			af = AF_INET;
			ip = &ipv4;
			break;
		case 16:
			memcpy(ipv6.s6_addr, octets, sizeof(ipv6.s6_addr));
			af = AF_INET6;
			ip = &ipv6;
			break;
		default:
			return (NULL);
	}
	if (inet_ntop(af, ip, buf, SNMP_INADDRS_STRSZ) == NULL) {
		warnx("inet_ntop failed - %s", strerror(errno));
		return (NULL);
	}
	return (buf);
}
static char * snmp_oct2inetaddr ( uint32_t len , char * octets , char * buf ) { int af ; void * ip ; struct in_addr ipv4 ; struct in6_addr ipv6 ; if ( len > MAX_OCTSTRING_LEN || octets == NULL || buf == NULL ) return ( NULL ) ; switch ( len ) { case 4 : memcpy ( & ipv4 . s_addr , octets , sizeof ( ipv4 . s_addr ) ) ; af = AF_INET ; ip = & ipv4 ; break ; case 16 : memcpy ( ipv6 . s6_addr , octets , sizeof ( ipv6 . s6_addr ) ) ; af = AF_INET6 ; ip = & ipv6 ; break ; default : return ( NULL ) ; } if ( inet_ntop ( af , ip , buf , SNMP_INADDRS_STRSZ ) == NULL ) { warnx ( " " , strerror ( errno ) ) ; return ( NULL ) ; } return ( buf ) ; }
InetAddress
 INET-ADDRESS-MIB, REVISION     "200502040000Z"
 InetAddress ::= TEXTUAL-CONVENTION
   STATUS      current
   DESCRIPTION
       "Denotes a generic Internet address.
InetAddress INET - ADDRESS - MIB REVISION " 200502040000Z " InetAddress :: = TEXTUAL - CONVENTION STATUS current DESCRIPTION " Denotes a generic Internet address .
/* XXX: FIXME - IPv4*/ /* XXX: FIXME - IPv4*/