lookup_stun_server.c 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2006 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * @file lookup_stun_server.c 
 * @brief Test app for STUN DNS-SRV lookups.
 *
 * @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
 *
 * @todo TODO
 * - picks one UDP and TLS target:port
 * - does not pick up A/AAAA records that might be delivered
 *   in 'Additional Data' section as defined in RFC2782
 */

#include <stdio.h>

#include <sofia-sip/su.h>
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_wait.h>
#define STUN_MAGIC_T su_root_t
#include <sofia-sip/stun.h>


static void lookup_cb(stun_dns_lookup_t *self,
		      su_root_t *root)
{
  const char *tls_target = NULL, *udp_target = NULL;
  uint16_t tls_port = 0, udp_port = 0;
  int res;

  res = stun_dns_lookup_get_results(self, 
				    &tls_target, &tls_port, 
				    &udp_target, &udp_port);
  if (res == 0) {
57
    printf("STUN DNS-SRV: stun (tcp) at %s:%u.\n", tls_target, tls_port); 
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    printf("STUN DNS-SRV: stun (udp) at %s:%u.\n", udp_target, udp_port); 
  }
  
  su_root_break(root);
}

int main(int argc, char *argv[])
{
  su_root_t *root;
  stun_dns_lookup_t *lookup;

  if (argc < 2) {
    printf("usage: ./lookup_stun_server <domain>\n");
    return -1;
  }

  /* step: initialize sofia su OS abstraction layer */
  su_init();

  /* step: create a su event loop and connect it to glib */
  root = su_root_create(NULL);

  /* step: initiate the DNS-SRV lookup */
  lookup = stun_dns_lookup(root, root, lookup_cb, argv[1]);
 
  /* step: enter the main loop (break fro lookup_cb()) */
  su_root_run(root);

  /* step: free any allocated resources */
  stun_dns_lookup_destroy(lookup);
  su_root_destroy(root);
  su_deinit();

  return 0;
}