addr_resolv_symbian.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/addr_resolv.h>
  20. #include <pj/assert.h>
  21. #include <pj/errno.h>
  22. #include <pj/ip_helper.h>
  23. #include <pj/log.h>
  24. #include <pj/sock.h>
  25. #include <pj/string.h>
  26. #include <pj/unicode.h>
  27. #include "os_symbian.h"
  28. #define THIS_FILE "addr_resolv_symbian.cpp"
  29. #define TRACE_ME 0
  30. // PJLIB API: resolve hostname
  31. PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he)
  32. {
  33. static pj_addrinfo ai;
  34. static char *aliases[2];
  35. static char *addrlist[2];
  36. unsigned count = 1;
  37. pj_status_t status;
  38. status = pj_getaddrinfo(PJ_AF_INET, name, &count, &ai);
  39. if (status != PJ_SUCCESS)
  40. return status;
  41. aliases[0] = ai.ai_canonname;
  42. aliases[1] = NULL;
  43. addrlist[0] = (char*) &ai.ai_addr.ipv4.sin_addr;
  44. addrlist[1] = NULL;
  45. pj_bzero(he, sizeof(*he));
  46. he->h_name = aliases[0];
  47. he->h_aliases = aliases;
  48. he->h_addrtype = PJ_AF_INET;
  49. he->h_length = 4;
  50. he->h_addr_list = addrlist;
  51. return PJ_SUCCESS;
  52. }
  53. // Resolve for specific address family
  54. static pj_status_t getaddrinfo_by_af(int af, const pj_str_t *name,
  55. unsigned *count, pj_addrinfo ai[])
  56. {
  57. unsigned i;
  58. pj_status_t status;
  59. PJ_ASSERT_RETURN(name && count && ai, PJ_EINVAL);
  60. #if !defined(PJ_HAS_IPV6) || !PJ_HAS_IPV6
  61. if (af == PJ_AF_INET6)
  62. return PJ_EIPV6NOTSUP;
  63. #endif
  64. // Return failure if access point is marked as down by app.
  65. PJ_SYMBIAN_CHECK_CONNECTION();
  66. // Get resolver for the specified address family
  67. RHostResolver &resv = PjSymbianOS::Instance()->GetResolver(af);
  68. // Convert name to Unicode
  69. wchar_t name16[PJ_MAX_HOSTNAME];
  70. pj_ansi_to_unicode(name->ptr, name->slen, name16, PJ_ARRAY_SIZE(name16));
  71. TPtrC16 data((const TUint16*)name16);
  72. // Resolve!
  73. TNameEntry nameEntry;
  74. TRequestStatus reqStatus;
  75. resv.GetByName(data, nameEntry, reqStatus);
  76. User::WaitForRequest(reqStatus);
  77. // Iterate each result
  78. i = 0;
  79. while (reqStatus == KErrNone && i < *count) {
  80. // Get the resolved TInetAddr
  81. TInetAddr inetAddr(nameEntry().iAddr);
  82. int addrlen;
  83. #if TRACE_ME
  84. if (1) {
  85. pj_sockaddr a;
  86. char ipaddr[PJ_INET6_ADDRSTRLEN+2];
  87. int namelen;
  88. namelen = sizeof(pj_sockaddr);
  89. if (PjSymbianOS::Addr2pj(inetAddr, a, &namelen,
  90. PJ_FALSE) == PJ_SUCCESS)
  91. {
  92. PJ_LOG(5,(THIS_FILE, "resolve %.*s: %s",
  93. (int)name->slen, name->ptr,
  94. pj_sockaddr_print(&a, ipaddr, sizeof(ipaddr), 2)));
  95. }
  96. }
  97. #endif
  98. // Ignore if this is not the same address family
  99. // Not a good idea, as Symbian mapps IPv4 to IPv6.
  100. //fam = inetAddr.Family();
  101. //if (fam != af) {
  102. // resv.Next(nameEntry, reqStatus);
  103. // User::WaitForRequest(reqStatus);
  104. // continue;
  105. //}
  106. // Convert IP address first to get IPv4 mapped address
  107. addrlen = sizeof(ai[i].ai_addr);
  108. status = PjSymbianOS::Addr2pj(inetAddr, ai[i].ai_addr,
  109. &addrlen, PJ_TRUE);
  110. if (status != PJ_SUCCESS)
  111. return status;
  112. // Ignore if address family doesn't match
  113. if (ai[i].ai_addr.addr.sa_family != af) {
  114. resv.Next(nameEntry, reqStatus);
  115. User::WaitForRequest(reqStatus);
  116. continue;
  117. }
  118. // Convert the official address to ANSI.
  119. pj_unicode_to_ansi((const wchar_t*)nameEntry().iName.Ptr(),
  120. nameEntry().iName.Length(),
  121. ai[i].ai_canonname, sizeof(ai[i].ai_canonname));
  122. // Next
  123. ++i;
  124. resv.Next(nameEntry, reqStatus);
  125. User::WaitForRequest(reqStatus);
  126. }
  127. *count = i;
  128. return PJ_SUCCESS;
  129. }
  130. /* Resolve IPv4/IPv6 address */
  131. PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename,
  132. unsigned *count, pj_addrinfo ai[])
  133. {
  134. unsigned start;
  135. pj_status_t status = PJ_EAFNOTSUP;
  136. PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 || af==PJ_AF_UNSPEC,
  137. PJ_EAFNOTSUP);
  138. PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL);
  139. start = 0;
  140. if (af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) {
  141. unsigned max = *count;
  142. status = getaddrinfo_by_af(PJ_AF_INET6, nodename,
  143. &max, &ai[start]);
  144. if (status == PJ_SUCCESS) {
  145. (*count) -= max;
  146. start += max;
  147. }
  148. }
  149. if (af==PJ_AF_INET || af==PJ_AF_UNSPEC) {
  150. unsigned max = *count;
  151. status = getaddrinfo_by_af(PJ_AF_INET, nodename,
  152. &max, &ai[start]);
  153. if (status == PJ_SUCCESS) {
  154. (*count) -= max;
  155. start += max;
  156. }
  157. }
  158. *count = start;
  159. if (*count) {
  160. return PJ_SUCCESS;
  161. } else {
  162. return status!=PJ_SUCCESS ? status : PJ_ENOTFOUND;
  163. }
  164. }