]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sunrpc/svc_udp.c
1 /* @(#)svc_udp.c 2.2 88/07/29 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid
[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
36 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
37 * achieving execute-at-most-once semantics.)
39 * Copyright (C) 1984, Sun Microsystems, Inc.
44 #include <sys/socket.h>
48 #define rpc_buffer(xprt) ((xprt)->xp_p1)
49 #define MAX(a, b) ((a > b) ? a : b)
51 static bool_t
svcudp_recv();
52 static bool_t
svcudp_reply();
53 static enum xprt_stat
svcudp_stat();
54 static bool_t
svcudp_getargs();
55 static bool_t
svcudp_freeargs();
56 static void svcudp_destroy();
58 static struct xp_ops svcudp_op
= {
75 u_int su_iosz
; /* byte size of send.recv buffer */
76 u_long su_xid
; /* transaction id */
77 XDR su_xdrs
; /* XDR handle */
78 char su_verfbody
[MAX_AUTH_BYTES
]; /* verifier body */
79 char * su_cache
; /* cached data, NULL if no cache */
81 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
85 * xprt = svcudp_create(sock);
87 * If sock<0 then a socket is created, else sock is used.
88 * If the socket, sock is not bound to a port then svcudp_create
89 * binds it to an arbitrary port. In any (successful) case,
90 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
91 * associated port number.
92 * Once *xprt is initialized, it is registered as a transporter;
93 * see (svc.h, xprt_register).
94 * The routines returns NULL if a problem occurred.
97 svcudp_bufcreate(sock
, sendsz
, recvsz
)
101 bool_t madesock
= FALSE
;
102 register SVCXPRT
*xprt
;
103 register struct svcudp_data
*su
;
104 struct sockaddr_in addr
;
105 int len
= sizeof(struct sockaddr_in
);
107 if (sock
== RPC_ANYSOCK
) {
108 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0) {
109 perror(_("svcudp_create: socket creation problem"));
110 return ((SVCXPRT
*)NULL
);
114 bzero((char *)&addr
, sizeof (addr
));
115 addr
.sin_family
= AF_INET
;
116 if (bindresvport(sock
, &addr
)) {
118 (void)bind(sock
, (struct sockaddr
*)&addr
, len
);
120 if (getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) {
121 perror(_("svcudp_create - cannot getsockname"));
124 return ((SVCXPRT
*)NULL
);
126 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
128 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
131 su
= (struct svcudp_data
*)mem_alloc(sizeof(*su
));
133 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
136 su
->su_iosz
= ((MAX(sendsz
, recvsz
) + 3) / 4) * 4;
137 if ((rpc_buffer(xprt
) = mem_alloc(su
->su_iosz
)) == NULL
) {
138 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
142 &(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_DECODE
);
144 xprt
->xp_p2
= (caddr_t
)su
;
145 xprt
->xp_verf
.oa_base
= su
->su_verfbody
;
146 xprt
->xp_ops
= &svcudp_op
;
147 xprt
->xp_port
= ntohs(addr
.sin_port
);
148 xprt
->xp_sock
= sock
;
158 return(svcudp_bufcreate(sock
, UDPMSGSIZE
, UDPMSGSIZE
));
161 static enum xprt_stat
170 svcudp_recv(xprt
, msg
)
171 register SVCXPRT
*xprt
;
174 register struct svcudp_data
*su
= su_data(xprt
);
175 register XDR
*xdrs
= &(su
->su_xdrs
);
181 xprt
->xp_addrlen
= sizeof(struct sockaddr_in
);
182 rlen
= recvfrom(xprt
->xp_sock
, rpc_buffer(xprt
), (int) su
->su_iosz
,
183 0, (struct sockaddr
*)&(xprt
->xp_raddr
), &(xprt
->xp_addrlen
));
184 if (rlen
== -1 && errno
== EINTR
)
186 if (rlen
< 16) /* < 4 32-bit ints? */
188 xdrs
->x_op
= XDR_DECODE
;
190 if (! xdr_callmsg(xdrs
, msg
))
192 su
->su_xid
= msg
->rm_xid
;
193 if (su
->su_cache
!= NULL
) {
194 if (cache_get(xprt
, msg
, &reply
, &replylen
)) {
195 (void) sendto(xprt
->xp_sock
, reply
, (int) replylen
, 0,
196 (struct sockaddr
*) &xprt
->xp_raddr
, xprt
->xp_addrlen
);
204 svcudp_reply(xprt
, msg
)
205 register SVCXPRT
*xprt
;
208 register struct svcudp_data
*su
= su_data(xprt
);
209 register XDR
*xdrs
= &(su
->su_xdrs
);
211 register bool_t stat
= FALSE
;
213 xdrs
->x_op
= XDR_ENCODE
;
215 msg
->rm_xid
= su
->su_xid
;
216 if (xdr_replymsg(xdrs
, msg
)) {
217 slen
= (int)XDR_GETPOS(xdrs
);
218 if (sendto(xprt
->xp_sock
, rpc_buffer(xprt
), slen
, 0,
219 (struct sockaddr
*)&(xprt
->xp_raddr
), xprt
->xp_addrlen
)
222 if (su
->su_cache
&& slen
>= 0) {
223 cache_set(xprt
, (u_long
) slen
);
231 svcudp_getargs(xprt
, xdr_args
, args_ptr
)
237 return ((*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
));
241 svcudp_freeargs(xprt
, xdr_args
, args_ptr
)
246 register XDR
*xdrs
= &(su_data(xprt
)->su_xdrs
);
248 xdrs
->x_op
= XDR_FREE
;
249 return ((*xdr_args
)(xdrs
, args_ptr
));
254 register SVCXPRT
*xprt
;
256 register struct svcudp_data
*su
= su_data(xprt
);
258 xprt_unregister(xprt
);
259 (void)close(xprt
->xp_sock
);
260 XDR_DESTROY(&(su
->su_xdrs
));
261 mem_free(rpc_buffer(xprt
), su
->su_iosz
);
262 mem_free((caddr_t
)su
, sizeof(struct svcudp_data
));
263 mem_free((caddr_t
)xprt
, sizeof(SVCXPRT
));
267 /***********this could be a separate file*********************/
270 * Fifo cache for udp server
271 * Copies pointers to reply buffers into fifo cache
272 * Buffers are sent again if retransmissions are detected.
275 #define SPARSENESS 4 /* 75% sparse */
277 #define CACHE_PERROR(msg) \
278 (void) fprintf(stderr,"%s\n", msg)
280 #define ALLOC(type, size) \
281 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
283 #define BZERO(addr, type, size) \
284 bzero((char *) addr, sizeof(type) * (int) (size))
287 * An entry in the cache
289 typedef struct cache_node
*cache_ptr
;
292 * Index into cache is xid, proc, vers, prog and address
298 struct sockaddr_in cache_addr
;
300 * The cached reply and length
303 u_long cache_replylen
;
305 * Next node on the list, if there is a collision
307 cache_ptr cache_next
;
316 u_long uc_size
; /* size of cache */
317 cache_ptr
*uc_entries
; /* hash table of entries in cache */
318 cache_ptr
*uc_fifo
; /* fifo list of entries in cache */
319 u_long uc_nextvictim
; /* points to next victim in fifo list */
320 u_long uc_prog
; /* saved program number */
321 u_long uc_vers
; /* saved version number */
322 u_long uc_proc
; /* saved procedure number */
323 struct sockaddr_in uc_addr
; /* saved caller's address */
328 * the hashing function
330 #define CACHE_LOC(transp, xid) \
331 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
335 * Enable use of the cache.
336 * Note: there is no disable.
338 svcudp_enablecache(transp
, size
)
342 struct svcudp_data
*su
= su_data(transp
);
343 struct udp_cache
*uc
;
345 if (su
->su_cache
!= NULL
) {
346 CACHE_PERROR(_("enablecache: cache already enabled"));
349 uc
= ALLOC(struct udp_cache
, 1);
351 CACHE_PERROR(_("enablecache: could not allocate cache"));
355 uc
->uc_nextvictim
= 0;
356 uc
->uc_entries
= ALLOC(cache_ptr
, size
* SPARSENESS
);
357 if (uc
->uc_entries
== NULL
) {
358 CACHE_PERROR(_("enablecache: could not allocate cache data"));
361 BZERO(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
362 uc
->uc_fifo
= ALLOC(cache_ptr
, size
);
363 if (uc
->uc_fifo
== NULL
) {
364 CACHE_PERROR(_("enablecache: could not allocate cache fifo"));
367 BZERO(uc
->uc_fifo
, cache_ptr
, size
);
368 su
->su_cache
= (char *) uc
;
374 * Set an entry in the cache
377 cache_set(xprt
, replylen
)
381 register cache_ptr victim
;
382 register cache_ptr
*vicp
;
383 register struct svcudp_data
*su
= su_data(xprt
);
384 struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
389 * Find space for the new entry, either by
390 * reusing an old entry, or by mallocing a new one
392 victim
= uc
->uc_fifo
[uc
->uc_nextvictim
];
393 if (victim
!= NULL
) {
394 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
395 for (vicp
= &uc
->uc_entries
[loc
];
396 *vicp
!= NULL
&& *vicp
!= victim
;
397 vicp
= &(*vicp
)->cache_next
)
400 CACHE_PERROR(_("cache_set: victim not found"));
403 *vicp
= victim
->cache_next
; /* remote from cache */
404 newbuf
= victim
->cache_reply
;
406 victim
= ALLOC(struct cache_node
, 1);
407 if (victim
== NULL
) {
408 CACHE_PERROR("cache_set: victim alloc failed");
411 newbuf
= mem_alloc(su
->su_iosz
);
412 if (newbuf
== NULL
) {
413 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
421 victim
->cache_replylen
= replylen
;
422 victim
->cache_reply
= rpc_buffer(xprt
);
423 rpc_buffer(xprt
) = newbuf
;
424 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_ENCODE
);
425 victim
->cache_xid
= su
->su_xid
;
426 victim
->cache_proc
= uc
->uc_proc
;
427 victim
->cache_vers
= uc
->uc_vers
;
428 victim
->cache_prog
= uc
->uc_prog
;
429 victim
->cache_addr
= uc
->uc_addr
;
430 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
431 victim
->cache_next
= uc
->uc_entries
[loc
];
432 uc
->uc_entries
[loc
] = victim
;
433 uc
->uc_fifo
[uc
->uc_nextvictim
++] = victim
;
434 uc
->uc_nextvictim
%= uc
->uc_size
;
438 * Try to get an entry from the cache
439 * return 1 if found, 0 if not found
442 cache_get(xprt
, msg
, replyp
, replylenp
)
449 register cache_ptr ent
;
450 register struct svcudp_data
*su
= su_data(xprt
);
451 register struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
453 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
455 loc
= CACHE_LOC(xprt
, su
->su_xid
);
456 for (ent
= uc
->uc_entries
[loc
]; ent
!= NULL
; ent
= ent
->cache_next
) {
457 if (ent
->cache_xid
== su
->su_xid
&&
458 ent
->cache_proc
== uc
->uc_proc
&&
459 ent
->cache_vers
== uc
->uc_vers
&&
460 ent
->cache_prog
== uc
->uc_prog
&&
461 EQADDR(ent
->cache_addr
, uc
->uc_addr
)) {
462 *replyp
= ent
->cache_reply
;
463 *replylenp
= ent
->cache_replylen
;
468 * Failed to find entry
469 * Remember a few things so we can do a set later
471 uc
->uc_proc
= msg
->rm_call
.cb_proc
;
472 uc
->uc_vers
= msg
->rm_call
.cb_vers
;
473 uc
->uc_prog
= msg
->rm_call
.cb_prog
;
474 uc
->uc_addr
= xprt
->xp_raddr
;