]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights | |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the | |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for | |
26 | * unrestricted use provided that this legend is included on all tape | |
27 | * media and as a part of the software program in whole or part. Users | |
28 | * may copy or modify Sun RPC without charge, but are not authorized | |
29 | * to license or distribute it to anyone else except as part of a product or | |
30 | * program developed by the user. | |
31 | * | |
32 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE | |
33 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR | |
34 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. | |
35 | * | |
36 | * Sun RPC is provided with no support and without any obligation on the | |
37 | * part of Sun Microsystems, Inc. to assist in its use, correction, | |
38 | * modification or enhancement. | |
39 | * | |
40 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE | |
41 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC | |
42 | * OR ANY PART THEREOF. | |
43 | * | |
44 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue | |
45 | * or profits or other special, indirect and consequential damages, even if | |
46 | * Sun has been advised of the possibility of such damages. | |
47 | * | |
48 | * Sun Microsystems, Inc. | |
49 | * 2550 Garcia Avenue | |
50 | * Mountain View, California 94043 | |
51 | */ | |
52 | ||
53 | #if defined(LIBC_SCCS) && !defined(lint) | |
54 | /*static char *sccsid = "from: @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/ | |
55 | /*static char *sccsid = "from: @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/ | |
56 | static char *rcsid = "$Id: svc_tcp.c,v 1.6 2004/06/11 16:28:07 majka Exp $"; | |
57 | #endif | |
58 | ||
59 | /* | |
60 | * svc_tcp.c, Server side for TCP/IP based RPC. | |
61 | * | |
62 | * Copyright (C) 1984, Sun Microsystems, Inc. | |
63 | * | |
64 | * Actually implements two flavors of transporter - | |
65 | * a tcp rendezvouser (a listner and connection establisher) | |
66 | * and a record/tcp stream. | |
67 | */ | |
68 | ||
69 | #include <stdio.h> | |
70 | #include <stdlib.h> | |
71 | #include <string.h> | |
72 | #include <unistd.h> | |
73 | #include <rpc/rpc.h> | |
74 | #include <sys/socket.h> | |
75 | #include <errno.h> | |
76 | ||
77 | #define max(a, b) (((a) > (b)) ? (a) : (b)) | |
78 | ||
79 | extern int bindresvport(); | |
80 | ||
81 | /* | |
82 | * Ops vector for TCP/IP based rpc service handle | |
83 | */ | |
84 | static bool_t svctcp_recv(); | |
85 | static enum xprt_stat svctcp_stat(); | |
86 | static bool_t svctcp_getargs(); | |
87 | static bool_t svctcp_reply(); | |
88 | static bool_t svctcp_freeargs(); | |
89 | static void svctcp_destroy(); | |
90 | ||
91 | static struct xp_ops svctcp_op = { | |
92 | svctcp_recv, | |
93 | svctcp_stat, | |
94 | svctcp_getargs, | |
95 | svctcp_reply, | |
96 | svctcp_freeargs, | |
97 | svctcp_destroy | |
98 | }; | |
99 | ||
100 | /* | |
101 | * Ops vector for TCP/IP rendezvous handler | |
102 | */ | |
103 | static bool_t rendezvous_abort(); | |
104 | static bool_t rendezvous_request(); | |
105 | static enum xprt_stat rendezvous_stat(); | |
106 | ||
107 | static struct xp_ops svctcp_rendezvous_op = { | |
108 | rendezvous_request, | |
109 | rendezvous_stat, | |
110 | rendezvous_abort, | |
111 | rendezvous_abort, | |
112 | rendezvous_abort, | |
113 | svctcp_destroy | |
114 | }; | |
115 | ||
116 | static int readtcp(), writetcp(); | |
117 | static SVCXPRT *makefd_xprt(); | |
118 | ||
119 | struct tcp_rendezvous { /* kept in xprt->xp_p1 */ | |
120 | u_int sendsize; | |
121 | u_int recvsize; | |
122 | }; | |
123 | ||
124 | struct tcp_conn { /* kept in xprt->xp_p1 */ | |
125 | enum xprt_stat strm_stat; | |
126 | u_long x_id; | |
127 | XDR xdrs; | |
128 | char verf_body[MAX_AUTH_BYTES]; | |
129 | }; | |
130 | ||
131 | /* | |
132 | * Usage: | |
133 | * xprt = svctcp_create(sock, send_buf_size, recv_buf_size); | |
134 | * | |
135 | * Creates, registers, and returns a (rpc) tcp based transporter. | |
136 | * Once *xprt is initialized, it is registered as a transporter | |
137 | * see (svc.h, xprt_register). This routine returns | |
138 | * a NULL if a problem occurred. | |
139 | * | |
140 | * If sock<0 then a socket is created, else sock is used. | |
141 | * If the socket, sock is not bound to a port then svctcp_create | |
142 | * binds it to an arbitrary port. The routine then starts a tcp | |
143 | * listener on the socket's associated port. In any (successful) case, | |
144 | * xprt->xp_sock is the registered socket number and xprt->xp_port is the | |
145 | * associated port number. | |
146 | * | |
147 | * Since tcp streams do buffered io similar to stdio, the caller can specify | |
148 | * how big the send and receive buffers are via the second and third parms; | |
149 | * 0 => use the system default. | |
150 | */ | |
151 | SVCXPRT * | |
152 | svctcp_create(sock, sendsize, recvsize) | |
153 | register int sock; | |
154 | u_int sendsize; | |
155 | u_int recvsize; | |
156 | { | |
157 | bool_t madesock = FALSE; | |
158 | register SVCXPRT *xprt; | |
159 | register struct tcp_rendezvous *r; | |
160 | struct sockaddr_in addr; | |
161 | int len = sizeof(struct sockaddr_in); | |
162 | ||
163 | if (sock == RPC_ANYSOCK) { | |
164 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { | |
165 | perror("svctcp_.c - udp socket creation problem"); | |
166 | return ((SVCXPRT *)NULL); | |
167 | } | |
168 | madesock = TRUE; | |
169 | } | |
170 | bzero((char *)&addr, sizeof (addr)); | |
171 | addr.sin_family = AF_INET; | |
172 | if (bindresvport(sock, &addr)) { | |
173 | addr.sin_port = 0; | |
174 | (void)bind(sock, (struct sockaddr *)&addr, len); | |
175 | } | |
176 | if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) || | |
177 | (listen(sock, 2) != 0)) { | |
178 | perror("svctcp_.c - cannot getsockname or listen"); | |
179 | if (madesock) | |
180 | (void)close(sock); | |
181 | return ((SVCXPRT *)NULL); | |
182 | } | |
183 | r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r)); | |
184 | if (r == NULL) { | |
185 | (void) fprintf(stderr, "svctcp_create: out of memory\n"); | |
186 | return (NULL); | |
187 | } | |
188 | r->sendsize = sendsize; | |
189 | r->recvsize = recvsize; | |
190 | xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); | |
191 | if (xprt == NULL) { | |
192 | (void) fprintf(stderr, "svctcp_create: out of memory\n"); | |
193 | return (NULL); | |
194 | } | |
195 | xprt->xp_p2 = NULL; | |
196 | xprt->xp_p1 = (caddr_t)r; | |
197 | xprt->xp_verf = _null_auth; | |
198 | xprt->xp_ops = &svctcp_rendezvous_op; | |
199 | xprt->xp_port = ntohs(addr.sin_port); | |
200 | xprt->xp_sock = sock; | |
201 | xprt_register(xprt); | |
202 | return (xprt); | |
203 | } | |
204 | ||
205 | /* | |
206 | * Like svtcp_create(), except the routine takes any *open* UNIX file | |
207 | * descriptor as its first input. | |
208 | */ | |
209 | SVCXPRT * | |
210 | svcfd_create(fd, sendsize, recvsize) | |
211 | int fd; | |
212 | u_int sendsize; | |
213 | u_int recvsize; | |
214 | { | |
215 | ||
216 | return (makefd_xprt(fd, sendsize, recvsize)); | |
217 | } | |
218 | ||
219 | static SVCXPRT * | |
220 | makefd_xprt(fd, sendsize, recvsize) | |
221 | int fd; | |
222 | u_int sendsize; | |
223 | u_int recvsize; | |
224 | { | |
225 | register SVCXPRT *xprt; | |
226 | register struct tcp_conn *cd; | |
227 | ||
228 | xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); | |
229 | if (xprt == (SVCXPRT *)NULL) { | |
230 | (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); | |
231 | goto done; | |
232 | } | |
233 | cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn)); | |
234 | if (cd == (struct tcp_conn *)NULL) { | |
235 | (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); | |
236 | mem_free((char *) xprt, sizeof(SVCXPRT)); | |
237 | xprt = (SVCXPRT *)NULL; | |
238 | goto done; | |
239 | } | |
240 | cd->strm_stat = XPRT_IDLE; | |
241 | xdrrec_create(&(cd->xdrs), sendsize, recvsize, | |
242 | (caddr_t)xprt, readtcp, writetcp); | |
243 | xprt->xp_p2 = NULL; | |
244 | xprt->xp_p1 = (caddr_t)cd; | |
245 | xprt->xp_verf.oa_base = cd->verf_body; | |
246 | xprt->xp_addrlen = 0; | |
247 | xprt->xp_ops = &svctcp_op; /* truely deals with calls */ | |
248 | xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ | |
249 | xprt->xp_sock = fd; | |
250 | xprt_register(xprt); | |
251 | done: | |
252 | return (xprt); | |
253 | } | |
254 | ||
255 | static bool_t | |
256 | rendezvous_abort() | |
257 | { | |
258 | abort(); | |
259 | return (FALSE); | |
260 | } | |
261 | ||
262 | static bool_t | |
263 | rendezvous_request(xprt) | |
264 | register SVCXPRT *xprt; | |
265 | { | |
266 | int sock; | |
267 | struct tcp_rendezvous *r; | |
268 | struct sockaddr_in addr; | |
269 | int len; | |
270 | ||
271 | r = (struct tcp_rendezvous *)xprt->xp_p1; | |
272 | again: | |
273 | len = sizeof(struct sockaddr_in); | |
274 | if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr, | |
275 | &len)) < 0) { | |
276 | if (errno == EINTR) | |
277 | goto again; | |
278 | return (FALSE); | |
279 | } | |
280 | /* | |
281 | * make a new transporter (re-uses xprt) | |
282 | */ | |
283 | xprt = makefd_xprt(sock, r->sendsize, r->recvsize); | |
284 | xprt->xp_raddr = addr; | |
285 | xprt->xp_addrlen = len; | |
286 | return (FALSE); /* there is never an rpc msg to be processed */ | |
287 | } | |
288 | ||
289 | static enum xprt_stat | |
290 | rendezvous_stat() | |
291 | { | |
292 | ||
293 | return (XPRT_IDLE); | |
294 | } | |
295 | ||
296 | static void | |
297 | svctcp_destroy(xprt) | |
298 | register SVCXPRT *xprt; | |
299 | { | |
300 | register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1; | |
301 | ||
302 | xprt_unregister(xprt); | |
303 | (void)close(xprt->xp_sock); | |
304 | if (xprt->xp_port != 0) { | |
305 | /* a rendezvouser socket */ | |
306 | xprt->xp_port = 0; | |
307 | } else { | |
308 | /* an actual connection socket */ | |
309 | XDR_DESTROY(&(cd->xdrs)); | |
310 | } | |
311 | mem_free((caddr_t)cd, sizeof(struct tcp_conn)); | |
312 | mem_free((caddr_t)xprt, sizeof(SVCXPRT)); | |
313 | } | |
314 | ||
315 | /* | |
316 | * All read operations timeout after 35 seconds. | |
317 | * A timeout is fatal for the connection. | |
318 | */ | |
319 | static struct timeval wait_per_try = { 35, 0 }; | |
320 | ||
321 | extern int svc_maxfd; | |
322 | ||
323 | /* | |
324 | * reads data from the tcp conection. | |
325 | * any error is fatal and the connection is closed. | |
326 | * (And a read of zero bytes is a half closed stream => error.) | |
327 | */ | |
328 | static int | |
329 | readtcp(xprt, buf, len) | |
330 | register SVCXPRT *xprt; | |
331 | caddr_t buf; | |
332 | register int len; | |
333 | { | |
334 | register int sock = xprt->xp_sock; | |
335 | fd_set readfds; | |
336 | bool_t ready = FALSE; | |
337 | ||
338 | do | |
339 | { | |
340 | FD_COPY(&svc_fdset, &readfds); | |
341 | FD_SET(sock, &readfds); | |
342 | if (select(max(svc_maxfd, sock) + 1, &readfds, NULL, NULL, &wait_per_try) <= 0) | |
343 | { | |
344 | if (errno == EINTR) continue; | |
345 | goto fatal_err; | |
346 | } | |
347 | else if (FD_ISSET(sock, &readfds)) | |
348 | { | |
349 | ready = TRUE; | |
350 | } | |
351 | } while (!ready); | |
352 | ||
353 | if ((len = read(sock, buf, len)) > 0) return len; | |
354 | ||
355 | fatal_err: | |
356 | ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; | |
357 | return -1; | |
358 | } | |
359 | ||
360 | /* | |
361 | * writes data to the tcp connection. | |
362 | * Any error is fatal and the connection is closed. | |
363 | */ | |
364 | static int | |
365 | writetcp(xprt, buf, len) | |
366 | register SVCXPRT *xprt; | |
367 | caddr_t buf; | |
368 | int len; | |
369 | { | |
370 | register int i, cnt; | |
371 | ||
372 | for (cnt = len; cnt > 0; cnt -= i, buf += i) { | |
373 | if ((i = write(xprt->xp_sock, buf, cnt)) < 0) { | |
374 | ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = | |
375 | XPRT_DIED; | |
376 | return (-1); | |
377 | } | |
378 | } | |
379 | return (len); | |
380 | } | |
381 | ||
382 | static enum xprt_stat | |
383 | svctcp_stat(xprt) | |
384 | SVCXPRT *xprt; | |
385 | { | |
386 | register struct tcp_conn *cd = | |
387 | (struct tcp_conn *)(xprt->xp_p1); | |
388 | ||
389 | if (cd->strm_stat == XPRT_DIED) | |
390 | return (XPRT_DIED); | |
391 | if (! xdrrec_eof(&(cd->xdrs))) | |
392 | return (XPRT_MOREREQS); | |
393 | return (XPRT_IDLE); | |
394 | } | |
395 | ||
396 | static bool_t | |
397 | svctcp_recv(xprt, msg) | |
398 | SVCXPRT *xprt; | |
399 | register struct rpc_msg *msg; | |
400 | { | |
401 | register struct tcp_conn *cd = | |
402 | (struct tcp_conn *)(xprt->xp_p1); | |
403 | register XDR *xdrs = &(cd->xdrs); | |
404 | ||
405 | xdrs->x_op = XDR_DECODE; | |
406 | (void)xdrrec_skiprecord(xdrs); | |
407 | if (xdr_callmsg(xdrs, msg)) { | |
408 | cd->x_id = msg->rm_xid; | |
409 | return (TRUE); | |
410 | } | |
411 | return (FALSE); | |
412 | } | |
413 | ||
414 | static bool_t | |
415 | svctcp_getargs(xprt, xdr_args, args_ptr) | |
416 | SVCXPRT *xprt; | |
417 | xdrproc_t xdr_args; | |
418 | caddr_t args_ptr; | |
419 | { | |
420 | ||
421 | return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr)); | |
422 | } | |
423 | ||
424 | static bool_t | |
425 | svctcp_freeargs(xprt, xdr_args, args_ptr) | |
426 | SVCXPRT *xprt; | |
427 | xdrproc_t xdr_args; | |
428 | caddr_t args_ptr; | |
429 | { | |
430 | register XDR *xdrs = | |
431 | &(((struct tcp_conn *)(xprt->xp_p1))->xdrs); | |
432 | ||
433 | xdrs->x_op = XDR_FREE; | |
434 | return ((*xdr_args)(xdrs, args_ptr)); | |
435 | } | |
436 | ||
437 | static bool_t | |
438 | svctcp_reply(xprt, msg) | |
439 | SVCXPRT *xprt; | |
440 | register struct rpc_msg *msg; | |
441 | { | |
442 | register struct tcp_conn *cd = | |
443 | (struct tcp_conn *)(xprt->xp_p1); | |
444 | register XDR *xdrs = &(cd->xdrs); | |
445 | register bool_t stat; | |
446 | ||
447 | xdrs->x_op = XDR_ENCODE; | |
448 | msg->rm_xid = cd->x_id; | |
449 | stat = xdr_replymsg(xdrs, msg); | |
450 | (void)xdrrec_endofrecord(xdrs, TRUE); | |
451 | return (stat); | |
452 | } |