2
* This file is part of the Nice GLib ICE library.
4
* (C) 2007 Nokia Corporation. All rights reserved.
5
* Contact: Rémi Denis-Courmont
7
* The contents of this file are subject to the Mozilla Public License Version
8
* 1.1 (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
10
* http://www.mozilla.org/MPL/
12
* Software distributed under the License is distributed on an "AS IS" basis,
13
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14
* for the specific language governing rights and limitations under the
17
* The Original Code is the Nice GLib ICE library.
19
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
20
* Corporation. All Rights Reserved.
23
* Rémi Denis-Courmont, Nokia
25
* Alternatively, the contents of this file may be used under the terms of the
26
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27
* case the provisions of LGPL are applicable instead of those above. If you
28
* wish to allow use of your version of this file only under the terms of the
29
* LGPL and not to allow others to use your version of this file under the
30
* MPL, indicate your decision by deleting the provisions above and replace
31
* them with the notice and other provisions required by the LGPL. If you do
32
* not delete the provisions above, a recipient may use your version of this
33
* file under either the MPL or the LGPL.
41
#include <sys/socket.h>
44
#include <sys/types.h>
45
#include "stun/stunagent.h"
46
#include "stun/usages/bind.h"
56
static int ai_flags = 0;
59
printaddr (const char *str, const struct sockaddr *addr, socklen_t addrlen)
61
char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
63
int val = getnameinfo (addr, addrlen, hostbuf, sizeof (hostbuf),
64
servbuf, sizeof (servbuf),
65
NI_NUMERICHOST | NI_NUMERICSERV);
67
printf ("%s: %s\n", str, gai_strerror (val));
69
printf ("%s: %s port %s\n", str, hostbuf, servbuf);
74
static int run (int family, const char *hostname, const char *service)
76
struct addrinfo hints, *res;
77
const struct addrinfo *ptr;
80
memset (&hints, 0, sizeof (hints));
81
hints.ai_family = family;
82
hints.ai_socktype = SOCK_DGRAM;
83
hints.ai_flags = ai_flags;
87
ret = getaddrinfo (hostname, service, &hints, &res);
90
fprintf (stderr, "%s (port %s): %s\n", hostname, service,
95
for (ptr = res; ptr != NULL; ptr = ptr->ai_next)
97
struct sockaddr_storage addr;
98
socklen_t addrlen = sizeof (addr);
99
StunUsageBindReturn val;
101
printaddr ("Server address", ptr->ai_addr, ptr->ai_addrlen);
103
val = stun_usage_bind_run (ptr->ai_addr, ptr->ai_addrlen,
104
(struct sockaddr *)&addr, &addrlen);
106
fprintf (stderr, "%d\n", val);
109
printaddr ("Mapped address", (struct sockaddr *)&addr, addrlen);
119
int main (int argc, char *argv[])
121
static const struct option opts[] =
123
{ "ipv4", no_argument, NULL, '4' },
124
{ "ipv6", no_argument, NULL, '6' },
125
{ "help", no_argument, NULL, 'h' },
126
{ "numeric", no_argument, NULL, 'n' },
127
{ "version", no_argument, NULL, 'V' },
130
const char *server = NULL, *port = NULL;
131
int family = AF_UNSPEC;
135
int val = getopt_long (argc, argv, "46hnV", opts, NULL);
150
printf ("Usage: %s [-4|-6] <server> [port number]\n"
151
"Performs STUN Binding Discovery\n"
153
" -4, --ipv4 Force IP version 4\n"
154
" -6, --ipv6 Force IP version 6\n"
159
ai_flags |= AI_NUMERICHOST;
163
printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
173
server = argv[optind++];
175
port = argv[optind++];
178
fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[optind]);
182
return run (family, server, port) ? 1 : 0;