Name and Address Conversion Functions
Name and Address Conversion Functions
Name and Address Conversion Functions
Outline
Name and Address Conversions
Domain Name System
gethostbyname Function
gethostbyaddr Function
gethostname Function
getservbyname and getservbyport 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
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
ripe
disi
www
ftp
www
net domain
ripe.net domain
google
isi sun tislabs
moon
ws2
ws1
6
IP address of a host
32 bit integer
MX
Mail Exchange
CNAME
Canonical name
create aliases
PTR
Pointer
Application
Application
code
Function call
UDP request
Function return
Resolver
code
Local
name server
Other
name server
UDP reply
Resolver
configuration
files
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
gethostbyname Function
2/3
gethostbyname Function
3/3
>hostent cnn.com
>hostent www
Execution
Next is a Web server with multiple IPv4 addresses.
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
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);
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
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
getipnodebyname Function
1/2
getipnodebyname Function
2/2