Merge up from branch.
[shibboleth/cpp-sp.git] / oncrpc / clnt_gen.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)clnt_generic.c   2.2 88/08/01 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  * 
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  * 
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  *
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  * 
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  * 
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";
43 #endif
44 /*
45  * Copyright (C) 1987, Sun Microsystems, Inc.
46  */
47 #ifdef WIN32
48 #include <rpc/rpc.h>
49 #include <errno.h>
50 #else
51 #include <rpc/rpc.h>
52 #include <sys/socket.h>
53 #include <sys/errno.h>
54 #include <netdb.h>
55 #endif
56
57 /*
58  * Generic client creation: takes (hostname, program-number, protocol) and
59  * returns client handle. Default options are set, which the user can 
60  * change using the rpc equivalent of ioctl()'s.
61  */
62 CLIENT *
63 clnt_create(hostname, prog, vers, proto)
64         char *hostname;
65         unsigned prog;
66         unsigned vers;
67         char *proto;
68 {
69         struct hostent *h;
70         struct protoent *p;
71         struct sockaddr_in sin;
72         int sock;
73         struct timeval tv;
74         CLIENT *client;
75
76         h = gethostbyname(hostname);
77         if (h == NULL) {
78                 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
79                 return (NULL);
80         }
81         if (h->h_addrtype != AF_INET) {
82                 /*
83                  * Only support INET for now
84                  */
85                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
86 #ifdef WIN32
87                 rpc_createerr.cf_error.re_errno = WSAEAFNOSUPPORT; 
88 #else
89                 rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; 
90 #endif
91                 return (NULL);
92         }
93         sin.sin_family = h->h_addrtype;
94         sin.sin_port = 0;
95         bzero(sin.sin_zero, sizeof(sin.sin_zero));
96         bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length);
97         p = getprotobyname(proto);
98         if (p == NULL) {
99                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
100 #ifdef WIN32
101                 rpc_createerr.cf_error.re_errno = WSAEPFNOSUPPORT;
102 #else
103                 rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
104 #endif 
105                 return (NULL);
106         }
107         sock = RPC_ANYSOCK;
108         switch (p->p_proto) {
109         case IPPROTO_UDP:
110                 tv.tv_sec = 5;
111                 tv.tv_usec = 0;
112                 client = clntudp_create(&sin, prog, vers, tv, &sock);
113                 if (client == NULL) {
114                         return (NULL);
115                 }
116                 tv.tv_sec = 25;
117                 clnt_control(client, CLSET_TIMEOUT, &tv);
118                 break;
119         case IPPROTO_TCP:
120                 client = clnttcp_create(&sin, prog, vers, &sock, 0, 0);
121                 if (client == NULL) {
122                         return (NULL);
123                 }
124                 tv.tv_sec = 25;
125                 tv.tv_usec = 0;
126                 clnt_control(client, CLSET_TIMEOUT, &tv);
127                 break;
128         default:
129                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130 #ifdef WIN32
131                 rpc_createerr.cf_error.re_errno = WSAEPFNOSUPPORT; 
132 #else
133                 rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 
134 #endif
135                 return (NULL);
136         }
137         return (client);
138 }