/*
 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
 * unrestricted use provided that this legend is included on all tape
 * media and as a part of the software program in whole or part.  Users
 * may copy or modify Sun RPC without charge, but are not authorized
 * to license or distribute it to anyone else except as part of a product or
 * program developed by the user.
 * 
 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 * 
 * Sun RPC is provided with no support and without any obligation on the
 * part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 * 
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
 * OR ANY PART THEREOF.
 * 
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even if
 * Sun has been advised of the possibility of such damages.
 * 
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */

#if !defined(lint) && defined(SCCSIDS)
static char sccsid[] = "@(#)clnt_tli.c  1.3  92/06/22  Copyr 1988 Sun Micro";
#endif

#include <stdio.h>
#include <rpc/rpc.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netconfig.h>
#include <errno.h>
#include "test.h"

#define	TESTBUFSIZE 33000

int fd;
extern char buf[];
extern int t_errno;
extern void *__rpc_setconf();
extern struct netconfig *__rpc_getconf();
struct netbuf *svcaddr;			/* servers address */

main(argc, argv)
	int argc;
	char *argv[];
{
	extern char host[];
	extern char transport[];
	extern struct netconfig *nconf;
	int i;
	char msgchar;
	struct t_bind *tbind;			/* bind info */
	struct t_call *sndcall, *rcvcall;
	void *handle;

	parseArgs(argc, argv);

	handle = __rpc_setconf(transport);
	nconf = __rpc_getconf(handle);
	if (nconf == NULL) {
		fprintf(stderr, "Invalid transport type: %s\n", transport);
		exit(1);
	}
	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
		perror("could not create socket");
		exit(1);
	}
	/*
	 * Get the address of the server
	 */
	tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
	if (tbind == (struct t_bind *)NULL) {
		fprintf(stderr, "could not get memory\n");
		exit(1);
	}

	svcaddr = &tbind->addr;
	if (rpcb_getaddr(PERF_TLI, VERS_ONE, nconf, svcaddr, host) == FALSE) {
		(void) t_free((char *)tbind, T_BIND);
		/* appropriate error number is set by rpcbind libraries */
		perror("could not get the address\n");
		exit(1);
	}
/*	A bug since svcaddr is still pointing here
	(void) t_free((char *)tbind, T_BIND);
*/ 

	if (t_bind(fd, (struct t_bind *)NULL, (struct t_bind *)NULL) == -1) {
		fprintf(stderr, "could not bind\n");
		exit(1);
	}
	if (nconf->nc_semantics != NC_TPI_CLTS) {
		/*
		 * Connect only if state is IDLE and svcaddr known
		 */
		rcvcall = (struct t_call *)t_alloc(fd, T_CALL, T_OPT|T_ADDR);
		sndcall = (struct t_call *)t_alloc(fd, T_CALL, T_OPT);	
	if ((rcvcall == NULL) || (sndcall == NULL)) {
			fprintf(stderr, "could not allocate memory\n");
			exit(1);
		}
		/*
		 * The underlying TLI TCP implementation sends back
		 * the options, even if the opt.maxlen = 0. A BUG.
		 */
		rcvcall->udata.maxlen = 0;
		sndcall->addr = *svcaddr;
		/*
		 * Even NULL could have sufficed for rcvcall, because
		 * the address returned is same for all cases except
		 * for the broadcast case, and hence required.
		 */
		if (t_connect(fd, sndcall, rcvcall) == -1) {
			perror("cannot connect ");
			exit(1);
		}
	}
	msgchar = transport[0];
	for (i = 0; i < TESTBUFSIZE - 1; i++)
		buf[i] = msgchar;
	buf[TESTBUFSIZE - 1] = NULL;

	for (i = 0; i < argc; i++)
		printf("%s ", argv[i]);
	printf("\n");

	runTests();
	exit(0);
}
