UNP Summary Chapter 11 names and address translation

Source: Internet
Author: User
Tags mx record htons fully qualified domain name

This chapter describes functions for converting between names and numeric addresses: GetHostByName and gethostbyaddr are converted between host names and IP addresses , and Getservbyname and Getservbyport are Convert between the server name and the port number .

1. Domain Name System

Domain name System,dns is used primarily for mapping between host names and IP addresses. Host names can be simple names, such as Solaris or BSDI, or fully qualified domain name FQDN (fully qualified domain name), such as solaris.kohala.com

1). Resource records

The entries in DNS are called resource record RRs (resource records) and are generally interested in the following: A a record maps the host name to a 32-bit IPV4 address. The AAAA AAAA record maps the host name to a 128-bit IPV6 address. A ptr PTR record, called a pointer record, maps an IP address to a host name. For the IPV4 address, the four-byte order of the 32-bit address is reversed, each byte is converted to his decimal ASCII value (0~255), and then the In-addr.arpa is appended, and the result string is used for the PTR query. For IPV6 addresses, 32 4-bit groups in 128-bit addresses are sent sequentially, each group is converted to the corresponding hexadecimal ASCII value (0~9, a~f), with ip6.int attached. The MX MX record specifies a host as a "mail exchanger" for a host. CNAME CNAME represents "canonical name", a common use of which is to assign a CNAME record to common services such as FTP and www.

2). Parser and Name server

Organizations run one or more name servers (name server), which are typically called bind (Berkeley Internet name Domain) programs. Various applications, such as the client and server programs we write in this book, contact the DNS server by calling a function in a library called a parser (resolver). The most common parser functions are gethostbyname and gethostbyaddr.

2.gethostbyname function

The most basic function for locating a hostname is gethostbyname, and if the call succeeds, it returns a structure pointer to the hostent that contains all the IPV4 addresses of the host you are looking for, and this function is limited to returning only the IPV4 address.

#include <netdb.h>
 
struct hostent *gethostbyname (const char *hostname);
Return: If success is a non-empty pointer, error is null set H_error

The null pointer returned by this function points to the following hostent structure

