X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Furi.c;h=ec5d426630b9300e925df81fa8d794b2ed282c94;hp=3581e307d33a0dfa5bdcb5932b0e5fb648b30114;hb=f73fabfda365d22e7dd180fb1614e37c446fbd9e;hpb=a4b92340642035d1eafeb1eead0ad01f64d2007d diff --git a/src/common/uri.c b/src/common/uri.c index 3581e307d..ec5d42663 100644 --- a/src/common/uri.c +++ b/src/common/uri.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#include #include #include #include @@ -103,6 +104,11 @@ static int set_ip_address(const char *addr, int af, char *dst, size_t size) unsigned char buf[sizeof(struct in6_addr)]; struct hostent *record; + assert(addr); + assert(dst); + + memset(dst, 0, size); + /* Network protocol */ ret = inet_pton(af, addr, buf); if (ret < 1) { @@ -117,7 +123,10 @@ static int set_ip_address(const char *addr, int af, char *dst, size_t size) /* Translate IP to string */ (void) inet_ntop(af, record->h_addr_list[0], dst, size); } else { - memcpy(dst, addr, size); + if (size > 0) { + strncpy(dst, addr, size); + dst[size - 1] = '\0'; + } } DBG2("IP address resolved to %s", dst); @@ -128,6 +137,40 @@ error: return -1; } +/* + * Build a string URL from a lttng_uri object. + */ +int uri_to_str_url(struct lttng_uri *uri, char *dst, size_t size) +{ + int ipver, ret; + const char *addr; + char proto[4], port[7]; + + assert(uri); + assert(dst); + + if (uri->dtype == LTTNG_DST_PATH) { + ipver = 0; + addr = uri->dst.path; + (void) snprintf(proto, sizeof(proto), "file"); + (void) snprintf(port, sizeof(port), "%s", ""); + } else { + ipver = (uri->dtype == LTTNG_DST_IPV4) ? 4 : 6; + addr = (ipver == 4) ? uri->dst.ipv4 : uri->dst.ipv6; + (void) snprintf(proto, sizeof(proto), "net%d", ipver); + (void) snprintf(port, sizeof(port), ":%d", uri->port); + } + + ret = snprintf(dst, size, "%s://%s%s%s%s/%s", proto, + (ipver == 6) ? "[" : "", addr, (ipver == 6) ? "]" : "", + port, uri->subdir); + if (ret < 0) { + PERROR("snprintf uri to url"); + } + + return ret; +} + /* * Compare two URIs. *