Version 0.1.0
[sipe-libnice.git] / stun / tools / stunbdc.c
blobc39f3f42a86537dd8cc371f6d45339786c47b3ea
1 /*
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
17 * License.
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.
24 * Contributors:
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.
39 #ifdef HAVE_CONFIG_H
40 # include <config.h>
41 #endif
43 #ifndef _WIN32
44 #include <sys/socket.h>
45 #include <netdb.h>
47 #include <sys/types.h>
48 #include "stun/stunagent.h"
49 #include "stun/usages/bind.h"
51 #include <unistd.h>
52 #include <getopt.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
58 static int ai_flags = 0;
60 static void
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);
68 if (val)
69 printf ("%s: %s\n", str, gai_strerror (val));
70 else
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;
80 int ret = -1;
82 memset (&hints, 0, sizeof (hints));
83 hints.ai_family = family;
84 hints.ai_socktype = SOCK_DGRAM;
85 hints.ai_flags = ai_flags;
86 if (service == NULL)
87 service = "3478";
89 ret = getaddrinfo (hostname, service, &hints, &res);
90 if (ret)
92 fprintf (stderr, "%s (port %s): %s\n", hostname, service,
93 gai_strerror (ret));
94 return -1;
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);
107 if (val)
108 fprintf (stderr, "%d\n", val);
109 else
111 printaddr ("Mapped address", (struct sockaddr *)&addr, addrlen);
112 ret = 0;
116 freeaddrinfo (res);
117 return ret;
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' },
130 { NULL, 0, NULL, 0 }
132 const char *server = NULL, *port = NULL;
133 int family = AF_UNSPEC;
135 for (;;)
137 int val = getopt_long (argc, argv, "46hnV", opts, NULL);
138 if (val == EOF)
139 break;
141 switch (val)
143 case '4':
144 family = AF_INET;
145 break;
147 case '6':
148 family = AF_INET6;
149 break;
151 case 'h':
152 printf ("Usage: %s [-4|-6] <server> [port number]\n"
153 "Performs STUN Binding Discovery\n"
154 "\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"
158 "\n", argv[0]);
159 return 0;
161 case 'n':
162 ai_flags |= AI_NUMERICHOST;
163 break;
165 case 'V':
166 printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
167 PACKAGE, VERSION);
168 return 0;
170 default:
171 return 2;
175 if (optind < argc)
176 server = argv[optind++];
177 if (optind < argc)
178 port = argv[optind++];
179 if (optind < argc)
181 fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[optind]);
182 return 2;
185 return run (family, server, port) ? 1 : 0;
187 #else
188 int main () {
189 return 0;
191 #endif