Chapter9 Elementary Name and Address Conversions.

10
Chapter9 Elementary Name and Address Conversions

Transcript of Chapter9 Elementary Name and Address Conversions.

Page 1: Chapter9 Elementary Name and Address Conversions.

Chapter9

Elementary Name and Address Conversions

Page 2: Chapter9 Elementary Name and Address Conversions.

IntroductionIntroduction Domain Name Server gethostbyname Function. gethostbyaddr Function. uname Function. gethostname Function.

getsvrbyname & getsvrbyport Function.

Page 3: Chapter9 Elementary Name and Address Conversions.

Domain Name SystemDomain Name System

FQDN(Fully qualified domain name) ex) comedu.chungbuk.ac.kr.

RRs(Resource Records)Types Description Exam

A Maps hostname into 32-bit IPv4 203.255.74.123AAAA Maps hostname into 128-bit IPv6 pp.238PTR Map IP addresses into hostnames

For An IPv4 address the 4bytes of32bit address are reversed (Dec.) IPv6 reversed sequence. (Hex)

123.74.255.203.in-addr.arpaIPv6 pp.238

MX Priority of mail serviceCNAME Service name map into server

namewww and ftp

Page 4: Chapter9 Elementary Name and Address Conversions.

Resolvers and Name ServersResolvers and Name Servers

Applicationcode

Resolvercode

Localname server

Othername server

Resolverconfiguration

files

Function call Function return

UDP request

UDP reply

Application

/etc/resolv.conf

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

Page 5: Chapter9 Elementary Name and Address Conversions.

gethostbyname Function#include <netdb.h>

struct hostent *gethostbyname (const char *hostname);

struct hostent {char *h_name; /* official (canonical) name of host */char **h_aliases; /* pointer to arrayof opinters to alises names */int h_addrtype; /* host address type : AF_INET or AF_INET6 */int h_length; /* length of address : 4 or 6 */char **h_addr_list; /* ptr to array of ptrs with IPv4 or IPv6 addrs */

};

hostent{}

h_name

h_aliases

h_addrtype

h_length

h_addr_list

official hostname \0

NULL

NULL

Alises #1 \0

Alises #2 \0

IP addr #1

IP addr #2

IP addr #3h_length =4

In_addr{}

In_addr{}

In_addr{}

AF_INET

4

Page 6: Chapter9 Elementary Name and Address Conversions.

RES_USE_INET6 Resolver Option

1. #include <resolv.h> res_init(); _res.option |= RES_USE_INET6;

2. export RES_OPTION=inet6; (shell once) (in .profile One User Only)

3. options inet6 (in /etc/resolv.conf) if all program support IPv6

Page 7: Chapter9 Elementary Name and Address Conversions.

#include <netdb.h>

struct hostent *gethostbyname2 (const char *hostname, int family);

gethostbyname2 Function

RES_USE_INET6 optionoff on

gethostbyname(host)

Search for A records. Iffound, return IPv4addresses( h_length=4 ).Else error.

Search for AAAA records. Iffound, return IPv6addresses(h_length=16).Else Search for A records. Iffound, return IPv4-mapped IPv6addresses(h_length=16).Else error.

Gethostbyname2(host,AF_INET)

Search for A records. Iffound, return IPv4addresses( h_length=4 ).Else error.

Search for A records. If found,return IPv4-mapped IPv6addresses(h_length=16).Else error.

Gethostbyname2(host,AF_INET6)

Search for AAAA records.If found, return IPv4addresses(h_length=16).Else error.

Search for AAAA records. Iffound, return IPv6addresses(h_length=16).Else error.

Page 8: Chapter9 Elementary Name and Address Conversions.

gethostbyaddr Function

#include <netdb.h>struct hostent *gethostbyaddr(const chr *addr, size_t len,int family);

The field of interest in this structure is normally h_name, the canonical hostname

Page 9: Chapter9 Elementary Name and Address Conversions.

uname Function#include <netdb.h>

int uname(struct utsname *name);#define _UTS_NAMESIZE 16#define _UTS_NODESIZE 256

struct ntsname {char sysname[_UTS_NAMESIZE]; /* name of this operationg system */char sysname[_UTS_NAMESIZE]; /* name of this node */char sysname[_UTS_NAMESIZE]; /* O. S. release level */char sysname[_UTS_NAMESIZE]; /* O. S. version level */char sysname[_UTS_NAMESIZE]; /* hardware type */

};

gethostname Function#include <netdb.h>

int gethostname(char *name, size_t namelen);

Page 10: Chapter9 Elementary Name and Address Conversions.

getservbyname Function#include <netdb.h>

struct servent *getservbyname(const char *servname, const char *protoname);

struct servent {char *s_name; /* official service name */char **s_ aliases; /* aliases list*/int s_port; /* port number, network by order */char *s_proto; /* protocol to use */

};

getservbyport Function#include <netdb.h>

struct servent *getservbyport(int port, const char *protoname);