OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [rpc/] [pmap_rmt.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* @(#)pmap_rmt.c       2.2 88/08/01 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 0
31
static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
32
#endif
33
 
34
/*
35
 * pmap_rmt.c
36
 * Client interface to pmap rpc service.
37
 * remote call and broadcast service
38
 *
39
 * Copyright (C) 1984, Sun Microsystems, Inc.
40
 */
41
 
42
#define __FORCE_GLIBC
43
#include <features.h>
44
 
45
#include <unistd.h>
46
#include <string.h>
47
#include <rpc/rpc.h>
48
#include <rpc/pmap_prot.h>
49
#include <rpc/pmap_clnt.h>
50
#include <rpc/pmap_rmt.h>
51
#include <sys/poll.h>
52
#include <sys/socket.h>
53
#include <stdio.h>
54
#include <errno.h>
55
#undef   _POSIX_SOURCE          /* Ultrix <sys/param.h> needs --roland@gnu */
56
#include <sys/param.h>          /* Ultrix needs before net/if --roland@gnu */
57
#include <net/if.h>
58
#include <sys/ioctl.h>
59
#include <arpa/inet.h>
60
#define MAX_BROADCAST_SIZE 1400
61
 
62
extern u_long _create_xid (void);
63
 
64
static const struct timeval timeout = {3, 0};
65
 
66
/*
67
 * pmapper remote-call-service interface.
68
 * This routine is used to call the pmapper remote call service
69
 * which will look up a service program in the port maps, and then
70
 * remotely call that routine with the given parameters.  This allows
71
 * programs to do a lookup and call in one step.
72
 */
73
enum clnt_stat
74
pmap_rmtcall (addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
75
     struct sockaddr_in *addr;
76
     u_long prog, vers, proc;
77
     xdrproc_t xdrargs, xdrres;
78
     caddr_t argsp, resp;
79
     struct timeval tout;
80
     u_long *port_ptr;
81
{
82
  int socket = -1;
83
  CLIENT *client;
84
  struct rmtcallargs a;
85
  struct rmtcallres r;
86
  enum clnt_stat stat;
87
 
88
  addr->sin_port = htons (PMAPPORT);
89
  client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &socket);
90
  if (client != (CLIENT *) NULL)
91
    {
92
      a.prog = prog;
93
      a.vers = vers;
94
      a.proc = proc;
95
      a.args_ptr = argsp;
96
      a.xdr_args = xdrargs;
97
      r.port_ptr = port_ptr;
98
      r.results_ptr = resp;
99
      r.xdr_results = xdrres;
100
      stat = CLNT_CALL (client, PMAPPROC_CALLIT, (xdrproc_t)xdr_rmtcall_args,
101
                        (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
102
                        (caddr_t)&r, tout);
103
      CLNT_DESTROY (client);
104
    }
105
  else
106
    {
107
      stat = RPC_FAILED;
108
    }
109
  /* (void)__close(socket); CLNT_DESTROY already closed it */
110
  addr->sin_port = 0;
111
  return stat;
112
}
113
 
114
 
115
/*
116
 * XDR remote call arguments
117
 * written for XDR_ENCODE direction only
118
 */
119
bool_t
120
xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
121
{
122
  u_int lenposition, argposition, position;
123
 
124
  if (xdr_u_long (xdrs, &(cap->prog)) &&
125
      xdr_u_long (xdrs, &(cap->vers)) &&
126
      xdr_u_long (xdrs, &(cap->proc)))
127
    {
128
      lenposition = XDR_GETPOS (xdrs);
129
      if (!xdr_u_long (xdrs, &(cap->arglen)))
130
        return FALSE;
131
      argposition = XDR_GETPOS (xdrs);
132
      if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
133
        return FALSE;
134
      position = XDR_GETPOS (xdrs);
135
      cap->arglen = (u_long) position - (u_long) argposition;
136
      XDR_SETPOS (xdrs, lenposition);
137
      if (!xdr_u_long (xdrs, &(cap->arglen)))
138
        return FALSE;
139
      XDR_SETPOS (xdrs, position);
140
      return TRUE;
141
    }
142
  return FALSE;
143
}
144
 
145
/*
146
 * XDR remote call results
147
 * written for XDR_DECODE direction only
148
 */
149
bool_t
150
xdr_rmtcallres (xdrs, crp)
151
     XDR *xdrs;
152
     struct rmtcallres *crp;
153
{
154
  caddr_t port_ptr;
155
 
156
  port_ptr = (caddr_t) crp->port_ptr;
157
  if (xdr_reference (xdrs, &port_ptr, sizeof (u_long), (xdrproc_t) xdr_u_long)
158
      && xdr_u_long (xdrs, &crp->resultslen))
159
    {
160
      crp->port_ptr = (u_long *) port_ptr;
161
      return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
162
    }
163
  return FALSE;
164
}
165
 
166
 
167
/*
168
 * The following is kludged-up support for simple rpc broadcasts.
169
 * Someday a large, complicated system will replace these trivial
170
 * routines which only support udp/ip .
171
 */
172
 
173
static int
174
internal_function
175
getbroadcastnets (struct in_addr *addrs, int sock, char *buf)
176
  /* int sock:  any valid socket will do */
177
  /* char *buf: why allocate more when we can use existing... */
178
{
179
  struct ifconf ifc;
180
  struct ifreq ifreq, *ifr;
181
  struct sockaddr_in *sin;
182
  int n, i;
183
 
184
  ifc.ifc_len = UDPMSGSIZE;
185
  ifc.ifc_buf = buf;
186
  if (ioctl (sock, SIOCGIFCONF, (char *) &ifc) < 0)
187
    {
188
      perror (_("broadcast: ioctl (get interface configuration)"));
189
      return (0);
190
    }
191
  ifr = ifc.ifc_req;
192
  for (i = 0, n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
193
    {
194
      ifreq = *ifr;
195
      if (ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
196
        {
197
          perror (_("broadcast: ioctl (get interface flags)"));
198
          continue;
199
        }
200
      if ((ifreq.ifr_flags & IFF_BROADCAST) &&
201
          (ifreq.ifr_flags & IFF_UP) &&
202
          ifr->ifr_addr.sa_family == AF_INET)
203
        {
204
          sin = (struct sockaddr_in *) &ifr->ifr_addr;
205
#ifdef SIOCGIFBRDADDR           /* 4.3BSD */
206
          if (ioctl (sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0)
207
            {
208
              addrs[i++] = inet_makeaddr (inet_netof
209
              /* Changed to pass struct instead of s_addr member
210
                 by roland@gnu.  */
211
                                          (sin->sin_addr), INADDR_ANY);
212
            }
213
          else
214
            {
215
              addrs[i++] = ((struct sockaddr_in *)
216
                            &ifreq.ifr_addr)->sin_addr;
217
            }
218
#else /* 4.2 BSD */
219
          addrs[i++] = inet_makeaddr (inet_netof
220
                                      (sin->sin_addr.s_addr), INADDR_ANY);
221
#endif
222
        }
223
    }
224
  return i;
225
}
226
 
227
 
228
enum clnt_stat
229
clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
230
     u_long prog;               /* program number */
231
     u_long vers;               /* version number */
232
     u_long proc;               /* procedure number */
233
     xdrproc_t xargs;           /* xdr routine for args */
234
     caddr_t argsp;             /* pointer to args */
235
     xdrproc_t xresults;        /* xdr routine for results */
236
     caddr_t resultsp;          /* pointer to results */
237
     resultproc_t eachresult;   /* call with each result obtained */
238
{
239
  enum clnt_stat stat = RPC_FAILED;
240
  AUTH *unix_auth = authunix_create_default ();
241
  XDR xdr_stream;
242
  XDR *xdrs = &xdr_stream;
243
  struct timeval t;
244
  int outlen, inlen, nets;
245
  socklen_t fromlen;
246
  int sock;
247
  int on = 1;
248
  struct pollfd fd;
249
  int milliseconds;
250
  int i;
251
  bool_t done = FALSE;
252
  u_long xid;
253
  u_long port;
254
  struct in_addr addrs[20];
255
  struct sockaddr_in baddr, raddr;      /* broadcast and response addresses */
256
  struct rmtcallargs a;
257
  struct rmtcallres r;
258
  struct rpc_msg msg;
259
  char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
260
 
261
  /*
262
   * initialization: create a socket, a broadcast address, and
263
   * preserialize the arguments into a send buffer.
264
   */
265
  if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
266
    {
267
      perror (_("Cannot create socket for broadcast rpc"));
268
      stat = RPC_CANTSEND;
269
      goto done_broad;
270
    }
271
#ifdef SO_BROADCAST
272
  if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
273
    {
274
      perror (_("Cannot set socket option SO_BROADCAST"));
275
      stat = RPC_CANTSEND;
276
      goto done_broad;
277
    }
278
#endif /* def SO_BROADCAST */
279
  fd.fd = sock;
280
  fd.events = POLLIN;
281
  nets = getbroadcastnets (addrs, sock, inbuf);
282
  bzero ((char *) &baddr, sizeof (baddr));
283
  baddr.sin_family = AF_INET;
284
  baddr.sin_port = htons (PMAPPORT);
285
  baddr.sin_addr.s_addr = htonl (INADDR_ANY);
286
/*      baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
287
  msg.rm_xid = xid = _create_xid ();
288
  t.tv_usec = 0;
289
  msg.rm_direction = CALL;
290
  msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
291
  msg.rm_call.cb_prog = PMAPPROG;
292
  msg.rm_call.cb_vers = PMAPVERS;
293
  msg.rm_call.cb_proc = PMAPPROC_CALLIT;
294
  msg.rm_call.cb_cred = unix_auth->ah_cred;
295
  msg.rm_call.cb_verf = unix_auth->ah_verf;
296
  a.prog = prog;
297
  a.vers = vers;
298
  a.proc = proc;
299
  a.xdr_args = xargs;
300
  a.args_ptr = argsp;
301
  r.port_ptr = &port;
302
  r.xdr_results = xresults;
303
  r.results_ptr = resultsp;
304
  xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
305
  if ((!xdr_callmsg (xdrs, &msg)) || (!xdr_rmtcall_args (xdrs, &a)))
306
    {
307
      stat = RPC_CANTENCODEARGS;
308
      goto done_broad;
309
    }
310
  outlen = (int) xdr_getpos (xdrs);
311
  xdr_destroy (xdrs);
312
  /*
313
   * Basic loop: broadcast a packet and wait a while for response(s).
314
   * The response timeout grows larger per iteration.
315
   */
316
  for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
317
    {
318
      for (i = 0; i < nets; i++)
319
        {
320
          baddr.sin_addr = addrs[i];
321
          if (sendto (sock, outbuf, outlen, 0,
322
                      (struct sockaddr *) &baddr,
323
                      sizeof (struct sockaddr)) != outlen)
324
            {
325
              perror (_("Cannot send broadcast packet"));
326
              stat = RPC_CANTSEND;
327
              goto done_broad;
328
            }
329
        }
330
      if (eachresult == NULL)
331
        {
332
          stat = RPC_SUCCESS;
333
          goto done_broad;
334
        }
335
    recv_again:
336
      msg.acpted_rply.ar_verf = _null_auth;
337
      msg.acpted_rply.ar_results.where = (caddr_t) & r;
338
      msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
339
      milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
340
      switch (poll(&fd, 1, milliseconds))
341
        {
342
 
343
        case 0:          /* timed out */
344
          stat = RPC_TIMEDOUT;
345
          continue;
346
 
347
        case -1:                /* some kind of error */
348
          if (errno == EINTR)
349
            goto recv_again;
350
          perror (_("Broadcast poll problem"));
351
          stat = RPC_CANTRECV;
352
          goto done_broad;
353
 
354
        }                       /* end of poll results switch */
355
    try_again:
356
      fromlen = sizeof (struct sockaddr);
357
      inlen = recvfrom (sock, inbuf, UDPMSGSIZE, 0,
358
                        (struct sockaddr *) &raddr, &fromlen);
359
      if (inlen < 0)
360
        {
361
          if (errno == EINTR)
362
            goto try_again;
363
          perror (_("Cannot receive reply to broadcast"));
364
          stat = RPC_CANTRECV;
365
          goto done_broad;
366
        }
367
      if ((size_t) inlen < sizeof (u_long))
368
        goto recv_again;
369
      /*
370
       * see if reply transaction id matches sent id.
371
       * If so, decode the results.
372
       */
373
      xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
374
      if (xdr_replymsg (xdrs, &msg))
375
        {
376
          if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
377
              (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
378
              (msg.acpted_rply.ar_stat == SUCCESS))
379
            {
380
              raddr.sin_port = htons ((u_short) port);
381
              done = (*eachresult) (resultsp, &raddr);
382
            }
383
          /* otherwise, we just ignore the errors ... */
384
        }
385
      else
386
        {
387
#ifdef notdef
388
          /* some kind of deserialization problem ... */
389
          if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
390
            fprintf (stderr, "Broadcast deserialization problem");
391
          /* otherwise, just random garbage */
392
#endif
393
        }
394
      xdrs->x_op = XDR_FREE;
395
      msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
396
      (void) xdr_replymsg (xdrs, &msg);
397
      (void) (*xresults) (xdrs, resultsp);
398
      xdr_destroy (xdrs);
399
      if (done)
400
        {
401
          stat = RPC_SUCCESS;
402
          goto done_broad;
403
        }
404
      else
405
        {
406
          goto recv_again;
407
        }
408
    }
409
done_broad:
410
  (void) close (sock);
411
  AUTH_DESTROY (unix_auth);
412
  return stat;
413
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.