📄 clnt_unix.c
字号:
static voidclntunix_geterr(h, errp) CLIENT *h; struct rpc_err *errp;{ register struct ct_data *ct = (struct ct_data *) h->cl_private; *errp = ct->ct_error;}static bool_tclntunix_freeres(cl, xdr_res, res_ptr) CLIENT *cl; xdrproc_t xdr_res; caddr_t res_ptr;{ register struct ct_data *ct = (struct ct_data *)cl->cl_private; register XDR *xdrs = &(ct->ct_xdrs); xdrs->x_op = XDR_FREE; return ((*xdr_res)(xdrs, res_ptr));}static voidclntunix_abort(){}static bool_tclntunix_control(cl, request, info) CLIENT *cl; int request; char *info;{ register struct ct_data *ct = (struct ct_data *)cl->cl_private; register struct timeval *tv; int len; switch (request) { case CLSET_FD_CLOSE: ct->ct_closeit = TRUE; break; case CLSET_FD_NCLOSE: ct->ct_closeit = FALSE; break; case CLSET_TIMEOUT: if (info == NULL) return(FALSE); tv = (struct timeval *)info; ct->ct_wait.tv_sec = tv->tv_sec; ct->ct_wait.tv_usec = tv->tv_usec; ct->ct_waitset = TRUE; break; case CLGET_TIMEOUT: if (info == NULL) return(FALSE); *(struct timeval *)info = ct->ct_wait; break; case CLGET_SERVER_ADDR: if (info == NULL) return(FALSE); *(struct sockaddr_un *)info = ct->ct_addr; break; case CLGET_FD: if (info == NULL) return(FALSE); *(int *)info = ct->ct_sock; break; case CLGET_XID: /* * use the knowledge that xid is the * first element in the call structure *. * This will get the xid of the PREVIOUS call */ if (info == NULL) return(FALSE); *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall); break; case CLSET_XID: /* This will set the xid of the NEXT call */ if (info == NULL) return(FALSE); *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1); /* decrement by 1 as clntunix_call() increments once */ case CLGET_VERS: /* * This RELIES on the information that, in the call body, * the version number field is the fifth field from the * begining of the RPC header. MUST be changed if the * call_struct is changed */ if (info == NULL) return(FALSE); *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)); break; case CLSET_VERS: if (info == NULL) return(FALSE); *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT) = htonl(*(u_long *)info); break; case CLGET_PROG: /* * This RELIES on the information that, in the call body, * the program number field is the field from the * begining of the RPC header. MUST be changed if the * call_struct is changed */ if (info == NULL) return(FALSE); *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)); break; case CLSET_PROG: if (info == NULL) return(FALSE); *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT) = htonl(*(u_long *)info); break; case CLGET_LOCAL_ADDR: len = sizeof(struct sockaddr); if (getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0) return(FALSE); break; case CLGET_RETRY_TIMEOUT: case CLSET_RETRY_TIMEOUT: case CLGET_SVC_ADDR: case CLSET_SVC_ADDR: case CLSET_PUSH_TIMOD: case CLSET_POP_TIMOD: default: return (FALSE); } return (TRUE);}static voidclntunix_destroy(h) CLIENT *h;{ register struct ct_data *ct = (struct ct_data *) h->cl_private; if (ct->ct_closeit) { (void)_RPC_close(ct->ct_sock); } XDR_DESTROY(&(ct->ct_xdrs)); mem_free((caddr_t)ct, sizeof(struct ct_data)); mem_free((caddr_t)h, sizeof(CLIENT));}/* * read() and write() are replaced with recvmsg()/sendmsg() so that * we can pass ancillary control data. In this case, the data constists * of credential information which the kernel will fill in for us. * XXX: This code is specific to FreeBSD and will not work on other * platforms without the requisite kernel modifications. */struct cmessage { struct cmsghdr cmsg; struct cmsgcred cmcred;};static int __msgread(sock, buf, cnt) int sock; void *buf; size_t cnt;{ struct iovec iov[1]; struct msghdr msg; struct cmessage cm; bzero((char *)&cm, sizeof(cm)); iov[0].iov_base = buf; iov[0].iov_len = cnt; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = (caddr_t)&cm; msg.msg_controllen = sizeof(struct cmessage); msg.msg_flags = 0; return(recvmsg(sock, &msg, 0));}static int __msgwrite(sock, buf, cnt) int sock; void *buf; size_t cnt;{ struct iovec iov[1]; struct msghdr msg; struct cmessage cm; bzero((char *)&cm, sizeof(cm)); iov[0].iov_base = buf; iov[0].iov_len = cnt; cm.cmsg.cmsg_type = SCM_CREDS; cm.cmsg.cmsg_level = SOL_SOCKET; cm.cmsg.cmsg_len = sizeof(struct cmessage); msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = (caddr_t)&cm; msg.msg_controllen = sizeof(struct cmessage); msg.msg_flags = 0; return(sendmsg(sock, &msg, 0));}/* * Interface between xdr serializer and unix connection. * Behaves like the system calls, read & write, but keeps some error state * around for the rpc level. */static intreadunix(ct, buf, len) register struct ct_data *ct; caddr_t buf; register int len;{ fd_set *fds, readfds; struct timeval start, after, duration, delta, tmp, tv; int r, save_errno; if (len == 0) return (0); if (ct->ct_sock + 1 > FD_SETSIZE) { int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask); fds = (fd_set *)malloc(bytes); if (fds == NULL) return (-1); memset(fds, 0, bytes); } else { fds = &readfds; FD_ZERO(fds); } gettimeofday(&start, NULL); delta = ct->ct_wait; while (TRUE) { /* XXX we know the other bits are still clear */ FD_SET(ct->ct_sock, fds); tv = delta; /* in case select writes back */ r = select(ct->ct_sock+1, fds, NULL, NULL, &tv); save_errno = errno; gettimeofday(&after, NULL); timersub(&start, &after, &duration); timersub(&delta, &duration, &tmp); delta = tmp; if (delta.tv_sec < 0 || !timerisset(&delta)) r = 0; switch (r) { case 0: if (fds != &readfds) free(fds); ct->ct_error.re_status = RPC_TIMEDOUT; return (-1); case -1: if (errno == EINTR) continue; if (fds != &readfds) free(fds); ct->ct_error.re_status = RPC_CANTRECV; ct->ct_error.re_errno = save_errno; return (-1); } break; } switch (len = __msgread(ct->ct_sock, buf, len)) { case 0: /* premature eof */ ct->ct_error.re_errno = ECONNRESET; ct->ct_error.re_status = RPC_CANTRECV; len = -1; /* it's really an error */ break; case -1: ct->ct_error.re_errno = errno; ct->ct_error.re_status = RPC_CANTRECV; break; } return (len);}static intwriteunix(ct, buf, len) struct ct_data *ct; caddr_t buf; int len;{ register int i, cnt; for (cnt = len; cnt > 0; cnt -= i, buf += i) { if ((i = __msgwrite(ct->ct_sock, buf, cnt)) == -1) { ct->ct_error.re_errno = errno; ct->ct_error.re_status = RPC_CANTSEND; return (-1); } } return (len);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -