Sync usage with man page.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / find_inet.c
blob91857a17390824bafc461d1dd0dd2603d06848fb
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* find_inet 3
6 /* SUMMARY
7 /* inet-domain name services
8 /* SYNOPSIS
9 /* #include <find_inet.h>
11 /* unsigned find_inet_addr(host)
12 /* const char *host;
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.
22 /* find_inet_addr() translates a symbolic or numerical hostname.
23 /* This function is deprecated. Use hostname_to_hostaddr() instead.
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 /*--*/
42 /* System libraries. */
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>
52 /* Application-specific. */
54 #include "msg.h"
55 #include "stringops.h"
56 #include "find_inet.h"
58 #ifndef INADDR_NONE
59 #define INADDR_NONE 0xffffffff
60 #endif
62 /* find_inet_addr - translate numerical or symbolic host name */
64 unsigned find_inet_addr(const char *host)
66 struct in_addr addr;
67 struct hostent *hp;
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);
79 return (addr.s_addr);
82 /* find_inet_port - translate numerical or symbolic service name */
84 int find_inet_port(const char *service, const char *protocol)
86 struct servent *sp;
87 int port;
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);