aboutsummaryrefslogtreecommitdiff
path: root/src/util/common_logging.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/common_logging.c')
-rw-r--r--src/util/common_logging.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 2fdd17ca7..8ea099122 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -398,6 +398,52 @@ GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
398 return (const char *) ret.encoding; 398 return (const char *) ret.encoding;
399} 399}
400 400
401
402
403/**
404 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
405 * (for printing debug messages). This is one of the very few calls
406 * in the entire API that is NOT reentrant!
407 *
408 * @param addr the address
409 * @param addrlen the length of the address
410 * @return nicely formatted string for the address
411 * will be overwritten by next call to GNUNET_a2s.
412 */
413const char *GNUNET_a2s (const struct sockaddr *addr,
414 socklen_t addrlen)
415{
416 static char buf[INET6_ADDRSTRLEN+8];
417 static char b2[6];
418 const struct sockaddr_in * v4;
419 const struct sockaddr_in6 *v6;
420 switch (addr->sa_family)
421 {
422 case AF_INET:
423 v4 = (const struct sockaddr_in*)addr;
424 inet_ntop(AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
425 if (0 == ntohs(v4->sin_port))
426 return buf;
427 strcat (buf, ":");
428 sprintf (b2, "%u", ntohs(v4->sin_port));
429 strcat (buf, b2);
430 return buf;
431 case AF_INET6:
432 v6 = (const struct sockaddr_in6*)addr;
433 buf[0] = '[';
434 inet_ntop(AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
435 if (0 == ntohs(v6->sin6_port))
436 return &buf[1];
437 strcat (buf, "]:");
438 sprintf (b2, "%u", ntohs(v6->sin6_port));
439 strcat (buf, b2);
440 return buf;
441 default:
442 return _("invalid address");
443 }
444}
445
446
401/** 447/**
402 * Initializer 448 * Initializer
403 */ 449 */