The network configuration can be kept in a number of places, like static files(/etc/hosts, /etc/services etc.), or can be managed by a name service, such as DNS(Domain Name System) or NIS(Network Information Service).
1) host
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Description of data base entry for a single host. */
struct hostent
{
char *h_name; /* Official name of host. */
char **h_aliases; /* Alias list. */
int h_addrtype; /* Host address type. */
int h_length; /* Length of address. */
char **h_addr_list; /* List of addresses from name server. */
#if defined __USE_MISC || defined __USE_GNU
# define h_addr h_addr_list[0] /* Address, for backward compatibility.*/
#endif
};
/* Open host data base files and mark them as staying open even after
a later search if STAY_OPEN is non-zero.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void sethostent (int __stay_open);
/* Close host data base files and clear `stay open' flag.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void endhostent (void);
/* Get next entry from host data base file. Open data base if
necessary.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct hostent *gethostent (void);
/* Return entry from host data base which address match ADDR with
length LEN and type TYPE.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct hostent *gethostbyaddr (const void *__addr, __socklen_t __len,
int __type);
/* Return entry from host data base for host with NAME.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct hostent *gethostbyname (const char *__name);
......
gethostent: return the next entry in the host database file.
sethostent: open the file or rewind it if it is already open
endhostent: close the file
2) networks
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Open network data base files and mark them as staying open even
after a later search if STAY_OPEN is non-zero.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void setnetent (int __stay_open);
/* Close network data base files and clear `stay open' flag.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void endnetent (void);
/* Get next entry from network data base file. Open data base if
necessary.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct netent *getnetent (void);
/* Return entry from network data base which address match NET and
type TYPE.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct netent *getnetbyaddr (uint32_t __net, int __type);
/* Return entry from network data base for network with NAME.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct netent *getnetbyname (const char *__name);
......
ubuntu@ip-172-31-23-227:~$ less /usr/include/x86_64-linux-gnu/bits/netdb.h
......
/* Description of data base entry for a single network. NOTE: here a
poor assumption is made. The network number is expected to fit
into an unsigned long int variable. */
struct netent
{
char *n_name; /* Official name of network. */
char **n_aliases; /* Alias list. */
int n_addrtype; /* Net address type. */
uint32_t n_net; /* Network number. */
};
......
3) Protocols
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Description of data base entry for a single service. */
struct protoent
{
char *p_name; /* Official protocol name. */
char **p_aliases; /* Alias list. */
int p_proto; /* Protocol number. */
};
/* Open protocol data base files and mark them as staying open even
after a later search if STAY_OPEN is non-zero.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void setprotoent (int __stay_open);
/* Close protocol data base files and clear `stay open' flag.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void endprotoent (void);
/* Get next entry from protocol data base file. Open data base if
necessary.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct protoent *getprotoent (void);
/* Return entry from protocol data base for network with NAME.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct protoent *getprotobyname (const char *__name);
/* Return entry from protocol data base which number is PROTO.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct protoent *getprotobynumber (int __proto);
......
4) services:
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Description of data base entry for a single service. */
struct servent
{
char *s_name; /* Official service name. */
char **s_aliases; /* Alias list. */
int s_port; /* Port number. */
char *s_proto; /* Protocol to use. */
};
/* Open service data base files and mark them as staying open even
after a later search if STAY_OPEN is non-zero.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void setservent (int __stay_open);
/* Close service data base files and clear `stay open' flag.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void endservent (void);
/* Get next entry from service data base file. Open data base if
necessary.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct servent *getservent (void);
/* Return entry from network data base for network with NAME and
protocol PROTO.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct servent *getservbyname (const char *__name, const char *__proto);
/* Return entry from service data base which matches port PORT and
protocol PROTO.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern struct servent *getservbyport (int __port, const char *__proto);
......
5) Address Information
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Translate name of a service location and/or a service name to set of
socket addresses.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int getaddrinfo (const char *__restrict __name,
const char *__restrict __service,
const struct addrinfo *__restrict __req,
struct addrinfo **__restrict __pai);
/* Free `addrinfo' structure AI including associated storage. */
extern void freeaddrinfo (struct addrinfo *__ai) __THROW;
......
/* Structure to contain information about address of a service provider. */
struct addrinfo
{
int ai_flags; /* Input flags. */
int ai_family; /* Protocol family for socket. */
int ai_socktype; /* Socket type. */
int ai_protocol; /* Protocol for socket. */
socklen_t ai_addrlen; /* Length of socket address. */
struct sockaddr *ai_addr; /* Socket address for socket. */
char *ai_canonname; /* Canonical name for service location. */
struct addrinfo *ai_next; /* Pointer to next in list. */
};
......
getaddrinfo: providing the host name, service name or both, to return one linked list of addrinfo strucutures.
freeaddrinfo: release each addrinfo structure in the linked list.
If getaddrinfo fails, we need to call gai_strerror to convert the error code returned into an error message.
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Convert error return from getaddrinfo() to a string. */
extern const char *gai_strerror (int __ecode) __THROW;
......
6) Converts an address into a host name and a service name
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
/* Translate a socket address to a location and service name.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int getnameinfo (const struct sockaddr *__restrict __sa,
socklen_t __salen, char *__restrict __host,
socklen_t __hostlen, char *__restrict __serv,
socklen_t __servlen, int __flags);
......
getnameinfo converts an unix networking address into a host name and a service name. flags give us some control about how the translation is done:
ubuntu@ip-172-31-23-227:~$ less /usr/include/netdb.h
......
# define NI_NUMERICHOST 1 /* Don't try to look up hostname. */
# define NI_NUMERICSERV 2 /* Don't convert port number to name. */
# define NI_NOFQDN 4 /* Only return nodename portion. */
# define NI_NAMEREQD 8 /* Don't return numeric addresses. */
# define NI_DGRAM 16 /* Look up UDP service rather than TCP. */
......