lookup_stun_server.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2006 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@internal
  25. * @file lookup_stun_server.c
  26. * @brief Test app for STUN DNS-SRV lookups.
  27. *
  28. * @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
  29. *
  30. * @todo TODO
  31. * - picks one UDP and TLS target:port
  32. * - does not pick up A/AAAA records that might be delivered
  33. * in 'Additional Data' section as defined in RFC2782
  34. */
  35. #include <stdio.h>
  36. #include <sofia-sip/su.h>
  37. #include <sofia-sip/su_alloc.h>
  38. #include <sofia-sip/su_wait.h>
  39. #define STUN_MAGIC_T su_root_t
  40. #include <sofia-sip/stun.h>
  41. static char* g_domain = NULL;
  42. static void lookup_cb(stun_dns_lookup_t *self,
  43. su_root_t *root)
  44. {
  45. # define RESULT(x) (x == 0 ? "OK" : "ERRORS")
  46. const char *tcp_target = NULL, *udp_target = NULL, *stp_target = NULL;
  47. uint16_t tcp_port = 0, udp_port = 0, stp_port = 0;
  48. int res = 0;
  49. printf("STUN DNS-SRV lookup results:\n");
  50. res = stun_dns_lookup_udp_addr(self, &udp_target, &udp_port);
  51. printf(" _stun._udp.%s: %s:%u (%s).\n", g_domain, udp_target, udp_port, RESULT(res));
  52. res = stun_dns_lookup_tcp_addr(self, &tcp_target, &tcp_port);
  53. printf(" _stun._tcp.%s: %s:%u (%s).\n", g_domain, tcp_target, tcp_port, RESULT(res));
  54. res = stun_dns_lookup_stp_addr(self, &stp_target, &stp_port);
  55. printf(" _stun-tls._tcp.%s: %s:%u (%s).\n", g_domain, stp_target, stp_port, RESULT(res));
  56. su_root_break(root);
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. su_root_t *root;
  61. stun_dns_lookup_t *lookup;
  62. if (argc < 2) {
  63. printf("usage: ./lookup_stun_server <domain>\n");
  64. return -1;
  65. }
  66. g_domain = argv[1];
  67. /* step: initialize sofia su OS abstraction layer */
  68. su_init();
  69. /* step: create a su event loop and connect it to glib */
  70. root = su_root_create(NULL);
  71. /* step: initiate the DNS-SRV lookup */
  72. lookup = stun_dns_lookup(root, root, lookup_cb, g_domain);
  73. if (lookup) {
  74. /* step: enter the main loop (break fro lookup_cb()) */
  75. su_root_run(root);
  76. /* step: free any allocated resources */
  77. stun_dns_lookup_destroy(lookup);
  78. }
  79. else {
  80. printf("ERROR: failed to create lookup object.\n");
  81. }
  82. su_root_destroy(root);
  83. su_deinit();
  84. return 0;
  85. }