xref: /freebsd/crypto/krb5/src/lib/rpc/unit-test/rpc_test_svc.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert #include "rpc_test.h"
2*7f2fe78bSCy Schubert #include <stdio.h>
3*7f2fe78bSCy Schubert #include <string.h>
4*7f2fe78bSCy Schubert #include <stdlib.h> /* getenv, exit */
5*7f2fe78bSCy Schubert #include <sys/types.h>
6*7f2fe78bSCy Schubert #include <syslog.h>
7*7f2fe78bSCy Schubert 
8*7f2fe78bSCy Schubert /* States a server can be in wrt request */
9*7f2fe78bSCy Schubert 
10*7f2fe78bSCy Schubert #define	_IDLE 0
11*7f2fe78bSCy Schubert #define	_SERVED 1
12*7f2fe78bSCy Schubert 
13*7f2fe78bSCy Schubert static int _rpcsvcstate = _IDLE;	/* Set when a request is serviced */
14*7f2fe78bSCy Schubert static int _rpcsvccount = 0;		/* Number of requests being serviced */
15*7f2fe78bSCy Schubert 
16*7f2fe78bSCy Schubert void
rpc_test_prog_1_svc(rqstp,transp)17*7f2fe78bSCy Schubert rpc_test_prog_1_svc(rqstp, transp)
18*7f2fe78bSCy Schubert 	struct svc_req *rqstp;
19*7f2fe78bSCy Schubert 	SVCXPRT *transp;
20*7f2fe78bSCy Schubert {
21*7f2fe78bSCy Schubert 	union {
22*7f2fe78bSCy Schubert 		char *rpc_test_echo_1_arg;
23*7f2fe78bSCy Schubert 	} argument;
24*7f2fe78bSCy Schubert 	char *result;
25*7f2fe78bSCy Schubert 	bool_t (*xdr_argument)(), (*xdr_result)();
26*7f2fe78bSCy Schubert 	char *(*local)();
27*7f2fe78bSCy Schubert 
28*7f2fe78bSCy Schubert 	_rpcsvccount++;
29*7f2fe78bSCy Schubert 	switch (rqstp->rq_proc) {
30*7f2fe78bSCy Schubert 	case NULLPROC:
31*7f2fe78bSCy Schubert 		(void) svc_sendreply(transp, xdr_void,
32*7f2fe78bSCy Schubert 			(char *)NULL);
33*7f2fe78bSCy Schubert 		_rpcsvccount--;
34*7f2fe78bSCy Schubert 		_rpcsvcstate = _SERVED;
35*7f2fe78bSCy Schubert 		return;
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert 	case RPC_TEST_ECHO:
38*7f2fe78bSCy Schubert 		xdr_argument = xdr_wrapstring;
39*7f2fe78bSCy Schubert 		xdr_result = xdr_wrapstring;
40*7f2fe78bSCy Schubert 		local = (char *(*)()) rpc_test_echo_1_svc;
41*7f2fe78bSCy Schubert 		break;
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert 	default:
44*7f2fe78bSCy Schubert 		svcerr_noproc(transp);
45*7f2fe78bSCy Schubert 		_rpcsvccount--;
46*7f2fe78bSCy Schubert 		_rpcsvcstate = _SERVED;
47*7f2fe78bSCy Schubert 		return;
48*7f2fe78bSCy Schubert 	}
49*7f2fe78bSCy Schubert 	(void) memset(&argument, 0, sizeof (argument));
50*7f2fe78bSCy Schubert 	if (!svc_getargs(transp, xdr_argument, &argument)) {
51*7f2fe78bSCy Schubert 		svcerr_decode(transp);
52*7f2fe78bSCy Schubert 		_rpcsvccount--;
53*7f2fe78bSCy Schubert 		_rpcsvcstate = _SERVED;
54*7f2fe78bSCy Schubert 		return;
55*7f2fe78bSCy Schubert 	}
56*7f2fe78bSCy Schubert 	result = (*local)(&argument, rqstp);
57*7f2fe78bSCy Schubert 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
58*7f2fe78bSCy Schubert 		svcerr_systemerr(transp);
59*7f2fe78bSCy Schubert 	}
60*7f2fe78bSCy Schubert 	if (!svc_freeargs(transp, xdr_argument, &argument)) {
61*7f2fe78bSCy Schubert 		syslog(LOG_ERR, "unable to free arguments");
62*7f2fe78bSCy Schubert 		exit(1);
63*7f2fe78bSCy Schubert 	}
64*7f2fe78bSCy Schubert 	_rpcsvccount--;
65*7f2fe78bSCy Schubert 	_rpcsvcstate = _SERVED;
66*7f2fe78bSCy Schubert 	return;
67*7f2fe78bSCy Schubert }
68