~ubuntu-branches/debian/squeeze/libnice/squeeze

« back to all changes in this revision

Viewing changes to stun/tools/stunbdc.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-01-04 17:45:34 UTC
  • Revision ID: james.westby@ubuntu.com-20090104174534-dh5u1pfonumqa99c
Tags: upstream-0.0.4
Import upstream version 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Nice GLib ICE library.
 
3
 *
 
4
 * (C) 2007 Nokia Corporation. All rights reserved.
 
5
 *  Contact: Rémi Denis-Courmont
 
6
 *
 
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/
 
11
 *
 
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
 
15
 * License.
 
16
 *
 
17
 * The Original Code is the Nice GLib ICE library.
 
18
 *
 
19
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 
20
 * Corporation. All Rights Reserved.
 
21
 *
 
22
 * Contributors:
 
23
 *   Rémi Denis-Courmont, Nokia
 
24
 *
 
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.
 
34
 */
 
35
 
 
36
#ifdef HAVE_CONFIG_H
 
37
# include <config.h>
 
38
#endif
 
39
 
 
40
#ifndef _WIN32
 
41
#include <sys/socket.h>
 
42
#include <netdb.h>
 
43
 
 
44
#include <sys/types.h>
 
45
#include "stun/stunagent.h"
 
46
#include "stun/usages/bind.h"
 
47
 
 
48
#include <unistd.h>
 
49
#include <getopt.h>
 
50
#include <errno.h>
 
51
 
 
52
#include <stdlib.h>
 
53
#include <stdio.h>
 
54
#include <string.h>
 
55
 
 
56
static int ai_flags = 0;
 
57
 
 
58
static void
 
59
printaddr (const char *str, const struct sockaddr *addr, socklen_t addrlen)
 
60
{
 
61
  char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
 
62
 
 
63
  int val = getnameinfo (addr, addrlen, hostbuf, sizeof (hostbuf),
 
64
                         servbuf, sizeof (servbuf),
 
65
                         NI_NUMERICHOST | NI_NUMERICSERV);
 
66
  if (val)
 
67
    printf ("%s: %s\n", str, gai_strerror (val));
 
68
  else
 
69
    printf ("%s: %s port %s\n", str, hostbuf, servbuf);
 
70
}
 
71
 
 
72
 
 
73
 
 
74
static int run (int family, const char *hostname, const char *service)
 
75
{
 
76
  struct addrinfo hints, *res;
 
77
  const struct addrinfo *ptr;
 
78
  int ret = -1;
 
79
 
 
80
  memset (&hints, 0, sizeof (hints));
 
81
  hints.ai_family = family;
 
82
  hints.ai_socktype = SOCK_DGRAM;
 
83
  hints.ai_flags = ai_flags;
 
84
  if (service == NULL)
 
85
    service = "3478";
 
86
 
 
87
  ret = getaddrinfo (hostname, service, &hints, &res);
 
88
  if (ret)
 
89
  {
 
90
    fprintf (stderr, "%s (port %s): %s\n", hostname, service,
 
91
             gai_strerror (ret));
 
92
    return -1;
 
93
  }
 
94
 
 
95
  for (ptr = res; ptr != NULL; ptr = ptr->ai_next)
 
96
  {
 
97
    struct sockaddr_storage addr;
 
98
    socklen_t addrlen = sizeof (addr);
 
99
    StunUsageBindReturn val;
 
100
 
 
101
    printaddr ("Server address", ptr->ai_addr, ptr->ai_addrlen);
 
102
 
 
103
    val = stun_usage_bind_run (ptr->ai_addr, ptr->ai_addrlen,
 
104
                         (struct sockaddr *)&addr, &addrlen);
 
105
    if (val)
 
106
      fprintf (stderr, "%d\n", val);
 
107
    else
 
108
    {
 
109
      printaddr ("Mapped address", (struct sockaddr *)&addr, addrlen);
 
110
      ret = 0;
 
111
    }
 
112
  }
 
113
 
 
114
  freeaddrinfo (res);
 
115
  return ret;
 
116
}
 
117
 
 
118
 
 
119
int main (int argc, char *argv[])
 
120
{
 
121
  static const struct option opts[] =
 
122
  {
 
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' },
 
128
    { NULL,      0,           NULL, 0   }
 
129
  };
 
130
  const char *server = NULL, *port = NULL;
 
131
  int family = AF_UNSPEC;
 
132
 
 
133
  for (;;)
 
134
  {
 
135
    int val = getopt_long (argc, argv, "46hnV", opts, NULL);
 
136
    if (val == EOF)
 
137
      break;
 
138
 
 
139
    switch (val)
 
140
    {
 
141
      case '4':
 
142
        family = AF_INET;
 
143
        break;
 
144
 
 
145
      case '6':
 
146
        family = AF_INET6;
 
147
        break;
 
148
 
 
149
      case 'h':
 
150
        printf ("Usage: %s [-4|-6] <server> [port number]\n"
 
151
                "Performs STUN Binding Discovery\n"
 
152
                "\n"
 
153
                "  -4, --ipv4 Force IP version 4\n"
 
154
                "  -6, --ipv6 Force IP version 6\n"
 
155
            "\n", argv[0]);
 
156
        return 0;
 
157
 
 
158
      case 'n':
 
159
        ai_flags |= AI_NUMERICHOST;
 
160
        break;
 
161
 
 
162
      case 'V':
 
163
        printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
 
164
                PACKAGE, VERSION);
 
165
        return 0;
 
166
 
 
167
      default:
 
168
        return 2;
 
169
    }
 
170
  }
 
171
 
 
172
  if (optind < argc)
 
173
    server = argv[optind++];
 
174
  if (optind < argc)
 
175
    port = argv[optind++];
 
176
  if (optind < argc)
 
177
  {
 
178
    fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[optind]);
 
179
    return 2;
 
180
  }
 
181
 
 
182
  return run (family, server, port) ? 1 : 0;
 
183
}
 
184
#else
 
185
int main () {
 
186
  return 0;
 
187
}
 
188
#endif