xref: /openbsd/lib/libc/rpc/getrpcport.c (revision cb7760d1)
1*cb7760d1Smillert /*	$OpenBSD: getrpcport.c,v 1.8 2010/09/01 14:43:34 millert Exp $ */
2df930be7Sderaadt 
3df930be7Sderaadt /*
4*cb7760d1Smillert  * Copyright (c) 2010, Oracle America, Inc.
5*cb7760d1Smillert  *
6*cb7760d1Smillert  * Redistribution and use in source and binary forms, with or without
7*cb7760d1Smillert  * modification, are permitted provided that the following conditions are
8*cb7760d1Smillert  * met:
9*cb7760d1Smillert  *
10*cb7760d1Smillert  *     * Redistributions of source code must retain the above copyright
11*cb7760d1Smillert  *       notice, this list of conditions and the following disclaimer.
12*cb7760d1Smillert  *     * Redistributions in binary form must reproduce the above
13*cb7760d1Smillert  *       copyright notice, this list of conditions and the following
14*cb7760d1Smillert  *       disclaimer in the documentation and/or other materials
15*cb7760d1Smillert  *       provided with the distribution.
16*cb7760d1Smillert  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*cb7760d1Smillert  *       contributors may be used to endorse or promote products derived
18*cb7760d1Smillert  *       from this software without specific prior written permission.
19*cb7760d1Smillert  *
20*cb7760d1Smillert  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*cb7760d1Smillert  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*cb7760d1Smillert  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*cb7760d1Smillert  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*cb7760d1Smillert  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*cb7760d1Smillert  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*cb7760d1Smillert  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*cb7760d1Smillert  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*cb7760d1Smillert  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*cb7760d1Smillert  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*cb7760d1Smillert  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*cb7760d1Smillert  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt #include <stdio.h>
35df930be7Sderaadt #include <string.h>
36df930be7Sderaadt #include <rpc/rpc.h>
37c2f135c9Stholo #include <rpc/pmap_clnt.h>
38df930be7Sderaadt #include <netdb.h>
39df930be7Sderaadt #include <sys/socket.h>
40df930be7Sderaadt 
410b259b35Sderaadt int
getrpcport(char * host,int prognum,int versnum,int proto)42384ec30aSotto getrpcport(char *host, int prognum, int versnum, int proto)
43df930be7Sderaadt {
44df930be7Sderaadt 	struct sockaddr_in addr;
45df930be7Sderaadt 	struct hostent *hp;
46df930be7Sderaadt 
47df930be7Sderaadt 	if ((hp = gethostbyname(host)) == NULL)
48df930be7Sderaadt 		return (0);
49df930be7Sderaadt 	memset(&addr, 0, sizeof(addr));
50df930be7Sderaadt 	addr.sin_len = sizeof(struct sockaddr_in);
51df930be7Sderaadt 	addr.sin_family = AF_INET;
52df930be7Sderaadt 	addr.sin_port =  0;
5373b69cd9Sderaadt 	memcpy((char *)&addr.sin_addr, hp->h_addr, hp->h_length);
54df930be7Sderaadt 	return (pmap_getport(&addr, prognum, versnum, proto));
55df930be7Sderaadt }
56