Name and Address Conversion Functions

30
Name and Address Conversions

description

Domain Name System System Calls

Transcript of Name and Address Conversion Functions

Page 1: Name and Address Conversion Functions

Name and Address Conversions

Page 2: Name and Address Conversion Functions

Outline

•Name and Address Conversions

Domain Name System

gethostbyname Function

gethostbyaddr Function

gethostname Function

getservbyname and getservbyport Functions

Page 3: Name and Address Conversion 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

Page 4: Name and Address Conversion Functions

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

Page 5: Name and Address Conversion Functions

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

Page 6: Name and Address Conversion Functions

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.

net domain

com domain

ripe.net domain

net com

ripe

www www

edu

isi tislabs

•disi

ws1ws2

• •

ftp

sun

moon

google

6

Page 7: Name and Address Conversion Functions

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.

Page 8: Name and Address Conversion Functions

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.

Page 9: Name and Address Conversion Functions

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 (0–255), and in-addr.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 (0–9,a–f), and ip6.arpa is appended.

Page 10: Name and Address Conversion Functions

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.

Page 11: Name and Address Conversion Functions

Domain Name System

Applicationcode

Resolvercode

Localname server

Othername server

Resolverconfiguration

files

Function call Function return

UDP request

UDP reply

Application

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

Typical arrangement of clients, resolvers, and name serversTypical arrangement of clients, resolvers, and name servers

Resolver code can also be link-edited into applications when built

Page 12: Name and Address Conversion Functions

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.

Page 13: Name and Address Conversion Functions

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

Page 14: Name and Address Conversion Functions

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 setstruct 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*/

}; hostent{}

h_nameh_aliasesh_addrtypeh_lengthh_addr_list

official hostname \0

NULL

NULL

Alias #1 \0

Alias #2 \0

IP addr #1

IP addr #2

IP addr #3h_length =4

in_addr{}

in_addr{}

in_addr{}

AF_INET

4

A host which has 2 aliases and 3 IP addresses

Page 15: Name and Address Conversion Functions

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

Page 16: Name and Address Conversion Functions

gethostbyname Function 3/3

•If error, sets global integer h_errno toHOST_NOT_FOUNDTRY_AGAINNO_RECOVERYNO_DATA specified name valid but does not have A recordsThese 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

Page 17: Name and Address Conversion Functions

Call gethostbyname and print returned information

Page 18: Name and Address Conversion Functions

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.

Page 19: Name and Address Conversion Functions

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

Page 20: Name and Address Conversion Functions

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 80char ThisHost[80]; gethostname (ThisHost, MAXHOSTNAME);

Page 21: Name and Address Conversion Functions

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; /* official service name */char **s_ aliases; /* aliases list*/int s_port; /* port number, network byte order */char *s_proto; /* protocol to use */

};

Page 22: Name and Address Conversion Functions

getservbyname and getservbyport Functions 2/6

struct servent *sptr;

sptr = getservbyname (“domain”,”udp”); //DNS using UDPsptr = getservbyname (“ftp”,”tcp”); //FTP using TCPsptr=getservbyname(“ftp”,”udp”); //this call will fail

Page 23: Name and Address Conversion Functions

getservbyname and getservbyport Functions 3/6

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

Page 24: Name and Address Conversion Functions

daytime client uses gethostbyname and getservbyname 4/6

Page 25: Name and Address Conversion Functions

daytime client uses gethostbyname and getservbyname 5/6

Page 26: Name and Address Conversion Functions

ExecutionIf 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

Page 27: Name and Address Conversion Functions

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.

Page 28: Name and Address Conversion Functions

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.

Page 29: Name and Address Conversion Functions

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.

Page 30: Name and Address Conversion Functions

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