#include "unp.h"
#include
int main(int argc, char **argv)
{
int sockfd;
ssize_t n;
char buff[MAXLINE];
time_t ticks;
socklen_t len;
struct sockaddr_storage clientaddr;
if(argc==2)
sockfd=Udp_server(NULL, argv[1],NULL);
else if(argc==3)
sockfd=Udp_server(argv[1], argv[2],NULL);
else
err_quit("usage: daytimeudpserv [ ] ");
for(;;)
{
len=sizeof(clientaddr);
n=Recvfrom(sockfd, buff, MAXLINE, 0, (SA *)&clientaddr, &len);
printf("datagram from %s\n", Sock_ntop((SA *)&clientaddr, len));
ticks=time(NULL);
snprintf(buff, sizeoff(buff), "%0.24s\r\n", ctime(&ticks));
Sendto(sockfd, buff, strlen(buff),0, (SA *)&clientaddr, len);
}
}
Protocol-independent UDP daytime server
‘getnameinfo’ Function
This function is the complement of getaddrinfo: It takes a socket address and returns a character string describing the host and another character string describing the service. This function provides this information in a protocol-independent fashion; that is, the caller does not care what type of protocol address is contained in the socket address structure, as that detail is handled by the function.
#include
int getnameinfo (const struct sockaddr *sockaddr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags) ;
The gethostbyname function from Section 11.3(See 8.9.3) presents an interesting problem that we have not yet examined in the text: It is not re-entrant. We will encounter this problem in general when we deal with threads in Chapter 26(See 9.15), but it is interesting to examine the problem now (without having to deal with the concept of threads) and to see how to fix it.
If we look at the name and address conversion functions presented in this chapter, along with the inet_XXX functions from Chapter 4(See 8.2), we note the following:
A similar problem occurs with the variable errno. Historically, there has been a single copy of this integer variable per process. If a process makes a system call that returns an error, an integer error code is stored in this variable. For example, when the function named close in the standard C library is called, it might execute something like the following pseudocode:
There are two ways to make a nonre-entrant function such as gethostbyname re-entrant.
1.Instead of filling in and returning a static structure, the caller allocates the structure and the re-entrant function fills in the caller’s structure.
2.The re-entrant function calls malloc and dynamically allocates the memory. This is the technique used by getaddrinfo.
#include
struct hostent *gethostbyname_r (const char *hostname, struct hostent *result, char *buf, int buflen, int *h_errnop) ;
struct hostent *gethostbyaddr_r (const char *addr, int len, int type, struct hostent *result, char *buf, int buflen, int *h_errnop) ;
While IPv6 was being developed, the API to request the lookup of an IPv6 address went through several iterations. The resulting API was complicated and not sufficiently flexible, so it was deprecated in RFC 2553 [Gilligan et al. 1999].
The gethostbyname2 Function
The gethostbyname2 function adds an address family argument to gethostbyname.
#include
#include
struct hostent *gethostbyname2 (const char *name, int af) ;
RFC 2553 [Gilligan et al. 1999] deprecated RES_USE_INET6 and gethostbyname2 because of the global nature of the RES_USE_INET6 flag and the wish to provide more control over the returned information. It introduced the getipnodebyname function to solve some of these problems.
#include
#include
struct hostent *getipnodebyname (const char *name, int af, int flags, int *error_num) ;
This function returns a pointer to the same hostent structure that we described with gethostbyname. The af and flags arguments map directly to getaddrinfo’s hints.ai_family and hints.ai_flags arguments. For thread safety, the return value is dynamically allocated, so it must be freed with the freehostent function.
#include
void freehostent (struct hostent *ptr) ;
Our focus in this chapter has been on hostnames and IP addresses and service names and their port numbers. But looking at the bigger picture, there are four types of information (related to networking) that an application might want to look up: hosts, networks, protocols, and services. Most lookups are for hosts (gethostbyname and gethòstbyaddr), with a smaller number for services (getservbyname and getservbyaddr), and an even smaller number for networks and protocols.
All four types of information can be stored in a file and three functions are defined for each of the four types:
1.A getXXXent function that reads the next entry in the file, opening the file if necessary.
2.A setXXXent function that opens (if not already open) and rewinds the file.
3.An endXXXent function that closes the file.