📄 clntpipe.c
字号:
/* clntpipe.c - implements a pipe based, client side RPC *//* Copyright 1998 Wind River Systems, Inc. */#include "copyright_wrs.h"/* * Copyright (c) 1987 Wind River Systems, Inc. * Copyright (C) 1984, Sun Microsystems, Inc. * * 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 *//*modification history--------------------01l,08jan99,c_c ifdef'd mailslotClose only for WIN32.01k,07jan99,c_c Properly handle WIN32 simulator reboot/shutdown (SPR #23884 and #23378).01j,12nov98,c_c make target server restart instead of exit when EPIPE error is found on mailslot (fix for SPR 22951).01i,24sep98,c_c WIN32 backend pipe will post a shutdown status if the simulator has restarted/disapeared.01h,18sep98,c_c Changed RPC_CANSEND to RPC_SYSTEM_ERROR when an EOF occurs on a write operation under WIN32.01g,21may98,dbt test if the name pipe is already used by another target server01f,27apr98,dbt create the named pipes if they do not exist.01e,30mar98,pdn tuned the mailslotSelect() for better responding.01d,30mar98,pdn defined _getpid() for use with msvcrt.dll.01c,27mar98,pdn fixed mailslotSelect().01b,26mar98,pdn implemented Windows side using mailslot instead of socket.01a,11mar98,pdn hacked up version of clnt_tty.c for pipe devices.*//* * clntpipe.c, Implements a UPD/IP based, client side RPC. * */#ifdef WIN32#define WIN32_LEAN_AND_MEAN 1#include <windows.h>#include <winbase.h>#include <winnt.h>#include <time.h>#include <process.h>#include "mailslot.c"#else#include <sys/tiuser.h>#include <strings.h>#include <sys/types.h>#include <sys/stat.h>#ifdef SUN4_SOLARIS2#include <sys/filio.h>#endif /* SUN4_SOLARIS2 */#endif /* WIN32 */#include <sys/stat.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <termios.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include <rpc/rpc.h>#include <rpc/types.h>#include <rpc/xdr.h>#include <rpc/auth.h>#include <rpc/clnt.h>#include <rpc/rpc_msg.h>#include <rpc/pmap_clnt.h>#include "wdbP.h"#include "wpwrutil.h"#include "tgtlib.h"/* dummy UDP/IP header info (in 16-bit words) */#define UDP_IP_HDR_SIZE 28#define IP_VERS_LEN 0 /* IP version + IP header length */#define IP_PROT 9 /* protocol to use */#define IP_DEST_ADDR 12 /* destination address */#define IP_SRC_ADDR 16 /* source address */#define UDP_SRC_PORT 20 /* source port */#define UDP_DEST_PORT 22 /* destination port */#define UDP_LEN 24 /* length */#define UDP_CKSUM 26 /* checksum */#ifndef IPPROTO_UDP#define IPPROTO_UDP 0x11#endif#ifdef WIN32static struct opaque_auth _null_auth;#define mem_alloc(bsize) malloc(bsize)#define mem_free(ptr, bsize) free(ptr)#define getpid _getpid#else /* !WIN32 */#define MAX_MSG_SIZE 1500#endif /* WIN32 *//* * TTY bases client side rpc operations */static enum clnt_stat clnttty_call ( CLIENT * cl, /* client handle */ u_long proc, /* procedure number */ xdrproc_t xargs, /* xdr routine for args */ caddr_t argsp, /* pointer to args */ xdrproc_t xresults, /* xdr routine for results */ caddr_t resultsp, /* pointer to results */ struct timeval utimeout /* seconds to wait before giving up - 4.0 */ );static void clnttty_abort ( CLIENT *h );static void clnttty_geterr ( CLIENT *cl, struct rpc_err *errp );static bool_t clnttty_control ( CLIENT *cl, int request, char *info );static void clnttty_destroy ( CLIENT *cl );static bool_t clnttty_freeres ( CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr );static int clnttty_send ( int fd, char * inBuf, int nBytes );static int clnttty_rcv ( int fd, char * buf, int nBytes );static int clnttty_open ( char * devName, int baudRate, struct timeval wait );/* * Private data kept per client handle */typedef struct cu_data { int cu_fd; int seqNum; struct timeval cu_wait; struct timeval cu_total; struct rpc_err cu_error; XDR cu_outxdrs; u_int cu_xdrpos; u_int cu_sendsz; char *cu_outbuf; u_int cu_recvsz; char cu_inbuf[1]; } CU_DATA, *PCU_DATA;/* local */static int t2h_fd = -1; /* target to host pipe file handle */static int h2t_fd = -1; /* host to target pipe file handle */static struct clnt_ops tty_ops = { clnttty_call, clnttty_abort, clnttty_geterr, clnttty_freeres, clnttty_destroy, clnttty_control };/********************************************************************************* mailslotSelect - check to see if message is available for read.** Routine to poll the mailslot for waitval seconds.* If waitval = -1 blocks infinitely.* * RETURNS:* 1 : mailslot has packet(s) for input.* 0 : nothing to read within the timeout.* -1 : error.*/#ifdef WIN32int mailslotSelect (int waitval) { return _mailslotSelect ((HANDLE) t2h_fd, waitval); }#endif/* * Create a TTY based client handle. * * wait is the amount of time used between retransmitting a call if * no response has been heard; retransmition occurs until the actual * rpc call times out. * * sendsz and recvsz are the maximum allowable packet sizes that can be * sent and received. */CLIENT * clnttty_bufcreate ( u_long program, u_long version, struct timeval wait, char *devName, int baudRate, u_int sendsz, u_int recvsz ) { CLIENT *cl; PCU_DATA cu = NULL; struct timeval now; struct rpc_msg call_msg; cl = (CLIENT *)mem_alloc(sizeof(CLIENT)); if(cl == NULL) { wpwrLogErr ("clnttty_create: out of memory\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto fooy; } sendsz = ((sendsz + 3) / 4) * 4; recvsz = ((recvsz + 3) / 4) * 4; cu = (PCU_DATA) mem_alloc(sizeof(CU_DATA) + sendsz + recvsz); if(cu == NULL) { wpwrLogErr ("clnttty_create: out of memory\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto fooy; } cu->cu_outbuf = &cu->cu_inbuf[recvsz];#ifndef WIN32 (void) gettimeofday (&now, (struct timezone *)0);#else { time_t time_val; (void)time(&time_val); now.tv_sec = time_val; now.tv_usec = 0; }#endif cl->cl_ops = &tty_ops; cl->cl_private = (caddr_t)cu; cu->seqNum = (getpid() << 16); cu->cu_wait = wait; cu->cu_total.tv_sec = -1; cu->cu_total.tv_usec = -1; cu->cu_sendsz = sendsz; cu->cu_recvsz = recvsz; call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec; call_msg.rm_direction = CALL; call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; call_msg.rm_call.cb_prog = program; call_msg.rm_call.cb_vers = version; xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE); if(! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) { goto fooy; } cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); /* open the pipe device */ cu->cu_fd = clnttty_open (devName, baudRate, wait); if(cu->cu_fd < 0) { wpwrLogErr ("clnttty_bufcreate can't open %s\n", devName); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto fooy; } cl->cl_auth = authnone_create(); return (cl); fooy: if(cu) mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz); if(cl) mem_free((caddr_t)cl, sizeof(CLIENT)); return ((CLIENT *)NULL); }CLIENT * clnttty_create ( char *devName, int baudRate, u_long program, u_long version, struct timeval wait ) { return (clnttty_bufcreate (program, version, wait, devName, baudRate, MAX_MSG_SIZE, MAX_MSG_SIZE)); }static enum clnt_stat clnttty_call ( register CLIENT *cl, /* client handle */ u_long proc, /* procedure number */ xdrproc_t xargs, /* xdr routine for args */ caddr_t argsp, /* pointer to args */ xdrproc_t xresults, /* xdr routine for results */ caddr_t resultsp, /* pointer to results */ struct timeval utimeout /* seconds to wait before giving up - 4.0 */ ) { register struct cu_data *cu = (struct cu_data *)cl->cl_private; register XDR *xdrs; register int outlen; register int inlen;#ifndef WIN32 fd_set readFds; fd_set mask;#endif struct rpc_msg reply_msg; XDR reply_xdrs; struct timeval time_waited; bool_t ok; int nrefreshes = 2; struct timeval timeout; if(cu->cu_total.tv_usec == -1) { timeout = utimeout; } else { timeout = cu->cu_total; } time_waited.tv_sec = 0; time_waited.tv_usec = 0; call_again: xdrs = &(cu->cu_outxdrs); xdrs->x_op = XDR_ENCODE; XDR_SETPOS(xdrs, cu->cu_xdrpos); /* * the transaction is the first thing in the out buffer */ (*(u_short *)(cu->cu_outbuf))++; if((! XDR_PUTLONG(xdrs, (long *)&proc)) || (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || (! (*xargs)(xdrs, argsp))) { return (cu->cu_error.re_status = RPC_CANTENCODEARGS); } outlen = (int)XDR_GETPOS(xdrs); send_again: if(clnttty_send (cu->cu_fd, cu->cu_outbuf, outlen) == 0) { cu->cu_error.re_errno = errno;#ifdef WIN32 /* * If the pipe has been close by the simulator, report a * RPC_SYSTEMERROR, so the target server will restart itself and try * to reconnect, else report a CANTSEND error. */ if (errno == EPIPE) { return (cu->cu_error.re_status = RPC_SYSTEMERROR); } else#endif return (cu->cu_error.re_status = RPC_CANTSEND); } /* * Hack to provide rpc-based message passing */ if(timeout.tv_sec == 0 && timeout.tv_usec == 0) { return (cu->cu_error.re_status = RPC_TIMEDOUT); } /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -