Вы находитесь на странице: 1из 30

Name and Address Conversions

Outline
Name and Address Conversions
Domain Name System
gethostbyname Function
gethostbyaddr Function
gethostname Function
getservbyname and getservbyport Functions

Domain Name System


The Domain Name System (DNS) is a hierarchical
distributed naming system for computers, services, or any
resource connected to the Internet or a private network.
The Domain Name System assigns domain names to
groups of Internet resources and users in a meaningful
way
Independent of each entity's physical location
Internet domain names are easier to remember than IP
addresses

Need For DNS


Getting the IP address of the remote endpoint is necessary for
every communication between TCP/IP applications
Humans are unable to memorize millions of IP addresses
(specially IPv6 addresses)
To a larger extent: the Domain Name System (DNS) provides
applications with several types of resources (domain name
servers, mail exchangers, reverse lookups, ) they need
DNS design
hierarchy
distribution
redundancy

Purpose of naming
Addresses are used to locate objects
Names are easier to remember than numbers
You would like to get to the address or other objects using a
name
DNS provides a mapping from names to resources of several
types

Domains
Domains are namespaces
Everything below .com is in the com domain.
Everything below ripe.net is in the ripe.net
domain and in the net domain.

com doma

net edu com

ripe

disi
www
ftp

www

net domain

ripe.net domain

google
isi sun tislabs
moon

ws2

ws1
6

Domain Name System


DNS is used primarily to map between hostnames and IP addresses.
Fully Qualified Domain Name FQDN Hostname
Entries in the DNS are known as resource records (RRs).
DNS uses Resource Records (RR )to store information about items
A

IP address of a host

32 bit integer

MX

Mail Exchange

priority, domain willing to accept email

CNAME

Canonical name

create aliases

PTR

Pointer

map IP addresses into host names

The DNS maps names into


data using Resource Records.

A & AAAA Resource Record


An A record maps a hostname into a 32-bit IPv4 address.
AAAA record, called a "quad A" record, maps a hostname into a 128-bit IPv6
address. The term "quad A" was chosen because a 128-bit address is four times
larger than a 32-bit address.

PTR Resource Record


pointer records map IP addresses into hostnames.
IPv4Address 4 bytes of the 32-bit address are reversed, each
byte is converted to its decimal ASCII value (0255), and inaddr.arpa is the appended. The resulting string is used in the
PTR query.
IPv6 Address the 32 4-bit nibbles of the 128-bit address are
reversed, each nibble is converted to its corresponding
hexadecimal ASCII value (09,af), and ip6.arpa is appended .

MX & CNAME Resource Record


MX record specifies a host to act as a "mail
exchanger" for the specified host.
CNAME stands for "canonical name." A
common use is to assign CNAME records
for common services, such as ftp and www.

Domain Name System


Resolver code can also be linkedited into applications when built

Application

Application
code
Function call

UDP request
Function return

Resolver
code

Local
name server

Other
name server

UDP reply

Resolver
configuration
files

/etc/resolv.conf (contains IP addresses of local name servers)

Typical arrangement of clients, resolvers, and name servers

Resolvers and Name Servers


Organizations run one or more name servers, often
the program known as BIND (Berkeley Internet
Name Domain).
Applications such as the clients and servers that
we are writing in this text contact a DNS server by
calling functions in a library known as the
resolver.
The common resolver functions are gethostbyname
and gethostbyaddr.

DNS Alternatives
Network Information System (NIS) or Lightweight
Directory Access Protocol (LDAP).
Solaris 2.x, HP-UX 10 and later, and FreeBSD 5.x and
later use the file /etc/nsswitch.conf, and AIX uses the
file /etc/netsvc.conf.
BIND 9.2.2 supplies its own version named the
Information Retrieval Service (IRS), which uses the file
/etc/irs.conf.
If a name server is to be used for hostname lookups, then
all these systems use the file /etc/resolv.conf to specify the
IP addresses of the name servers.

C:\Windows\System32\drivers\etc

gethostbyname Function

1/2

#include <netdb.h>
struct hostent *gethostbyname (const char *hostname);
Returns: non-null pointer if OK, NULL on error with h_errno set
struct hostent {
char
*h_name;
/* official (canonical) name of host */
char
**h_aliases;
/* pointer to array of 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 of ptrs with IPv4 addrs*/
};
official hostname \0
hostent{}
h_name
h_aliases
h_addrtype
h_length
h_addr_list

Alias #1 \0
Alias #2 \0

AF_INET

NULL

in_addr{}
in_addr{}
in_addr{}

NULL

IP addr #1
IP addr #2
IP addr #3
h_length =4

A host which has


2 aliases and 3
IP addresses

gethostbyname Function

2/3

#define h_addr h_addr_list[0] /* for backward compatibility */


