1 / 25

Chapter 11 Advanced Name and Address Conversion

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>

zona
Télécharger la présentation

Chapter 11 Advanced Name and Address Conversion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11Advanced Name and Address Conversion

  2. Introduction • gethostbyname, gethostbyaddr: protocol dependent • getaddrinfo: • a function providing protocol independence for appications • get a member of socket address structure • getnameinfo

  3. 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 };

  4. 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

  5. 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

  6. gai_strerror function #include <netdb.h> char *gai_strerror(int error); returns: pointer to string describing error message

  7. freeaddrinfo function #include <netdb.h> void freeaddrinfo(struct addrinfo *ai)

  8. getaddrinfo Function: IPv6 and unix Domain

  9. getaddrinfo Function:Examples • testga • f inet: the address family • c : the canonical name • h bsdi: the hostname • s domain: the service name

  10. 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

  11. host_serv function

  12. >daytimetcpcli bsdi daytime connected to 206.62.226.35 Fri May 30 12:33:32 1999 >daytimetcpcli aix daytime

  13. 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);

  14. Udp_client function unconnected UDP #include “unp.h” int udp_client(const char *hostname, const char *service, void **saptr, socklen_t *lenp);

  15. 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);

  16. 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);

More Related