Add support for windows's compilation through MinGW
[sipe-libnice.git] / stun / tools / stunbdc.c
blobb539ab99dba93348c399df7b9c564661c3504bba
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 #ifdef _WIN32
44 # include <winsock2.h>
45 #else
46 # include <sys/socket.h>
47 # include <netdb.h>
48 #endif
50 #include <sys/types.h>
51 #include "stun/stunagent.h"
52 #include "stun/usages/bind.h"
54 #include <unistd.h>
55 #include <getopt.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
61 static int ai_flags = 0;
63 static void
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);
71 if (val)
72 printf ("%s: %s\n", str, gai_strerror (val));
73 else
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;
83 int ret = -1;
85 memset (&hints, 0, sizeof (hints));
86 hints.ai_family = family;
87 hints.ai_socktype = SOCK_DGRAM;
88 hints.ai_flags = ai_flags;
89 if (service == NULL)
90 service = "3478";
92 ret = getaddrinfo (hostname, service, &hints, &res);
93 if (ret)
95 fprintf (stderr, "%s (port %s): %s\n", hostname, service,
96 gai_strerror (ret));
97 return -1;
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);
110 if (val)
111 fprintf (stderr, "%d\n", val);
112 else
114 printaddr ("Mapped address", (struct sockaddr *)&addr, addrlen);
115 ret = 0;
119 freeaddrinfo (res);
120 return ret;
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' },
133 { NULL, 0, NULL, 0 }
135 const char *server = NULL, *port = NULL;
136 int family = AF_UNSPEC;
138 for (;;)
140 int val = getopt_long (argc, argv, "46hnV", opts, NULL);
141 if (val == EOF)
142 break;
144 switch (val)
146 case '4':
147 family = AF_INET;
148 break;
150 case '6':
151 family = AF_INET6;
152 break;
154 case 'h':
155 printf ("Usage: %s [-4|-6] <server> [port number]\n"
156 "Performs STUN Binding Discovery\n"
157 "\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"
161 "\n", argv[0]);
162 return 0;
164 case 'n':
165 ai_flags |= AI_NUMERICHOST;
166 break;
168 case 'V':
169 printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
170 PACKAGE, VERSION);
171 return 0;
173 default:
174 return 2;
178 if (optind < argc)
179 server = argv[optind++];
180 if (optind < argc)
181 port = argv[optind++];
182 if (optind < argc)
184 fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[optind]);
185 return 2;
188 return run (family, server, port) ? 1 : 0;