Add centralized IP/Peer formatting functions

This stops us from needing to manually check against ":" to
add brackets around IPv6 addresses everywhere.
This commit is contained in:
Matt Stancliff
2014-10-23 12:40:02 -04:00
parent 3cd36a4dd9
commit 2d90619f88
4 changed files with 37 additions and 0 deletions

View File

@ -962,6 +962,15 @@ sds sdsjoin(char **argv, int argc, char *sep) {
return join;
}
sds sdsformatip(char *ip, int port) {
if (port >= 0)
return sdscatfmt(sdsempty(),
strchr(ip,':') ? "[%s]:%i" : "%s:%i", ip, port);
else
return sdscatfmt(sdsempty(),
strchr(ip,':') ? "[%s]" : "%s", ip);
}
#ifdef SDS_TEST_MAIN
#include <stdio.h>
#include "testhelp.h"