Vous êtes sur la page 1sur 18

Elementary Name and Address

Conversions
Domain name system
gethostbyname Function
gethostbyname2 Function and IPv6
support
gethostbyaddr Function
uname and gethostname Functions
getservbyname and getservbyport
Functions
Other networking information
1

Introduction DNS
1. What is
the IP address
of udel.edu ?
It is
128.175.13.92
1. What is
the host name
of
128.175.13.74
It is
strauss.udel.edu
2

DNS
Components

Resolvers:
Client programs that extract information from
Name Servers.

Name Servers:
Server programs which hold information about
the structure and the names.

Resolve
rs
A Resolver maps a name to an address and
vice versa.

Query
Response

Resolver

Name Server

Iterative
Resolution
a.root
server
a3.nstl
d.com

a.gtldserver

udel
server

ns1.goo
gle.com

7
iterative response (referral)
I don't know. Try a.root-servers.net.
iterative response (referral)
9
I don't know. Try a.gtld-servers.net.
1 iterative response (referral)
iterative response (referral)
I don't know. Try a3.nstld.com.
2 4
I don't know. Try ns1.google.com.
6
iterative response
8
The IP address of www.google.com
is 216.239.37.99.
client 10
iterative request
What is the IP address of
5
www.google.com?
3

Recursive
Resolution
root
server
edu
server
udel
serve
r

com

3
8

server

google
server

9
1
10
client

recursive request
What is the IP address of
www.google.com?
recursive response
The IP address of www.google.com is
216.239.37.99.

Domain Name System


Entries in DNS: resource records (RRs) for a host

e.g.

A record: maps a hostname to a 32-bit IPv4 addr


AAAA (quad A) record: maps to a 128-bit IPv6 addr
PTR record: maps IP addr to hostname
MX record: specifies a mail exchanger of the host
CNAME record: assigns canonical name for common
services
solaris IN
IN
IN
IN
IN
www IN

A
206.62.226.33
AAAA 5f1b:df00:ce3e:e200:0020:0800:2078:e3e3
MX
5 solaris.kohala.com
MX
10 mailhost.kohala.com
PTR
33.226.62.206.in-addr.arpa
CNAME bsdi.kohala.com
7

DNS: Application, Resolver, Name Serv


application
application
code
UDP
call
return
request
resolver
local
code
name
UDP server
reply
resolver
configuration
files

other
name
servers

resolver functions:
gethostbyname/gethostbyaddr
name server: BIND
(Berkeley Internet Name Domain)
static hosts files (DNS alternatives):
/etc/hosts
resolver configuration file (specifies name serve
/etc/resolv.conf
8

gethostbyname Function
performs a DNS query for an A record or a AAAA record

#include <netdb.h>
struct hostent *gethostbyname (const char *hostname);
returns: nonnull pointer if OK, NULL on error with h_err
struct hostent {
char *h_name;
/* official (canonical) name of host *
char **h_aliases; /* ptr to array of ptrs to alias names
/ *A list of aliases that can be accessed with arrays*/
int
h_addrtype; /* host addr type: AF_INET or AF_INE
int
h_length;
/* length of address: 4 or 16 */
char **h_addr_list;
/* ptr to array of ptrs with IPv
};
#define h_addr h_addr_list[0] /* first address in list */

hostent Structure Returned by gethostbyn


hostent { }
h_name
h_aliases
h_addrtype AF_INET/6
h_length
4/16
h_addr_list

canonical hostname \0
alias #1 \0
NULL

NULL

alias #2 \0
in/6_addr { }
IP addr #1
in/6_addr { }
IP addr #2
in/6_addr { }
IP addr #3

10

Call gethostbyname and Print Returned In


(See program in Figure 9.4)

11

gethostbyname2 Function and IPv6 Sup

#include <netdb.h>
struct hostent *gethostbyname2 (const char *hostname, int family
returns: nonnull pointer if oK, NULL on error with h_errno se

gethostbyname
(host)
gethostbyname2
(host, AF_INET)
gethostbyname2
(host, AF_INET6)

RES_USE_INET6 option
of
on
A record
AAAA record
or A record returning
IPv4-mapped IPv6 addr
A record
A record returning
IPv4-mapped IPv6 addr
AAAA record AAAA record

13

gethostbyaddr Function
binary IP address to hostent structure

#include <netdb.h>
struct hostent *gethostbyaddr (const char *addr, size_t len
returns: nonnull pointer if OK, NULL on error with h_e

addr argument: a pointer to an in_addr or in6_add


h_name in hostent: canonical hostname
gethostbyaddr: queries a DNS name server for a P
in the in-addr.arpa domain for IPv4 or a PTR
in the ip6.int domain for IPv6.

14

uname and gethostname Functions

#include <sys/utsname.h>
Thegethostname() function shall return the standard host nam
the current
machine.
int uname (struct utsname *name);
returns: nonnegative value if OK, -1 on error
struct utsname {
char sysname[_UTS_NAMESIZE]; /* name of OS */
char nodename[_UTS_NODESIZE]; /* name of this node
char release[_UTS_NAMESIZE]; /* OS release level */
char version[_UTS_NAMESIZE]; /* OS version level */
char machine[_UTS_NAMESIZE]; /* hardware type */
#include <unistd.h>
int gethostname (char *name, size_t namelen);
returns: 0 if OK, -1 on error

15

getservbyname and getservbyport Fun

#include <netdb.h>
struct servent *getservbyname (const char *servname, const
returns: nonnull pointer if OK, NULL on error
struct servent *getservbyport (int port, const char *protonam
returns: nonnull pointer if OK, NULL on error
struct servent {
char *s_name;
/* official service name */
char **s_aliases; /*alias list */
int
s_port;
/* port number, network-byte
char *s_proto;
/* protocol, TCP or UDP, to use */

Mapping from name to port number: in /etc/services


Services that support multiple protocols often use the same T
port number. But its not always true:
shell 514/tcp
syslog514/udp
16

17

daytime Client

using gethostbyname and


getservbyname
(see Figure 9.8)
Call gethostbyname and getservbyname
Try each server address
Call connect
Check for failure
Read servers reply
18

Other Networking Info


Four types of info:
hosts (gethostbyname, gethostbyaddr)
through DNS or /etc/hosts, hostent structure

networks (getnetbyname, getnetbyaddr)


through DNS or /etc/networks, netent structure

protocols (getprotobyname, getprotobynumber)


through /etc/protocols, protoent structure

services (getservbyname, getservbyport)


through /etc/servies, servent structure
19