1*47c0e0c3Stron /* $NetBSD: getrpcport.c,v 1.18 2013/03/11 20:19:29 tron Exp $ */
29e15c989Scgd
363d7b677Scgd /*
4*47c0e0c3Stron * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd *
6*47c0e0c3Stron * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron * met:
963d7b677Scgd *
10*47c0e0c3Stron * * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron * notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron * * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron * copyright notice, this list of conditions and the following
14*47c0e0c3Stron * disclaimer in the documentation and/or other materials
15*47c0e0c3Stron * provided with the distribution.
16*47c0e0c3Stron * * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron * contributors may be used to endorse or promote products derived
18*47c0e0c3Stron * from this software without specific prior written permission.
1963d7b677Scgd *
20*47c0e0c3Stron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd */
3363d7b677Scgd
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)getrpcport.c 1.3 87/08/11 SMI";
38c63c52b2Schristos static char *sccsid = "@(#)getrpcport.c 2.1 88/07/29 4.0 RPCSRC";
39c63c52b2Schristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: getrpcport.c,v 1.18 2013/03/11 20:19:29 tron Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd
4463d7b677Scgd /*
4563d7b677Scgd * Copyright (c) 1985 by Sun Microsystems, Inc.
4663d7b677Scgd */
4763d7b677Scgd
4843fa6fe3Sjtc #include "namespace.h"
4946e6c5e8Slukem
5046e6c5e8Slukem #include <sys/types.h>
5146e6c5e8Slukem #include <sys/socket.h>
5246e6c5e8Slukem
53b48252f3Slukem #include <assert.h>
5446e6c5e8Slukem #include <netdb.h>
5563d7b677Scgd #include <stdio.h>
563c565bbeSjtc #include <string.h>
5746e6c5e8Slukem
5863d7b677Scgd #include <rpc/rpc.h>
59018a568cScgd #include <rpc/pmap_clnt.h>
6063d7b677Scgd
6143fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(getrpcport,_getrpcport)6260549036Smycroft __weak_alias(getrpcport,_getrpcport)
6343fa6fe3Sjtc #endif
6443fa6fe3Sjtc
6518ec2ba0Sjtc int
66adb74221Smatt getrpcport(char *host, int prognum, int versnum, int proto)
6763d7b677Scgd {
6863d7b677Scgd struct sockaddr_in addr;
6963d7b677Scgd struct hostent *hp;
7063d7b677Scgd
71b48252f3Slukem _DIAGASSERT(host != NULL);
72b48252f3Slukem
7363d7b677Scgd if ((hp = gethostbyname(host)) == NULL)
7463d7b677Scgd return (0);
753c565bbeSjtc memset(&addr, 0, sizeof(addr));
765860921eSmycroft addr.sin_len = sizeof(struct sockaddr_in);
7763d7b677Scgd addr.sin_family = AF_INET;
7863d7b677Scgd addr.sin_port = 0;
79ee376261Schristos if (hp->h_length > addr.sin_len)
80ee376261Schristos hp->h_length = addr.sin_len;
81ee376261Schristos memcpy(&addr.sin_addr.s_addr, hp->h_addr, (size_t)hp->h_length);
82ee376261Schristos /* Inconsistent interfaces need casts! :-( */
83ee376261Schristos return (pmap_getport(&addr, (u_long)prognum, (u_long)versnum,
84ee376261Schristos (u_int)proto));
8563d7b677Scgd }
86