common setup
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@3307 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
122
appl/test/common.c
Normal file
122
appl/test/common.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 1997 Kungliga Tekniska H<>gskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by Kungliga Tekniska
|
||||
* H<>gskolan and its contributors.
|
||||
*
|
||||
* 4. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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.
|
||||
*/
|
||||
|
||||
#include "test_locl.h"
|
||||
|
||||
RCSID("$Id$");
|
||||
|
||||
static int help_flag;
|
||||
static int version_flag;
|
||||
static char *port_str;
|
||||
char *service = SERVICE;
|
||||
|
||||
static struct getargs args[] = {
|
||||
{ "port", 'p', arg_string, &port_str, "port to listen to", "port" },
|
||||
{ "service", 's', arg_string, &service, "service to use", "service" },
|
||||
{ "help", 'h', arg_flag, &help_flag },
|
||||
{ "version", 0, arg_flag, &version_flag }
|
||||
};
|
||||
|
||||
static int num_args = sizeof(args) / sizeof(args[0]);
|
||||
|
||||
static void
|
||||
server_usage(int code, struct getargs *args, int num_args)
|
||||
{
|
||||
arg_printusage(args, num_args, "");
|
||||
exit(code);
|
||||
}
|
||||
|
||||
static void
|
||||
client_usage(int code, struct getargs *args, int num_args)
|
||||
{
|
||||
arg_printusage(args, num_args, "host");
|
||||
exit(code);
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
common_setup(krb5_context *context, int *argc, char **argv,
|
||||
void (*usage)(int, struct getargs*, int))
|
||||
{
|
||||
int port;
|
||||
*argc = krb5_program_setup(context, *argc, argv, args, num_args, usage);
|
||||
|
||||
if(help_flag)
|
||||
(*usage)(0, args, num_args);
|
||||
if(version_flag)
|
||||
krb5_errx(*context, 0, "%s", heimdal_version);
|
||||
|
||||
if(port_str){
|
||||
struct servent *s = getservbyname(port_str, "tcp");
|
||||
if(s)
|
||||
port = s->s_port;
|
||||
else {
|
||||
char *ptr;
|
||||
|
||||
port = strtol (optarg, &ptr, 10);
|
||||
if (port == 0 && ptr == optarg)
|
||||
errx (1, "Bad port `%s'", optarg);
|
||||
port = htons(port);
|
||||
}
|
||||
}
|
||||
|
||||
if (port == 0)
|
||||
port = krb5_getportbyname (*context, PORT, "tcp", 4711);
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
int
|
||||
server_setup(krb5_context *context, int argc, char **argv)
|
||||
{
|
||||
int port = common_setup(context, &argc, argv, server_usage);
|
||||
if(argv[argc] != NULL)
|
||||
server_usage(1, args, num_args);
|
||||
return port;
|
||||
}
|
||||
|
||||
int
|
||||
client_setup(krb5_context *context, int *argc, char **argv)
|
||||
{
|
||||
int optind = *argc;
|
||||
int port = common_setup(context, &optind, argv, client_usage);
|
||||
if(*argc - optind != 1)
|
||||
client_usage(1, args, num_args);
|
||||
*argc = optind;
|
||||
return port;
|
||||
}
|
Reference in New Issue
Block a user