Back to index

glibc  2.9
pm_getport.c
Go to the documentation of this file.
00001 /* @(#)pmap_getport.c       2.2 88/08/01 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";
00032 #endif
00033 
00034 /*
00035  * pmap_getport.c
00036  * Client interface to pmap rpc service.
00037  *
00038  * Copyright (C) 1984, Sun Microsystems, Inc.
00039  */
00040 
00041 #include <stdbool.h>
00042 #include <unistd.h>
00043 #include <rpc/rpc.h>
00044 #include <rpc/pmap_prot.h>
00045 #include <rpc/pmap_clnt.h>
00046 #include <sys/socket.h>
00047 
00048 static const struct timeval timeout =
00049 {5, 0};
00050 static const struct timeval tottimeout =
00051 {60, 0};
00052 
00053 /*
00054  * Create a socket that is locally bound to a non-reserve port. For
00055  * any failures, -1 is returned which will cause the RPC code to
00056  * create the socket.
00057  */
00058 int
00059 internal_function
00060 __get_socket (struct sockaddr_in *saddr)
00061 {
00062   int so = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
00063   if (so < 0)
00064     return -1;
00065 
00066   struct sockaddr_in laddr;
00067   socklen_t namelen = sizeof (laddr);
00068   laddr.sin_family = AF_INET;
00069   laddr.sin_port = 0;
00070   laddr.sin_addr.s_addr = htonl (INADDR_ANY);
00071 
00072   int cc = __bind (so, (struct sockaddr *) &laddr, namelen);
00073   if (__builtin_expect (cc < 0, 0))
00074     {
00075     fail:
00076       __close (so);
00077       return -1;
00078     }
00079 
00080   cc = __connect (so, (struct sockaddr *) saddr, namelen);
00081   if (__builtin_expect (cc < 0, 0))
00082     goto fail;
00083 
00084   return so;
00085 }
00086 
00087 
00088 /*
00089  * Find the mapped port for program,version.
00090  * Calls the pmap service remotely to do the lookup.
00091  * Returns 0 if no map exists.
00092  */
00093 u_short
00094 pmap_getport (address, program, version, protocol)
00095      struct sockaddr_in *address;
00096      u_long program;
00097      u_long version;
00098      u_int protocol;
00099 {
00100   u_short port = 0;
00101   int socket = -1;
00102   CLIENT *client;
00103   struct pmap parms;
00104   bool closeit = false;
00105 
00106   address->sin_port = htons (PMAPPORT);
00107   if (protocol == IPPROTO_TCP)
00108     {
00109       /* Don't need a reserved port to get ports from the portmapper.  */
00110       socket = __get_socket(address);
00111       if (socket != -1)
00112        closeit = true;
00113       client = INTUSE(clnttcp_create) (address, PMAPPROG, PMAPVERS, &socket,
00114                                    RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
00115     }
00116   else
00117     client = INTUSE(clntudp_bufcreate) (address, PMAPPROG, PMAPVERS, timeout,
00118                                    &socket, RPCSMALLMSGSIZE,
00119                                    RPCSMALLMSGSIZE);
00120   if (client != (CLIENT *) NULL)
00121     {
00122       struct rpc_createerr *ce = &get_rpc_createerr ();
00123       parms.pm_prog = program;
00124       parms.pm_vers = version;
00125       parms.pm_prot = protocol;
00126       parms.pm_port = 0;    /* not needed or used */
00127       if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t)INTUSE(xdr_pmap),
00128                    (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_u_short),
00129                    (caddr_t)&port, tottimeout) != RPC_SUCCESS)
00130        {
00131          ce->cf_stat = RPC_PMAPFAILURE;
00132          clnt_geterr (client, &ce->cf_error);
00133        }
00134       else if (port == 0)
00135        {
00136          ce->cf_stat = RPC_PROGNOTREGISTERED;
00137        }
00138       CLNT_DESTROY (client);
00139     }
00140   /* We only need to close the socket here if we opened  it.  */
00141   if (closeit)
00142     (void) __close (socket);
00143   address->sin_port = 0;
00144   return port;
00145 }
00146 libc_hidden_def (pmap_getport)