Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

598 Network IPC: Sockets Chapter 16


We can get network names and numbers with a similar set of interfaces.

#include <netdb.h>
struct netent *getnetbyaddr(uint32_tnet,inttype);
struct netent *getnetbyname(const char *name);
struct netent *getnetent(void);
All return: pointer if OK,NULLon error
void setnetent(intstayopen);
void endnetent(void);

Thenetentstructurecontains at least the following fields:
struct netent {
char *n_name; /* network name */
char **n_aliases; /* alternate network name array pointer */
int n_addrtype; /* address type */
uint32_t n_net; /* network number */
..
.
};

The network number is returned in network byte order.The address type is one of the
address family constants (AF_INET,for example).
We can map between protocol names and numbers with the following functions.

#include <netdb.h>
struct protoent *getprotobyname(const char *name);
struct protoent *getprotobynumber(intproto);
struct protoent *getprotoent(void);
All return: pointer if OK,NULLon error
void setprotoent(intstayopen);
void endprotoent(void);

Theprotoentstructure as defined by POSIX.1 has at least the following members:
struct protoent {
char *p_name; /* protocol name */
char **p_aliases; /* pointer to alternate protocol name array */
int p_proto; /* protocol number */
..
.
};

Services arerepresented by the port number portion of the address. Each service is
offered on a unique, well-known port number.Wecan map a service name to a port
Free download pdf