📄 wdbrpc.c
字号:
/* wdbrpc.c - Remote Procedure Call (RPC) backend library *//* Copyright 1984-1995 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------02v,11feb99,elg Added the WDB port configuration (SPR 22257).02u,15jun98,c_c Changed the init_2 routine to initialize to be in sync with the doc.02t,20mar98,c_c Changed FALSE to ERROR in inet_addr test.02s,02mar98,pcn Removed warnings.02r,09feb98,c_c Ported to backend V2 API.02q,03feb98,dbt corrected clntudp_create() call.02p,28jun96,c_s just include <fcntl.h>; this is more portable.02o,15jan96,elp added variable to store external functions + modified wdbrpcInit() to initialize this variable.02n,15sep95,p_m changed tgtLink.name to "WDB agent RPC" (SPR# 4471).02m,11jun95,p_m added errno = WTX_ERR_SVR_TARGET_UNKNOWN when target address is invalid.02l,01jun95,tpr split the backend in two files wdbrpc.c and rpccore.c. added input paramters to wdrrpcInit().02k,30may95,p_m added Solaris-2 and HP-UX support.02j,24may95,p_m added wdbRpcMemScan(). Added Solaris 2 support.02i,23may95,p_m removed extra memcopy() in wdbRpcRegsGet().02h,02may95,tpr reworked following WDB protocole modification. + p_m fixed wdbRpcVIOWrite() that was not returning the number of bytes written.02g,04mar95,p_m map WTX agent modes to WDB agent modes.02f,31mar95,p_m make backend generated errors be WTX_ERR_AGENT_XXX.02e,28mar95,p_m added rpcBckndMemMove().02d,17mar95,jcf fixed bug in rpcBckndMemRead. 02c,14mar95,p_m completed rpcBckndTgtDisconnect().02b,09mar95,p_m added rpcBckndCacheTextUpdate(), rpcBckndModeSet() and rpcBckndMemProtect(). Changed xdr_wrapstring to xdr_STRING_DATA for WDB_EVALUATE_GOPHER.02a,08mar95,pad made message usage more coherent.01z,06mar95,p_m took care of latest agent.01y,05mar95,tpr added bckndLogClose() call in rpcBckndTgtDisconnect(). changed bckndDebugInit() and bckndDebug() with bckndLogInit() bckndlog(). added local variable rpcBcbkndSvrAdded.01x,21feb95,tpr removed tgtInfo global variable and rpcBckndMemCtxtSet(). added tgtInfo in outArgs union. changed fprintf () with wpwrLogMsg () in bckndTgtConnect(). moved outArgs union into tgtlib.h added tgtOutArgs. replaced WDB_MSG_SIZE with rpcBckndMtu. added documentation. jcf changed tgtLink.name string.01w,09feb95,p_m added target reboot handling. tpr removed rpcBckndMemReadZ(), rpcBckndMemWriteZ(), rpcBckndMemReadFct() and rpcBckndMemWriteFct(). changed some return type to STATUS instead of RESULT_STATUS. added status field in outArgs. changed rpcBckndRequestSendInt() to rpcBckndCallStatus () changed rpcBckndRequestSend() to rpcBckndCallRStatus()01v,08feb95,pad changed rpcBckndInit() into rpcbckndInit().01u,08feb95,p_m added clearing of out parameters after a call to tsWdbKnownSvcAdd().01t,06feb95,tpr removed unused functions. removed BCKND_OPS definition. added TGT_OPS * pTgtOps arg in bckndInit (). removed bckndTimeoutCalc (). replaced bckndXXX () with rpcBckndXXX (). added htons () in rpcBckndInit().01s,26jan95,p_m added scalable agent feature. some cleanup. replaced printf() by fprintf (stderr,..) in bckndTgtConnect().01r,24jan95,tpr moved logFile in bckndlog.c added bckndDebugInit() and bckndDebug() call. reduce WDB_MSG_SIZE value.01q,23jan95,tpr changed bckndRegsSet() and bckndRegsGet() prototype. changed bckndMemZero() to bckndMemFill(). replaced #include "wdb/wdbtypes.h" with #include "wdbtypes.h". replaced #include "wdb/wdb.h" with #include "wdb.h". replaced #include "wdb/xdrwdb.h" with #include "xdrwdb.h". added bckndDirectCall().01p,20jan95,jcf updated includes.01o,14nov94,tpr added logFile, log file creation and #include "irpcLib.h". changed printf() into wpwrLogErr(). moved bckndDebug() in bckndlog.c file. added bckndRequestSendInt() to follow wdb protocole modif.. added RPC_PROCUNAVAIL test in bckndClntCall().01n,13nov94,tpr removed acc warnings. added bckndContextResume().01m,11nov94,tpr changed bckndGopherEval() and bckndEventpointDelete() arg.01l,10nov94,tpr added bckndGopherEval().01k,09nov94,tpr changed wdbClnt->cl_private cast.01j,09nov94,tpr added bckndEventGet input arguments.01i,09nov94,tpr removed UDP_DATA_SIZE, DATA_BLOCK_MAX and INT_BLOCK_MAX. added WDB_MSG_SIZE macro. added bckndFuncCall().01h,08nov94,tpr changed #include "wdbtypes.h" into #include "wdb/wdbtypes.h". changed #include "wdb.h" into #include "wdb/wdb.h". changed #include "xdrwdb.h" into #include "wdb/xdrwdb.h".01g,07nov94,tpr changed WDB_EVT_NOTIFY by WDB_EVENT_NOTIFY.01f,24oct94,tpr added WDBPORT in bckndInit(). added bckndDebug(). changed all contextSourceStep to contextStep. changed eventPointAddRtn and eventPointDeleteRtn to eventpointAddRtn and eventpointDeleteRtn. changed eventPointGetRtn to eventGetRtn. changed ADD_SERVICE structure name to WDB_SERVICE. changed bckndEventpointDelete input arguments type. added bckndEvtPending() and bckndEvtPendingClear().01e,20oct94,tpr added function comments. changed UDP data size management in bckndMemReadFct () and bckndMemWriteFct (). changed bckndTimeoutCalc () prototype. added memory used by the output arguments clear.01d,20oct94,tpr changed RPC error management.01c,19oct94,tpr added UDP data size management in bckndMemReadFct () and bckndMemWriteFct ().01b,19oct94,tpr changed MEM_COPY to MEM_READ in the bckndMemRead() and bckndMemReadZ(). added #include <arpa/inet.h>.01a,21sep94,tpr written.*//*DESCRIPTIONThis file is a target server backend using the RPC mechanism to dialog witha WDB target agent. This backend is the default backend attached when the userstart the target server without specifying a backend. It's also attached whenthe target server is started with the option -a followed by "wdbrpc".The communication protocol used is UDP/IP. To increase the reliability of thisprotocol a simple mechanism of timeout and re-sending is implemented. When aservice is called a timer (implemented in the RPC client mechanism) isalso started. If the timeout arrived before the service has replied then therequest is re-sent. The number of time a same request can be re-sent is limited.When this limit is reached and the service call fails one more in timeout thelink is declared as broken. The default timeout value is handled by WDB_TIMEOUTbut this value can be changed by the user. He must start the target server withthe option -Bt followed by a number. This number is the timeout value to use(in second). The user can also modify the maxinum number of time that a samerequest can be re-sent. In this case the option -Br must be used. The default value of this option is handled by WDB_MAX_RESEND.The TCP/IP was not used because it requests timer. This feature is not alwaysavailable on all target boards. Moreover the implementation of this protocolrequer more memory space as the UDP protocol. As the WDB target agent mustbe the smallest possible the UDP protocol is the obligatory choise.This backend file handles all functions that a target serveur backend request.The diagram below gives the function link. The wdbRpcClntCall () isthe root function. It allows to call any WDB service and performs the linkreliability control as describe before. This function is called by 7 functions:5 are backend functions and 2 are internal to this backend. This 2 functions(wdbRpcCallRStatus() and wdbRpcCallStatus()) are used by the otherbackend functions that don't call wdbRpcClntCall() directly. They allow tocall a WDB service but this service must return either a RESULT_STATUS (forwdbRpcCallRStatus()) or a STATUS (for wdbRpcCallStatus()). *//* includes */#include <stdio.h>#include <netdb.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <rpc/rpc.h>#ifdef SUN4_SOLARIS2#include <rpc/clnt_soc.h>#endif#include <sys/time.h>#include <fcntl.h>#include "tgtlib.h"#include "tgtsvr.h"#include "tssvcmgt.h"#include "host.h"#include "wdb.h"#include "wdbP.h"#include "bkendlib.h"#include "bkendlog.h"#include "wpwrutil.h"/* forward declarations */extern STATUS rpcCoreInit (CLIENT *, u_int, u_int, TGT_OPS *);/* locals */LOCAL u_int wdbRpcTimeout = BKEND_TIMEOUT;/* backend timeout */LOCAL u_int wdbRpcRetry = BKEND_RESEND; /* backend resend number */LOCAL u_int wdbRpcPort = WDBPORT; /* WDB port number */static FLAG_DESC wdbrpcFlags [] = { { "-Btimeout","-Bt", (PARSE_RTN) flagInt, &wdbRpcTimeout, "-Bt[imeout] value\tWDB RPC Backend request timeout (default 1sec.)." }, { "-Bresend", "-Br", (PARSE_RTN) flagInt, &wdbRpcRetry, "-Br[esend] number\tWDB RPC Backend request re-send number(default 3)." }, { "-port", "-p", (PARSE_RTN) flagInt, &wdbRpcPort, "-p[ort] number\t\tUDP port number to connect (default 0x4321)." }, {NULL, NULL, NULL, NULL, NULL} /* End Of Record delimiter */ };/********************************************************************************* wdbrpcFlagsGet - Get the supported flags for this backend** This function is called by the target server when it wants to get the flags* which are supported by the currently attached backend. The following options* can be set trough command line parameters.** <timeout> is the time out duration to use for the communication with the* WDB target agent. This input argument is set when the target server is* started with the option -Bt. Otherwise set to zero.** <recallNum> is the maximun number of time that a request is re-sent before* to admit the communication link is broken. Set when the target server is* started with the option -Br, otherwise reset to zero.** Here we return our supported flags.**/DLL_EXPORT FLAG_DESC * wdbrpcFlagsGet (void) { return (FLAG_DESC *) wdbrpcFlags; }/********************************************************************************* wdbrpcInitialize - initialize the backend interface** This function is called by the target server when it wants to attach this* backend. Three input arguments are accepted:** <tgtName> is a pointer to a string that handles either the name or the* internet address (in dotted-decimal * format) of the target board. ** <pTgtOps> is a pointer to TGT_OPS structure (defined in tgtlib.h file).* This structure handles function pointers used by the target server to call* a backend function. The backend attachment is performed by filling this* structure with the backend function address. ** <pBkInfo> is a structure to fill for the target server to know if the backend* exports a selectable file descriptor, a select-like function, or nothing for* the asynchronous event notification. * Remember that the target server now dedicates a thread to the event* notification.** This function initializes the backend. It creates the backend client, resets* the udp datagram sequence number and fills the TGT_OPS structure with* the information describing the backend and returns a pointer to this * structure.** CAVEAT: its name is built from the file name concatenated with the string* "Inittialize". This is compulsory.* * RETURNS: TRUE if the backend is properly initialized, otherwise FALSE.*/DLL_EXPORT STATUS wdbrpcInitialize ( char * tgtName, /* target name to connect to */ TGT_OPS * pTgtOps, /* backend function */ BKEND_INFO * pBkInfo /* Backend info and select method */ ) { CLIENT * pClnt; /* target client */ u_long addr; /* target address: long int format */ struct sockaddr_in dest_addr; /* target address information */ struct hostent * dest_host; /* target address */ int wdbSocket; /* backend client socket */ struct timeval tv; /* connection timeout */ u_int resendCnt; /* connection re-send counter */ /* clear dest_addr */ memset ((void *) &dest_addr, 0x00, sizeof (dest_addr)); /* convert <tgtName> into the target board Internet address */ if ((dest_host = (struct hostent *) gethostbyname (tgtName)) == NULL) { if ((addr = inet_addr (tgtName)) == (unsigned long) ERROR) { errno = WTX_ERR_SVR_TARGET_UNKNOWN; wpwrLogErr ("Target %s unknown. Attach failed.\n", tgtName); return (ERROR); } dest_addr.sin_addr.s_addr = addr; } else dest_addr.sin_addr.s_addr = * ((u_long *) dest_host->h_addr_list[0]); /* set the socket with the input informaton */ dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons ((u_short) wdbRpcPort); wdbSocket = RPC_ANYSOCK; /* set the connection timeout to the current value */ tv.tv_sec = wdbRpcTimeout; tv.tv_usec = 0; /* * set the resendCnt counter with the maximun of time the client creation * will be re-tried. */ resendCnt = wdbRpcRetry; do { /* create the backend client and connect the target deamon */ pClnt = clntudp_create (&dest_addr, WDBPROG, WDBVERS, tv, &wdbSocket); } while ((pClnt == NULL) && (--resendCnt > 0)); /* * check the client creation : If the creation failed, log a message with * the RPC error and return ERROR to stop the target server initialization. */ if (pClnt == NULL) { wpwrLogErr ("%s", clnt_spcreateerror("wdbrpc backend client create")); return (ERROR); } /* initialize the rpc core */ rpcCoreInit (pClnt, wdbRpcTimeout, wdbRpcRetry, pTgtOps); /* * Describe the link used between the backend and the target. * There is no way to know if our RPC connection goes over Ethernet * or a serial at this point so we use UNKNOWN. */ pTgtOps->tgtLink.name = "WDB Agent RPC"; pTgtOps->tgtLink.type = TGT_LINK_UNKNOWN_RPC; pTgtOps->tgtLink.speed = SPEED_UNKNOWN; pBkInfo->tgtBkVersion = BKEND_VERSION_2; /* Backend version 2 */ /* We give here our polling method : a selectable socket */ pBkInfo->INFO.POLLING_METHOD.bkEndPollingMethod = POLL_SELECT_MODE; pBkInfo->INFO.POLLING_METHOD.BKEND_NOTIF_METHOD.pollingFd = pTgtOps->tgtEventFd; return (OK); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -