getrpcport.c revision 1.17.2.1 1 1.17.2.1 tls /* $NetBSD: getrpcport.c,v 1.17.2.1 2013/06/23 06:21:05 tls Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.17.2.1 tls * Copyright (c) 2010, Oracle America, Inc.
5 1.17.2.1 tls *
6 1.17.2.1 tls * Redistribution and use in source and binary forms, with or without
7 1.17.2.1 tls * modification, are permitted provided that the following conditions are
8 1.17.2.1 tls * met:
9 1.17.2.1 tls *
10 1.17.2.1 tls * * Redistributions of source code must retain the above copyright
11 1.17.2.1 tls * notice, this list of conditions and the following disclaimer.
12 1.17.2.1 tls * * Redistributions in binary form must reproduce the above
13 1.17.2.1 tls * copyright notice, this list of conditions and the following
14 1.17.2.1 tls * disclaimer in the documentation and/or other materials
15 1.17.2.1 tls * provided with the distribution.
16 1.17.2.1 tls * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.17.2.1 tls * contributors may be used to endorse or promote products derived
18 1.17.2.1 tls * from this software without specific prior written permission.
19 1.17.2.1 tls *
20 1.17.2.1 tls * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.17.2.1 tls * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.17.2.1 tls * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.17.2.1 tls * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.17.2.1 tls * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.17.2.1 tls * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.17.2.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.17.2.1 tls * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.17.2.1 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.17.2.1 tls * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.17.2.1 tls * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.17.2.1 tls * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.8 christos #include <sys/cdefs.h>
35 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
36 1.8 christos #if 0
37 1.8 christos static char *sccsid = "@(#)getrpcport.c 1.3 87/08/11 SMI";
38 1.8 christos static char *sccsid = "@(#)getrpcport.c 2.1 88/07/29 4.0 RPCSRC";
39 1.8 christos #else
40 1.17.2.1 tls __RCSID("$NetBSD: getrpcport.c,v 1.17.2.1 2013/06/23 06:21:05 tls Exp $");
41 1.8 christos #endif
42 1.1 cgd #endif
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Copyright (c) 1985 by Sun Microsystems, Inc.
46 1.1 cgd */
47 1.1 cgd
48 1.9 jtc #include "namespace.h"
49 1.12 lukem
50 1.12 lukem #include <sys/types.h>
51 1.12 lukem #include <sys/socket.h>
52 1.12 lukem
53 1.14 lukem #include <assert.h>
54 1.12 lukem #include <netdb.h>
55 1.1 cgd #include <stdio.h>
56 1.4 jtc #include <string.h>
57 1.12 lukem
58 1.1 cgd #include <rpc/rpc.h>
59 1.7 cgd #include <rpc/pmap_clnt.h>
60 1.9 jtc
61 1.9 jtc #ifdef __weak_alias
62 1.16 mycroft __weak_alias(getrpcport,_getrpcport)
63 1.9 jtc #endif
64 1.1 cgd
65 1.6 jtc int
66 1.17 matt getrpcport(char *host, int prognum, int versnum, int proto)
67 1.1 cgd {
68 1.1 cgd struct sockaddr_in addr;
69 1.1 cgd struct hostent *hp;
70 1.14 lukem
71 1.14 lukem _DIAGASSERT(host != NULL);
72 1.1 cgd
73 1.1 cgd if ((hp = gethostbyname(host)) == NULL)
74 1.1 cgd return (0);
75 1.4 jtc memset(&addr, 0, sizeof(addr));
76 1.5 mycroft addr.sin_len = sizeof(struct sockaddr_in);
77 1.1 cgd addr.sin_family = AF_INET;
78 1.1 cgd addr.sin_port = 0;
79 1.13 christos if (hp->h_length > addr.sin_len)
80 1.13 christos hp->h_length = addr.sin_len;
81 1.13 christos memcpy(&addr.sin_addr.s_addr, hp->h_addr, (size_t)hp->h_length);
82 1.13 christos /* Inconsistent interfaces need casts! :-( */
83 1.13 christos return (pmap_getport(&addr, (u_long)prognum, (u_long)versnum,
84 1.13 christos (u_int)proto));
85 1.1 cgd }
86