Home | History | Annotate | Download | only in rpc_svc_1
      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 (Cyril.Lacabanne (at) bull.net)
     25 *
     26 */
     27 
     28 #include <stdio.h>
     29 #include <rpc/rpc.h>
     30 #include <utmp.h>
     31 
     32 //Standard define
     33 #define VERSNUM 1
     34 //Simple Ping proc
     35 #define PROCSIMPLEPING	1
     36 //RPC Error proc
     37 #define SVCGETCALLTEST	2
     38 #define PROGSYSERROR	10
     39 #define PROGAUTHERROR	100
     40 #define PROGWKAUTHERROR	101
     41 //DataInt procs
     42 #define INTPROCNUM 1000
     43 #define DBLPROCNUM 2000
     44 #define LNGPROCNUM 3000
     45 #define STRPROCNUM 4000
     46 #define SVCGETARGSPROC 5000
     47 
     48 void rcp_service(register struct svc_req *rqstp, register SVCXPRT * transp);
     49 
     50 //static int argument;
     51 union u_argument {
     52 	int varInt;
     53 	double dbl;
     54 	long lng;
     55 	char *str;
     56 } argument;
     57 
     58 //****************************************//
     59 //***           Main Function          ***//
     60 //****************************************//
     61 int main(int argn, char *argc[])
     62 {
     63 	int progNum = atoi(argc[1]);
     64 	SVCXPRT *transpTCP = NULL;
     65 	SVCXPRT *transpUDP = NULL;
     66 	//char *simplePing();
     67 
     68 	//Initialization
     69 	pmap_unset(progNum, VERSNUM);
     70 	svc_unregister(progNum, VERSNUM);
     71 
     72 	//registerrpc(progNum, VERSNUM, PROCSIMPLEPING,
     73 	//                  simplePing, xdr_int, xdr_int);
     74 	transpTCP = svctcp_create(RPC_ANYSOCK, 1000, 1000);
     75 	transpUDP = svcudp_create(RPC_ANYSOCK);
     76 
     77 	if (!svc_register
     78 	    (transpTCP, progNum, VERSNUM, (void *)rcp_service, IPPROTO_TCP)) {
     79 		fprintf(stderr, "svc_register: error (TCP)\n");
     80 	}
     81 
     82 	if (!svc_register
     83 	    (transpUDP, progNum, VERSNUM, (void *)rcp_service, IPPROTO_UDP)) {
     84 		fprintf(stderr, "svc_register: error (UDP)\n");
     85 	}
     86 
     87 	svc_run();
     88 	fprintf(stderr, "Error: svc_run returned!\n");
     89 	exit(1);
     90 }
     91 
     92 //****************************************//
     93 //***        Remotes Procedures        ***//
     94 //****************************************//
     95 char *simplePing(union u_argument *inVar, SVCXPRT * transp)
     96 {
     97 	static int result;
     98 	result = inVar->varInt;
     99 	return (char *)&result;
    100 }
    101 
    102 char *svc_getcaller_test(union u_argument *inVar, SVCXPRT * transp)
    103 {
    104 	//In this function we test svc_getcaller function basically (simple call)
    105 	struct sockaddr_in *sa = NULL;
    106 	static int result;
    107 
    108 	sa = svc_getcaller(transp);
    109 	//If the result is not NULL we consider that function call succeeds
    110 	//so returns 0 (PASS)
    111 	result = (sa != NULL) ? 0 : 1;
    112 	return (char *)&result;
    113 }
    114 
    115 char *intTestProc(union u_argument *in, SVCXPRT * transp)
    116 {
    117 	//printf("*** in intTestProc.\n");
    118 	//returns what received
    119 	static int result;
    120 	result = in->varInt;
    121 	//printf("%d\n", result);
    122 	return (char *)&result;
    123 }
    124 
    125 char *lngTestProc(union u_argument *in, SVCXPRT * transp)
    126 {
    127 	//printf("*** in lngTestProc.\n");
    128 	//returns what received
    129 	static long result;
    130 	result = in->lng;
    131 	//printf("%ld\n", result);
    132 	return (char *)&result;
    133 }
    134 
    135 char *dblTestProc(union u_argument *in, SVCXPRT * transp)
    136 {
    137 	//printf("*** in dblTestProc.\n");
    138 	//returns what received
    139 	static double result;
    140 	result = in->dbl;
    141 	//printf("%lf\n", result);
    142 	return (char *)&result;
    143 }
    144 
    145 char *strTestProc(union u_argument *in, SVCXPRT * transp)
    146 {
    147 	//printf("*** in strTestProc.\n");
    148 	//returns what received
    149 	static char *result;
    150 	result = in->str;
    151 	//printf("%s\n", result);
    152 	return (char *)&result;
    153 }
    154 
    155 char *svcGetargsProc(union u_argument *in, SVCXPRT * transp)
    156 {
    157 	//printf("*** in svcGetargsProc.\n");
    158 	//returns what received inside this procedure : test svc_getargs function
    159 	union u_argument args;
    160 
    161 	static char *result;
    162 	result = in->str;
    163 
    164 	if ((svc_getargs(transp, (xdrproc_t) xdr_int, (char *)&args)) == FALSE) {
    165 		svcerr_decode(transp);
    166 		return NULL;
    167 	}
    168 	//printf("%s\n", result);
    169 	return (char *)&result;
    170 }
    171 
    172 //****************************************//
    173 //***       Dispatch Function          ***//
    174 //****************************************//
    175 void rcp_service(register struct svc_req *rqstp, register SVCXPRT * transp)
    176 {
    177 	//printf("* in Dispatch Func.\n");
    178 	/*union {
    179 	   int varIn;
    180 	   } argument; */
    181 
    182 	char *result;
    183 	xdrproc_t xdr_argument;
    184 	xdrproc_t xdr_result;
    185 	char *(*proc) (union u_argument *, SVCXPRT *);
    186 	enum auth_stat why;
    187 
    188 	switch (rqstp->rq_proc) {
    189 	case PROCSIMPLEPING:
    190 		{
    191 			//printf("** in PROCSIMPLEPING dispatch Func.\n");
    192 			xdr_argument = (xdrproc_t) xdr_int;
    193 			xdr_result = (xdrproc_t) xdr_int;
    194 			proc =
    195 			    (char *(*)(union u_argument *, SVCXPRT *))
    196 			    simplePing;
    197 			break;
    198 		}
    199 	case SVCGETCALLTEST:
    200 		{
    201 			//printf("** in SVCGETCALLTEST dispatch Func.\n");
    202 			xdr_argument = (xdrproc_t) xdr_int;
    203 			xdr_result = (xdrproc_t) xdr_int;
    204 			proc =
    205 			    (char *(*)(union u_argument *, SVCXPRT *))
    206 			    svc_getcaller_test;
    207 			break;
    208 		}
    209 	case PROGSYSERROR:
    210 		{
    211 			//printf("** in PROGSYSERROR dispatch Func.\n");
    212 			//Simulate an error
    213 			svcerr_systemerr(transp);
    214 			return;
    215 		}
    216 	case PROGAUTHERROR:
    217 		{
    218 			//printf("** in PROGAUTHERROR dispatch Func.\n");
    219 			//Simulate an authentification error
    220 			svcerr_auth(transp, why);
    221 			return;
    222 		}
    223 	case PROGWKAUTHERROR:
    224 		{
    225 			//printf("** in PROGWKAUTHERROR dispatch Func.\n");
    226 			//Simulate an authentification error
    227 			svcerr_weakauth(transp);
    228 			return;
    229 		}
    230 	case INTPROCNUM:
    231 		{
    232 			//printf("** in INTPROCNUM dispatch Func.\n");
    233 			xdr_argument = (xdrproc_t) xdr_int;
    234 			xdr_result = (xdrproc_t) xdr_int;
    235 			proc =
    236 			    (char *(*)(union u_argument *, SVCXPRT *))
    237 			    intTestProc;
    238 			//(char *(*)(union u_argument *))
    239 			break;
    240 		}
    241 	case DBLPROCNUM:
    242 		{
    243 			//printf("** in DBLPROCNUM dispatch Func.\n");
    244 			xdr_argument = (xdrproc_t) xdr_double;
    245 			xdr_result = (xdrproc_t) xdr_double;
    246 			proc =
    247 			    (char *(*)(union u_argument *, SVCXPRT *))
    248 			    dblTestProc;
    249 			break;
    250 		}
    251 	case LNGPROCNUM:
    252 		{
    253 			//printf("** in LNGPROCNUM dispatch Func.\n");
    254 			xdr_argument = (xdrproc_t) xdr_long;
    255 			xdr_result = (xdrproc_t) xdr_long;
    256 			proc =
    257 			    (char *(*)(union u_argument *, SVCXPRT *))
    258 			    lngTestProc;
    259 			break;
    260 		}
    261 	case STRPROCNUM:
    262 		{
    263 			//printf("** in STRPROCNUM dispatch Func.\n");
    264 			xdr_argument = (xdrproc_t) xdr_wrapstring;
    265 			xdr_result = (xdrproc_t) xdr_wrapstring;
    266 			proc =
    267 			    (char *(*)(union u_argument *, SVCXPRT *))
    268 			    strTestProc;
    269 			break;
    270 		}
    271 	case SVCGETARGSPROC:
    272 		{
    273 			//printf("** in SVCGETARGSPROC dispatch Func.\n");
    274 			xdr_argument = (xdrproc_t) xdr_int;
    275 			xdr_result = (xdrproc_t) xdr_int;
    276 			proc =
    277 			    (char *(*)(union u_argument *, SVCXPRT *))
    278 			    svcGetargsProc;
    279 			break;
    280 		}
    281 	default:
    282 		{
    283 			//printf("** in NOT DEFINED dispatch Func.\n");
    284 			//Proc is unavaible
    285 			svcerr_noproc(transp);
    286 			return;
    287 		}
    288 	}
    289 
    290 	memset((char *)&argument, (int)0, sizeof(argument));
    291 	if (svc_getargs(transp, xdr_argument, (char *)&argument) == FALSE) {
    292 		svcerr_decode(transp);
    293 		return;
    294 	}
    295 
    296 	result = (char *)(*proc) ((union u_argument *)&argument, transp);
    297 
    298 	if ((result != NULL)
    299 	    && (svc_sendreply(transp, xdr_result, result) == FALSE)) {
    300 		svcerr_systemerr(transp);
    301 	}
    302 	if (svc_freeargs(transp, xdr_argument, (char *)&argument) == FALSE) {
    303 		(void)fprintf(stderr, "unable to free arguments\n");
    304 		exit(1);
    305 	}
    306 }
    307