📄 pmap_clnt.c
字号:
#ifndef lintstatic char *sccsid = "@(#)pmap_clnt.c 4.2 ULTRIX 9/4/90";#endif lint/************************************************************************ * * * Copyright (c) 1986 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * Portions of this software have been licensed to * Digital Equipment Company, Maynard, MA. * Copyright (c) 1986 Sun Microsystems, Inc. ALL RIGHTS RESERVED. *//* * pmap_clnt.c * Client interface to pmap rpc service. * */#include <rpc/types.h>#include <netinet/in.h>#include <rpc/xdr.h>#include <rpc/auth.h>#include <rpc/clnt.h>#include <rpc/rpc_msg.h>#include <rpc/pmap_prot.h> #include <rpc/pmap_clnt.h>#include <sys/socket.h>#include <sys/time.h>#include <stdio.h>#include <net/if.h>#include <sys/ioctl.h>#include <arpa/inet.h>#define NAMELEN 255static struct timeval timeout = { 5, 0 };static struct timeval tottimeout = { 60, 0 };static struct sockaddr_in myaddress;void clnt_perror();/* * Set a mapping between program,version and port. * Calls the pmap service remotely to do the mapping. */bool_tpmap_set(program, version, protocol, port) u_long program; u_long version; u_long protocol; u_short port;{ struct sockaddr_in myaddress; int socket = -1; register CLIENT *client; struct pmap parms; bool_t rslt; get_myaddress(&myaddress); client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS, timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); if (client == (CLIENT *)NULL) return (FALSE); parms.pm_prog = program; parms.pm_vers = version; parms.pm_prot = protocol; parms.pm_port = port; if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt, tottimeout) != RPC_SUCCESS) { clnt_perror(client, "Cannot register service"); return (FALSE); } CLNT_DESTROY(client); (void)close(socket); return (rslt);}/* * Remove the mapping between program,version and port. * Calls the pmap service remotely to do the un-mapping. */bool_tpmap_unset(program, version) u_long program; u_long version;{ struct sockaddr_in myaddress; int socket = -1; register CLIENT *client; struct pmap parms; bool_t rslt; get_myaddress(&myaddress); client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS, timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); if (client == (CLIENT *)NULL) return (FALSE); parms.pm_prog = program; parms.pm_vers = version; parms.pm_port = parms.pm_prot = 0; CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt, tottimeout); CLNT_DESTROY(client); (void)close(socket); return (rslt);}/* * don't use gethostbyname, which would invoke yellow pages */get_myaddress(addr) struct sockaddr_in *addr;{ int s; char buf[BUFSIZ]; struct ifconf ifc; struct ifreq ifreq, *ifr; int len; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("get_myaddress: socket"); exit(1); } ifc.ifc_len = sizeof (buf); ifc.ifc_buf = buf; if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { perror("get_myaddress: ioctl (get interface configuration)"); exit(1); } ifr = ifc.ifc_req; for (len = ifc.ifc_len; len; len -= sizeof ifreq) { ifreq = *ifr; if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { perror("get_myaddress: ioctl"); exit(1); } if ((ifreq.ifr_flags & IFF_UP) && ifr->ifr_addr.sa_family == AF_INET) { *addr = *((struct sockaddr_in *)&ifr->ifr_addr); addr->sin_port = htons(PMAPPORT); break; } ifr++; } close(s);}/* * don't use gethostbyname, which would invoke yellow pages * This version takes a destination (where we are trying to get to) * and finds an exact network match to goto. Not sure if this will * work with ptop interfaces. */get_myaddr_dest(addr, dest) struct sockaddr_in *addr; struct sockaddr_in *dest;{ int s; char buf[BUFSIZ]; struct ifconf ifc; struct ifreq ifreq, *ifr; int len; struct sockaddr_in netmask, maskaddr; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("get_myaddr_dest: socket"); exit(1); } ifc.ifc_len = sizeof (buf); ifc.ifc_buf = buf; if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { perror("get_myaddr_dest: ioctl (get interface configuration)"); exit(1); } ifr = ifc.ifc_req; for (len = ifc.ifc_len; len; len -= sizeof ifreq) { ifreq = *ifr; if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { perror("get_myaddr_dest: ioctl"); exit(1); } if ((ifreq.ifr_flags & IFF_UP) && ifr->ifr_addr.sa_family == AF_INET) { maskaddr.sin_addr = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr; ifreq = *ifr; if(ioctl(s, SIOCGIFNETMASK, &ifreq) < 0) { perror("get_myaddr_dest: ioctl"); } netmask.sin_addr = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr; if((dest->sin_addr.s_addr&netmask.sin_addr.s_addr) == (maskaddr.sin_addr.s_addr & netmask.sin_addr.s_addr)) { ifreq = *ifr; if(ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0){ perror("get_myaddr_dest: ioctl"); exit(1); } *addr = *((struct sockaddr_in *)&ifr->ifr_addr); addr->sin_port = htons(PMAPPORT); goto done; } } ifr++; }done: close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -