- Forward


Using the Domaing Name System
with Examples in C


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • The Domain Name System (DNS):
    • A distributed database that allows users to map human readable names to IP addresses
  • Interaction with the DNS:
    • Handled by a resolver that first consults a local DNS server and then, if necessary, consults foreign DNS servers
  • Domain Names:
    • Are hierarchical
    • Portions are separated by '.'
    • Left portion is most specific, right portion is DNS root
Review (cont.)
Back SMYC Forward
  • Byte Order:
    • Network byte order is big-endian (i.e., most-significant-byte first)
  • Addresses:
    • There are structures for both IPv4 and IPv6 addresses
DNS Forward Resolution: getaddrinfo() getaddrinfo
Back SMYC Forward
int getaddrinfo(const char *host, const char *service, const struct addrinfo *hints, struct addrinfo **first)
Purpose:
Get the IP addresses for a given host (and service) name
Details:
host The host name
service The service name (or port number)
hints Further criteria to use
first The first IP address (in a linked structure)
Return 0 on success; non-zero on error
#define _POSIX_C_SOURCE >= 201112L
#include <netdb.h> netdb.h
#include <sys/socket.h> sys/socket.h
#include <sys/types.h> sys/types.h
DNS Forward Resolution (cont.)
Back SMYC Forward
struct addrinfo { int ai_flags; int ai_family; // AF_INET, AF_INET6 int ai_socktype; // SOCK_STREAM, SOCK_DGRAM int ai_protocol; // Protocol (name or number) socklen_t ai_addrlen; // sizeof(si_addr) struct sockaddr *ai_addr; // Socket address char *ai_canonname; // Canonical name of host (or NULL) struct addrinfo *ai_next; // Pointer to next struct in linked structure };

Note: ai_canonname will be NULL for all elements in the linked structure after the first.

DNS Forward Resolution (cont.): freeaddrinfo() freeaddrinfo
Back SMYC Forward
void freeaddrinfo(struct addrinfo *first)
Purpose:
Free the memory allocated by a call to getaddrinfo()
Details:
first The first element in the linked structure
#include <netdb.h> netdb.h
#include <sys/socket.h> sys/socket.h
DNS Forward Resolution (cont.)
Back SMYC Forward
unixexamples/dns/get_address.c
 
DNS Reverse Resolution: getnameinfo() getnameinfo
Back SMYC Forward
int getnameinfo(const struct sockaddr *address, socklen_t addrlen, char *host, socklen_t hostlen, char* service, socklen servlen, int flags)
Purpose:
Get the host name (and service) for a given IP address
Details:
address The IP address of interest
addrlen The length of the IP address
host The corresponding host nam
hostlen The maximum length of a host name
service The corresponding service
servlen The maximum length of a service name
flags A bit mask of flags
Return 0 on success; non-zero on error
#define _GNU_SOURCE
#define _POSIX_C_SOURCE >= 201112L
#include <netdb.h> netdb.h
#include <sys/socket.h> sys/socket.h
DNS Forward Resolution (cont.)
Back SMYC Forward
unixexamples/dns/get_name.c
 
There's Always More to Learn
Back -