xref: /dflybsd-src/lib/libc/rpc/getrpcport.c (revision ba96d07f7c72db2df2f4855fd9cef54db7111960)
1*ba96d07fShrs /*-
2*ba96d07fShrs  * Copyright (c) 2009, Sun Microsystems, Inc.
3*ba96d07fShrs  * All rights reserved.
4984263bcSMatthew Dillon  *
5*ba96d07fShrs  * Redistribution and use in source and binary forms, with or without
6*ba96d07fShrs  * modification, are permitted provided that the following conditions are met:
7*ba96d07fShrs  * - Redistributions of source code must retain the above copyright notice,
8*ba96d07fShrs  *   this list of conditions and the following disclaimer.
9*ba96d07fShrs  * - Redistributions in binary form must reproduce the above copyright notice,
10*ba96d07fShrs  *   this list of conditions and the following disclaimer in the documentation
11*ba96d07fShrs  *   and/or other materials provided with the distribution.
12*ba96d07fShrs  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*ba96d07fShrs  *   contributors may be used to endorse or promote products derived
14*ba96d07fShrs  *   from this software without specific prior written permission.
15984263bcSMatthew Dillon  *
16*ba96d07fShrs  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*ba96d07fShrs  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*ba96d07fShrs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*ba96d07fShrs  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*ba96d07fShrs  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*ba96d07fShrs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*ba96d07fShrs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*ba96d07fShrs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*ba96d07fShrs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*ba96d07fShrs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*ba96d07fShrs  * POSSIBILITY OF SUCH DAMAGE.
271de703daSMatthew Dillon  *
281de703daSMatthew Dillon  * @(#)getrpcport.c 1.3 87/08/11 SMI
291de703daSMatthew Dillon  * @(#)getrpcport.c	2.1 88/07/29 4.0 RPCSRC
30ce0e08e2SPeter Avalos  * $NetBSD: getrpcport.c,v 1.16 2000/01/22 22:19:18 mycroft Exp $
31ce0e08e2SPeter Avalos  * $FreeBSD: src/lib/libc/rpc/getrpcport.c,v 1.13 2004/10/16 06:11:34 obrien Exp $
32c9b6181eSSascha Wildner  * $DragonFly: src/lib/libc/rpc/getrpcport.c,v 1.3 2005/11/13 12:27:04 swildner Exp $
33984263bcSMatthew Dillon  */
34984263bcSMatthew Dillon 
35984263bcSMatthew Dillon /*
36984263bcSMatthew Dillon  * Copyright (c) 1985 by Sun Microsystems, Inc.
37984263bcSMatthew Dillon  */
38984263bcSMatthew Dillon 
39ce0e08e2SPeter Avalos #include "namespace.h"
40ce0e08e2SPeter Avalos #include <sys/types.h>
41ce0e08e2SPeter Avalos #include <sys/socket.h>
42ce0e08e2SPeter Avalos 
43ce0e08e2SPeter Avalos #include <assert.h>
44ce0e08e2SPeter Avalos #include <netdb.h>
45984263bcSMatthew Dillon #include <stdio.h>
46984263bcSMatthew Dillon #include <string.h>
47ce0e08e2SPeter Avalos 
48984263bcSMatthew Dillon #include <rpc/rpc.h>
49984263bcSMatthew Dillon #include <rpc/pmap_clnt.h>
50ce0e08e2SPeter Avalos #include "un-namespace.h"
51984263bcSMatthew Dillon 
52984263bcSMatthew Dillon int
getrpcport(char * host,int prognum,int versnum,int proto)53c9b6181eSSascha Wildner getrpcport(char *host, int prognum, int versnum, int proto)
54984263bcSMatthew Dillon {
55984263bcSMatthew Dillon 	struct sockaddr_in addr;
56984263bcSMatthew Dillon 	struct hostent *hp;
57984263bcSMatthew Dillon 
58ce0e08e2SPeter Avalos 	assert(host != NULL);
59ce0e08e2SPeter Avalos 
60984263bcSMatthew Dillon 	if ((hp = gethostbyname(host)) == NULL)
61984263bcSMatthew Dillon 		return (0);
62984263bcSMatthew Dillon 	memset(&addr, 0, sizeof(addr));
63984263bcSMatthew Dillon 	addr.sin_len = sizeof(struct sockaddr_in);
64984263bcSMatthew Dillon 	addr.sin_family = AF_INET;
65984263bcSMatthew Dillon 	addr.sin_port =  0;
66ce0e08e2SPeter Avalos 	if (hp->h_length > addr.sin_len)
67ce0e08e2SPeter Avalos 		hp->h_length = addr.sin_len;
68ce0e08e2SPeter Avalos 	memcpy(&addr.sin_addr.s_addr, hp->h_addr, (size_t)hp->h_length);
69ce0e08e2SPeter Avalos 	/* Inconsistent interfaces need casts! :-( */
70ce0e08e2SPeter Avalos 	return (pmap_getport(&addr, (u_long)prognum, (u_long)versnum,
71ce0e08e2SPeter Avalos 	    (u_int)proto));
72984263bcSMatthew Dillon }
73