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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* @(#)svc_tcp.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[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32
#endif
33
 
34
/*
35
 * svc_tcp.c, Server side for TCP/IP based RPC.
36
 *
37
 * Copyright (C) 1984, Sun Microsystems, Inc.
38
 *
39
 * Actually implements two flavors of transporter -
40
 * a tcp rendezvouser (a listener and connection establisher)
41
 * and a record/tcp stream.
42
 */
43
 
44
#define __FORCE_GLIBC
45
#define _GNU_SOURCE
46
#include <features.h>
47
 
48
#include <stdio.h>
49
#include <unistd.h>
50
#include <string.h>
51
#include <rpc/rpc.h>
52
#include <sys/socket.h>
53
#include <sys/poll.h>
54
#include <errno.h>
55
#include <stdlib.h>
56
 
57
#ifdef USE_IN_LIBIO
58
# include <wchar.h>
59
# include <libio/iolibio.h>
60
# define fputs(s, f) _IO_fputs (s, f)
61
#endif
62
 
63
/*
64
 * Ops vector for TCP/IP based rpc service handle
65
 */
66
static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
67
static enum xprt_stat svctcp_stat (SVCXPRT *);
68
static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
69
static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
70
static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
71
static void svctcp_destroy (SVCXPRT *);
72
 
73
static const struct xp_ops svctcp_op =
74
{
75
  svctcp_recv,
76
  svctcp_stat,
77
  svctcp_getargs,
78
  svctcp_reply,
79
  svctcp_freeargs,
80
  svctcp_destroy
81
};
82
 
83
/*
84
 * Ops vector for TCP/IP rendezvous handler
85
 */
86
static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
87
static enum xprt_stat rendezvous_stat (SVCXPRT *);
88
static void svctcp_rendezvous_abort (void);
89
 
90
/* This function makes sure abort() relocation goes through PLT
91
   and thus can be lazy bound.  */
92
static void
93
svctcp_rendezvous_abort (void)
94
{
95
  abort ();
96
};
97
 
98
static const struct xp_ops svctcp_rendezvous_op =
99
{
100
  rendezvous_request,
101
  rendezvous_stat,
102
  (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
103
  (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
104
  (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
105
  svctcp_destroy
106
};
107
 
108
static int readtcp (char*, char *, int);
109
static int writetcp (char *, char *, int);
110
static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
111
 
112
struct tcp_rendezvous
113
  {                             /* kept in xprt->xp_p1 */
114
    u_int sendsize;
115
    u_int recvsize;
116
  };
117
 
118
struct tcp_conn
119
  {                             /* kept in xprt->xp_p1 */
120
    enum xprt_stat strm_stat;
121
    u_long x_id;
122
    XDR xdrs;
123
    char verf_body[MAX_AUTH_BYTES];
124
  };
125
 
126
/*
127
 * Usage:
128
 *      xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
129
 *
130
 * Creates, registers, and returns a (rpc) tcp based transporter.
131
 * Once *xprt is initialized, it is registered as a transporter
132
 * see (svc.h, xprt_register).  This routine returns
133
 * a NULL if a problem occurred.
134
 *
135
 * If sock<0 then a socket is created, else sock is used.
136
 * If the socket, sock is not bound to a port then svctcp_create
137
 * binds it to an arbitrary port.  The routine then starts a tcp
138
 * listener on the socket's associated port.  In any (successful) case,
139
 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
140
 * associated port number.
141
 *
142
 * Since tcp streams do buffered io similar to stdio, the caller can specify
143
 * how big the send and receive buffers are via the second and third parms;
144
 * 0 => use the system default.
145
 */
146
SVCXPRT *
147
svctcp_create (int sock, u_int sendsize, u_int recvsize)
148
{
149
  bool_t madesock = FALSE;
150
  SVCXPRT *xprt;
151
  struct tcp_rendezvous *r;
152
  struct sockaddr_in addr;
153
  socklen_t len = sizeof (struct sockaddr_in);
154
 
155
  if (sock == RPC_ANYSOCK)
156
    {
157
      if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
158
        {
159
          perror (_("svc_tcp.c - tcp socket creation problem"));
160
          return (SVCXPRT *) NULL;
161
        }
162
      madesock = TRUE;
163
    }
164
  bzero ((char *) &addr, sizeof (addr));
165
  addr.sin_family = AF_INET;
166
  if (bindresvport (sock, &addr))
167
    {
168
      addr.sin_port = 0;
169
      (void) bind (sock, (struct sockaddr *) &addr, len);
170
    }
171
  if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
172
      (listen (sock, 2) != 0))
173
    {
174
      perror (_("svc_tcp.c - cannot getsockname or listen"));
175
      if (madesock)
176
        (void) close (sock);
177
      return (SVCXPRT *) NULL;
178
    }
179
  r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
180
  xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
181
  if (r == NULL || xprt == NULL)
182
    {
183
#ifdef USE_IN_LIBIO
184
      if (_IO_fwide (stderr, 0) > 0)
185
        (void) __fwprintf (stderr, L"%s", _("svctcp_create: out of memory\n"));
186
      else
187
#endif
188
        (void) fputs (_("svctcp_create: out of memory\n"), stderr);
189
      mem_free (r, sizeof (*r));
190
      mem_free (xprt, sizeof (SVCXPRT));
191
      return NULL;
192
    }
193
  r->sendsize = sendsize;
194
  r->recvsize = recvsize;
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 (int fd, u_int sendsize, u_int recvsize)
211
{
212
  return makefd_xprt (fd, sendsize, recvsize);
213
}
214
 
215
static SVCXPRT *
216
internal_function
217
makefd_xprt (int fd, u_int sendsize, u_int recvsize)
218
{
219
  SVCXPRT *xprt;
220
  struct tcp_conn *cd;
221
 
222
  xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
223
  cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
224
  if (xprt == (SVCXPRT *) NULL || cd == NULL)
225
    {
226
#ifdef USE_IN_LIBIO
227
      if (_IO_fwide (stderr, 0) > 0)
228
        (void) __fwprintf (stderr, L"%s",
229
                           _("svc_tcp: makefd_xprt: out of memory\n"));
230
      else
231
#endif
232
        (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
233
      mem_free (xprt, sizeof (SVCXPRT));
234
      mem_free (cd, sizeof (struct tcp_conn));
235
      return NULL;
236
    }
237
  cd->strm_stat = XPRT_IDLE;
238
  xdrrec_create (&(cd->xdrs), sendsize, recvsize,
239
                 (caddr_t) xprt, readtcp, writetcp);
240
  xprt->xp_p2 = NULL;
241
  xprt->xp_p1 = (caddr_t) cd;
242
  xprt->xp_verf.oa_base = cd->verf_body;
243
  xprt->xp_addrlen = 0;
244
  xprt->xp_ops = &svctcp_op;    /* truly deals with calls */
245
  xprt->xp_port = 0;             /* this is a connection, not a rendezvouser */
246
  xprt->xp_sock = fd;
247
  xprt_register (xprt);
248
  return xprt;
249
}
250
 
251
static bool_t
252
rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
253
{
254
  int sock;
255
  struct tcp_rendezvous *r;
256
  struct sockaddr_in addr;
257
  socklen_t len;
258
 
259
  r = (struct tcp_rendezvous *) xprt->xp_p1;
260
again:
261
  len = sizeof (struct sockaddr_in);
262
  if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
263
    {
264
      if (errno == EINTR)
265
        goto again;
266
      return FALSE;
267
    }
268
  /*
269
   * make a new transporter (re-uses xprt)
270
   */
271
  xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
272
  memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
273
  xprt->xp_addrlen = len;
274
  return FALSE;         /* there is never an rpc msg to be processed */
275
}
276
 
277
static enum xprt_stat
278
rendezvous_stat (SVCXPRT *xprt)
279
{
280
  return XPRT_IDLE;
281
}
282
 
283
static void
284
svctcp_destroy (SVCXPRT *xprt)
285
{
286
  struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
287
 
288
  xprt_unregister (xprt);
289
  (void) close (xprt->xp_sock);
290
  if (xprt->xp_port != 0)
291
    {
292
      /* a rendezvouser socket */
293
      xprt->xp_port = 0;
294
    }
295
  else
296
    {
297
      /* an actual connection socket */
298
      XDR_DESTROY (&(cd->xdrs));
299
    }
300
  mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
301
  mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
302
}
303
 
304
 
305
/*
306
 * reads data from the tcp connection.
307
 * any error is fatal and the connection is closed.
308
 * (And a read of zero bytes is a half closed stream => error.)
309
 */
310
static int
311
readtcp (char *xprtptr, char *buf, int len)
312
{
313
  SVCXPRT *xprt = (SVCXPRT *)xprtptr;
314
  int sock = xprt->xp_sock;
315
  int milliseconds = 35 * 1000;
316
  struct pollfd pollfd;
317
 
318
  do
319
    {
320
      pollfd.fd = sock;
321
      pollfd.events = POLLIN;
322
      switch (poll (&pollfd, 1, milliseconds))
323
        {
324
        case -1:
325
          if (errno == EINTR)
326
            continue;
327
          /*FALLTHROUGH*/
328
        case 0:
329
          goto fatal_err;
330
        default:
331
          if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
332
              || (pollfd.revents & POLLNVAL))
333
            goto fatal_err;
334
          break;
335
        }
336
    }
337
  while ((pollfd.revents & POLLIN) == 0);
338
 
339
  if ((len = read (sock, buf, len)) > 0)
340
    return len;
341
 
342
 fatal_err:
343
  ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
344
  return -1;
345
}
346
 
347
/*
348
 * writes data to the tcp connection.
349
 * Any error is fatal and the connection is closed.
350
 */
351
static int
352
writetcp (char *xprtptr, char * buf, int len)
353
{
354
  SVCXPRT *xprt = (SVCXPRT *)xprtptr;
355
  int i, cnt;
356
 
357
  for (cnt = len; cnt > 0; cnt -= i, buf += i)
358
    {
359
      if ((i = write (xprt->xp_sock, buf, cnt)) < 0)
360
        {
361
          ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
362
          return -1;
363
        }
364
    }
365
  return len;
366
}
367
 
368
static enum xprt_stat
369
svctcp_stat (SVCXPRT *xprt)
370
{
371
  struct tcp_conn *cd =
372
  (struct tcp_conn *) (xprt->xp_p1);
373
 
374
  if (cd->strm_stat == XPRT_DIED)
375
    return XPRT_DIED;
376
  if (!xdrrec_eof (&(cd->xdrs)))
377
    return XPRT_MOREREQS;
378
  return XPRT_IDLE;
379
}
380
 
381
static bool_t
382
svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
383
{
384
  struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
385
  XDR *xdrs = &(cd->xdrs);
386
 
387
  xdrs->x_op = XDR_DECODE;
388
  (void) xdrrec_skiprecord (xdrs);
389
  if (xdr_callmsg (xdrs, msg))
390
    {
391
      cd->x_id = msg->rm_xid;
392
      return TRUE;
393
    }
394
  cd->strm_stat = XPRT_DIED;    /* XXXX */
395
  return FALSE;
396
}
397
 
398
static bool_t
399
svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
400
{
401
  return ((*xdr_args) (&(((struct tcp_conn *)
402
                          (xprt->xp_p1))->xdrs), args_ptr));
403
}
404
 
405
static bool_t
406
svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
407
{
408
  XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
409
 
410
  xdrs->x_op = XDR_FREE;
411
  return ((*xdr_args) (xdrs, args_ptr));
412
}
413
 
414
static bool_t
415
svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
416
{
417
  struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
418
  XDR *xdrs = &(cd->xdrs);
419
  bool_t stat;
420
 
421
  xdrs->x_op = XDR_ENCODE;
422
  msg->rm_xid = cd->x_id;
423
  stat = xdr_replymsg (xdrs, msg);
424
  (void) xdrrec_endofrecord (xdrs, TRUE);
425
  return stat;
426
}

powered by: WebSVN 2.1.0

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