xref: /freebsd/lib/libc/rpc/getrpcport.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
18360efbdSAlfred Perlstein /*	$NetBSD: getrpcport.c,v 1.16 2000/01/22 22:19:18 mycroft Exp $	*/
28360efbdSAlfred Perlstein 
32e322d37SHiroki Sato /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
62e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato  * All rights reserved.
899064799SGarrett Wollman  *
92e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
132e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
162e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato  *   from this software without specific prior written permission.
1999064799SGarrett Wollman  *
202e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman  */
3299064799SGarrett Wollman 
3399064799SGarrett Wollman /*
3499064799SGarrett Wollman  * Copyright (c) 1985 by Sun Microsystems, Inc.
3599064799SGarrett Wollman  */
3699064799SGarrett Wollman 
378360efbdSAlfred Perlstein #include "namespace.h"
388360efbdSAlfred Perlstein #include <sys/types.h>
398360efbdSAlfred Perlstein #include <sys/socket.h>
408360efbdSAlfred Perlstein 
418360efbdSAlfred Perlstein #include <assert.h>
428360efbdSAlfred Perlstein #include <netdb.h>
4399064799SGarrett Wollman #include <stdio.h>
444c3af266SPoul-Henning Kamp #include <string.h>
458360efbdSAlfred Perlstein 
4699064799SGarrett Wollman #include <rpc/rpc.h>
47726212fcSPeter Wemm #include <rpc/pmap_clnt.h>
488360efbdSAlfred Perlstein #include "un-namespace.h"
4999064799SGarrett Wollman 
50726212fcSPeter Wemm int
getrpcport(char * host,int prognum,int versnum,int proto)51587cf682SCraig Rodrigues getrpcport(char *host, int prognum, int versnum, int proto)
5299064799SGarrett Wollman {
5399064799SGarrett Wollman 	struct sockaddr_in addr;
5499064799SGarrett Wollman 	struct hostent *hp;
5599064799SGarrett Wollman 
568360efbdSAlfred Perlstein 	assert(host != NULL);
578360efbdSAlfred Perlstein 
58*5299d64bSEdward Tomasz Napierala 	if ((hp = gethostbyname2(host, AF_INET)) == NULL)
5999064799SGarrett Wollman 		return (0);
6054edc0bbSPeter Wemm 	memset(&addr, 0, sizeof(addr));
6154edc0bbSPeter Wemm 	addr.sin_len = sizeof(struct sockaddr_in);
6299064799SGarrett Wollman 	addr.sin_family = AF_INET;
6399064799SGarrett Wollman 	addr.sin_port =  0;
64*5299d64bSEdward Tomasz Napierala 	if (hp->h_length > sizeof(addr.sin_addr.s_addr))
65*5299d64bSEdward Tomasz Napierala 		hp->h_length = sizeof(addr.sin_addr.s_addr);
668360efbdSAlfred Perlstein 	memcpy(&addr.sin_addr.s_addr, hp->h_addr, (size_t)hp->h_length);
678360efbdSAlfred Perlstein 	/* Inconsistent interfaces need casts! :-( */
688360efbdSAlfred Perlstein 	return (pmap_getport(&addr, (u_long)prognum, (u_long)versnum,
698360efbdSAlfred Perlstein 	    (u_int)proto));
7099064799SGarrett Wollman }
71