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