1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*++
/* NAME
/* host_port 3
/* SUMMARY
/* split string into host and port, destroy string
/* SYNOPSIS
/* #include <host_port.h>
/*
/* const char *host_port(string, host, def_host, port, def_service)
/* char *string;
/* char **host;
/* char *def_host;
/* char **port;
/* char *def_service;
/* DESCRIPTION
/* host_port() splits a string into substrings with the host
/* name or address, and the service name or port number.
/* The input string is modified.
/*
/* Host/domain names are validated with valid_utf8_hostname(),
/* and host addresses are validated with valid_hostaddr().
/*
/* The following input formats are understood (null means
/* a null pointer argument):
/*
/* When def_service is not null, and def_host is null:
/*
/* [host]:port, [host]:, [host]
/*
/* host:port, host:, host
/*
/* When def_host is not null, and def_service is null:
/*
/* :port, port
/*
/* Other combinations of def_service and def_host are
/* not supported and produce undefined results.
/* DIAGNOSTICS
/* The result is a null pointer in case of success.
/* In case of problems the result is a string pointer with
/* the problem type.
/* CLIENT EXAMPLE
/* .ad
/* .fi
/* Typical client usage allows the user to omit the service port,
/* in which case the client connects to a pre-determined default
/* port:
/* .nf
/* .na
/*
/* buf = mystrdup(endpoint);
/* if ((parse_error = host_port(buf, &host, NULL, &port, defport)) != 0)
/* msg_fatal("%s in \"%s\"", parse_error, endpoint);
/* if ((aierr = hostname_to_sockaddr(host, port, SOCK_STREAM, &res)) != 0)
/* msg_fatal("%s: %s", endpoint, MAI_STRERROR(aierr));
/* myfree(buf);
/* SERVER EXAMPLE
/* .ad
/* .fi
/* Typical server usage allows the user to omit the host, meaning
/* listen on all available network addresses:
/* .nf
/* .na
/*
/* buf = mystrdup(endpoint);
/* if ((parse_error = host_port(buf, &host, "", &port, NULL)) != 0)
/* msg_fatal("%s in \"%s\"", parse_error, endpoint);
/* if (*host == 0)
/* host = 0;
/* if ((aierr = hostname_to_sockaddr(host, port, SOCK_STREAM, &res)) != 0)
/* msg_fatal("%s: %s", endpoint, MAI_STRERROR(aierr));
/* myfree(buf);
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System library. */
#include <sys_defs.h>
#include <string.h>
#include <ctype.h>
/* Utility library. */
#include <msg.h>
#include <split_at.h>
#include <stringops.h> /* XXX util_utf8_enable */
#include <valid_utf8_hostname.h>
/* Global library. */
#include <host_port.h>
/*
* Point-fix workaround. The libutil library should be email agnostic, but
* we can't rip up the library APIs in the stable releases.
*/
#include <string.h>
#ifdef STRCASECMP_IN_STRINGS_H
#include <strings.h>
#endif
#define IPV6_COL "IPv6:" /* RFC 2821 */
#define IPV6_COL_LEN (sizeof(IPV6_COL) - 1)
#define HAS_IPV6_COL(str) (strncasecmp((str), IPV6_COL, IPV6_COL_LEN) == 0)
/* host_port - parse string into host and port, destroy string */
const char *host_port(char *buf, char **host, char *def_host,
char **port, char *def_service)
{
char *cp = buf;
int ipv6 = 0;
/*-
* [host]:port, [host]:, [host].
* [ipv6:ipv6addr]:port, [ipv6:ipv6addr]:, [ipv6:ipv6addr].
*/
if (*cp == '[') {
++cp;
if ((ipv6 = HAS_IPV6_COL(cp)) != 0)
cp += IPV6_COL_LEN;
*host = cp;
if ((cp = split_at(cp, ']')) == 0)
return ("missing \"]\"");
if (*cp && *cp++ != ':')
return ("garbage after \"]\"");
if (ipv6 && !valid_ipv6_hostaddr(*host, DONT_GRIPE))
return ("malformed IPv6 address");
*port = *cp ? cp : def_service;
}
/*
* host:port, host:, host, :port, port.
*/
else {
if ((cp = split_at_right(buf, ':')) != 0) {
*host = *buf ? buf : def_host;
*port = *cp ? cp : def_service;
} else {
*host = def_host ? def_host : (*buf ? buf : 0);
*port = def_service ? def_service : (*buf ? buf : 0);
}
}
if (*host == 0)
return ("missing host information");
if (*port == 0)
return ("missing service information");
/*
* Final sanity checks. We're still sloppy, allowing bare numerical
* network addresses instead of requiring proper [ipaddress] forms.
*/
if (*host != def_host
&& !valid_utf8_hostname(util_utf8_enable, *host, DONT_GRIPE)
&& !valid_hostaddr(*host, DONT_GRIPE))
return ("valid hostname or network address required");
if (*port != def_service && ISDIGIT(**port) && !alldig(*port))
return ("garbage after numerical service");
return (0);
}
#ifdef TEST
#include <vstream.h>
#include <vstring.h>
#include <vstring_vstream.h>
#define STR(x) vstring_str(x)
int main(int unused_argc, char **unused_argv)
{
VSTRING *in_buf = vstring_alloc(10);
VSTRING *parse_buf = vstring_alloc(10);
char *host;
char *port;
const char *err;
while (vstring_fgets_nonl(in_buf, VSTREAM_IN)) {
vstream_printf(">> %s\n", STR(in_buf));
vstream_fflush(VSTREAM_OUT);
if (*STR(in_buf) == '#')
continue;
vstring_strcpy(parse_buf, STR(in_buf));
if ((err = host_port(STR(parse_buf), &host, (char *) 0, &port, "default-service")) != 0) {
msg_warn("%s in %s", err, STR(in_buf));
} else {
vstream_printf("host %s port %s\n", host, port);
vstream_fflush(VSTREAM_OUT);
}
}
vstring_free(in_buf);
vstring_free(parse_buf);
return (0);
}
#endif
|