This section briefly shows the getaddrinfo(3)-based API for fetching DNS SRV records. It applies to RULI versions greater than 0.31.
Applications which use getaddrinfo(3) for DNS lookup can be easily extended to support SRV records by invoking RULI's getaddrinfo(3)-based API.
This document is supposed to provide introductory information about the getaddrinfo(3)-based API. Further details can be found in the getaddrinfo(3) manual page.
Put query hints into a struct addrinfo hints. Refer to getaddrinfo(3) manual page for details.
struct addrinfo hints; struct protoent *pe; char *protoname = "tcp"; if (!strcasecmp(protoname, "tcp")) hints.ai_socktype = SOCK_STREAM; else if (!strcasecmp(protoname, "udp")) hints.ai_socktype = SOCK_DGRAM; else { printf("bad-socket-type: %s\n", protoname); exit(1); } pe = getprotobyname(protoname); if (!pe) { printf("bad-protocol: %s\n", protoname); exit(1); } hints.ai_protocol = pe->p_proto; hints.ai_flags = AI_CANONNAME; hints.ai_family = PF_UNSPEC; hints.ai_addrlen = 0; hints.ai_addr = 0; hints.ai_canonname = 0; |
Use the following function to submit a query:
int ruli_getaddrinfo(const char *domain, const char *service, const struct addrinfo *hints, struct addrinfo **res);
Example:
struct addrinfo *ai_res; int result; char *domain = "domain.tld"; char *service = "ftp"; result = ruli_getaddrinfo(domain, service, &hints, &ai_res); if (result) { printf("getaddrinfo-failed: %s\n", gai_strerror(result)); exit(1); } |
Scan the result as in the following example:
struct addrinfo *ai; for (ai = ai_res; ai; ai = ai->ai_next) { switch (ai->ai_family) { case PF_INET: { struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr; printf(" canon=%s port=%d IPv4/%s\n", ai->ai_canonname, ntohs(sa->sin_port), inet_ntoa(sa->sin_addr)); } break; case PF_INET6: { struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->ai_addr; printf(" canon=%s port=%d IPv6/", ai->ai_canonname, ntohs(sa->sin6_port)); ruli_inet6_print(stdout, &sa->sin6_addr); printf("\n"); } break; default: assert(0); } } /* for: scan ai list */ |
Use ruli_freeaddrinfo() to delete the result.
void ruli_freeaddrinfo(struct addrinfo *res);
Example:
ruli_freeaddrinfo(ai_res); |
For a small example program which uses the ruli_getaddrinfo() API, please see the following file shipped with RULI source distribution:
sample/ruli-getaddrinfo.c
See below how to use such sample program in the command line:
$ echo _ftp._tcp.domain.tld | ./ruli-getaddrinfo _ftp._tcp.domain.tld canon=phantom.domain.tld. port=21 addresses=IPv4/1.2.3.4 _ftp._tcp.domain.tld canon=sitemail.host.tld. port=21 addresses=IPv6/1:2:3:4:5:6:7:8 $ |
In order to compile your own application, add the following header to the source file:
#include <ruli.h>
Then link your program against libruli, by using the following linker option:
-lruli