struct Hostent {
   char  *h_name;       /* Official (canonical) Name of host */
   char **h_aliases;    /* Pointer to array of pointers to alias names    /int h_addrtype;   /* Host address type:af_inet
   *    /int h_length;     /* Length of Address:4 * *
   char **h_addr_list;  /* ptr to array ' ptrs with IPv4 Addrs
/};

According to DNS, gethostbyname executes a record query for a. It can only return IPV4 address

The hostent structure is shown below

GetHostByName is different from the other sets of interface functions we are introducing: When an error occurs, he does not set errno, but instead sets the global integer h_errno to one of the following constant values defined in header file <netdb.h>: host_not_ FOUND try_again no_recoverty no_data (equivalent to no_address)

3.gethostbyaddr function

The GETHOSTBYADDR function attempts to have a binary IP address to locate the corresponding hostname, just as opposed to the gethostbyname function behavior

#include <netdb.h>
 
struct hostent *gethostbyaddr (const char *ADDR, socklen_t len, int family);
Return: Success is null pointer, error is null and set H_errno

This function returns a pointer to the hostent structure as described earlier.

The parameter addr is not actually a char * type, but a pointer to a IN_ADDR structure that holds the IPV4 address; The Len parameter is the size of this structure: the region IPV4 address is 4,family parameter is af_inet

4.getservbyname and Getservbyport functions

1. Like a host, a service is often recognized by its name, and the Getservbyname function is used to find the corresponding service based on the given name, that is, to return related service information corresponding to the given service name and protocol name (for example: port number)

#include <netdb.h>

struct servent * getservbyname (const char * servname, const char * protoname);
 Back: Non-null pointer-successful, NULL pointer-error

The null pointer returned by this function points to the following servent structure

struct Servent {
  char   *s_name;      /* Official service Name
  *  /char **s_aliases;   /* Alias list
  *     /int s-port;      /* port number, network-byte order * *
  char   *s_proto;     * Protocol to use *
/};

The service name ServName must be specified, and if a protocol is also specified (that is, Protoname is a non-empty pointer), the resulting table entry must also have a matching protocol.

The primary field we care about in the servent structure is the port number. Since the port number is returned in network byte order, it must not be called htons when it is stored in the socket address structure, and a typical call to this function is:

struct servent *sptr;

Sptr = getservbyname ("Domain", "UDP"); /* DNS using UDP *
/sptr = Getservbyname ("FTP", "TCP");    /* FTP using TCP
/sptr = Getservbyname ("ftp", NULL);     /* FTP using TCP
/sptr = Getservbyname ("ftp", "UDP");    /* This call would fail * *


2. Function Getservbyport for a given port number and an optional protocol to find the appropriate service

#include <netdb.h>
 
struct servent *getservbyport (int port, const char *protoname);
Return: Successful non-empty pointer, error NULL

The value of the port parameter must be a network byte order, and the typical invocation of this function is as follows:

struct servent *sptr;

Sptr = Getservbyport (htons (), "UDP"); /* DNS using UDP
/sptr = Getservbyport (htons), "TCP";/* FTP using TCP
/sptr = Getservbyport (htons (21) , NULL);  /* FTP using TCP
/sptr = Getservbyport (htons), "UDP";/* This call would fail * *

For UDP, the last call fails because no service uses port 21.

5.getaddrinfo function

The Getaddrinfo function handles the conversion of the first name to the address and the service to the port, and returns a SOCKADDR structure instead of an address list, which can then be used directly by the socket function

#include <netdb.h>
 
int getaddrinfo (const char *hostname, const char *service, const struct addrinfo *hints, str UCT addrinfo **result);
 Return: If success is 0, error is not 0 (see figure 11-7 below)

The hostname parameter is a host name or address string, and the service parameter is a name or decimal port string.

This function returns a pointer to the ADDRINFO structure list by using the result pointer parameter, while the ADDRINFO structure is defined in the header file <netdb.h>

struct Addrinfo {
   int          ai_flags;           /* ai_passive, ai_canonname
   *          /int ai_family;          /* af_xxx
   *          /int ai_socktype;        /* sock_xxx
   *          /int ai_protocol;        /* 0 or Ipproto_xxx for IPv4 and IPv6 *
   socklen_t    Ai_addrlen;         /* Length of AI_ADDR * *
   char        *ai_canonname;       /* ptr to canonical name for host *
   /struct sockaddr    *ai_addr;     /* PTR to SOCKET address structure * *
   struct addrinfo    *ai_next;     /* ptr to next structure in linked list *
/};

Similarly, the parameter hints of the Getaddrinfo function is also of type. A null pointer to the hints parameter can also point to a pointer to a ADDRINFO structure in which the caller can fill in a hint about the type of information expected to return. For example, if the specified service supports both TCP and UDP, the caller can set the Ai_socktype member in the hints structure to sock_dgram so that only information that is appropriate for the datagram socket interface is returned.

The members that the caller can set in the hints structure are: ai_flags (0 or more or together ai_xxx values) ai_family (a af_xxx value) Ai_socktype (a sock_xxx value) ai_protocol

The flag values available to Ai_flags members and their meanings, for example, have ai_passive (sockets will be used for passive opening), Ai_canonname (Tell the Getaddrinfo function to return the canonical name of the host), and so on.

If this function returns successfully, the variable pointed to by the result parameter has been filled in with a pointer to the ADDRINFO structure linked by the Ai_next member. The following 2 cases can result in the return of multiple addrinfo structures: If there are multiple addresses associated with the hostname parameter, each address that applies to the requested address cluster returns a corresponding structure. If the service parameter specifies that services support multiple sets of interface types, each set of interface types may return a corresponding structure, depending on the ai_socktype member of the hints structure.

We must first assign a hints structure, clear it 0 after filling in the required fields, then call getaddrinfo and then iterate through a list of each return address.

Instance Program:

struct Addrinfo          hints, *res;

Bzero (&hints, sizeof (hints));
Hints.ai_flags = Ai_canonname;
hints.ai_family = af_inet;

Getaddrinfo ("Freebsd4", "Domain", &hints, &res);

Return to the following figure:

--about getaddrinfo if used for IPV6, see UNP

6.gai_strerror function

The following figure gives the name and meaning of the getaddrinfo return non 0 error value

Gai_strerror, with these values as its arguments, returns a pointer to the corresponding error message string.

#include <netdb.h>
 
const char *gai_strerror (int error);
Returns a pointer to the error description message string

7.freeaddrinfo function

All storage space returned by function getaddrinfo is returned to the system by calling Freeaddrinfo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.