📄 clnt_ttysvr.c
字号:
/* clnt_ttysvr.c - implements a portServer line based, client side RPC */ /* Copyright 1984-1994 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--------------------01c,01jul98,c_c Ported to backend V2 API.01b,05may98,rlp added WDB_PORTSVR_SOCKET environment variable for windows port.01a,29jan98,rlp written, based on v01g of backend/wdbserial/clnt_tty.c*/#ifndef lint/* static char sccsid[] = "@(#)clnt_tty.c 1.1 86/02/03 Copyr 1984 Sun Micro"; */#endif/* * clnt_ttysvr.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>#else /* WIN32 */#include <strings.h>#endif /* WIN32 */#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <termios.h>#include <unistd.h>#include <malloc.h>#include <stdlib.h>#include <sys/socket.h>#include <netinet/in.h>#include <rpc/types.h>#include <rpc/xdr.h>#include <rpc/auth.h>#include <rpc/clnt.h>#include <rpc/rpc_msg.h>#include <rpc/rpc.h>#include <rpc/pmap_clnt.h>#include "wdbP.h"#include "wpwrutil.h"/* defines */#undef DEBUG #ifdef DEBUG#define BACKEND_DEBUG 0x01 int debug = 0x01;#define BACKEND_PRINT(FLG,X) \ if (debug & FLG) printf X #else /* DEBUG */ #define BACKEND_PRINT(FLG,X) #endif /* DEBUG */ extern bool_t xdr_opaque_auth();/* * TTYSVR bases client side rpc operations */static enum clnt_stat clntttysvr_call();static void clntttysvr_abort();static void clntttysvr_geterr();static bool_t clntttysvr_control();static void clntttysvr_destroy();static bool_t clntttysvr_freeres();#ifdef WIN32static struct opaque_auth _null_auth;/* static struct rpc_createerr rpc_createerr; */#define mem_alloc(bsize) malloc(bsize)#define mem_free(ptr, bsize) free(ptr)#endif /* WIN32 *//* * Other forward static function declarations */LOCAL int clntttysvr_send ();LOCAL int clntttysvr_rcv ();LOCAL int clntttysvr_open (); /* global */#ifdef WIN32int dbg_on; /* turn on to print debug messages */int dbg_ch_printed = 0; OVERLAPPED ovrlapped_s;#endif /* WIN32 *//* locals */static struct clnt_ops ttysvr_ops = { clntttysvr_call, clntttysvr_abort, clntttysvr_geterr, clntttysvr_freeres, clntttysvr_destroy, clntttysvr_control};/* * Private data kept per client handle */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];};static int port; /* portServer port Number */static char * pPortSvrAddr; /* portServer ip_address *//* * default size fo send/rcv buffers */#define TTYMSGSIZE 1500/* default console baud rate */#define CONSOLE_BAUD 9600/* * Create a TTYSVR 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 *clntttysvr_bufcreate(program, version, wait, pDest_addr, baudRate, portNum, sendsz, recvsz) u_long program; u_long version; struct timeval wait; struct sockaddr_in *pDest_addr; int baudRate; int portNum; u_int sendsz; u_int recvsz;{ CLIENT *cl; register struct cu_data *cu; struct timeval now; struct rpc_msg call_msg; char strgInit[100]; char * pWindBase; cl = (CLIENT *)mem_alloc(sizeof(CLIENT)); if (cl == NULL) { wpwrLogErr ("clntttysvr_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 = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz); if (cu == NULL) { wpwrLogErr ("clntttysvr_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 /* WIN32 */ { time_t time_val; (void)time(&time_val); now.tv_sec = time_val; now.tv_usec = 0; }#endif /* WIN32 */ cl->cl_ops = &ttysvr_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 a socket on portServer */ cu->cu_fd = clntttysvr_open (pDest_addr); if (cu->cu_fd < 0) { wpwrLogErr ("clntttysvr_bufcreate can't create socket\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto fooy; } port = portNum; /* port Number */ pWindBase = wpwrGetEnv ("WIND_BASE"); /* get the WPWR path */ /* configure the porserver for a wdb communication */#ifndef WIN32 sprintf(strgInit, "wtxtcl %s/host/resource/wdb/wdbportsvr/portSvrConfig.tcl off %d %d %s", pWindBase, baudRate, port, pPortSvrAddr);#else /* WIN32 */ sprintf(strgInit, "wtxtcl %s\\host\\resource\\wdb\\wdbportsvr\\portSvrConfig.tcl off %d %d %s", pWindBase, baudRate, port, pPortSvrAddr);#endif /* WIN32 */ system (strgInit); 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 *clntttysvr_create(pDest_addr, program, version, wait, baudRate, portNum) struct sockaddr_in *pDest_addr; u_long program; u_long version; struct timeval wait; int baudRate; int portNum;{ return (clntttysvr_bufcreate (program, version, wait, pDest_addr, baudRate, portNum, TTYMSGSIZE, TTYMSGSIZE));}static enum clnt_statclntttysvr_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout) 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 u_int inlen; fd_set readFds; fd_set mask; 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 (clntttysvr_send (cu->cu_fd, cu->cu_outbuf, outlen) == 0) { cu->cu_error.re_errno = errno; 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); } /* * sub-optimal code appears inside the loop because we have * some clock time to spare while the packets are in flight. * (We assume that this is actually only executed once.) */ reply_msg.acpted_rply.ar_verf = _null_auth; reply_msg.acpted_rply.ar_results.where = resultsp; reply_msg.acpted_rply.ar_results.proc = xresults; FD_ZERO (&mask); FD_SET (cu->cu_fd, &mask); for (;;) { readFds = mask; switch (select (FD_SETSIZE, &readFds, (fd_set *)NULL, (fd_set *)NULL, &(cu->cu_wait))) { case 0: time_waited.tv_sec += cu->cu_wait.tv_sec; time_waited.tv_usec += cu->cu_wait.tv_usec; while (time_waited.tv_usec >= 1000000) { time_waited.tv_sec++; time_waited.tv_usec -= 1000000; } if ((time_waited.tv_sec < timeout.tv_sec) || ((time_waited.tv_sec == timeout.tv_sec) && (time_waited.tv_usec < timeout.tv_usec))) goto send_again; return (cu->cu_error.re_status = RPC_TIMEDOUT); /* * buggy in other cases because time_waited is not being * updated */ case -1: if (errno == EINTR) continue; cu->cu_error.re_errno = errno; return (cu->cu_error.re_status = RPC_CANTRECV); } do { inlen = clntttysvr_rcv (cu->cu_fd, cu->cu_inbuf, cu->cu_recvsz); } while (inlen < 0 && (errno == EINTR)); if (inlen < 0) {#ifdef WIN32 if (errno == WSAEWOULDBLOCK)#else /* WIN32 */ if (errno == EWOULDBLOCK)#endif /* WIN32 */ continue; cu->cu_error.re_errno = errno; return (cu->cu_error.re_status = RPC_CANTRECV); } if (inlen < sizeof(u_long)) continue; /* see if reply transaction id matches sent id */ if (*((u_long *)(cu->cu_inbuf)) != *((u_long *)(cu->cu_outbuf))) continue; /* we now assume we have the proper reply */ break; } /* * now decode and validate the response */ xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE); ok = xdr_replymsg(&reply_xdrs, &reply_msg); /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ if (ok) {#ifndef SUN4_SOLARIS2 _seterr_reply(&reply_msg, &(cu->cu_error));#else __seterr_reply(&reply_msg, &(cu->cu_error));#endif /* !SUN4_SOLARIS2 */ if (cu->cu_error.re_status == RPC_SUCCESS) { if (! AUTH_VALIDATE(cl->cl_auth, &reply_msg.acpted_rply.ar_verf)) { cu->cu_error.re_status = RPC_AUTHERROR; cu->cu_error.re_why = AUTH_INVALIDRESP; } if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { xdrs->x_op = XDR_FREE; (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf)); } } /* end successful completion */ else { /* maybe our credentials need to be refreshed ... */ if (nrefreshes > 0 && /* 4.0 */ AUTH_REFRESH(cl->cl_auth)) goto call_again; } /* end of unsuccessful completion */ } /* end of valid reply message */ else { cu->cu_error.re_status = RPC_CANTDECODERES; } return (cu->cu_error.re_status);}static void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -