source: rtems-libbsd/freebsd/lib/libc/rpc/rpc_soc.c @ 60b1d40

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 60b1d40 was 60b1d40, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 08:23:57

RPC(3): Import from FreeBSD

  • Property mode set to 100644
File size: 13.9 KB
RevLine 
[60b1d40]1#include <machine/rtems-bsd-user-space.h>
2
3/*      $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $     */
4
5/*-
6 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 *   this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 *   this list of conditions and the following disclaimer in the documentation
15 *   and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 *   contributors may be used to endorse or promote products derived
18 *   from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/* #ident       "@(#)rpc_soc.c  1.17    94/04/24 SMI" */
34
35/*
36 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37 * In addition, portions of such source code were derived from Berkeley
38 * 4.3 BSD under license from the Regents of the University of
39 * California.
40 */
41
42#if defined(LIBC_SCCS) && !defined(lint)
43static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
44#endif
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD$");
47
48#ifdef PORTMAP
49/*
50 * rpc_soc.c
51 *
52 * The backward compatibility routines for the earlier implementation
53 * of RPC, where the only transports supported were tcp/ip and udp/ip.
54 * Based on berkeley socket abstraction, now implemented on the top
55 * of TLI/Streams
56 */
57
58#include "namespace.h"
59#include "reentrant.h"
60#include <sys/types.h>
61#include <sys/socket.h>
62#include <stdio.h>
63#include <rpc/rpc.h>
64#include <rpc/pmap_clnt.h>
65#include <rpc/pmap_prot.h>
66#include <rpc/nettype.h>
67#include <syslog.h>
68#include <netinet/in.h>
69#include <netdb.h>
70#include <errno.h>
71#include <syslog.h>
72#include <stdlib.h>
73#include <string.h>
74#include <unistd.h>
75#include "un-namespace.h"
76
77#include "rpc_com.h"
78#include "mt_misc.h"
79
80static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
81    int *, u_int, u_int, char *);
82static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
83static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
84
85/* XXX */
86#define IN4_LOCALHOST_STRING    "127.0.0.1"
87#define IN6_LOCALHOST_STRING    "::1"
88
89/*
90 * A common clnt create routine
91 */
92static CLIENT *
93clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
94        struct sockaddr_in *raddr;
95        rpcprog_t prog;
96        rpcvers_t vers;
97        int *sockp;
98        u_int sendsz;
99        u_int recvsz;
100        char *tp;
101{
102        CLIENT *cl;
103        int madefd = FALSE;
104        int fd = *sockp;
105        struct netconfig *nconf;
106        struct netbuf bindaddr;
107
108        mutex_lock(&rpcsoc_lock);
109        if ((nconf = __rpc_getconfip(tp)) == NULL) {
110                rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
111                mutex_unlock(&rpcsoc_lock);
112                return (NULL);
113        }
114        if (fd == RPC_ANYSOCK) {
115                fd = __rpc_nconf2fd(nconf);
116                if (fd == -1)
117                        goto syserror;
118                madefd = TRUE;
119        }
120
121        if (raddr->sin_port == 0) {
122                u_int proto;
123                u_short sport;
124
125                mutex_unlock(&rpcsoc_lock);     /* pmap_getport is recursive */
126                proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
127                sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
128                    proto);
129                if (sport == 0) {
130                        goto err;
131                }
132                raddr->sin_port = htons(sport);
133                mutex_lock(&rpcsoc_lock);       /* pmap_getport is recursive */
134        }
135
136        /* Transform sockaddr_in to netbuf */
137        bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
138        bindaddr.buf = raddr;
139
140        bindresvport(fd, NULL);
141        cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
142                                sendsz, recvsz);
143        if (cl) {
144                if (madefd == TRUE) {
145                        /*
146                         * The fd should be closed while destroying the handle.
147                         */
148                        (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
149                        *sockp = fd;
150                }
151                (void) freenetconfigent(nconf);
152                mutex_unlock(&rpcsoc_lock);
153                return (cl);
154        }
155        goto err;
156
157syserror:
158        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
159        rpc_createerr.cf_error.re_errno = errno;
160
161err:    if (madefd == TRUE)
162                (void)_close(fd);
163        (void) freenetconfigent(nconf);
164        mutex_unlock(&rpcsoc_lock);
165        return (NULL);
166}
167
168CLIENT *
169clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
170        struct sockaddr_in *raddr;
171        u_long prog;
172        u_long vers;
173        struct timeval wait;
174        int *sockp;
175        u_int sendsz;
176        u_int recvsz;
177{
178        CLIENT *cl;
179
180        cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
181            sendsz, recvsz, "udp");
182        if (cl == NULL) {
183                return (NULL);
184        }
185        (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
186        return (cl);
187}
188
189CLIENT *
190clntudp_create(raddr, program, version, wait, sockp)
191        struct sockaddr_in *raddr;
192        u_long program;
193        u_long version;
194        struct timeval wait;
195        int *sockp;
196{
197
198        return clntudp_bufcreate(raddr, program, version, wait, sockp,
199                                        UDPMSGSIZE, UDPMSGSIZE);
200}
201
202CLIENT *
203clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
204        struct sockaddr_in *raddr;
205        u_long prog;
206        u_long vers;
207        int *sockp;
208        u_int sendsz;
209        u_int recvsz;
210{
211
212        return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
213            sendsz, recvsz, "tcp");
214}
215
216CLIENT *
217clntraw_create(prog, vers)
218        u_long prog;
219        u_long vers;
220{
221
222        return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
223}
224
225/*
226 * A common server create routine
227 */
228static SVCXPRT *
229svc_com_create(fd, sendsize, recvsize, netid)
230        int fd;
231        u_int sendsize;
232        u_int recvsize;
233        char *netid;
234{
235        struct netconfig *nconf;
236        SVCXPRT *svc;
237        int madefd = FALSE;
238        int port;
239        struct sockaddr_in sin;
240
241        if ((nconf = __rpc_getconfip(netid)) == NULL) {
242                (void) syslog(LOG_ERR, "Could not get %s transport", netid);
243                return (NULL);
244        }
245        if (fd == RPC_ANYSOCK) {
246                fd = __rpc_nconf2fd(nconf);
247                if (fd == -1) {
248                        (void) freenetconfigent(nconf);
249                        (void) syslog(LOG_ERR,
250                        "svc%s_create: could not open connection", netid);
251                        return (NULL);
252                }
253                madefd = TRUE;
254        }
255
256        memset(&sin, 0, sizeof sin);
257        sin.sin_family = AF_INET;
258        bindresvport(fd, &sin);
259        _listen(fd, SOMAXCONN);
260        svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
261        (void) freenetconfigent(nconf);
262        if (svc == NULL) {
263                if (madefd)
264                        (void)_close(fd);
265                return (NULL);
266        }
267        port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
268        svc->xp_port = ntohs(port);
269        return (svc);
270}
271
272SVCXPRT *
273svctcp_create(fd, sendsize, recvsize)
274        int fd;
275        u_int sendsize;
276        u_int recvsize;
277{
278
279        return svc_com_create(fd, sendsize, recvsize, "tcp");
280}
281
282SVCXPRT *
283svcudp_bufcreate(fd, sendsz, recvsz)
284        int fd;
285        u_int sendsz, recvsz;
286{
287
288        return svc_com_create(fd, sendsz, recvsz, "udp");
289}
290
291SVCXPRT *
292svcfd_create(fd, sendsize, recvsize)
293        int fd;
294        u_int sendsize;
295        u_int recvsize;
296{
297
298        return svc_fd_create(fd, sendsize, recvsize);
299}
300
301
302SVCXPRT *
303svcudp_create(fd)
304        int fd;
305{
306
307        return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
308}
309
310SVCXPRT *
311svcraw_create()
312{
313
314        return svc_raw_create();
315}
316
317int
318get_myaddress(addr)
319        struct sockaddr_in *addr;
320{
321
322        memset((void *) addr, 0, sizeof(*addr));
323        addr->sin_family = AF_INET;
324        addr->sin_port = htons(PMAPPORT);
325        addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
326        return (0);
327}
328
329/*
330 * For connectionless "udp" transport. Obsoleted by rpc_call().
331 */
332int
333callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
334        const char *host;
335        int prognum, versnum, procnum;
336        xdrproc_t inproc, outproc;
337        void *in, *out;
338{
339
340        return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
341            (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
342}
343
344/*
345 * For connectionless kind of transport. Obsoleted by rpc_reg()
346 */
347int
348registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
349        int prognum, versnum, procnum;
350        char *(*progname)(char [UDPMSGSIZE]);
351        xdrproc_t inproc, outproc;
352{
353
354        return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
355            (rpcproc_t)procnum, progname, inproc, outproc, "udp");
356}
357
358/*
359 * All the following clnt_broadcast stuff is convulated; it supports
360 * the earlier calling style of the callback function
361 */
362static thread_key_t     clnt_broadcast_key;
363static resultproc_t     clnt_broadcast_result_main;
364static once_t           clnt_broadcast_once = ONCE_INITIALIZER;
365
366static void
367clnt_broadcast_key_init(void)
368{
369
370        thr_keycreate(&clnt_broadcast_key, free);
371}
372
373/*
374 * Need to translate the netbuf address into sockaddr_in address.
375 * Dont care about netid here.
376 */
377/* ARGSUSED */
378static bool_t
379rpc_wrap_bcast(resultp, addr, nconf)
380        char *resultp;          /* results of the call */
381        struct netbuf *addr;    /* address of the guy who responded */
382        struct netconfig *nconf; /* Netconf of the transport */
383{
384        resultproc_t clnt_broadcast_result;
385
386        if (strcmp(nconf->nc_netid, "udp"))
387                return (FALSE);
388        if (thr_main())
389                clnt_broadcast_result = clnt_broadcast_result_main;
390        else
391                clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
392        return (*clnt_broadcast_result)(resultp,
393                                (struct sockaddr_in *)addr->buf);
394}
395
396/*
397 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
398 */
399enum clnt_stat
400clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
401        u_long          prog;           /* program number */
402        u_long          vers;           /* version number */
403        u_long          proc;           /* procedure number */
404        xdrproc_t       xargs;          /* xdr routine for args */
405        void           *argsp;          /* pointer to args */
406        xdrproc_t       xresults;       /* xdr routine for results */
407        void           *resultsp;       /* pointer to results */
408        resultproc_t    eachresult;     /* call with each result obtained */
409{
410
411        if (thr_main())
412                clnt_broadcast_result_main = eachresult;
413        else {
414                thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
415                thr_setspecific(clnt_broadcast_key, (void *) eachresult);
416        }
417        return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
418            (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
419            (resultproc_t) rpc_wrap_bcast, "udp");
420}
421
422/*
423 * Create the client des authentication object. Obsoleted by
424 * authdes_seccreate().
425 */
426AUTH *
427authdes_create(servername, window, syncaddr, ckey)
428        char *servername;               /* network name of server */
429        u_int window;                   /* time to live */
430        struct sockaddr *syncaddr;      /* optional hostaddr to sync with */
431        des_block *ckey;                /* optional conversation key to use */
432{
433        AUTH *dummy;
434        AUTH *nauth;
435        char hostname[NI_MAXHOST];
436
437        if (syncaddr) {
438                /*
439                 * Change addr to hostname, because that is the way
440                 * new interface takes it.
441                 */
442                if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
443                    sizeof hostname, NULL, 0, 0) != 0)
444                        goto fallback;
445
446                nauth = authdes_seccreate(servername, window, hostname, ckey);
447                return (nauth);
448        }
449fallback:
450        dummy = authdes_seccreate(servername, window, NULL, ckey);
451        return (dummy);
452}
453
454/*
455 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
456 */
457CLIENT *
458clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
459        struct sockaddr_un *raddr;
460        u_long prog;
461        u_long vers;
462        int *sockp;
463        u_int sendsz;
464        u_int recvsz;
465{
466        struct netbuf *svcaddr;
467        struct netconfig *nconf;
468        CLIENT *cl;
469        int len;
470
471        cl = NULL;
472        nconf = NULL;
473        svcaddr = NULL;
474        if ((raddr->sun_len == 0) ||
475           ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
476           ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
477                if (svcaddr != NULL)
478                        free(svcaddr);
479                rpc_createerr.cf_stat = RPC_SYSTEMERROR;
480                rpc_createerr.cf_error.re_errno = errno;
481                return(cl);
482        }
483        if (*sockp < 0) {
484                *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
485                len = raddr->sun_len = SUN_LEN(raddr);
486                if ((*sockp < 0) || (_connect(*sockp,
487                    (struct sockaddr *)raddr, len) < 0)) {
488                        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
489                        rpc_createerr.cf_error.re_errno = errno;
490                        if (*sockp != -1)
491                                (void)_close(*sockp);
492                        goto done;
493                }
494        }
495        svcaddr->buf = raddr;
496        svcaddr->len = raddr->sun_len;
497        svcaddr->maxlen = sizeof (struct sockaddr_un);
498        cl = clnt_vc_create(*sockp, svcaddr, prog,
499            vers, sendsz, recvsz);
500done:
501        free(svcaddr->buf);
502        free(svcaddr);
503        return(cl);
504}
505
506/*
507 * Creates, registers, and returns a (rpc) unix based transporter.
508 * Obsoleted by svc_vc_create().
509 */
510SVCXPRT *
511svcunix_create(sock, sendsize, recvsize, path)
512        int sock;
513        u_int sendsize;
514        u_int recvsize;
515        char *path;
516{
517        struct netconfig *nconf;
518        void *localhandle;
519        struct sockaddr_un sun;
520        struct sockaddr *sa;
521        struct t_bind taddr;
522        SVCXPRT *xprt;
523        int addrlen;
524
525        xprt = (SVCXPRT *)NULL;
526        localhandle = setnetconfig();
527        while ((nconf = getnetconfig(localhandle)) != NULL) {
528                if (nconf->nc_protofmly != NULL &&
529                    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
530                        break;
531        }
532        if (nconf == NULL)
533                return(xprt);
534
535        if ((sock = __rpc_nconf2fd(nconf)) < 0)
536                goto done;
537
538        memset(&sun, 0, sizeof sun);
539        sun.sun_family = AF_LOCAL;
540        if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
541            sizeof(sun.sun_path))
542                goto done;
543        sun.sun_len = SUN_LEN(&sun);
544        addrlen = sizeof (struct sockaddr_un);
545        sa = (struct sockaddr *)&sun;
546
547        if (_bind(sock, sa, addrlen) < 0)
548                goto done;
549
550        taddr.addr.len = taddr.addr.maxlen = addrlen;
551        taddr.addr.buf = malloc(addrlen);
552        if (taddr.addr.buf == NULL)
553                goto done;
554        memcpy(taddr.addr.buf, sa, addrlen);
555
556        if (nconf->nc_semantics != NC_TPI_CLTS) {
557                if (_listen(sock, SOMAXCONN) < 0) {
558                        free(taddr.addr.buf);
559                        goto done;
560                }
561        }
562
563        xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
564
565done:
566        endnetconfig(localhandle);
567        return(xprt);
568}
569
570/*
571 * Like svunix_create(), except the routine takes any *open* UNIX file
572 * descriptor as its first input. Obsoleted by svc_fd_create();
573 */
574SVCXPRT *
575svcunixfd_create(fd, sendsize, recvsize)
576        int fd;
577        u_int sendsize;
578        u_int recvsize;
579{
580        return (svc_fd_create(fd, sendsize, recvsize));
581}
582
583#endif /* PORTMAP */
Note: See TracBrowser for help on using the repository browser.