2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/
56 /*static char *sccsid = "from: @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/
57 static char *rcsid
= "$Id: svc_tcp.c,v 1.5 2003/06/23 17:24:59 majka Exp $";
61 * svc_tcp.c, Server side for TCP/IP based RPC.
63 * Copyright (C) 1984, Sun Microsystems, Inc.
65 * Actually implements two flavors of transporter -
66 * a tcp rendezvouser (a listner and connection establisher)
67 * and a record/tcp stream.
75 #include <sys/socket.h>
78 #define max(a, b) (((a) > (b)) ? (a) : (b))
80 extern int bindresvport();
83 * Ops vector for TCP/IP based rpc service handle
85 static bool_t
svctcp_recv();
86 static enum xprt_stat
svctcp_stat();
87 static bool_t
svctcp_getargs();
88 static bool_t
svctcp_reply();
89 static bool_t
svctcp_freeargs();
90 static void svctcp_destroy();
92 static struct xp_ops svctcp_op
= {
102 * Ops vector for TCP/IP rendezvous handler
104 static bool_t
rendezvous_abort();
105 static bool_t
rendezvous_request();
106 static enum xprt_stat
rendezvous_stat();
108 static struct xp_ops svctcp_rendezvous_op
= {
117 static int readtcp(), writetcp();
118 static SVCXPRT
*makefd_xprt();
120 struct tcp_rendezvous
{ /* kept in xprt->xp_p1 */
125 struct tcp_conn
{ /* kept in xprt->xp_p1 */
126 enum xprt_stat strm_stat
;
129 char verf_body
[MAX_AUTH_BYTES
];
134 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
136 * Creates, registers, and returns a (rpc) tcp based transporter.
137 * Once *xprt is initialized, it is registered as a transporter
138 * see (svc.h, xprt_register). This routine returns
139 * a NULL if a problem occurred.
141 * If sock<0 then a socket is created, else sock is used.
142 * If the socket, sock is not bound to a port then svctcp_create
143 * binds it to an arbitrary port. The routine then starts a tcp
144 * listener on the socket's associated port. In any (successful) case,
145 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
146 * associated port number.
148 * Since tcp streams do buffered io similar to stdio, the caller can specify
149 * how big the send and receive buffers are via the second and third parms;
150 * 0 => use the system default.
153 svctcp_create(sock
, sendsize
, recvsize
)
158 bool_t madesock
= FALSE
;
159 register SVCXPRT
*xprt
;
160 register struct tcp_rendezvous
*r
;
161 struct sockaddr_in addr
;
162 int len
= sizeof(struct sockaddr_in
);
164 if (sock
== RPC_ANYSOCK
) {
165 if ((sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0) {
166 perror("svctcp_.c - udp socket creation problem");
167 return ((SVCXPRT
*)NULL
);
171 bzero((char *)&addr
, sizeof (addr
));
172 addr
.sin_family
= AF_INET
;
173 if (bindresvport(sock
, &addr
)) {
175 (void)bind(sock
, (struct sockaddr
*)&addr
, len
);
177 if ((getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) ||
178 (listen(sock
, 2) != 0)) {
179 perror("svctcp_.c - cannot getsockname or listen");
182 return ((SVCXPRT
*)NULL
);
184 r
= (struct tcp_rendezvous
*)mem_alloc(sizeof(*r
));
186 (void) fprintf(stderr
, "svctcp_create: out of memory\n");
189 r
->sendsize
= sendsize
;
190 r
->recvsize
= recvsize
;
191 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
193 (void) fprintf(stderr
, "svctcp_create: out of memory\n");
197 xprt
->xp_p1
= (caddr_t
)r
;
198 xprt
->xp_verf
= _null_auth
;
199 xprt
->xp_ops
= &svctcp_rendezvous_op
;
200 xprt
->xp_port
= ntohs(addr
.sin_port
);
201 xprt
->xp_sock
= sock
;
207 * Like svtcp_create(), except the routine takes any *open* UNIX file
208 * descriptor as its first input.
211 svcfd_create(fd
, sendsize
, recvsize
)
217 return (makefd_xprt(fd
, sendsize
, recvsize
));
221 makefd_xprt(fd
, sendsize
, recvsize
)
226 register SVCXPRT
*xprt
;
227 register struct tcp_conn
*cd
;
229 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
230 if (xprt
== (SVCXPRT
*)NULL
) {
231 (void) fprintf(stderr
, "svc_tcp: makefd_xprt: out of memory\n");
234 cd
= (struct tcp_conn
*)mem_alloc(sizeof(struct tcp_conn
));
235 if (cd
== (struct tcp_conn
*)NULL
) {
236 (void) fprintf(stderr
, "svc_tcp: makefd_xprt: out of memory\n");
237 mem_free((char *) xprt
, sizeof(SVCXPRT
));
238 xprt
= (SVCXPRT
*)NULL
;
241 cd
->strm_stat
= XPRT_IDLE
;
242 xdrrec_create(&(cd
->xdrs
), sendsize
, recvsize
,
243 (caddr_t
)xprt
, readtcp
, writetcp
);
245 xprt
->xp_p1
= (caddr_t
)cd
;
246 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
247 xprt
->xp_addrlen
= 0;
248 xprt
->xp_ops
= &svctcp_op
; /* truely deals with calls */
249 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
264 rendezvous_request(xprt
)
265 register SVCXPRT
*xprt
;
268 struct tcp_rendezvous
*r
;
269 struct sockaddr_in addr
;
272 r
= (struct tcp_rendezvous
*)xprt
->xp_p1
;
274 len
= sizeof(struct sockaddr_in
);
275 if ((sock
= accept(xprt
->xp_sock
, (struct sockaddr
*)&addr
,
282 * make a new transporter (re-uses xprt)
284 xprt
= makefd_xprt(sock
, r
->sendsize
, r
->recvsize
);
285 xprt
->xp_raddr
= addr
;
286 xprt
->xp_addrlen
= len
;
287 return (FALSE
); /* there is never an rpc msg to be processed */
290 static enum xprt_stat
299 register SVCXPRT
*xprt
;
301 register struct tcp_conn
*cd
= (struct tcp_conn
*)xprt
->xp_p1
;
303 xprt_unregister(xprt
);
304 (void)close(xprt
->xp_sock
);
305 if (xprt
->xp_port
!= 0) {
306 /* a rendezvouser socket */
309 /* an actual connection socket */
310 XDR_DESTROY(&(cd
->xdrs
));
312 mem_free((caddr_t
)cd
, sizeof(struct tcp_conn
));
313 mem_free((caddr_t
)xprt
, sizeof(SVCXPRT
));
317 * All read operations timeout after 35 seconds.
318 * A timeout is fatal for the connection.
320 static struct timeval wait_per_try
= { 35, 0 };
322 extern int svc_maxfd
;
325 * reads data from the tcp conection.
326 * any error is fatal and the connection is closed.
327 * (And a read of zero bytes is a half closed stream => error.)
330 readtcp(xprt
, buf
, len
)
331 register SVCXPRT
*xprt
;
335 register int sock
= xprt
->xp_sock
;
337 bool_t ready
= FALSE
;
341 FD_COPY(&svc_fdset
, &readfds
);
342 FD_SET(sock
, &readfds
);
343 if (select(max(svc_maxfd
, sock
) + 1, &readfds
, NULL
, NULL
, &wait_per_try
) <= 0)
345 if (errno
== EINTR
) continue;
348 else if (FD_ISSET(sock
, &readfds
))
354 svc_getreqset(&readfds
);
359 if ((len
= read(sock
, buf
, len
)) > 0) return len
;
362 ((struct tcp_conn
*)(xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
367 * writes data to the tcp connection.
368 * Any error is fatal and the connection is closed.
371 writetcp(xprt
, buf
, len
)
372 register SVCXPRT
*xprt
;
378 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
) {
379 if ((i
= write(xprt
->xp_sock
, buf
, cnt
)) < 0) {
380 ((struct tcp_conn
*)(xprt
->xp_p1
))->strm_stat
=
388 static enum xprt_stat
392 register struct tcp_conn
*cd
=
393 (struct tcp_conn
*)(xprt
->xp_p1
);
395 if (cd
->strm_stat
== XPRT_DIED
)
397 if (! xdrrec_eof(&(cd
->xdrs
)))
398 return (XPRT_MOREREQS
);
403 svctcp_recv(xprt
, msg
)
405 register struct rpc_msg
*msg
;
407 register struct tcp_conn
*cd
=
408 (struct tcp_conn
*)(xprt
->xp_p1
);
409 register XDR
*xdrs
= &(cd
->xdrs
);
411 xdrs
->x_op
= XDR_DECODE
;
412 (void)xdrrec_skiprecord(xdrs
);
413 if (xdr_callmsg(xdrs
, msg
)) {
414 cd
->x_id
= msg
->rm_xid
;
421 svctcp_getargs(xprt
, xdr_args
, args_ptr
)
427 return ((*xdr_args
)(&(((struct tcp_conn
*)(xprt
->xp_p1
))->xdrs
), args_ptr
));
431 svctcp_freeargs(xprt
, xdr_args
, args_ptr
)
437 &(((struct tcp_conn
*)(xprt
->xp_p1
))->xdrs
);
439 xdrs
->x_op
= XDR_FREE
;
440 return ((*xdr_args
)(xdrs
, args_ptr
));
444 svctcp_reply(xprt
, msg
)
446 register struct rpc_msg
*msg
;
448 register struct tcp_conn
*cd
=
449 (struct tcp_conn
*)(xprt
->xp_p1
);
450 register XDR
*xdrs
= &(cd
->xdrs
);
451 register bool_t stat
;
453 xdrs
->x_op
= XDR_ENCODE
;
454 msg
->rm_xid
= cd
->x_id
;
455 stat
= xdr_replymsg(xdrs
, msg
);
456 (void)xdrrec_endofrecord(xdrs
, TRUE
);