Merge up from branch.
[shibboleth/cpp-sp.git] / oncrpc / svc_tcp.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 /* @(#)svc_tcp.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[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * svc_tcp.c, Server side for TCP/IP based RPC.
47  *
48  * Copyright (C) 1984, Sun Microsystems, Inc.
49  *
50  * Actually implements two flavors of transporter -
51  * a tcp rendezvouser (a listner and connection establisher)
52  * and a record/tcp stream.
53  */
54
55 #include <stdio.h>
56 #include <rpc/rpc.h>
57 #include <errno.h>
58 #ifndef WIN32
59 #include <sys/socket.h>
60 #endif
61 #include <errno.h>
62 #ifdef WIN32
63 int xabort();
64 #else
65 extern bool_t abort();
66 extern errno;
67 #endif
68 /*
69  * Ops vector for TCP/IP based rpc service handle
70  */
71 static bool_t           svctcp_recv();
72 static enum xprt_stat   svctcp_stat();
73 static bool_t           svctcp_getargs();
74 static bool_t           svctcp_reply();
75 static bool_t           svctcp_freeargs();
76 static void             svctcp_destroy();
77
78 static struct xp_ops svctcp_op = {
79         svctcp_recv,
80         svctcp_stat,
81         svctcp_getargs,
82         svctcp_reply,
83         svctcp_freeargs,
84         svctcp_destroy
85 };
86
87 /*
88  * Ops vector for TCP/IP rendezvous handler
89  */
90 static bool_t           rendezvous_request();
91 static enum xprt_stat   rendezvous_stat();
92
93 static struct xp_ops svctcp_rendezvous_op = {
94         rendezvous_request,
95         rendezvous_stat,
96 #ifdef WIN32
97         xabort,
98         xabort,
99         xabort,
100 #else
101         abort,
102         abort,
103         abort,
104 #endif
105         svctcp_destroy
106 };
107
108 static int readtcp(), writetcp();
109 static SVCXPRT *makefd_xprt();
110
111 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
112         u_int sendsize;
113         u_int recvsize;
114 };
115
116 struct tcp_conn {  /* kept in xprt->xp_p1 */
117         enum xprt_stat strm_stat;
118         u_long x_id;
119         XDR xdrs;
120         char verf_body[MAX_AUTH_BYTES];
121 };
122
123 /*
124  * Usage:
125  *      xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
126  *
127  * Creates, registers, and returns a (rpc) tcp based transporter.
128  * Once *xprt is initialized, it is registered as a transporter
129  * see (svc.h, xprt_register).  This routine returns
130  * a NULL if a problem occurred.
131  *
132  * If sock<0 then a socket is created, else sock is used.
133  * If the socket, sock is not bound to a port then svctcp_create
134  * binds it to an arbitrary port.  The routine then starts a tcp
135  * listener on the socket's associated port.  In any (successful) case,
136  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
137  * associated port number.
138  *
139  * Since tcp streams do buffered io similar to stdio, the caller can specify
140  * how big the send and receive buffers are via the second and third parms;
141  * 0 => use the system default.
142  */
143 SVCXPRT *
144 svctcp_create(sock, sendsize, recvsize)
145         register int sock;
146         u_int sendsize;
147         u_int recvsize;
148 {
149         bool_t madesock = FALSE;
150         register SVCXPRT *xprt;
151         register struct tcp_rendezvous *r;
152         struct sockaddr_in addr;
153         int len = sizeof(struct sockaddr_in);
154
155         if (sock == RPC_ANYSOCK) {
156 #ifdef WIN32
157                 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
158 #else
159                 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
160 #endif
161                         perror("svctcp_.c - udp socket creation problem");
162                         return ((SVCXPRT *)NULL);
163                 }
164                 madesock = TRUE;
165         }
166         bzero((char *)&addr, sizeof (addr));
167         addr.sin_family = AF_INET;
168         if (bindresvport(sock, &addr)) {
169                 addr.sin_port = 0;
170                 (void)bind(sock, (struct sockaddr *)&addr, len);
171         }
172         if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0)  ||
173             (listen(sock, 2) != 0)) {
174                 perror("svctcp_.c - cannot getsockname or listen");
175                 if (madesock)
176 #ifdef WIN32
177                        (void)closesocket(sock);
178 #else
179                        (void)close(sock);
180 #endif
181                 return ((SVCXPRT *)NULL);
182         }
183         r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
184         if (r == NULL) {
185 #ifdef WIN32
186                 nt_rpc_report("svctcp_create: out of memory\n");
187 #else
188                 (void) fprintf(stderr, "svctcp_create: out of memory\n");
189 #endif
190                 return (NULL);
191         }
192         r->sendsize = sendsize;
193         r->recvsize = recvsize;
194         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
195         if (xprt == NULL) {
196 #ifdef WIN32
197                 nt_rpc_report("svctcp_create: out of memory\n");
198 #else
199                 (void) fprintf(stderr, "svctcp_create: out of memory\n");
200 #endif
201                 return (NULL);
202         }
203         xprt->xp_p2 = NULL;
204         xprt->xp_p1 = (caddr_t)r;
205         xprt->xp_verf = _null_auth;
206         xprt->xp_ops = &svctcp_rendezvous_op;
207         xprt->xp_port = ntohs(addr.sin_port);
208         xprt->xp_sock = sock;
209         xprt_register(xprt);
210         return (xprt);
211 }
212
213 /*
214  * Like svtcp_create(), except the routine takes any *open* UNIX file
215  * descriptor as its first input.
216  */
217 SVCXPRT *
218 svcfd_create(fd, sendsize, recvsize)
219         int fd;
220         u_int sendsize;
221         u_int recvsize;
222 {
223
224         return (makefd_xprt(fd, sendsize, recvsize));
225 }
226
227 static SVCXPRT *
228 makefd_xprt(fd, sendsize, recvsize)
229         int fd;
230         u_int sendsize;
231         u_int recvsize;
232 {
233         register SVCXPRT *xprt;
234         register struct tcp_conn *cd;
235
236         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
237         if (xprt == (SVCXPRT *)NULL) {
238 #ifdef WIN32
239                 nt_rpc_report("svc_tcp: makefd_xprt: out of memory\n");
240 #else
241                 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
242 #endif
243                 goto done;
244         }
245         cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
246         if (cd == (struct tcp_conn *)NULL) {
247 #ifdef WIN32
248                 nt_rpc_report("svc_tcp: makefd_xprt: out of memory\n");
249 #else
250                 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
251 #endif
252                 mem_free((char *) xprt, sizeof(SVCXPRT));
253                 xprt = (SVCXPRT *)NULL;
254                 goto done;
255         }
256         cd->strm_stat = XPRT_IDLE;
257         xdrrec_create(&(cd->xdrs), sendsize, recvsize,
258             (caddr_t)xprt, readtcp, writetcp);
259         xprt->xp_p2 = NULL;
260         xprt->xp_p1 = (caddr_t)cd;
261         xprt->xp_verf.oa_base = cd->verf_body;
262         xprt->xp_addrlen = 0;
263         xprt->xp_ops = &svctcp_op;  /* truely deals with calls */
264         xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
265         xprt->xp_sock = fd;
266         xprt_register(xprt);
267     done:
268         return (xprt);
269 }
270
271 static bool_t
272 rendezvous_request(xprt)
273         register SVCXPRT *xprt;
274 {
275         int sock;
276         struct tcp_rendezvous *r;
277         struct sockaddr_in addr;
278         int len;
279
280         r = (struct tcp_rendezvous *)xprt->xp_p1;
281     again:
282         len = sizeof(struct sockaddr_in);
283         if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
284             &len)) < 0) {
285 #ifdef WIN32
286                 if (WSAerrno == WSAEINTR)
287 #else
288                 if (errno == EINTR)
289 #endif
290                         goto again;
291                return (FALSE);
292         }
293         /*
294          * make a new transporter (re-uses xprt)
295          */
296         xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
297         xprt->xp_raddr = addr;
298         xprt->xp_addrlen = len;
299         return (FALSE); /* there is never an rpc msg to be processed */
300 }
301
302 static enum xprt_stat
303 rendezvous_stat()
304 {
305
306         return (XPRT_IDLE);
307 }
308
309 static void
310 svctcp_destroy(xprt)
311         register SVCXPRT *xprt;
312 {
313         register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
314
315         xprt_unregister(xprt);
316 #ifdef WIN32
317         (void)closesocket(xprt->xp_sock);
318 #else
319         (void)close(xprt->xp_sock);
320 #endif
321         if (xprt->xp_port != 0) {
322                 /* a rendezvouser socket */
323                 xprt->xp_port = 0;
324         } else {
325                 /* an actual connection socket */
326                 XDR_DESTROY(&(cd->xdrs));
327         }
328         mem_free((caddr_t)cd, sizeof(struct tcp_conn));
329         mem_free((caddr_t)xprt, sizeof(SVCXPRT));
330 }
331
332 /*
333  * All read operations timeout after 35 seconds.
334  * A timeout is fatal for the connection.
335  */
336 static struct timeval wait_per_try = { 35, 0 };
337
338 /*
339  * reads data from the tcp conection.
340  * any error is fatal and the connection is closed.
341  * (And a read of zero bytes is a half closed stream => error.)
342  */
343 static int
344 readtcp(xprt, buf, len)
345         register SVCXPRT *xprt;
346         caddr_t buf;
347         register int len;
348 {
349         register int sock = xprt->xp_sock;
350 #ifdef FD_SETSIZE
351         fd_set mask;
352         fd_set readfds;
353
354         FD_ZERO(&mask);
355         FD_SET(sock, &mask);
356 #else
357         register int mask = 1 << sock;
358         int readfds;
359 #endif /* def FD_SETSIZE */
360         do {
361                 readfds = mask;
362 #ifdef WIN32
363                 if (select(0 /* unused in winsock */, &readfds, (int*)NULL, (int*)NULL,
364 #else
365                 if (select(FD_SETSIZE, &readfds, NULL, NULL, 
366 #endif
367                            &wait_per_try) <= 0) {
368 #ifdef WIN32
369                         if (WSAerrno == WSAEINTR) {
370 #else
371                         if (errno == EINTR) {
372 #endif
373                                 continue;
374                         }
375                         goto fatal_err;
376                 }
377 #ifdef FD_SETSIZE
378         } while (!FD_ISSET(sock, &readfds));
379 #else
380         } while (readfds != mask);
381 #endif /* def FD_SETSIZE */
382 #ifdef WIN32
383         if ((len = recv(sock, buf, len, 0)) > 0) {
384 #else
385         if ((len = read(sock, buf, len)) > 0) {
386 #endif
387                 return (len);
388         }
389 fatal_err:
390         ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
391         return (-1);
392 }
393
394 /*
395  * writes data to the tcp connection.
396  * Any error is fatal and the connection is closed.
397  */
398 static int
399 writetcp(xprt, buf, len)
400         register SVCXPRT *xprt;
401         caddr_t buf;
402         int len;
403 {
404         register int i, cnt;
405
406         for (cnt = len; cnt > 0; cnt -= i, buf += i) {
407 #ifdef WIN32
408                 if ((i = send(xprt->xp_sock, buf, cnt, 0)) < 0) {
409 #else
410                 if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
411 #endif
412                         ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
413                             XPRT_DIED;
414                         return (-1);
415                 }
416         }
417         return (len);
418 }
419
420 static enum xprt_stat
421 svctcp_stat(xprt)
422         SVCXPRT *xprt;
423 {
424         register struct tcp_conn *cd =
425             (struct tcp_conn *)(xprt->xp_p1);
426
427         if (cd->strm_stat == XPRT_DIED)
428                 return (XPRT_DIED);
429         if (! xdrrec_eof(&(cd->xdrs)))
430                 return (XPRT_MOREREQS);
431         return (XPRT_IDLE);
432 }
433
434 static bool_t
435 svctcp_recv(xprt, msg)
436         SVCXPRT *xprt;
437         register struct rpc_msg *msg;
438 {
439         register struct tcp_conn *cd =
440             (struct tcp_conn *)(xprt->xp_p1);
441         register XDR *xdrs = &(cd->xdrs);
442
443         xdrs->x_op = XDR_DECODE;
444         (void)xdrrec_skiprecord(xdrs);
445         if (xdr_callmsg(xdrs, msg)) {
446                 cd->x_id = msg->rm_xid;
447                 return (TRUE);
448         }
449         return (FALSE);
450 }
451
452 static bool_t
453 svctcp_getargs(xprt, xdr_args, args_ptr)
454         SVCXPRT *xprt;
455         xdrproc_t xdr_args;
456         caddr_t args_ptr;
457 {
458
459         return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
460 }
461
462 static bool_t
463 svctcp_freeargs(xprt, xdr_args, args_ptr)
464         SVCXPRT *xprt;
465         xdrproc_t xdr_args;
466         caddr_t args_ptr;
467 {
468         register XDR *xdrs =
469             &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
470
471         xdrs->x_op = XDR_FREE;
472         return ((*xdr_args)(xdrs, args_ptr));
473 }
474
475 static bool_t
476 svctcp_reply(xprt, msg)
477         SVCXPRT *xprt;
478         register struct rpc_msg *msg;
479 {
480         register struct tcp_conn *cd =
481             (struct tcp_conn *)(xprt->xp_p1);
482         register XDR *xdrs = &(cd->xdrs);
483         register bool_t stat;
484
485         xdrs->x_op = XDR_ENCODE;
486         msg->rm_xid = cd->x_id;
487         stat = xdr_replymsg(xdrs, msg);
488         (void)xdrrec_endofrecord(xdrs, TRUE);
489         return (stat);
490 }
491
492 #ifdef WIN32
493 int xabort()
494 {
495         abort();
496 }
497 #endif