host_serv.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* include host_serv */
  2. #include "unp.h"
  3. struct addrinfo *
  4. host_serv(const char *host, const char *serv, int family, int socktype)
  5. {
  6. int n;
  7. struct addrinfo hints, *res;
  8. bzero(&hints, sizeof(struct addrinfo));
  9. hints.ai_flags = AI_CANONNAME; /* always return canonical name */
  10. hints.ai_family = family; /* AF_UNSPEC, AF_INET, AF_INET6, etc. */
  11. hints.ai_socktype = socktype; /* 0, SOCK_STREAM, SOCK_DGRAM, etc. */
  12. if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
  13. return(NULL);
  14. return(res); /* return pointer to first on linked list */
  15. }
  16. /* end host_serv */
  17. /*
  18. * There is no easy way to pass back the integer return code from
  19. * getaddrinfo() in the function above, short of adding another argument
  20. * that is a pointer, so the easiest way to provide the wrapper function
  21. * is just to duplicate the simple function as we do here.
  22. */
  23. struct addrinfo *
  24. Host_serv(const char *host, const char *serv, int family, int socktype)
  25. {
  26. int n;
  27. struct addrinfo hints, *res;
  28. bzero(&hints, sizeof(struct addrinfo));
  29. hints.ai_flags = AI_CANONNAME; /* always return canonical name */
  30. hints.ai_family = family; /* 0, AF_INET, AF_INET6, etc. */
  31. hints.ai_socktype = socktype; /* 0, SOCK_STREAM, SOCK_DGRAM, etc. */
  32. if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
  33. err_quit("host_serv error for %s, %s: %s",
  34. (host == NULL) ? "(no hostname)" : host,
  35. (serv == NULL) ? "(no service name)" : serv,
  36. gai_strerror(n));
  37. return(res); /* return pointer to first on linked list */
  38. }