Back to index

glibc  2.9
pmap_clnt.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 /*
00030  * Copyright (C) 1984, Sun Microsystems, Inc.
00031  */
00032 /*
00033  * pmap_clnt.c
00034  * Client interface to pmap rpc service.
00035  */
00036 
00037 #include <stdio.h>
00038 #include <unistd.h>
00039 #include <libintl.h>
00040 #include <net/if.h>
00041 #include <ifaddrs.h>
00042 #include <sys/ioctl.h>
00043 #include <sys/socket.h>
00044 #include <netinet/in.h>
00045 #include <arpa/inet.h>
00046 #include <rpc/rpc.h>
00047 #include <rpc/pmap_prot.h>
00048 #include <rpc/pmap_clnt.h>
00049 
00050 /*
00051  * Same as get_myaddress, but we try to use the loopback
00052  * interface. portmap caches interfaces, and on DHCP clients,
00053  * it could be that only loopback is started at this time.
00054  */
00055 static bool_t
00056 __get_myaddress (struct sockaddr_in *addr)
00057 {
00058   struct ifaddrs *ifa;
00059 
00060   if (getifaddrs (&ifa) != 0)
00061     {
00062       perror ("get_myaddress: getifaddrs");
00063       exit (1);
00064     }
00065 
00066   int loopback = 1;
00067   struct ifaddrs *run;
00068 
00069  again:
00070   run = ifa;
00071   while (run != NULL)
00072     {
00073       if ((run->ifa_flags & IFF_UP)
00074          && run->ifa_addr != NULL
00075          && run->ifa_addr->sa_family == AF_INET
00076          && ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0))
00077        {
00078          *addr = *((struct sockaddr_in *) run->ifa_addr);
00079          addr->sin_port = htons (PMAPPORT);
00080          goto out;
00081        }
00082 
00083       run = run->ifa_next;
00084     }
00085 
00086   if (loopback == 1)
00087     {
00088       loopback = 0;
00089       goto again;
00090     }
00091  out:
00092   freeifaddrs (ifa);
00093 
00094   return run == NULL ? FALSE : TRUE;
00095 }
00096 
00097 
00098 static const struct timeval timeout = {5, 0};
00099 static const struct timeval tottimeout = {60, 0};
00100 
00101 /*
00102  * Set a mapping between program,version and port.
00103  * Calls the pmap service remotely to do the mapping.
00104  */
00105 bool_t
00106 pmap_set (u_long program, u_long version, int protocol, u_short port)
00107 {
00108   struct sockaddr_in myaddress;
00109   int socket = -1;
00110   CLIENT *client;
00111   struct pmap parms;
00112   bool_t rslt;
00113 
00114   if (!__get_myaddress (&myaddress))
00115     return FALSE;
00116   client = INTUSE(clntudp_bufcreate) (&myaddress, PMAPPROG, PMAPVERS,
00117                                   timeout, &socket, RPCSMALLMSGSIZE,
00118                                   RPCSMALLMSGSIZE);
00119   if (client == (CLIENT *) NULL)
00120     return (FALSE);
00121   parms.pm_prog = program;
00122   parms.pm_vers = version;
00123   parms.pm_prot = protocol;
00124   parms.pm_port = port;
00125   if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)INTUSE(xdr_pmap),
00126                (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt,
00127                tottimeout) != RPC_SUCCESS)
00128     {
00129       clnt_perror (client, _("Cannot register service"));
00130       rslt = FALSE;
00131     }
00132   CLNT_DESTROY (client);
00133   /* (void)close(socket); CLNT_DESTROY closes it */
00134   return rslt;
00135 }
00136 libc_hidden_def (pmap_set)
00137 
00138 /*
00139  * Remove the mapping between program,version and port.
00140  * Calls the pmap service remotely to do the un-mapping.
00141  */
00142 bool_t
00143 pmap_unset (u_long program, u_long version)
00144 {
00145   struct sockaddr_in myaddress;
00146   int socket = -1;
00147   CLIENT *client;
00148   struct pmap parms;
00149   bool_t rslt;
00150 
00151   if (!__get_myaddress (&myaddress))
00152     return FALSE;
00153   client = INTUSE(clntudp_bufcreate) (&myaddress, PMAPPROG, PMAPVERS,
00154                                   timeout, &socket, RPCSMALLMSGSIZE,
00155                                   RPCSMALLMSGSIZE);
00156   if (client == (CLIENT *) NULL)
00157     return FALSE;
00158   parms.pm_prog = program;
00159   parms.pm_vers = version;
00160   parms.pm_port = parms.pm_prot = 0;
00161   CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)INTUSE(xdr_pmap),
00162             (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt,
00163             tottimeout);
00164   CLNT_DESTROY (client);
00165   /* (void)close(socket); CLNT_DESTROY already closed it */
00166   return rslt;
00167 }
00168 libc_hidden_def (pmap_unset)