/*++ /* NAME /* myaddrinfo 3 /* SUMMARY /* addrinfo encapsulation and emulation /* SYNOPSIS /* #include <myaddrinfo.h> /* /* #define MAI_V4ADDR_BITS ... /* #define MAI_V6ADDR_BITS ... /* #define MAI_V4ADDR_BYTES ... /* #define MAI_V6ADDR_BYTES ... /* /* typedef struct { char buf[....]; } MAI_HOSTNAME_STR; /* typedef struct { char buf[....]; } MAI_HOSTADDR_STR; /* typedef struct { char buf[....]; } MAI_SERVNAME_STR; /* typedef struct { char buf[....]; } MAI_SERVPORT_STR; /* /* int hostname_to_sockaddr(hostname, service, socktype, result) /* const char *hostname; /* const char *service; /* int socktype; /* struct addrinfo **result; /* /* int hostname_to_sockaddr_pf(hostname, pf, service, socktype, result) /* const char *hostname; /* int pf; /* const char *service; /* int socktype; /* struct addrinfo **result; /* /* int hostaddr_to_sockaddr(hostaddr, service, socktype, result) /* const char *hostaddr; /* const char *service; /* int socktype; /* struct addrinfo **result; /* /* int sockaddr_to_hostaddr(sa, salen, hostaddr, portnum, socktype) /* const struct sockaddr *sa; /* SOCKADDR_SIZE salen; /* MAI_HOSTADDR_STR *hostaddr; /* MAI_SERVPORT_STR *portnum; /* int socktype; /* /* int sockaddr_to_hostname(sa, salen, hostname, service, socktype) /* const struct sockaddr *sa; /* SOCKADDR_SIZE salen; /* MAI_HOSTNAME_STR *hostname; /* MAI_SERVNAME_STR *service; /* int socktype; /* /* const char *MAI_STRERROR(error) /* int error; /* DESCRIPTION /* This module provides a simplified user interface to the /* getaddrinfo(3) and getnameinfo(3) routines (which provide /* a unified interface to manipulate IPv4 and IPv6 socket /* address structures). /* /* On systems without getaddrinfo(3) and getnameinfo(3) support, /* emulation for IPv4 only can be enabled by defining /* EMULATE_IPV4_ADDRINFO. /* /* hostname_to_sockaddr() looks up the binary addresses for /* the specified symbolic hostname or numeric address. The /* result should be destroyed with freeaddrinfo(). A null host /* pointer converts to the null host address. /* /* hostname_to_sockaddr_pf() is an extended interface that /* provides a protocol family override. /* /* hostaddr_to_sockaddr() converts a printable network address /* into the corresponding binary form. The result should be /* destroyed with freeaddrinfo(). A null host pointer converts /* to the null host address. /* /* sockaddr_to_hostaddr() converts a binary network address /* into printable form. The result buffers should be large /* enough to hold the printable address or port including the /* null terminator. /* This function strips off the IPv6 datalink suffix. /* /* sockaddr_to_hostname() converts a binary network address /* into a hostname or service. The result buffer should be /* large enough to hold the hostname or service including the /* null terminator. This routine rejects malformed hostnames /* or numeric hostnames and pretends that the lookup failed. /* /* MAI_STRERROR() is an unsafe macro (it evaluates the argument /* multiple times) that invokes strerror() or gai_strerror() /* as appropriate. /* /* This module exports the following constants that should be /* user for storage allocation of name or address information: /* .IP MAI_V4ADDR_BITS /* .IP MAI_V6ADDR_BITS /* .IP MAI_V4ADDR_BYTES /* .IP MAI_V6ADDR_BYTES /* The number of bits or bytes needed to store a binary /* IPv4 or IPv6 network address. /* .PP /* The types MAI_HOST{NAME,ADDR}_STR and MAI_SERV{NAME,PORT}_STR /* implement buffers for the storage of the string representations /* of symbolic or numerical hosts or services. Do not use /* buffer types other than the ones that are expected here, /* or things will blow up with buffer overflow problems. /* /* Arguments: /* .IP hostname /* On input to hostname_to_sockaddr(), a numeric or symbolic /* hostname, or a null pointer (meaning the wild-card listen /* address). On output from sockaddr_to_hostname(), storage /* for the result hostname, or a null pointer. /* .IP pf /* Protocol type: PF_UNSPEC (meaning: use any protocol that is /* available), PF_INET, or PF_INET6. This argument is ignored /* in EMULATE_IPV4_ADDRINFO mode. /* .IP hostaddr /* On input to hostaddr_to_sockaddr(), a numeric hostname, /* or a null pointer (meaning the wild-card listen address). /* On output from sockaddr_to_hostaddr(), storage for the /* result hostaddress, or a null pointer. /* .IP service /* On input to hostname/addr_to_sockaddr(), a numeric or /* symbolic service name, or a null pointer in which case the /* socktype argument is ignored. On output from /* sockaddr_to_hostname/addr(), storage for the result service /* name, or a null pointer. /* .IP portnum /* Storage for the result service port number, or a null pointer. /* .IP socktype /* Socket type: SOCK_STREAM, SOCK_DGRAM, etc. This argument is /* ignored when no service or port are specified. /* .IP sa /* Protocol-independent socket address structure. /* .IP salen /* Protocol-dependent socket address structure size in bytes. /* SEE ALSO /* getaddrinfo(3), getnameinfo(3), freeaddrinfo(3), gai_strerror(3) /* DIAGNOSTICS /* All routines either return 0 upon success, or an error code /* that is compatible with gai_strerror(). /* /* On systems where addrinfo support is emulated by Postfix, /* some out-of-memory errors are not reported to the caller, /* but are handled by mymalloc(). /* BUGS /* The IPv4-only emulation code does not support requests that /* specify a service but no socket type. It returns an error /* indication, instead of enumerating all the possible answers. /* /* The hostname/addr_to_sockaddr() routines should accept a /* list of address families that the caller is interested in, /* and they should return only information of those types. /* /* Unfortunately, it is not possible to remove unwanted address /* family results from hostname_to_sockaddr(), because we /* don't know how the system library routine getaddrinfo() /* allocates memory. For example, getaddrinfo() could save /* space by referencing the same string object from multiple /* addrinfo structures; or it could allocate a string object /* and the addrinfo structure as one memory block. /* /* We could get around this by copying getaddrinfo() results /* to our own private data structures, but that would only /* make an already expensive API even more expensive. /* /* A better workaround is to return a vector of addrinfo /* pointers to the elements that contain only the elements /* that the caller is interested in. The pointer to the /* original getaddrinfo() result can be hidden at the end /* after the null terminator, or before the first element. /* LICENSE /* .ad /* .fi /* The Secure Mailer license must be distributed with this software. /* AUTHOR(S) /* Wietse Venema /* IBM T.J. Watson Research /* P.O. Box 704 /* Yorktown Heights, NY 10598, USA /* /* Wietse Venema /* Google, Inc. /* 111 8th Avenue /* New York, NY 10011, USA
/*--*/
/* *Artifactofimplementation:tolerateanullpointerargument.
*/ for (ap = ai; ap != 0; ap = next) {
next = ap->ai_next; if (ap->ai_canonname)
myfree(ap->ai_canonname); /* ap->ai_addr is allocated within this memory block */
myfree((void *) ap);
}
}
staticchar *ai_errlist[] = { "Success", "Address family for hostname not supported", /* EAI_ADDRFAMILY */ "Temporary failure in name resolution", /* EAI_AGAIN */ "Invalid value for ai_flags", /* EAI_BADFLAGS */ "Non-recoverable failure in name resolution", /* EAI_FAIL */ "ai_family not supported", /* EAI_FAMILY */ "Memory allocation failure", /* EAI_MEMORY */ "No address associated with hostname", /* EAI_NODATA */ "hostname nor servname provided, or not known", /* EAI_NONAME */ "service name not supported for ai_socktype", /* EAI_SERVICE */ "ai_socktype not supported", /* EAI_SOCKTYPE */ "System error returned in errno", /* EAI_SYSTEM */ "Invalid value for hints", /* EAI_BADHINTS */ "Resolved protocol is unknown", /* EAI_PROTOCOL */ "Unknown error", /* EAI_MAX */
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.