struct hostent * hp = gethostbyname(argv[1]);
bcopy ( hp->h_addr, &server.sin_addr, hp->h_length);
//see intro/daytimetcpcli_hostname.c
Will only retrieve IPv4 addresses, performs a query for an A record
Some versions of gethostbyname will allow the following
hptr = gethostbyname (192.168.42.2); not portable

gethostbyname Function

3/3

If error, sets global integer h_errno to


HOST_NOT_FOUND
TRY_AGAIN
NO_RECOVERY
NO_DATA specified name valid but does
not have A records
These are included in netdb.h
Can use hstrerror function to get a description of the error (value of
h_errno)
See names/hostent.c for an example
Example Usage
>hostent ap1

>hostent cnn.com

>hostent www

Call gethostbyname and print returned


information

Execution
Next is a Web server with multiple IPv4 addresses.

Next is a name that is having a CNAME record.

To see the error strings returned by the hstrerror function, we first specify a non-existent
hostname, and then a name that has only an MX record.

gethostbyaddr Function
Takes a binary IPv4 address and tries to find the hostname
corresponding to that address
Performs a query for a PTR record
#include <netdb.h>
struct hostent *gethostbyaddr(const char *addr, socklen_t len, int
family);
Returns non-null pointer if OK, NULL on error with h_errno set

Field of interest in the returning structure is h_name (canonical host


name)
addr argument is not a char* but really a pointer to an in_addr
structure containing the IPv4 address

gethostname Function
Obtains the host name
#include <unistd.h>
int gethostname(char *name, size_t len);
// On success, zero is returned. On error, -1 is returned, and errno
is set appropriately
Example
#define MAXHOSTNAME 80
char ThisHost[80];
gethostname (ThisHost, MAXHOSTNAME);

getservbyname and getservbyport Functions 1/6


#include <netdb.h>
struct servent *getservbyname(const char *servname, const char
*protoname);
//returns non-null pointer if OK, NULL on error
struct servent *getservbyport(int port, const char *protoname);
//returns non-null pointer is OK, NULL on error
//port value must by in network byte order
struct servent {
char *s_name;
char **s_ aliases;
int s_port;
char *s_proto;
};

/* official service name */


/* aliases list*/
/* port number, network byte order */
/* protocol to use */

getservbyname and getservbyport Functions 2/6


struct servent *sptr;
sptr = getservbyname (domain,udp); //DNS using UDP
sptr = getservbyname (ftp,tcp); //FTP using TCP
sptr=getservbyname(ftp,udp); //this call will fail

getservbyname and getservbyport Functions 3/6


sptr = getsrvbyport(htons(21),tcp); // FTP using TCP
sptr = getsrvbyport(htons(21),NULL); // FTP using TCP
sptr = getsrvbyport(htons(21),udp); // This call will fail

daytime client uses gethostbyname and getservbyname 4/6

daytime client uses gethostbyname and getservbyname


5/6

Execution
If we run this program specifying one of our hosts that is running the daytime server

If we run the program to a multihomed system that is not running the daytime
server

Obsolete IPv6 Address Lookup Functions Ipv6 support in DNS

The RES_USE_INET6 Constant


gethostbyname doesn't have an argument to specify what address family is of
interest.
first revision of the API used the RES_USE_INET6 constant, which had to be
added to the resolver flags using a private, internal interface.
This API was not very portable since systems that used a different internal
resolver interface.
Enabling RES_USE_INET6 caused gethostbyname to look up AAAA records
first, and only look up A records .
Since the hostent structure only has one address length field, gethostbyname
could only return either IPv6 or IPv4 addresses, but not both.
Enabling RES_USE_INET6 also caused gethostbyname2 to return IPv4
addresses as IPv4-mapped IPv6 addresses.

gethostbyname2 Function
gethostbyname2 function adds an address
family argument to gethostbyname.
#include <sys/socket.h>
#include <netdb.h>
struct hostent *gethostbyname2 (const char *name, int af) ;
Returns: non-null pointer if OK, NULL on error with h_errno set

af argument is AF_INET, AF_INET, gethostbyname2


behaves just like gethostbyname returns only A
records for IPv4 addresses..
af argument is AF_INET6, AF_INET6,
gethostbyname2 looks up and returns only AAAA
records for IPv6 addresses.

getipnodebyname Function

1/2

The global nature of the RES_USE_INET6 flag and the


wish to provide more control over the returned
information.
#include <sys/socket.h>
#include <netdb.h>
struct hostent *getipnodebyname (const char *name, int af, int flags, int
*error_num) ;
Returns: non-null pointer if OK, NULL on error with error_num set

returns a pointer to the same hostent structure that


we described with gethostbyname.
af and flags arguments map directly to getaddrinfo's
hints.ai_family and hints.ai_flags arguments.

getipnodebyname Function

2/2

For thread safety, the return value is dynamically allocated,


so it must be freed with the freehostent function.
#include <netdb.h>
void freehostent (struct hostent *ptr) ;

Four types of network-related information

Вам также может понравиться