2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2007-2009 Nokia Corporation. All rights reserved.
7 * Contact: Rémi Denis-Courmont
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
25 * Youness Alaoui, Collabora Ltd.
26 * Rémi Denis-Courmont, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
44 # include <winsock2.h>
46 # include <sys/socket.h>
50 #include <sys/types.h>
51 #include "stun/stunagent.h"
52 #include "stun/usages/bind.h"
62 static int ai_flags = 0;
65 printaddr (const char *str, const struct sockaddr *addr, socklen_t addrlen)
67 char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
69 int val = getnameinfo (addr, addrlen, hostbuf, sizeof (hostbuf),
70 servbuf, sizeof (servbuf),
71 NI_NUMERICHOST | NI_NUMERICSERV);
73 printf ("%s: %s\n", str, gai_strerror (val));
75 printf ("%s: %s port %s\n", str, hostbuf, servbuf);
80 static int run (int family, const char *hostname, const char *service)
82 struct addrinfo hints, *res;
83 const struct addrinfo *ptr;
86 memset (&hints, 0, sizeof (hints));
87 hints.ai_family = family;
88 hints.ai_socktype = SOCK_DGRAM;
89 hints.ai_flags = ai_flags;
93 ret = getaddrinfo (hostname, service, &hints, &res);
96 fprintf (stderr, "%s (port %s): %s\n", hostname, service,
101 for (ptr = res; ptr != NULL; ptr = ptr->ai_next)
104 struct sockaddr_storage storage;
105 struct sockaddr addr;
107 socklen_t addrlen = sizeof (addr);
108 StunUsageBindReturn val;
110 printaddr ("Server address", ptr->ai_addr, ptr->ai_addrlen);
112 val = stun_usage_bind_run (ptr->ai_addr, ptr->ai_addrlen, &addr.storage,
115 fprintf (stderr, "%d\n", val);
118 printaddr ("Mapped address", &addr.addr, addrlen);
128 int main (int argc, char *argv[])
130 const char *server = NULL, *port = NULL;
131 int family = AF_UNSPEC;
137 WSAStartup (0x0202, &w);
140 for (i = 1; i < argc; ++i)
142 const char *arg = argv[i];
147 if (strcmp (arg, "--ipv4") == 0 || strcmp (arg, "-4") == 0)
151 else if (strcmp (arg, "--ipv6") == 0 || strcmp (arg, "-6") == 0)
155 else if (strcmp (arg, "--help") == 0 || strcmp (arg, "-h") == 0)
157 printf ("Usage: %s [-4|-6] <server> [port number]\n"
158 "Performs STUN Binding Discovery\n"
160 " -4, --ipv4 Force IP version 4\n"
161 " -6, --ipv6 Force IP version 6\n"
162 " -n, --numeric Server in numeric form\n"
166 else if (strcmp (arg, "--numeric") == 0 || strcmp (arg, "-n") == 0)
168 ai_flags |= AI_NUMERICHOST;
170 else if (strcmp (arg, "--version") == 0 || strcmp (arg, "-V") == 0)
172 printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
176 fprintf (stderr, "Unexpected command line argument '%s'", arg);
187 fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[i]);
191 result = run (family, server, port) ? 1 : 0;