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 <sys/socket.h>
47 #include <sys/types.h>
48 #include "stun/stunagent.h"
49 #include "stun/usages/bind.h"
58 static int ai_flags
= 0;
61 printaddr (const char *str
, const struct sockaddr
*addr
, socklen_t addrlen
)
63 char hostbuf
[NI_MAXHOST
], servbuf
[NI_MAXSERV
];
65 int val
= getnameinfo (addr
, addrlen
, hostbuf
, sizeof (hostbuf
),
66 servbuf
, sizeof (servbuf
),
67 NI_NUMERICHOST
| NI_NUMERICSERV
);
69 printf ("%s: %s\n", str
, gai_strerror (val
));
71 printf ("%s: %s port %s\n", str
, hostbuf
, servbuf
);
76 static int run (int family
, const char *hostname
, const char *service
)
78 struct addrinfo hints
, *res
;
79 const struct addrinfo
*ptr
;
82 memset (&hints
, 0, sizeof (hints
));
83 hints
.ai_family
= family
;
84 hints
.ai_socktype
= SOCK_DGRAM
;
85 hints
.ai_flags
= ai_flags
;
89 ret
= getaddrinfo (hostname
, service
, &hints
, &res
);
92 fprintf (stderr
, "%s (port %s): %s\n", hostname
, service
,
97 for (ptr
= res
; ptr
!= NULL
; ptr
= ptr
->ai_next
)
99 struct sockaddr_storage addr
;
100 socklen_t addrlen
= sizeof (addr
);
101 StunUsageBindReturn val
;
103 printaddr ("Server address", ptr
->ai_addr
, ptr
->ai_addrlen
);
105 val
= stun_usage_bind_run (ptr
->ai_addr
, ptr
->ai_addrlen
,
106 (struct sockaddr
*)&addr
, &addrlen
);
108 fprintf (stderr
, "%d\n", val
);
111 printaddr ("Mapped address", (struct sockaddr
*)&addr
, addrlen
);
121 int main (int argc
, char *argv
[])
123 static const struct option opts
[] =
125 { "ipv4", no_argument
, NULL
, '4' },
126 { "ipv6", no_argument
, NULL
, '6' },
127 { "help", no_argument
, NULL
, 'h' },
128 { "numeric", no_argument
, NULL
, 'n' },
129 { "version", no_argument
, NULL
, 'V' },
132 const char *server
= NULL
, *port
= NULL
;
133 int family
= AF_UNSPEC
;
137 int val
= getopt_long (argc
, argv
, "46hnV", opts
, NULL
);
152 printf ("Usage: %s [-4|-6] <server> [port number]\n"
153 "Performs STUN Binding Discovery\n"
155 " -4, --ipv4 Force IP version 4\n"
156 " -6, --ipv6 Force IP version 6\n"
157 " -n, --numeric Server in numeric form\n"
162 ai_flags
|= AI_NUMERICHOST
;
166 printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
176 server
= argv
[optind
++];
178 port
= argv
[optind
++];
181 fprintf (stderr
, "%s: extra parameter `%s'\n", argv
[0], argv
[optind
]);
185 return run (family
, server
, port
) ? 1 : 0;