5
/* split string into host and port, destroy string
7
/* #include <host_port.h>
9
/* const char *host_port(string, host, port, def_service)
15
/* host_port() splits a string into substrings with the host
16
/* name or address, and the service name or port number.
17
/* The input string is modified.
19
/* The following input formats are understood:
21
/* [host]:port, [host]:, [host].
23
/* host:port, host:, host.
25
/* The result is a null pointer in case of success.
26
/* In case of problems the result is a string pointer with
31
/* The Secure Mailer license must be distributed with this software.
34
/* IBM T.J. Watson Research
36
/* Yorktown Heights, NY 10598, USA
45
/* Utility library. */
49
#include <stringops.h>
50
#include <valid_hostname.h>
54
#include <host_port.h>
56
/* host_port - parse string into host and port, destroy string */
58
const char *host_port(char *buf, char **host, char **port,
64
* [host]:port, [host]:, [host].
68
if ((cp = split_at(cp, ']')) == 0)
69
return ("missing \"]\"");
70
if (*cp && *cp++ != ':')
71
return ("garbage after \"]\"");
78
* [host:port], [host:]. These conflict with IPV6 address literals.
81
if (strchr(*host, ':')) {
82
msg_warn("old-style address form: [%s]", *host);
83
msg_warn("support for [host:port] forms will go away");
84
msg_warn("specify [host]:port instead");
85
return (host_port(buf + 1, host, port, def_service));
91
* host:port, host:, host.
95
if ((cp = split_at_right(cp, ':')) == 0 || *cp == 0)
102
* Final sanity checks. We're still sloppy, allowing bare numerical
103
* network addresses instead of requiring proper [ipaddress] forms.
105
if (!valid_hostname(*host, DONT_GRIPE)
106
&& !valid_hostaddr(*host, DONT_GRIPE))
107
return ("valid hostname or network address required");
108
if (ISDIGIT(**port) && !alldig(*port))
109
return ("garbage after numerical service");
117
#include <vstring_vstream.h>
119
#define STR(x) vstring_str(x)
121
int main(int unused_argc, char **unused_argv)
123
VSTRING *in_buf = vstring_alloc(10);
124
VSTRING *parse_buf = vstring_alloc(10);
129
while (vstring_fgets_nonl(in_buf, VSTREAM_IN)) {
130
vstream_printf(">> %s\n", STR(in_buf));
131
vstream_fflush(VSTREAM_OUT);
132
vstring_strcpy(parse_buf, STR(in_buf));
133
if ((err = host_port(STR(parse_buf), &host, &port, "default-service")) != 0) {
134
msg_warn("%s in %s", err, STR(in_buf));
136
vstream_printf("host %s port %s\n", host, port);
137
vstream_fflush(VSTREAM_OUT);
140
vstring_free(in_buf);
141
vstring_free(parse_buf);