1 /* 2 * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "test_locl.h" 35 36 RCSID("$Id: common.c,v 1.10 2000/02/12 21:30:47 assar Exp $"); 37 38 static int help_flag; 39 static int version_flag; 40 static char *port_str; 41 char *service = SERVICE; 42 int fork_flag; 43 44 static struct getargs args[] = { 45 { "port", 'p', arg_string, &port_str, "port to listen to", "port" }, 46 { "service", 's', arg_string, &service, "service to use", "service" }, 47 { "fork", 'f', arg_flag, &fork_flag, "do fork" }, 48 { "help", 'h', arg_flag, &help_flag }, 49 { "version", 0, arg_flag, &version_flag } 50 }; 51 52 static int num_args = sizeof(args) / sizeof(args[0]); 53 54 static void 55 server_usage(int code, struct getargs *args, int num_args) 56 { 57 arg_printusage(args, num_args, NULL, ""); 58 exit(code); 59 } 60 61 static void 62 client_usage(int code, struct getargs *args, int num_args) 63 { 64 arg_printusage(args, num_args, NULL, "host"); 65 exit(code); 66 } 67 68 69 static int 70 common_setup(krb5_context *context, int *argc, char **argv, 71 void (*usage)(int, struct getargs*, int)) 72 { 73 int port = 0; 74 *argc = krb5_program_setup(context, *argc, argv, args, num_args, usage); 75 76 if(help_flag) 77 (*usage)(0, args, num_args); 78 if(version_flag) { 79 print_version(NULL); 80 exit(0); 81 } 82 83 if(port_str){ 84 struct servent *s = roken_getservbyname(port_str, "tcp"); 85 if(s) 86 port = s->s_port; 87 else { 88 char *ptr; 89 90 port = strtol (port_str, &ptr, 10); 91 if (port == 0 && ptr == port_str) 92 errx (1, "Bad port `%s'", port_str); 93 port = htons(port); 94 } 95 } 96 97 if (port == 0) 98 port = krb5_getportbyname (*context, PORT, "tcp", 4711); 99 100 return port; 101 } 102 103 int 104 server_setup(krb5_context *context, int argc, char **argv) 105 { 106 int port = common_setup(context, &argc, argv, server_usage); 107 if(argv[argc] != NULL) 108 server_usage(1, args, num_args); 109 return port; 110 } 111 112 int 113 client_setup(krb5_context *context, int *argc, char **argv) 114 { 115 int optind = *argc; 116 int port = common_setup(context, &optind, argv, client_usage); 117 if(*argc - optind != 1) 118 client_usage(1, args, num_args); 119 *argc = optind; 120 return port; 121 } 122 123 int 124 client_doit (const char *hostname, int port, const char *service, 125 int (*func)(int, const char *hostname, const char *service)) 126 { 127 struct addrinfo *ai, *a; 128 struct addrinfo hints; 129 int error; 130 char portstr[NI_MAXSERV]; 131 132 memset (&hints, 0, sizeof(hints)); 133 hints.ai_socktype = SOCK_STREAM; 134 hints.ai_protocol = IPPROTO_TCP; 135 136 snprintf (portstr, sizeof(portstr), "%u", ntohs(port)); 137 138 error = getaddrinfo (hostname, portstr, &hints, &ai); 139 if (error) { 140 errx (1, "%s: %s", hostname, gai_strerror(error)); 141 return -1; 142 } 143 144 for (a = ai; a != NULL; a = a->ai_next) { 145 int s; 146 147 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol); 148 if (s < 0) 149 continue; 150 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) { 151 warn ("connect(%s)", hostname); 152 close (s); 153 continue; 154 } 155 freeaddrinfo (ai); 156 return (*func) (s, hostname, service); 157 } 158 warnx ("failed to contact %s", hostname); 159 freeaddrinfo (ai); 160 return 1; 161 } 162