Skip to content

Commit

Permalink
add summary mode (-s). from yoshfuji with modifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
itojun committed Oct 20, 2000
1 parent 8d44063 commit c61e980
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 51 deletions.
47 changes: 45 additions & 2 deletions kame/kame/bindtest/bindtest.1
Expand Up @@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: bindtest.1,v 1.4 2000/01/19 06:24:51 itojun Exp $
.\" $Id: bindtest.1,v 1.5 2000/10/20 16:42:06 itojun Exp $
.\"
.Dd October 6, 1999
.Dt BINDTEST 1
Expand All @@ -37,7 +37,7 @@
.\"
.Sh SYNOPSIS
.Nm
.Op Fl t
.Op Fl st
.Fl p Ar port
.\"
.Sh DESCRIPTION
Expand Down Expand Up @@ -84,6 +84,49 @@ TCP/UDP port number must be specified with
The
.Ar port
needs to be vacant.
.Pp
With
.Fl s ,
.Nm
generates summarized output instead of verbose output.
Here are legends for summarized output.
.Nm
will make a two set of
.Xr socket 2
and
.Xr bind 2 .
The first set is presented above the table.
The second set is presented on the lefthand side.
.Bl -tag -width "xxxx"
.It o
The second
.Xr bind 2
attempt succeed after the first
.Xr bind 2 .
.It x
.Xr bind 2
failed for the second set, with
.Dv EADDRINUSE .
.It !1
.Xr socket 2
failed for the first set.
Normally you will not see this, unless you run
.Nm
on IPv4-only/IPv6-only kernel.
.It !2
.Xr socket 2
failed for the second set.
Normally you will not see this, unless you run
.Nm
on IPv4-only/IPv6-only kernel.
.It ?1
.Xr bind 2
failed for the first set.
.It ?2
.Xr bind 2
failed for the second set, with some error other than
.Dv EADDRINUSE .
.El
.\"
.Sh RETURN VALUES
.Nm
Expand Down
159 changes: 110 additions & 49 deletions kame/kame/bindtest/bindtest.c
@@ -1,3 +1,31 @@
/*
* Copyright (C) 2000 USAGI Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without loop prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (C) 1999 WIDE Project.
* All rights reserved.
Expand Down Expand Up @@ -27,7 +55,7 @@
* SUCH DAMAGE.
*/
/*
* $Id: bindtest.c,v 1.8 1999/10/06 08:10:05 itojun Exp $
* $Id: bindtest.c,v 1.9 2000/10/20 16:42:06 itojun Exp $
*/

#include <sys/types.h>
Expand All @@ -44,17 +72,32 @@

#include <netinet/in.h>

static struct testitem{
const char *name;
const sa_family_t family;
const char *host;
struct addrinfo *res;
} testitems[] = {
{ "wild4", AF_INET, NULL, NULL },
{ "wild6", AF_INET6, NULL, NULL },
{ "wildm", AF_INET6, "::ffff:0.0.0.0", NULL },
{ "loop4", AF_INET, "127.0.0.1", NULL },
{ "loop6", AF_INET6, "::1", NULL },
{ "loopm", AF_INET6, "::ffff:127.0.0.1", NULL },
{ "one4", AF_INET, "0.0.0.1", NULL },
{ "onem", AF_INET6, "::ffff:0.0.0.1", NULL },
{ NULL, 0, NULL, NULL }
};

int main __P((int, char **));
static void usage __P((void));
static struct addrinfo *getres __P((int, const char *, const char *));
static const char *printres __P((struct addrinfo *));
static int test __P((const char *, struct addrinfo *, struct addrinfo *));
static int test __P((struct testitem *, struct testitem *));

static struct addrinfo *wild4, *wild6;
static struct addrinfo *loop4, *loop6;
static struct addrinfo *one4, *map4;
static char *port = NULL;
static int socktype = SOCK_DGRAM;
static int summary = 0;

int
main(argc, argv)
Expand All @@ -64,12 +107,16 @@ main(argc, argv)
int ch;
extern int optind;
extern char *optarg;
struct testitem *testi, *testj;

