250 likes | 382 Views
Chapter 11 Advanced Name and Address Conversion. Introduction. gethostbyname, gethostbyaddr: protocol dependent getaddrinfo: a function providing protocol independence for appications get a member of socket address structure getnameinfo. getaddrinfo Function. #include <stdio.h>
E N D
Introduction • gethostbyname, gethostbyaddr: protocol dependent • getaddrinfo: • a function providing protocol independence for appications • get a member of socket address structure • getnameinfo
getaddrinfo Function #include <stdio.h> int getaddrinfo(const addr *hostname, const char *service, const struct addrinfo *hints, struct addrinfo **result); returns : 0 if OK, nonzero on error struct info { int ai_flags; // AI_PASSIVE, AI_CANONNAME int ai_family; // AF_XXX int ai_socktype; // SOCK_XXX int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6 size_t ai_addrlen; // length of ai_addr char *ai_canonname; // ptr to cannonical name for host struct sockaddr *ai_addr; // ptr to socket address structure struct addrinfo *ai_next; // ptr to next structure in linked list };
hostname: • a hostname or an address string(dotted-decimal for IPv4 or a hex string for IPv6) • service • a service name or a decimal port number string • hints • a null pointer or a pointer to an addrinfo structure that the caller fills in • ai_flags: AI_PASSSIVE, AI_CANONNAME • ai_family (an AF_xxx value) • ai_socktype (a SOCK_xxx value • ai_protocol
Result • a pointer to a linked list of addrinfo structures • four addrinfo structures for hint: null, two IP address • first IP address, SOCK_STREAM • first IP address, SOCK_DGRAM • second IP address, SOCK_STREAM • second IP address, SOCK_DGRAM • see Figure 11.1
gai_strerror function #include <netdb.h> char *gai_strerror(int error); returns: pointer to string describing error message
freeaddrinfo function #include <netdb.h> void freeaddrinfo(struct addrinfo *ai)
getaddrinfo Function:Examples • testga • f inet: the address family • c : the canonical name • h bsdi: the hostname • s domain: the service name
Testga -f inet -t stream -h gateway.tuc.noao.edu -s daytime (-t socktype, -s service name) testga -h alpha -s ftp testga -p -s 8888 (-p AI_PASSIVE) testga -c -p -h /local -s /tmp/test.1
>daytimetcpcli bsdi daytime connected to 206.62.226.35 Fri May 30 12:33:32 1999 >daytimetcpcli aix daytime
tcp_listen function -create a TCP socket, bind the server’s well-known port -allow incoming connection requests to be accepted #include “unp.h” int tcp_listen(const char *hostname, const char *service, socklen_t *lenptr);
Udp_client function unconnected UDP #include “unp.h” int udp_client(const char *hostname, const char *service, void **saptr, socklen_t *lenp);
udp_connect function #include “unp.h” int udp_connect(const char *hostname, const char *service); udp_server function #include “unp.h” int udp_server(const char *hostname, const char *service, socklen_t *lenptr);
getnameinfo function • Takes a socket address and returns a character string describing the host and another character nstring describing the service #include <netdb.h> int getnameinfo(const struct sockaddr *sockaddr, socklen_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);