Commit 6f5e2791 authored by Tim-Philipp Müller's avatar Tim-Philipp Müller Committed by Olivier Crête

stun: tools: drop getopt for arg parsing for better windows compatibility

Just implement argument parsing ourselves here.

It's not really more lines of code than the getopt.h variant
and makes things build on Windows with MSVC without having to
ship a separate getopt implementation.
parent 94a7d8eb
......@@ -81,7 +81,7 @@ cdata.set('NICEAPI_EXPORT', true,
description: 'Public library function implementation')
# headers
foreach h : ['arpa/inet.h', 'net/in.h', 'netdb.h', 'ifaddrs.h']
foreach h : ['arpa/inet.h', 'net/in.h', 'netdb.h', 'ifaddrs.h', 'unistd.h']
if cc.has_header(h)
define = 'HAVE_' + h.underscorify().to_upper()
cdata.set(define, 1)
......
......@@ -51,8 +51,9 @@
#include "stun/stunagent.h"
#include "stun/usages/bind.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <getopt.h>
#endif
#include <stdlib.h>
#include <stdio.h>
......@@ -126,65 +127,58 @@ static int run (int family, const char *hostname, const char *service)
int main (int argc, char *argv[])
{
static const struct option opts[] =
{
{ "ipv4", no_argument, NULL, '4' },
{ "ipv6", no_argument, NULL, '6' },
{ "help", no_argument, NULL, 'h' },
{ "numeric", no_argument, NULL, 'n' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
const char *server = NULL, *port = NULL;
int family = AF_UNSPEC;
int i;
for (;;)
for (i = 1; i < argc; ++i)
{
int val = getopt_long (argc, argv, "46hnV", opts, NULL);
if (val == EOF)
const char *arg = argv[i];
if (arg[0] != '-')
break;
switch (val)
if (strcmp (arg, "--ipv4") == 0 || strcmp (arg, "-4") == 0)
{
family = AF_INET;
}
else if (strcmp (arg, "--ipv6") == 0 || strcmp (arg, "-6") == 0)
{
family = AF_INET6;
}
else if (strcmp (arg, "--help") == 0 || strcmp (arg, "-h") == 0)
{
printf ("Usage: %s [-4|-6] <server> [port number]\n"
"Performs STUN Binding Discovery\n"
"\n"
" -4, --ipv4 Force IP version 4\n"
" -6, --ipv6 Force IP version 6\n"
" -n, --numeric Server in numeric form\n"
"\n", argv[0]);
return 0;
}
else if (strcmp (arg, "--numeric") == 0 || strcmp (arg, "-n") == 0)
{
ai_flags |= AI_NUMERICHOST;
}
else if (strcmp (arg, "--version") == 0 || strcmp (arg, "-V") == 0)
{
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
case 'h':
printf ("Usage: %s [-4|-6] <server> [port number]\n"
"Performs STUN Binding Discovery\n"
"\n"
" -4, --ipv4 Force IP version 4\n"
" -6, --ipv6 Force IP version 6\n"
" -n, --numeric Server in numeric form\n"
"\n", argv[0]);
return 0;
case 'n':
ai_flags |= AI_NUMERICHOST;
break;
case 'V':
printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
PACKAGE, VERSION);
return 0;
default:
return 2;
printf ("stunbcd: STUN Binding Discovery client (%s v%s)\n",
PACKAGE, VERSION);
return 0;
} else {
fprintf (stderr, "Unexpected command line argument '%s'", arg);
return 2;
}
}
if (optind < argc)
server = argv[optind++];
if (optind < argc)
port = argv[optind++];
if (optind < argc)
if (i < argc)
server = argv[i++];
if (i < argc)
port = argv[i++];
if (i < argc)
{
fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[optind]);
fprintf (stderr, "%s: extra parameter `%s'\n", argv[0], argv[i]);
return 2;
}
......
......@@ -60,9 +60,15 @@
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#else
# define close(fd) _close(fd)
#endif
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#ifndef SOL_IP
# define SOL_IP IPPROTO_IP
......@@ -280,29 +286,31 @@ int main (int argc, char *argv[])
{
int family = AF_INET;
unsigned port = IPPORT_STUN;
int i;
for (;;)
for (i = 1; i < argc; ++i)
{
int c = getopt (argc, argv, "46");
if (c == EOF)
break;
const char *arg = argv[i];
switch (c)
if (strcmp (arg, "-4") == 0)
{
default:
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
family = AF_INET;
}
else if (strcmp (arg, "-6") == 0)
{
family = AF_INET6;
}
else if (arg[0] < '0' || arg[0] > '9')
{
fprintf (stderr, "Unexpected command line argument '%s'", arg);
}
else
{
port = atoi (arg);
break;
}
}
if (optind < argc)
port = atoi (argv[optind++]);
signal (SIGINT, exit_handler);
signal (SIGTERM, exit_handler);
return run (family, IPPROTO_UDP, port) ? EXIT_FAILURE : EXIT_SUCCESS;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment