#include <sys_defs.h>
#include <string.h>
#include <ctype.h>
#include <msg.h>
#include <split_at.h>
#include <stringops.h>
#include <valid_hostname.h>
#include <host_port.h>
const char *host_port(char *buf, char **host, char **port,
char *def_service)
{
char *cp = buf;
if (*cp == '[') {
*host = ++cp;
if ((cp = split_at(cp, ']')) == 0)
return ("missing \"]\"");
if (*cp && *cp++ != ':')
return ("garbage after \"]\"");
if (*cp == 0)
*port = def_service;
else
*port = cp;
#if 1
if (strchr(*host, ':')) {
msg_warn("old-style address form: [%s]", *host);
msg_warn("support for [host:port] forms will go away");
msg_warn("specify [host]:port instead");
return (host_port(buf + 1, host, port, def_service));
}
#endif
}
else {
*host = cp;
if ((cp = split_at_right(cp, ':')) == 0 || *cp == 0)
*port = def_service;
else
*port = cp;
}
if (!valid_hostname(*host, DONT_GRIPE)
&& !valid_hostaddr(*host, DONT_GRIPE))
return ("valid hostname or network address required");
if (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);
vstring_strcpy(parse_buf, STR(in_buf));
if ((err = host_port(STR(parse_buf), &host, &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