getrpcport.c revision 1.16.64.2 1 1.16.64.2 yamt /* $NetBSD: getrpcport.c,v 1.16.64.2 2014/05/22 11:36:53 yamt Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.16.64.2 yamt * Copyright (c) 2010, Oracle America, Inc.
5 1.16.64.2 yamt *
6 1.16.64.2 yamt * Redistribution and use in source and binary forms, with or without
7 1.16.64.2 yamt * modification, are permitted provided that the following conditions are
8 1.16.64.2 yamt * met:
9 1.16.64.2 yamt *
10 1.16.64.2 yamt * * Redistributions of source code must retain the above copyright
11 1.16.64.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.16.64.2 yamt * * Redistributions in binary form must reproduce the above
13 1.16.64.2 yamt * copyright notice, this list of conditions and the following
14 1.16.64.2 yamt * disclaimer in the documentation and/or other materials
15 1.16.64.2 yamt * provided with the distribution.
16 1.16.64.2 yamt * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.16.64.2 yamt * contributors may be used to endorse or promote products derived
18 1.16.64.2 yamt * from this software without specific prior written permission.
19 1.16.64.2 yamt *
20 1.16.64.2 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.16.64.2 yamt * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.16.64.2 yamt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.16.64.2 yamt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.16.64.2 yamt * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.16.64.2 yamt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.16.64.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.16.64.2 yamt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.16.64.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.16.64.2 yamt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.16.64.2 yamt * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.16.64.2 yamt * 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.16.64.2 yamt __RCSID("$NetBSD: getrpcport.c,v 1.16.64.2 2014/05/22 11:36:53 yamt 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.16.64.1 yamt 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