ip_helper_symbian.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/ip_helper.h>
  20. #include <pj/addr_resolv.h>
  21. #include <pj/assert.h>
  22. #include <pj/errno.h>
  23. #include <pj/log.h>
  24. #include <pj/string.h>
  25. #include <pj/compat/socket.h>
  26. #include "os_symbian.h"
  27. #define THIS_FILE "ip_helper_symbian.cpp"
  28. #define TRACE_ME 0
  29. static pj_status_t rsock_enum_interface(int af,
  30. unsigned *p_cnt,
  31. pj_sockaddr ifs[])
  32. {
  33. TInt rc;
  34. RSocket rSock;
  35. TPckgBuf<TSoInetInterfaceInfo> info;
  36. unsigned i;
  37. if (PjSymbianOS::Instance()->Connection()) {
  38. rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
  39. af, PJ_SOCK_DGRAM, KProtocolInetUdp,
  40. *PjSymbianOS::Instance()->Connection());
  41. } else {
  42. rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
  43. af, PJ_SOCK_DGRAM, KProtocolInetUdp);
  44. }
  45. if (rc != KErrNone)
  46. return PJ_RETURN_OS_ERROR(rc);
  47. rSock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
  48. for (i=0; i<*p_cnt &&
  49. rSock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl,
  50. info) == KErrNone; )
  51. {
  52. TInetAddr &iAddress = info().iAddress;
  53. int namelen;
  54. #if TRACE_ME
  55. if (1) {
  56. pj_sockaddr a;
  57. char ipaddr[PJ_INET6_ADDRSTRLEN+2];
  58. namelen = sizeof(pj_sockaddr);
  59. if (PjSymbianOS::Addr2pj(iAddress, a, &namelen,
  60. PJ_FALSE) == PJ_SUCCESS)
  61. {
  62. PJ_LOG(5,(THIS_FILE, "Enum: found address %s",
  63. pj_sockaddr_print(&a, ipaddr, sizeof(ipaddr), 2)));
  64. }
  65. }
  66. #endif
  67. namelen = sizeof(ifs[i]);
  68. if (PjSymbianOS::Addr2pj(iAddress, ifs[i], &namelen,
  69. PJ_TRUE) != PJ_SUCCESS)
  70. {
  71. continue;
  72. }
  73. if (ifs[i].addr.sa_family != af)
  74. continue;
  75. ++i;
  76. }
  77. rSock.Close();
  78. // Done
  79. *p_cnt = i;
  80. return PJ_SUCCESS;
  81. }
  82. /*
  83. * Enumerate the local IP interface currently active in the host.
  84. */
  85. PJ_DEF(pj_status_t) pj_enum_ip_interface(int af,
  86. unsigned *p_cnt,
  87. pj_sockaddr ifs[])
  88. {
  89. unsigned start;
  90. pj_status_t status = PJ_SUCCESS;
  91. start = 0;
  92. /* Get IPv6 interface first. */
  93. if (af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) {
  94. unsigned max = *p_cnt;
  95. status = rsock_enum_interface(PJ_AF_INET6, &max, &ifs[start]);
  96. if (status == PJ_SUCCESS) {
  97. (*p_cnt) -= max;
  98. start += max;
  99. }
  100. }
  101. /* Get IPv4 interface. */
  102. if (af==PJ_AF_INET || af==PJ_AF_UNSPEC) {
  103. unsigned max = *p_cnt;
  104. status = rsock_enum_interface(PJ_AF_INET, &max, &ifs[start]);
  105. if (status == PJ_SUCCESS) {
  106. (*p_cnt) -= max;
  107. start += max;
  108. }
  109. }
  110. *p_cnt = start;
  111. return start ? PJ_SUCCESS : PJ_ENOTFOUND;
  112. }
  113. /*
  114. * Enumerate the local IP interface currently active in the host.
  115. */
  116. PJ_DEF(pj_status_t) pj_enum_ip_interface2( const pj_enum_ip_option *opt,
  117. unsigned *count,
  118. pj_sockaddr ifs[])
  119. {
  120. pj_enum_ip_option opt_;
  121. if (opt && opt->omit_deprecated_ipv6)
  122. return PJ_ENOTSUP;
  123. if (opt)
  124. opt_ = *opt;
  125. else
  126. pj_enum_ip_option_default(&opt_);
  127. return pj_enum_ip_interface(opt_.af, count, ifs);
  128. }
  129. /*
  130. * Enumerate the IP routing table for this host.
  131. */
  132. PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt,
  133. pj_ip_route_entry routes[])
  134. {
  135. PJ_ASSERT_RETURN(p_cnt && *p_cnt > 0 && routes, PJ_EINVAL);
  136. *p_cnt = 0;
  137. return PJ_ENOTSUP;
  138. }