test_dns.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (C) 2014 Mamadou DIOP.
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO 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 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO 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 Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. *
  18. */
  19. #ifndef TNET_TEST_DNS_H
  20. #define TNET_TEST_DNS_H
  21. //#include "tnet_utils.h" /* tnet_address_t */
  22. void test_dns_query()
  23. {
  24. tnet_dns_ctx_t *ctx = tnet_dns_ctx_create();
  25. tnet_dns_response_t *response = tsk_null;
  26. const tsk_list_item_t* item;
  27. const tnet_dns_rr_t* rr;
  28. //if((response = tnet_dns_resolve(ctx, "_sip._udp.sip2sip.info", qclass_in, qtype_srv)))
  29. if((response = tnet_dns_resolve(ctx, "sip2sip.info", qclass_in, qtype_naptr))) {
  30. if(TNET_DNS_RESPONSE_IS_SUCCESS(response)) {
  31. TSK_DEBUG_INFO("We got a success response from the DNS server.");
  32. // loop through the answers
  33. tsk_list_foreach(item, response->Answers) {
  34. rr = item->data;
  35. if(rr->qtype == qtype_naptr) {
  36. const tnet_dns_naptr_t *naptr = (const tnet_dns_naptr_t*)rr;
  37. TSK_DEBUG_INFO("order=%u pref=%u flags=%s services=%s regexp=%s replacement=%s",
  38. naptr->order,
  39. naptr->preference,
  40. naptr->flags,
  41. naptr->services,
  42. naptr->regexp,
  43. naptr->replacement);
  44. }
  45. }
  46. }
  47. else {
  48. TSK_DEBUG_ERROR("We got an error response from the DNS server. Erro code: %u", response->Header.RCODE);
  49. }
  50. }
  51. tnet_dns_cache_clear(ctx);
  52. TSK_OBJECT_SAFE_FREE(response);
  53. TSK_OBJECT_SAFE_FREE(ctx);
  54. tsk_thread_sleep(2000);
  55. }
  56. void test_dns_srv()
  57. {
  58. tnet_dns_ctx_t *ctx = tnet_dns_ctx_create();
  59. char* hostname = 0;
  60. tnet_port_t port = 0;
  61. if(!tnet_dns_query_srv(ctx, "_sip._udp.sip2sip.info", &hostname, &port)) {
  62. TSK_DEBUG_INFO("DNS SRV succeed ==> hostname=%s and port=%u", hostname, port);
  63. }
  64. TSK_FREE(hostname);
  65. TSK_OBJECT_SAFE_FREE(ctx);
  66. tsk_thread_sleep(2000);
  67. }
  68. void test_dns_naptr_srv()
  69. {
  70. tnet_dns_ctx_t *ctx = tnet_dns_ctx_create();
  71. char* hostname = tsk_null;
  72. tnet_port_t port = 0;
  73. if(!tnet_dns_query_naptr_srv(ctx, "sip2sip.info", "SIP+D2U", &hostname, &port)) {
  74. TSK_DEBUG_INFO("DNS NAPTR+SRV succeed ==> hostname=%s and port=%u", hostname, port);
  75. }
  76. TSK_FREE(hostname);
  77. TSK_OBJECT_SAFE_FREE(ctx);
  78. tsk_thread_sleep(2000);
  79. }
  80. void test_enum()
  81. {
  82. tnet_dns_ctx_t *ctx = tnet_dns_ctx_create();
  83. tnet_dns_response_t* response = tsk_null;
  84. // const tsk_list_item_t* item;
  85. // const tnet_dns_naptr_t* record;
  86. char* uri = tsk_null;
  87. const char* e164num = "+1-800-555-5555";
  88. //const char* e164num = "+33660188661";
  89. //tnet_dns_add_server(ctx, "192.168.16.9");
  90. //if((uri = tnet_dns_enum_2(ctx, "E2U+SIP", e164num, "e164.org"))){
  91. if((uri = tnet_dns_enum_2(ctx, "E2U+SIP", e164num, "e164.org"))) {
  92. TSK_DEBUG_INFO("URI=%s", uri);
  93. TSK_FREE(uri);
  94. }
  95. else {
  96. TSK_DEBUG_ERROR("ENUM(%s) failed", e164num);
  97. }
  98. /*if((response = tnet_dns_enum(ctx, "+1-800-555-5555", "e164.org"))){
  99. if(TNET_DNS_RESPONSE_IS_SUCCESS(response)){
  100. TSK_DEBUG_INFO("We got a success response from the DNS server.");
  101. // loop through the answers
  102. tsk_list_foreach(item, response->Answers){
  103. record = item->data;
  104. TSK_DEBUG_INFO("order=%u pref=%u flags=%s services=%s regexp=%s replacement=%s",
  105. record->order,
  106. record->preference,
  107. record->flags,
  108. record->services,
  109. record->regexp,
  110. record->replacement);
  111. }
  112. }
  113. else{
  114. TSK_DEBUG_ERROR("We got an error response from the DNS server. Erro code: %u", response->Header.RCODE);
  115. }
  116. }*/
  117. TSK_OBJECT_SAFE_FREE(response);
  118. TSK_OBJECT_SAFE_FREE(ctx);
  119. tsk_thread_sleep(2000);
  120. }
  121. typedef struct regexp_test_s {
  122. const char* e164num;
  123. const char* regexp;
  124. const char* xres;
  125. }
  126. regexp_test_t;
  127. regexp_test_t regexp_tests[] = {
  128. "+18005551234", "!^.*$!sip:customer-service@example.com!i", "sip:customer-service@example.com",
  129. "+18005551234", "!^.*$!mailto:information@example.com!i", "mailto:information@example.com",
  130. "+18005555555", "!^\\+1800(.*)$!sip:1641641800\\1@tollfree.sip-happens.com!", "sip:16416418005555555@tollfree.sip-happens.com",
  131. "+18005555555", "!^\\+1800(.*)$!sip:1641641800\\1@sip.tollfreegateway.com!", "sip:16416418005555555@sip.tollfreegateway.com",
  132. "+468971234", "!^+46(.*)$!ldap://ldap.telco.se/cn=0\\1!", "ldap://ldap.telco.se/cn=08971234",
  133. "+468971234", "!^+46(.*)$!mailto:spam@paf.se!", "mailto:spam@paf.se",
  134. "urn:cid:199606121851.1@bar.example.com", "!!^urn:cid:.+@([^\\.]+\\.)(.*)$!\\2!i", "example.com",
  135. };
  136. void test_regex()
  137. {
  138. char* ret;
  139. size_t i;
  140. for(i=0; i< sizeof(regexp_tests)/sizeof(regexp_test_t); i++) {
  141. if((ret = tnet_dns_regex_parse(regexp_tests[i].e164num, regexp_tests[i].regexp))) {
  142. TSK_DEBUG_INFO("ENUM(%s) = %s", regexp_tests[i].e164num, ret);
  143. if(!tsk_strequals(ret, regexp_tests[i].xres)) {
  144. TSK_DEBUG_ERROR("Failed to match ENUM(%s)", regexp_tests[i].e164num);
  145. }
  146. TSK_FREE(ret);
  147. }
  148. else {
  149. TSK_DEBUG_ERROR("Failed to parse ENUM(%s)", regexp_tests[i].e164num);
  150. }
  151. TSK_DEBUG_INFO("---------");
  152. }
  153. }
  154. void test_resolvconf()
  155. {
  156. tnet_addresses_L_t * servers;
  157. const tnet_address_t* address;
  158. const tsk_list_item_t* item;
  159. const char* path = "C:\\tmp\\resolv32.conf";
  160. //const char* path = "C:\\tmp\\resolv.conf";
  161. //const char* path = "/etc/resolv.conf";
  162. if((servers = tnet_dns_resolvconf_parse(path))) {
  163. tsk_list_foreach(item, servers) {
  164. address = item->data;
  165. TSK_DEBUG_INFO("DNS Server host=%s Family=%d", address->ip, address->family);
  166. }
  167. TSK_OBJECT_SAFE_FREE(servers);
  168. }
  169. else {
  170. TSK_DEBUG_ERROR("Failed to parse DNS servers from %s.", path);
  171. }
  172. }
  173. void test_dns()
  174. {
  175. test_dns_naptr_srv();
  176. //test_dns_srv();
  177. //test_dns_query();
  178. //test_enum();
  179. //test_regex();
  180. //test_resolvconf();
  181. }
  182. #endif /* TNET_TEST_DNS_H */