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