/*
This file is part of GNUnet.
- Copyright (C) 2005-2013 GNUnet e.V.
+ Copyright (C) 2005-2017 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
-
/**
* @file util/strings.c
* @brief string functions
#include <unistr.h>
#include <uniconv.h>
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__)
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-strings", syscall)
/**
}
+/**
+ * Convert a peer path to a human-readable string.
+ *
+ * @param pids array of PIDs to convert to a string
+ * @param num_pids length of the @a pids array
+ * @return string representing the array of @a pids
+ */
+char *
+GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids,
+ unsigned int num_pids)
+{
+ char *buf;
+ size_t off;
+ size_t plen = num_pids * 5 + 1;
+
+ off = 0;
+ buf = GNUNET_malloc (plen);
+ for (unsigned int i = 0;
+ i < num_pids;
+ i++)
+ {
+ off += GNUNET_snprintf (&buf[off],
+ plen - off,
+ "%s%s",
+ GNUNET_i2s (&pids[i]),
+ (i == num_pids -1) ? "" : "-");
+ }
+ return buf;
+}
+
+
/**
* Given a buffer of a given size, find "count"
* 0-terminated strings in the buffer and assign
/**
- * Tries to convert 'zt_addr' string to an IPv6 address.
+ * Tries to convert @a zt_addr string to an IPv6 address.
* The string is expected to have the format "[ABCD::01]:80".
*
* @param zt_addr 0-terminated string. May be mangled by the function.
* the contents of @a r_buf are undefined.
*/
int
-GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
+GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
+ uint16_t addrlen,
struct sockaddr_in *r_buf)
{
unsigned int temps[4];
if (addrlen < 9)
return GNUNET_SYSERR;
- cnt = SSCANF (zt_addr, "%u.%u.%u.%u:%u", &temps[0], &temps[1], &temps[2], &temps[3], &port);
+ cnt = SSCANF (zt_addr,
+ "%u.%u.%u.%u:%u",
+ &temps[0],
+ &temps[1],
+ &temps[2],
+ &temps[3],
+ &port);
if (5 != cnt)
return GNUNET_SYSERR;
for (cnt = 0; cnt < 4; cnt++)
* @param addrlen number of bytes in @a addr (if addr is 0-terminated,
* 0-terminator should not be counted towards addrlen).
* @param r_buf a buffer to fill.
- * @return #GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
- * case the contents of r_buf are undefined.
+ * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
+ * case the contents of @a r_buf are undefined.
*/
int
GNUNET_STRINGS_to_address_ip (const char *addr,
}
+/**
+ * Parse an address given as a string into a
+ * `struct sockaddr`.
+ *
+ * @param addr the address
+ * @param[out] af set to the parsed address family (i.e. AF_INET)
+ * @param[out] sa set to the parsed address
+ * @return 0 on error, otherwise number of bytes in @a sa
+ */
+size_t
+GNUNET_STRINGS_parse_socket_addr (const char *addr,
+ uint8_t *af,
+ struct sockaddr **sa)
+{
+ char *cp = GNUNET_strdup (addr);
+
+ *af = AF_UNSPEC;
+ if ('[' == *addr)
+ {
+ /* IPv6 */
+ *sa = GNUNET_malloc (sizeof (struct sockaddr_in6));
+ if (GNUNET_OK !=
+ GNUNET_STRINGS_to_address_ipv6 (cp,
+ strlen (cp),
+ (struct sockaddr_in6 *) *sa))
+ {
+ GNUNET_free (*sa);
+ *sa = NULL;
+ GNUNET_free (cp);
+ return 0;
+ }
+ *af = AF_INET6;
+ GNUNET_free (cp);
+ return sizeof (struct sockaddr_in6);
+ }
+ else
+ {
+ /* IPv4 */
+ *sa = GNUNET_malloc (sizeof (struct sockaddr_in));
+ if (GNUNET_OK !=
+ GNUNET_STRINGS_to_address_ipv4 (cp,
+ strlen (cp),
+ (struct sockaddr_in *) *sa))
+ {
+ GNUNET_free (*sa);
+ *sa = NULL;
+ GNUNET_free (cp);
+ return 0;
+ }
+ *af = AF_INET;
+ GNUNET_free (cp);
+ return sizeof (struct sockaddr_in);
+ }
+}
+
+
/**
* Makes a copy of argv that consists of a single memory chunk that can be
* freed with a single call to GNUNET_free();
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/
int
-GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
+GNUNET_STRINGS_get_utf8_args (int argc,
+ char *const *argv,
+ int *u8argc,
+ char *const **u8argv)
{
#if WINDOWS
wchar_t *wcmd;