X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=net.c;h=ba19408e11337a82aaa380d2e8fe96ceca0c5673;hp=672e09e971717a742eb85b9c57b05f661f0b97dd;hb=4fbe16430b47;hpb=631fb4db2cea7bc44e91631957db8cd852fe75ee diff --git a/net.c b/net.c index 672e09e9..ba19408e 100644 --- a/net.c +++ b/net.c @@ -180,6 +180,36 @@ failed: return NULL; } +/** + * Pretty-print a host/port pair. + * + * \param url NULL, or any string accepted by \ref parse_url(). + * \param default_port Applies if url has no port. + * + * If the url argument is NULL, the function returns the string + * 0.0.0.0:default_port. Otherwise it calls \ref parse_url() to check the + * syntax of the input string given by url. On errors the string "?" is + * returned. Otherwise, if url contains a port, a copy of url is returned. If + * no port was supplied, a colon and the default port are appended to url. + * + * \return In all cases the returned string is a allocated with malloc(3) and + * has to be freed by the caller. + */ +char *format_url(const char *url, int default_port) +{ + char host[MAX_HOSTLEN]; + int url_port; + + if (!url) + return make_message("0.0.0.0:%d", default_port); + if (!parse_url(url, host, sizeof(host), &url_port)) + return make_message("?"); + if (url_port < 0) + return make_message("%s:%d", url, default_port); + else + return para_strdup(url); +} + /** * Stringify port number, resolve into service name where defined. *