source: rtems/c/src/lib/librpc/svc_udp.c @ eb299afc

4.104.114.84.95
Last change on this file since eb299afc was 4721cf1, checked in by Joel Sherrill <joel.sherrill@…>, on 12/03/98 at 23:54:14

Patch from Emmanuel Raguet <raguet@…> to add remote debug server
and RPC support to RTEMS. Thanks. :) Email follows:

Hello,

For Xmas, here is the Remote Debugger on RTEMS !

Here are 2 patches for the Remote Debugger on RTEMS for pc386 from Linux
host :

  • one for RTEMS it self,
  • one for GDB-4.17.

1/ RTEMS patch
--------------

This patch adds 2 libraries :

  • a simplified SUN RPC library
  • the Remote Debugger library

The configuration command is the following :
../rtems4/configure --target=i386-rtemself --enable-rtemsbsp=pc386
--enable-rdbg

The SUN RPC library is built only if networking is set.
The RDBG library is built if networking and enable-rdbg are set.

The function used to initialize the debugger is :

rtems_rdbg_initialize ();

A special function has been created to force a task to be
in a "debug" state : enterRdbg().
The use of this function is not mandatory.

2/ GDB-4.17 patch
-----------------

This patch create a new RTEMS target for GDB-4.17.

The configuration command is the following :
./configure --enable-shared --target=i386RTEMS

To connect to a target, use :

target rtems [your_site_address]

Then, attach the target using : attach 1

And... Debug ;)

You can obtain the original GDB-4.17 on
ftp://ftp.debian.org/debian/dists/stable/main/source/devel/gdb_4.17.orig.tar.gz

This has been tested from a Debian 2.0.1 linux host.

  • Property mode set to 100644