while ((ch = getopt(argc, argv, "p:t")) != EOF) {
while ((ch = getopt(argc, argv, "p:st")) != EOF) {
switch (ch) {
case 'p':
port = strdup(optarg);
break;
case 's':
summary = 1;
break;
case 't':
socktype = SOCK_STREAM;
break;
Expand All @@ -85,44 +132,37 @@ main(argc, argv)
#endif

if (port == NULL) {
errx(1, "no port specified");
/*NOTREACHED*/
usage();
exit(1);
}

wild4 = getres(AF_INET, NULL, port);
wild6 = getres(AF_INET6, NULL, port);
loop4 = getres(AF_INET, "127.0.0.1", port);
loop6 = getres(AF_INET6, "::1", port);
one4 = getres(AF_INET, "0.0.0.1", port);
map4 = getres(AF_INET6, "::ffff:127.0.0.1", port);
for (testi = testitems; testi->name; testi++)
testi->res = getres(testi->family, testi->host, port);

printf("starting tests, socktype = %s\n",
socktype == SOCK_DGRAM ? "SOCK_DGRAM" : "SOCK_STREAM");
#define TESTIT(x, y) test(#x " then " #y, (x), (y))
#define TESTALL(x) \
do { \
TESTIT(x, wild4); \
TESTIT(x, wild6); \
TESTIT(x, loop4); \
TESTIT(x, loop6); \
TESTIT(x, one4); \
TESTIT(x, map4); \
} while (0)

TESTALL(wild4);
TESTALL(wild6);
TESTALL(loop4);
TESTALL(loop6);
TESTALL(one4);
TESTALL(map4);
if (summary) {
for (testi = testitems; testi->name; testi++)
printf("\t%s", testi->name);
printf("\n");
}
for (testi = testitems; testi->name; testi++) {
if (summary)
printf("%s:", testi->name);
for (testj = testitems; testj->name; testj++)
test(testi, testj);
if (summary)
printf("\n");
}

exit(0);
}

static void
usage()
{
fprintf(stderr, "usage: bindtest [-t] -p port\n");

fprintf(stderr, "usage: bindtest [-st] -p port\n");
}

static struct addrinfo *
Expand Down Expand Up @@ -156,48 +196,69 @@ printres(res)
}

static int
test(title, a, b)
const char *title;
struct addrinfo *a;
struct addrinfo *b;
test(t1, t2)
struct testitem *t1;
struct testitem *t2;
{
struct addrinfo *a = t1->res;
struct addrinfo *b = t2->res;
int sa = -1, sb = -1;

printf("%s\n", title);
if (!summary)
printf("%s then %s\n", t1->name, t2->name);

#if 0
printf("\tallocating socket for %s\n", printres(a));
if (!summary)
printf("\tallocating socket for %s\n", printres(a));
#endif
sa = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
if (sa < 0) {
printf("\tfailed socket for %s, %s\n",
printres(a), strerror(errno));
if (summary)
printf("\tfailed socket for %s, %s\n",
printres(a), strerror(errno));
else
printf("\t!1");
goto fail;
}
#if 0
printf("\tallocating socket for %s\n", printres(b));
if (!summary)
printf("\tallocating socket for %s\n", printres(b));
#endif
sb = socket(b->ai_family, b->ai_socktype, b->ai_protocol);
if (sb < 0) {
printf("\tfailed socket for %s, %s\n",
printres(b), strerror(errno));
if (!summary)
printf("\tfailed socket for %s, %s\n",
printres(b), strerror(errno));
else
printf("\t!2");
goto fail;
}

printf("\tbind socket for %s\n", printres(a));
if (!summary)
printf("\tbind socket for %s\n", printres(a));
if (bind(sa, a->ai_addr, a->ai_addrlen) < 0) {
printf("\tfailed bind for %s, %s\n",
printres(a), strerror(errno));
if (!summary)
printf("\tfailed bind for %s, %s\n",
printres(a), strerror(errno));
else
printf("\t?1");
goto fail;
}

printf("\tbind socket for %s\n", printres(b));
if (!summary)
printf("\tbind socket for %s\n", printres(b));
if (bind(sb, b->ai_addr, b->ai_addrlen) < 0) {
printf("\tfailed bind for %s, %s\n",
printres(b), strerror(errno));
if (!summary)
printf("\tfailed bind for %s, %s\n",
printres(b), strerror(errno));
else
printf(errno == EADDRINUSE ? "\tx" : "\t?2");
goto fail;
}

if (summary)
printf("\to");

if (sa >= 0)
close(sa);
if (sb >= 0)
Expand Down

0 comments on commit c61e980

Please sign in to comment.