getrpcport.c revision 1.16.66.1 1 1.16.66.1 riz /* $NetBSD: getrpcport.c,v 1.16.66.1 2013/03/14 22:03:15 riz Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.16.66.1 riz * Copyright (c) 2010, Oracle America, Inc.
5 1.16.66.1 riz *
6 1.16.66.1 riz * Redistribution and use in source and binary forms, with or without
7 1.16.66.1 riz * modification, are permitted provided that the following conditions are
8 1.16.66.1 riz * met:
9 1.16.66.1 riz *
10 1.16.66.1 riz * * Redistributions of source code must retain the above copyright
11 1.16.66.1 riz * notice, this list of conditions and the following disclaimer.
12 1.16.66.1 riz * * Redistributions in binary form must reproduce the above
13 1.16.66.1 riz * copyright notice, this list of conditions and the following
14 1.16.66.1 riz * disclaimer in the documentation and/or other materials
15 1.16.66.1 riz * provided with the distribution.
16 1.16.66.1 riz * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.16.66.1 riz * contributors may be used to endorse or promote products derived
18 1.16.66.1 riz * from this software without specific prior written permission.
19 1.16.66.1 riz *
20 1.16.66.1 riz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.16.66.1 riz * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.16.66.1 riz * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.16.66.1 riz * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.16.66.1 riz * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.16.66.1 riz * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.16.66.1 riz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.16.66.1 riz * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.16.66.1 riz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.16.66.1 riz * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.16.66.1 riz * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.16.66.1 riz * 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.66.1 riz __RCSID("$NetBSD: getrpcport.c,v 1.16.66.1 2013/03/14 22:03:15 riz 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.1 cgd getrpcport(host, prognum, versnum, proto)
67 1.11 lukem char *host;
68 1.11 lukem int prognum, versnum, proto;
69 1.1 cgd {
70 1.1 cgd struct sockaddr_in addr;
71 1.1 cgd struct hostent *hp;
72 1.14 lukem
73 1.14 lukem _DIAGASSERT(host != NULL);
74 1.1 cgd
75 1.1 cgd if ((hp = gethostbyname(host)) == NULL)
76 1.1 cgd return (0);
77 1.4 jtc memset(&addr, 0, sizeof(addr));
78 1.5 mycroft addr.sin_len = sizeof(struct sockaddr_in);
79 1.1 cgd addr.sin_family = AF_INET;
80 1.1 cgd addr.sin_port = 0;
81 1.13 christos if (hp->h_length > addr.sin_len)
82 1.13 christos hp->h_length = addr.sin_len;
83 1.13 christos memcpy(&addr.sin_addr.s_addr, hp->h_addr, (size_t)hp->h_length);
84 1.13 christos /* Inconsistent interfaces need casts! :-( */
85 1.13 christos return (pmap_getport(&addr, (u_long)prognum, (u_long)versnum,
86 1.13 christos (u_int)proto));
87 1.1 cgd }
88