1 /* $NetBSD: find_inet.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* find_inet 3 6 /* SUMMARY 7 /* inet-domain name services 8 /* SYNOPSIS 9 /* #include <find_inet.h> 10 /* 11 /* unsigned find_inet_addr(host) 12 /* const char *host; 13 /* 14 /* int find_inet_port(port, proto) 15 /* const char *port; 16 /* const char *proto; 17 /* DESCRIPTION 18 /* These functions translate network address information from 19 /* between printable form to the internal the form used by the 20 /* BSD TCP/IP network software. 21 /* 22 /* find_inet_addr() translates a symbolic or numerical hostname. 23 /* This function is deprecated. Use hostname_to_hostaddr() instead. 24 /* 25 /* find_inet_port() translates a symbolic or numerical port name. 26 /* BUGS 27 /* find_inet_addr() ignores all but the first address listed for 28 /* a symbolic hostname. 29 /* DIAGNOSTICS 30 /* Lookup and conversion errors are fatal. 31 /* LICENSE 32 /* .ad 33 /* .fi 34 /* The Secure Mailer license must be distributed with this software. 35 /* AUTHOR(S) 36 /* Wietse Venema 37 /* IBM T.J. Watson Research 38 /* P.O. Box 704 39 /* Yorktown Heights, NY 10598, USA 40 /*--*/ 41 42 /* System libraries. */ 43 44 #include <sys_defs.h> 45 #include <sys/socket.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 #include <netdb.h> 49 #include <stdlib.h> 50 #include <string.h> 51 52 /* Application-specific. */ 53 54 #include "msg.h" 55 #include "stringops.h" 56 #include "find_inet.h" 57 58 #ifndef INADDR_NONE 59 #define INADDR_NONE 0xffffffff 60 #endif 61 62 /* find_inet_addr - translate numerical or symbolic host name */ 63 64 unsigned find_inet_addr(const char *host) 65 { 66 struct in_addr addr; 67 struct hostent *hp; 68 69 addr.s_addr = inet_addr(host); 70 if ((addr.s_addr == INADDR_NONE) || (addr.s_addr == 0)) { 71 if ((hp = gethostbyname(host)) == 0) 72 msg_fatal("host not found: %s", host); 73 if (hp->h_addrtype != AF_INET) 74 msg_fatal("unexpected address family: %d", hp->h_addrtype); 75 if (hp->h_length != sizeof(addr)) 76 msg_fatal("unexpected address length %d", hp->h_length); 77 memcpy((char *) &addr, hp->h_addr, hp->h_length); 78 } 79 return (addr.s_addr); 80 } 81 82 /* find_inet_port - translate numerical or symbolic service name */ 83 84 int find_inet_port(const char *service, const char *protocol) 85 { 86 struct servent *sp; 87 int port; 88 89 if (alldig(service) && (port = atoi(service)) != 0) { 90 if (port < 0 || port > 65535) 91 msg_fatal("bad port number: %s", service); 92 return (htons(port)); 93 } else { 94 if ((sp = getservbyname(service, protocol)) == 0) 95 msg_fatal("unknown service: %s/%s", service, protocol); 96 return (sp->s_port); 97 } 98 } 99