File size: 11.9 KB
Line 
1/* @(#)svc_udp.c        2.2 88/07/29 4.0 RPCSRC */
2/*
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.
9 *
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.
13 *
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.
17 *
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.
21 *
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.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California  94043
29 */
30#if !defined(lint) && defined(SCCSIDS)
31static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
32#endif
33
34/*
35 * svc_udp.c,
36 * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
37 * achieving execute-at-most-once semantics.)
38 *
39 * Copyright (C) 1984, Sun Microsystems, Inc.
40 */
41
42/*
43 * Adapted for rgdb specifi purposes.
44 * Modifications are under #ifndef NO_RGDB.
45 */
46
47#include <stdio.h>              /* printf */
48#include <stdlib.h>             /* free */
49#include <string.h>             /* bzero, bcmp */
50#include <rpc/rpc.h>
51#include <sys/socket.h>
52
53#define rpc_buffer(xprt) ((xprt)->xp_p1)
54#define MAX(a, b)     ((a > b) ? a : b)
55
56static bool_t           svcudp_recv();
57static bool_t           svcudp_reply();
58static enum xprt_stat   svcudp_stat();
59static bool_t           svcudp_getargs();
60static bool_t           svcudp_freeargs();
61static void             svcudp_destroy();
62static int              cache_get();
63static void             cache_set();
64
65static struct xp_ops svcudp_op = {
66        svcudp_recv,
67        svcudp_stat,
68        svcudp_getargs,
69        svcudp_reply,
70        svcudp_freeargs,
71        svcudp_destroy
72};
73
74extern int errno;
75
76/*
77 * kept in xprt->xp_p2
78 */
79struct svcudp_data {
80        u_int   su_iosz;        /* byte size of send.recv buffer */
81        u_long  su_xid;         /* transaction id */
82        XDR     su_xdrs;        /* XDR handle */
83        char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
84        char *  su_cache;       /* cached data, NULL if no cache */
85};
86#define su_data(xprt)   ((struct svcudp_data *)(xprt->xp_p2))
87
88/*
89 * Usage:
90 *      xprt = svcudp_create(sock);
91 *
92 * If sock<0 then a socket is created, else sock is used.
93 * If the socket, sock is not bound to a port then svcudp_create
94 * binds it to an arbitrary port.  In any (successful) case,
95 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
96 * associated port number.
97 * Once *xprt is initialized, it is registered as a transporter;
98 * see (svc.h, xprt_register).
99 * The routines returns NULL if a problem occurred.
100 */
101SVCXPRT *
102svcudp_bufcreate(sock, sendsz, recvsz)
103        register int sock;
104        u_int   sendsz, recvsz;
105{
106        register SVCXPRT *xprt;
107        register struct svcudp_data *su;
108        struct sockaddr_in addr;
109        int len = sizeof(struct sockaddr_in);
110
111        if (sock == RPC_ANYSOCK) {
112            (void)printf
113                ("svcudp_create: socket should be created and bound\n");
114            return ((SVCXPRT *) NULL);
115        }
116
117        if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
118            (void)printf("svcudp_create - cannot getsockname");
119            return ((SVCXPRT *)NULL);
120        }
121
122        xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
123
124        if (xprt == NULL) {
125                (void)printf("svcudp_create: out of memory\n");
126                return (NULL);
127        }
128        su = (struct svcudp_data *)mem_alloc(sizeof(*su));
129        if (su == NULL) {
130                (void)printf("svcudp_create: out of memory\n");
131                return (NULL);
132        }
133        su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
134        if ((rpc_buffer(xprt) = (caddr_t) mem_alloc(su->su_iosz)) == NULL) {
135                (void)printf("svcudp_create: out of memory\n");
136                return (NULL);
137        }
138        xdrmem_create(
139            &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
140        su->su_cache = NULL;
141        xprt->xp_p2 = (caddr_t)su;
142        xprt->xp_verf.oa_base = su->su_verfbody;
143        xprt->xp_ops = &svcudp_op;
144        xprt->xp_port = ntohs(addr.sin_port);
145        xprt->xp_sock = sock;
146        return (xprt);
147}
148
149SVCXPRT *
150svcudp_create(sock)
151        int sock;
152{
153
154/*      return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));*/
155        return(svcudp_bufcreate(sock, 1500, 1500));
156}
157
158static enum xprt_stat
159svcudp_stat(xprt)
160        SVCXPRT *xprt;
161{
162
163        return (XPRT_IDLE);
164}
165
166static bool_t
167svcudp_recv(xprt, msg)
168        register SVCXPRT *xprt;
169        struct rpc_msg *msg;
170{
171        register struct svcudp_data *su = su_data(xprt);
172        register XDR *xdrs = &(su->su_xdrs);
173        register int rlen;
174        char *reply;
175        u_long replylen;
176
177    again:
178        xprt->xp_addrlen = sizeof(struct sockaddr_in);
179        rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
180            0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
181        if (rlen == -1 /* && errno == EINTR */)
182                goto again;
183        if (rlen < 4*sizeof(u_long))
184                return (FALSE);
185        xdrs->x_op = XDR_DECODE;
186        XDR_SETPOS(xdrs, 0);
187        if (! xdr_callmsg(xdrs, msg))
188                return (FALSE);
189        su->su_xid = msg->rm_xid;
190        if (su->su_cache != NULL) {
191                if (cache_get(xprt, msg, &reply, &replylen)) {
192                        (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
193                          (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
194                        return (FALSE);
195                }
196        }
197        return (TRUE);
198}
199
200static bool_t
201svcudp_reply(xprt, msg)
202        register SVCXPRT *xprt;
203        struct rpc_msg *msg;
204{
205        register struct svcudp_data *su = su_data(xprt);
206        register XDR *xdrs = &(su->su_xdrs);
207        register int slen;
208        register bool_t stat = FALSE;
209
210        xdrs->x_op = XDR_ENCODE;
211        XDR_SETPOS(xdrs, 0);
212        msg->rm_xid = su->su_xid;
213        if (xdr_replymsg(xdrs, msg)) {
214                slen = (int)XDR_GETPOS(xdrs);
215                if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
216                    (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
217                    == slen) {
218                        stat = TRUE;
219                        if (su->su_cache && slen >= 0) {
220                                cache_set(xprt, (u_long) slen);
221                        }
222                }
223        }
224        return (stat);
225}
226
227static bool_t
228svcudp_getargs(xprt, xdr_args, args_ptr)
229        SVCXPRT *xprt;
230        xdrproc_t xdr_args;
231        caddr_t args_ptr;
232{
233
234        return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
235}
236
237static bool_t
238svcudp_freeargs(xprt, xdr_args, args_ptr)
239        SVCXPRT *xprt;
240        xdrproc_t xdr_args;
241        caddr_t args_ptr;
242{
243        register XDR *xdrs = &(su_data(xprt)->su_xdrs);
244
245        xdrs->x_op = XDR_FREE;
246        return ((*xdr_args)(xdrs, args_ptr));
247}
248
249static void
250svcudp_destroy(xprt)
251        register SVCXPRT *xprt;
252{
253        register struct svcudp_data *su = su_data(xprt);
254/*
255        xprt_unregister(xprt);
256        (void)close(xprt->xp_sock);
257*/
258        XDR_DESTROY(&(su->su_xdrs));
259        mem_free(rpc_buffer(xprt), su->su_iosz);
260        mem_free((caddr_t)su, sizeof(struct svcudp_data));
261        mem_free((caddr_t)xprt, sizeof(SVCXPRT));
262}
263
264
265/***********this could be a separate file*********************/
266
267/*
268 * Fifo cache for udp server
269 * Copies pointers to reply buffers into fifo cache
270 * Buffers are sent again if retransmissions are detected.
271 */
272
273#define SPARSENESS 4    /* 75% sparse */
274
275#define CACHE_PERROR(msg)       \
276        (void) printf("%s\n", msg)
277
278#define ALLOC(type, size)       \
279        (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
280
281#define BZERO(addr, type, size)  \
282        bzero((char *) addr, sizeof(type) * (int) (size))
283
284/*
285 * An entry in the cache
286 */
287typedef struct cache_node *cache_ptr;
288struct cache_node {
289        /*
290         * Index into cache is xid, proc, vers, prog and address
291         */
292        u_long cache_xid;
293        u_long cache_proc;
294        u_long cache_vers;
295        u_long cache_prog;
296        struct sockaddr_in cache_addr;
297        /*
298         * The cached reply and length
299         */
300        char * cache_reply;
301        u_long cache_replylen;
302        /*
303         * Next node on the list, if there is a collision
304         */
305        cache_ptr cache_next;   
306};
307
308
309
310/*
311 * The entire cache
312 */
313struct udp_cache {
314        u_long uc_size;         /* size of cache */
315        cache_ptr *uc_entries;  /* hash table of entries in cache */
316        cache_ptr *uc_fifo;     /* fifo list of entries in cache */
317        u_long uc_nextvictim;   /* points to next victim in fifo list */
318        u_long uc_prog;         /* saved program number */
319        u_long uc_vers;         /* saved version number */
320        u_long uc_proc;         /* saved procedure number */
321        struct sockaddr_in uc_addr; /* saved caller's address */
322};
323
324
325/*
326 * the hashing function
327 */
328#define CACHE_LOC(transp, xid)  \
329 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
330
331
332/*
333 * Enable use of the cache.
334 * Note: there is no disable.
335 */
336    int
337svcudp_enablecache(transp, size)
338        SVCXPRT *transp;
339        u_long size;
340{
341        struct svcudp_data *su = su_data(transp);
342        struct udp_cache *uc;
343
344        if (su->su_cache != NULL) {
345                CACHE_PERROR("enablecache: cache already enabled");
346                return(0);     
347        }
348        uc = ALLOC(struct udp_cache, 1);
349        if (uc == NULL) {
350                CACHE_PERROR("enablecache: could not allocate cache");
351                return(0);
352        }
353        uc->uc_size = size;
354        uc->uc_nextvictim = 0;
355        uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
356        if (uc->uc_entries == NULL) {
357                CACHE_PERROR("enablecache: could not allocate cache data");
358                return(0);
359        }
360        BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
361        uc->uc_fifo = ALLOC(cache_ptr, size);
362        if (uc->uc_fifo == NULL) {
363                CACHE_PERROR("enablecache: could not allocate cache fifo");
364                return(0);
365        }
366        BZERO(uc->uc_fifo, cache_ptr, size);
367        su->su_cache = (char *) uc;
368        return(1);
369}
370
371
372/*
373 * Set an entry in the cache
374 */
375static void
376cache_set(xprt, replylen)
377        SVCXPRT *xprt;
378        u_long replylen;       
379{
380        register cache_ptr victim;     
381        register cache_ptr *vicp;
382        register struct svcudp_data *su = su_data(xprt);
383        struct udp_cache *uc = (struct udp_cache *) su->su_cache;
384        u_int loc;
385        char *newbuf;
386
387        /*
388         * Find space for the new entry, either by
389         * reusing an old entry, or by mallocing a new one
390         */
391        victim = uc->uc_fifo[uc->uc_nextvictim];
392        if (victim != NULL) {
393                loc = CACHE_LOC(xprt, victim->cache_xid);
394                for (vicp = &uc->uc_entries[loc];
395                  *vicp != NULL && *vicp != victim;
396                  vicp = &(*vicp)->cache_next)
397                                ;
398                if (*vicp == NULL) {
399                        CACHE_PERROR("cache_set: victim not found");
400                        return;
401                }
402                *vicp = victim->cache_next;     /* remote from cache */
403                newbuf = victim->cache_reply;
404        } else {
405                victim = ALLOC(struct cache_node, 1);
406                if (victim == NULL) {
407                        CACHE_PERROR("cache_set: victim alloc failed");
408                        return;
409                }
410                newbuf = (char*) mem_alloc(su->su_iosz);
411                if (newbuf == NULL) {
412                        CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
413                        return;
414                }
415        }
416
417        /*
418         * Store it away
419         */
420        victim->cache_replylen = replylen;
421        victim->cache_reply = rpc_buffer(xprt);
422        rpc_buffer(xprt) = newbuf;
423        xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
424        victim->cache_xid = su->su_xid;
425        victim->cache_proc = uc->uc_proc;
426        victim->cache_vers = uc->uc_vers;
427        victim->cache_prog = uc->uc_prog;
428        victim->cache_addr = uc->uc_addr;
429        loc = CACHE_LOC(xprt, victim->cache_xid);
430        victim->cache_next = uc->uc_entries[loc];       
431        uc->uc_entries[loc] = victim;
432        uc->uc_fifo[uc->uc_nextvictim++] = victim;
433        uc->uc_nextvictim %= uc->uc_size;
434}
435
436/*
437 * Try to get an entry from the cache
438 * return 1 if found, 0 if not found
439 */
440static int
441cache_get(xprt, msg, replyp, replylenp)
442        SVCXPRT *xprt;
443        struct rpc_msg *msg;
444        char **replyp;
445        u_long *replylenp;
446{
447        u_int loc;
448        register cache_ptr ent;
449        register struct svcudp_data *su = su_data(xprt);
450        register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
451
452#       define EQADDR(a1, a2)   (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
453
454        loc = CACHE_LOC(xprt, su->su_xid);
455        for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
456                if (ent->cache_xid == su->su_xid &&
457                  ent->cache_proc == uc->uc_proc &&
458                  ent->cache_vers == uc->uc_vers &&
459                  ent->cache_prog == uc->uc_prog &&
460                  EQADDR(ent->cache_addr, uc->uc_addr)) {
461                        *replyp = ent->cache_reply;
462                        *replylenp = ent->cache_replylen;
463                        return(1);
464                }
465        }
466        /*
467         * Failed to find entry
468         * Remember a few things so we can do a set later
469         */
470        uc->uc_proc = msg->rm_call.cb_proc;
471        uc->uc_vers = msg->rm_call.cb_vers;
472        uc->uc_prog = msg->rm_call.cb_prog;
473        uc->uc_addr = xprt->xp_raddr;
474        return(0);
475}
476
Note: See TracBrowser for help on using the repository browser.