fake-getnameinfo.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * fake library for ssh
  3. *
  4. * This file includes getnameinfo().
  5. * These functions are defined in rfc2133.
  6. *
  7. * But these functions are not implemented correctly. The minimum subset
  8. * is implemented for ssh use only. For example, this routine assumes
  9. * that ai_family is AF_INET. Don't use it for another purpose.
  10. */
  11. #include "system.h"
  12. #include "fake-getnameinfo.h"
  13. #include "fake-getaddrinfo.h"
  14. #if !HAVE_DECL_GETNAMEINFO
  15. int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) {
  16. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  17. struct hostent *hp;
  18. int len;
  19. if(sa->sa_family != AF_INET) {
  20. return EAI_FAMILY;
  21. }
  22. if(serv && servlen) {
  23. len = snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
  24. if(len < 0 || len >= servlen) {
  25. return EAI_MEMORY;
  26. }
  27. }
  28. if(!host || !hostlen) {
  29. return 0;
  30. }
  31. if(flags & NI_NUMERICHOST) {
  32. len = snprintf(host, hostlen, "%s", inet_ntoa(sin->sin_addr));
  33. if(len < 0 || len >= hostlen) {
  34. return EAI_MEMORY;
  35. }
  36. return 0;
  37. }
  38. hp = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET);
  39. if(!hp || !hp->h_name || !hp->h_name[0]) {
  40. return EAI_NODATA;
  41. }
  42. len = snprintf(host, hostlen, "%s", hp->h_name);
  43. if(len < 0 || len >= hostlen) {
  44. return EAI_MEMORY;
  45. }
  46. return 0;
  47. }
  48. #endif /* !HAVE_GETNAMEINFO */