Back to index

glibc  2.9
clnt_gen.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) 1987, Sun Microsystems, Inc.
00031  */
00032 
00033 #include <alloca.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #include <rpc/rpc.h>
00037 #include <sys/socket.h>
00038 #include <netdb.h>
00039 
00040 /*
00041  * Generic client creation: takes (hostname, program-number, protocol) and
00042  * returns client handle. Default options are set, which the user can
00043  * change using the rpc equivalent of ioctl()'s.
00044  */
00045 CLIENT *
00046 clnt_create (const char *hostname, u_long prog, u_long vers,
00047             const char *proto)
00048 {
00049   struct hostent hostbuf, *h;
00050   size_t hstbuflen;
00051   char *hsttmpbuf;
00052   struct protoent protobuf, *p;
00053   size_t prtbuflen;
00054   char *prttmpbuf;
00055   struct sockaddr_in sin;
00056   struct sockaddr_un sun;
00057   int sock;
00058   struct timeval tv;
00059   CLIENT *client;
00060   int herr;
00061 
00062   if (strcmp (proto, "unix") == 0)
00063     {
00064       __bzero ((char *)&sun, sizeof (sun));
00065       sun.sun_family = AF_UNIX;
00066       strcpy (sun.sun_path, hostname);
00067       sock = RPC_ANYSOCK;
00068       client = INTUSE(clntunix_create) (&sun, prog, vers, &sock, 0, 0);
00069       if (client == NULL)
00070        return NULL;
00071 #if 0
00072       /* This is not wanted.  This would disable the user from having
00073         a timeout in the clnt_call() call.  Only a call to cnlt_control()
00074         by the user should set the timeout value.  */
00075       tv.tv_sec = 25;
00076       tv.tv_usec = 0;
00077       clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
00078 #endif
00079       return client;
00080     }
00081 
00082   hstbuflen = 1024;
00083   hsttmpbuf = __alloca (hstbuflen);
00084   while (__gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
00085                          &h, &herr) != 0
00086         || h == NULL)
00087     if (herr != NETDB_INTERNAL || errno != ERANGE)
00088       {
00089        get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
00090        return NULL;
00091       }
00092     else
00093       {
00094        /* Enlarge the buffer.  */
00095        hstbuflen *= 2;
00096        hsttmpbuf = __alloca (hstbuflen);
00097       }
00098 
00099   if (h->h_addrtype != AF_INET)
00100     {
00101       /*
00102        * Only support INET for now
00103        */
00104       struct rpc_createerr *ce = &get_rpc_createerr ();
00105       ce->cf_stat = RPC_SYSTEMERROR;
00106       ce->cf_error.re_errno = EAFNOSUPPORT;
00107       return NULL;
00108     }
00109   sin.sin_family = h->h_addrtype;
00110   sin.sin_port = 0;
00111   __bzero (sin.sin_zero, sizeof (sin.sin_zero));
00112   memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
00113 
00114   prtbuflen = 1024;
00115   prttmpbuf = __alloca (prtbuflen);
00116   while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
00117         || p == NULL)
00118     if (errno != ERANGE)
00119       {
00120        struct rpc_createerr *ce = &get_rpc_createerr ();
00121        ce->cf_stat = RPC_UNKNOWNPROTO;
00122        ce->cf_error.re_errno = EPFNOSUPPORT;
00123        return NULL;
00124       }
00125     else
00126       {
00127        /* Enlarge the buffer.  */
00128        prtbuflen *= 2;
00129        prttmpbuf = __alloca (prtbuflen);
00130       }
00131 
00132   sock = RPC_ANYSOCK;
00133   switch (p->p_proto)
00134     {
00135     case IPPROTO_UDP:
00136       tv.tv_sec = 5;
00137       tv.tv_usec = 0;
00138       client = INTUSE(clntudp_create) (&sin, prog, vers, tv, &sock);
00139       if (client == NULL)
00140        {
00141          return NULL;
00142        }
00143 #if 0
00144       /* This is not wanted.  This would disable the user from having
00145         a timeout in the clnt_call() call.  Only a call to cnlt_control()
00146         by the user should set the timeout value.  */
00147       tv.tv_sec = 25;
00148       clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
00149 #endif
00150       break;
00151     case IPPROTO_TCP:
00152       client = INTUSE(clnttcp_create) (&sin, prog, vers, &sock, 0, 0);
00153       if (client == NULL)
00154        {
00155          return NULL;
00156        }
00157 #if 0
00158       /* This is not wanted.  This would disable the user from having
00159         a timeout in the clnt_call() call.  Only a call to cnlt_control()
00160         by the user should set the timeout value.  */
00161       tv.tv_sec = 25;
00162       tv.tv_usec = 0;
00163       clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
00164 #endif
00165       break;
00166     default:
00167       {
00168        struct rpc_createerr *ce = &get_rpc_createerr ();
00169        ce->cf_stat = RPC_SYSTEMERROR;
00170        ce->cf_error.re_errno = EPFNOSUPPORT;
00171       }
00172       return (NULL);
00173     }
00174   return client;
00175 }
00176 INTDEF (clnt_create)