xref: /aosp_15_r20/external/ltp/testcases/network/rpc/rpc-tirpc/tests_pack/rpc_svc_2/rpc_svc_2.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2 * Copyright (c) Bull S.A.  2007 All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * History:
24 * Created by: Cyril Lacabanne ([email protected])
25 *
26 */
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <utmp.h>
31 #include "rpc.h"
32 
33 //Standard define
34 #define VERSNUM 1
35 //Complex procs
36 #define CALCPROC   10000
37 
38 void rcp_service(register struct svc_req *rqstp, register SVCXPRT * transp);
39 
40 struct datas {
41 	double a;
42 	double b;
43 	double c;
44 } argument;
45 
46 //XDR Struct function
xdr_datas(XDR * pt_xdr,struct datas * pt)47 bool_t xdr_datas(XDR * pt_xdr, struct datas *pt)
48 {
49 	return (xdr_double(pt_xdr, &(pt->a)) &&
50 		xdr_double(pt_xdr, &(pt->b)) && xdr_double(pt_xdr, &(pt->c)));
51 }
52 
53 //****************************************//
54 //***           Main Function          ***//
55 //****************************************//
main(int argn,char * argc[])56 int main(int argn, char *argc[])
57 {
58 	//Server parameter is : argc[1] : Server Program Number
59 	//                                          others arguments depend on server program
60 	int run_mode = 1;
61 	int progNum = atoi(argc[1]);
62 	SVCXPRT *transpTCP = NULL;
63 	SVCXPRT *transpUDP = NULL;
64 	//char *simplePing();
65 
66 	//Initialization
67 	pmap_unset(progNum, VERSNUM);
68 	svc_unregister(progNum, VERSNUM);
69 
70 	//registerrpc(progNum, VERSNUM, PROCSIMPLEPING,
71 	//                  simplePing, xdr_int, xdr_int);
72 	transpTCP = svctcp_create(RPC_ANYSOCK, 1000, 1000);
73 	transpUDP = svcudp_create(RPC_ANYSOCK);
74 
75 	if (run_mode) {
76 		printf("SVC TCP : %p\n", transpTCP);
77 		printf("SVC UDP : %p\n", transpUDP);
78 	}
79 
80 	if (!svc_register
81 	    (transpTCP, progNum, VERSNUM, (void *)rcp_service, IPPROTO_TCP)) {
82 		fprintf(stderr, "svc_register: error (TCP)\n");
83 	}
84 
85 	if (!svc_register
86 	    (transpUDP, progNum, VERSNUM, (void *)rcp_service, IPPROTO_UDP)) {
87 		fprintf(stderr, "svc_register: error (UDP)\n");
88 	}
89 
90 	svc_run();
91 	fprintf(stderr, "Error: svc_run returned!\n");
92 	exit(1);
93 }
94 
95 //****************************************//
96 //***        Remotes Procedures        ***//
97 //****************************************//
98 
calcProc(struct datas * dt,SVCXPRT * svc)99 char *calcProc(struct datas *dt, SVCXPRT * svc)
100 {
101 	//Makes a + b * c from structure dt and returns double
102 	//printf("*** In calcProc ***\n");
103 	static double result = 0;
104 	result = dt->a + (dt->b * dt->c);
105 	//printf("Received : %lf, %lf, %lf\n", dt->a, dt->b, dt->c);
106 	return (char *)&result;
107 }
108 
109 //****************************************//
110 //***       Dispatch Function          ***//
111 //****************************************//
rcp_service(register struct svc_req * rqstp,register SVCXPRT * transp)112 void rcp_service(register struct svc_req *rqstp, register SVCXPRT * transp)
113 {
114 	//printf("* in Dispatch Func.\n");
115 
116 	char *result;
117 	xdrproc_t xdr_argument;
118 	xdrproc_t xdr_result;
119 	char *(*proc) (struct datas *, SVCXPRT *);
120 
121 	switch (rqstp->rq_proc) {
122 	case CALCPROC:
123 		{
124 			//printf("** in CALCPROC dispatch Func.\n");
125 			xdr_argument = (xdrproc_t) xdr_datas;
126 			xdr_result = (xdrproc_t) xdr_double;
127 			proc = (char *(*)(struct datas *, SVCXPRT *))calcProc;
128 			break;
129 		}
130 	default:
131 		{
132 			//printf("** in NOT DEFINED dispatch Func.\n");
133 			//Proc is unavaible
134 			svcerr_noproc(transp);
135 			return;
136 		}
137 	}
138 
139 	memset((char *)&argument, (int)0, sizeof(argument));
140 	if (svc_getargs(transp, xdr_argument, (char *)&argument) == FALSE) {
141 		svcerr_decode(transp);
142 		return;
143 	}
144 
145 	result = (char *)(*proc) ((struct datas *)&argument, transp);
146 
147 	if ((result != NULL)
148 	    && (svc_sendreply(transp, xdr_result, result) == FALSE)) {
149 		svcerr_systemerr(transp);
150 	}
151 	if (svc_freeargs(transp, xdr_argument, (char *)&argument) == FALSE) {
152 		(void)fprintf(stderr, "unable to free arguments\n");
153 		exit(1);
154 	}
155 }
156