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"
61 static int ai_flags
= 0;
64 printaddr (const char *str
, const struct sockaddr
*addr
, socklen_t addrlen
)
66 char hostbuf
[NI_MAXHOST
], servbuf
[NI_MAXSERV
];
68 int val
= getnameinfo (addr
, addrlen
, hostbuf
, sizeof (hostbuf
),
69 servbuf
, sizeof (servbuf
),
70 NI_NUMERICHOST
| NI_NUMERICSERV
);
72 printf ("%s: %s\n", str
, gai_strerror (val
));
74 printf ("%s: %s port %s\n", str
, hostbuf
, servbuf
);
79 static int run (int family
, const char *hostname
, const char *service
)
81 struct addrinfo hints
, *res
;
82 const struct addrinfo
*ptr
;
85 memset (&hints
, 0, sizeof (hints
));
86 hints
.ai_family
= family
;
87 hints
.ai_socktype
= SOCK_DGRAM
;
88 hints
.ai_flags
= ai_flags
;
92 ret
= getaddrinfo (hostname
, service
, &hints
, &res
);
95 fprintf (stderr
, "%s (port %s): %s\n", hostname
, service
,
100 for (ptr
= res
; ptr
!= NULL
; ptr
= ptr
->ai_next
)
102 struct sockaddr_storage addr
;
103 socklen_t addrlen
= sizeof (addr
);
104 StunUsageBindReturn val
;
106 printaddr ("Server address", ptr
->ai_addr
, ptr
->ai_addrlen
);
108 val
= stun_usage_bind_run (ptr
->ai_addr
, ptr
->ai_addrlen
,
109 (struct sockaddr
*)&addr
, &addrlen
);
111 fprintf (stderr
, "%d\n", val
);
114 printaddr ("Mapped address", (struct sockaddr
*)&addr
, addrlen
);
124 int main (int argc
, char *argv
[])
126 static const struct option opts
[] =
128 { "ipv4", no_argument
, NULL
, '4' },
129 { "ipv6", no_argument
, NULL
, '6' },
130 { "help", no_argument
, NULL
, 'h' },
131 { "numeric", no_argument
, NULL
, 'n' },
132 { "version", no_argument
, NULL
, 'V' },
135 const char *server
= NULL
, *port
= NULL
;
136 int family
= AF_UNSPEC
;
140 int val
= getopt_long (argc
, argv
, "46hnV", opts
, NULL
);
155 printf ("Usage: %s [-4|-6] <server> [port number]\n"
156 "Performs STUN Binding Discovery\n"
158 " -4, --ipv4 Force IP version 4\n"
159 " -6, --ipv6 Force IP version 6\n"
160 " -n, --numeric Server in numeric form\n"
165 ai_flags
|= AI_NUMERICHOST
;
169 printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
179 server
= argv
[optind
++];
181 port
= argv
[optind
++];
184 fprintf (stderr
, "%s: extra parameter `%s'\n", argv
[0], argv
[optind
]);
188 return run (family
, server
, port
) ? 1 : 0;