2 This file is part of GNUnet.
3 (C) 2005, 2006 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/strings.c
23 * @brief string functions
25 * @author Christian Grothoff
32 #include "gnunet_common.h"
33 #include "gnunet_strings_lib.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
42 * Fill a buffer of the given size with
43 * count 0-terminated strings (given as varargs).
44 * If "buffer" is NULL, only compute the amount of
45 * space required (sum of "strlen(arg)+1").
47 * Unlike using "snprintf" with "%s", this function
48 * will add 0-terminators after each string. The
49 * "GNUNET_string_buffer_tokenize" function can be
50 * used to parse the buffer back into individual
53 * @param buffer the buffer to fill with strings, can
54 * be NULL in which case only the necessary
55 * amount of space will be calculated
56 * @param size number of bytes available in buffer
57 * @param count number of strings that follow
58 * @param ... count 0-terminated strings to copy to buffer
59 * @return number of bytes written to the buffer
60 * (or number of bytes that would have been written)
63 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
74 s = va_arg (ap, const char *);
76 slen = strlen (s) + 1;
79 GNUNET_assert (needed + slen <= size);
80 memcpy (&buffer[needed], s, slen);
91 * Given a buffer of a given size, find "count"
92 * 0-terminated strings in the buffer and assign
93 * the count (varargs) of type "const char**" to the
94 * locations of the respective strings in the
97 * @param buffer the buffer to parse
98 * @param size size of the buffer
99 * @param count number of strings to locate
100 * @return offset of the character after the last 0-termination
101 * in the buffer, or 0 on error.
104 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
105 unsigned int count, ...)
113 va_start (ap, count);
116 r = va_arg (ap, const char **);
119 while ((needed < size) && (buffer[needed] != '\0'))
124 return 0; /* error */
127 needed++; /* skip 0-termination */
136 * Convert a given filesize into a fancy human-readable format.
138 * @param size number of bytes
139 * @return fancy representation of the size (possibly rounded) for humans
142 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
144 const char *unit = _( /* size unit */ "b");
167 ret = GNUNET_malloc (32);
168 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
174 * Unit conversion table entry for 'convert_with_table'.
176 struct ConversionTable
179 * Name of the unit (or NULL for end of table).
184 * Factor to apply for this unit.
186 unsigned long long value;
191 * Convert a string of the form "4 X 5 Y" into a numeric value
192 * by interpreting "X" and "Y" as units and then multiplying
193 * the numbers with the values associated with the respective
194 * unit from the conversion table.
196 * @param input input string to parse
197 * @param table table with the conversion of unit names to numbers
198 * @param output where to store the result
199 * @return GNUNET_OK on success, GNUNET_SYSERR on error
202 convert_with_table (const char *input,
203 const struct ConversionTable *table,
204 unsigned long long *output)
206 unsigned long long ret;
209 unsigned long long last;
214 in = GNUNET_strdup (input);
215 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
218 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
220 if (table[i].name != NULL)
221 last *= table[i].value;
226 if (1 != SSCANF (tok, "%llu", &last))
229 return GNUNET_SYSERR; /* expected number */
241 * Convert a given fancy human-readable size to bytes.
243 * @param fancy_size human readable string (i.e. 1 MB)
244 * @param size set to the size in bytes
245 * @return GNUNET_OK on success, GNUNET_SYSERR on error
248 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
249 unsigned long long *size)
251 static const struct ConversionTable table[] =
256 { "MiB", 1024 * 1024},
257 { "MB", 1000 * 1000},
258 { "GiB", 1024 * 1024 * 1024},
259 { "GB", 1000 * 1000 * 1000},
260 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
261 { "TB", 1000LL * 1000LL * 1000LL * 1024LL},
262 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
263 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
264 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
265 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
269 return convert_with_table (fancy_size,
276 * Convert a given fancy human-readable time to our internal
279 * @param fancy_time human readable string (i.e. 1 minute)
280 * @param rtime set to the relative time
281 * @return GNUNET_OK on success, GNUNET_SYSERR on error
284 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
285 struct GNUNET_TIME_Relative *rtime)
287 static const struct ConversionTable table[] =
293 { "minutes", 60 * 1000},
295 { "h", 60 * 60 * 1000},
296 { "d", 24 * 60 * 60 * 1000},
297 { "a", 31536000000LL /* year */ },
301 unsigned long long val;
303 ret = convert_with_table (fancy_time,
306 rtime->rel_value = (uint64_t) val;
311 * Convert the len characters long character sequence
312 * given in input that is in the given input charset
313 * to a string in given output charset.
314 * @return the converted string (0-terminated),
315 * if conversion fails, a copy of the orignal
316 * string is returned.
319 GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
323 #if ENABLE_NLS && HAVE_ICONV
330 cd = iconv_open (output_charset, input_charset);
331 if (cd == (iconv_t) - 1)
333 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
334 LOG (GNUNET_ERROR_TYPE_WARNING, _("Character sets requested were `%s'->`%s'\n"),
335 input_charset, output_charset);
336 ret = GNUNET_malloc (len + 1);
337 memcpy (ret, input, len);
341 tmpSize = 3 * len + 4;
342 tmp = GNUNET_malloc (tmpSize);
346 #if FREEBSD || DARWIN || WINDOWS
347 (const char **) &input,
351 &len, &itmp, &finSize) == SIZE_MAX)
353 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
356 ret = GNUNET_malloc (len + 1);
357 memcpy (ret, input, len);
361 ret = GNUNET_malloc (tmpSize - finSize + 1);
362 memcpy (ret, tmp, tmpSize - finSize);
363 ret[tmpSize - finSize] = '\0';
365 if (0 != iconv_close (cd))
366 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
369 ret = GNUNET_malloc (len + 1);
370 memcpy (ret, input, len);
378 * Convert the len characters long character sequence
379 * given in input that is in the given charset
381 * @return the converted string (0-terminated),
382 * if conversion fails, a copy of the orignal
383 * string is returned.
386 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
388 return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
392 * Convert the len bytes-long UTF-8 string
393 * given in input to the given charset.
395 * @return the converted string (0-terminated),
396 * if conversion fails, a copy of the orignal
397 * string is returned.
400 GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
402 return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
406 * Convert the utf-8 input string to lowercase
407 * Output needs to be allocated appropriately
409 * @param input input string
410 * @param output output buffer
413 GNUNET_STRINGS_utf8_tolower(const char* input, char** output)
418 tmp_in = u8_tolower ((uint8_t*)input, strlen ((char *) input),
419 NULL, UNINORM_NFD, NULL, &len);
420 memcpy(*output, tmp_in, len);
421 (*output)[len] = '\0';
426 * Convert the utf-8 input string to uppercase
427 * Output needs to be allocated appropriately
429 * @param input input string
430 * @param output output buffer
433 GNUNET_STRINGS_utf8_toupper(const char* input, char** output)
438 tmp_in = u8_toupper ((uint8_t*)input, strlen ((char *) input),
439 NULL, UNINORM_NFD, NULL, &len);
440 memcpy(*output, tmp_in, len);
441 (*output)[len] = '\0';
447 * Complete filename (a la shell) from abbrevition.
448 * @param fil the name of the file, may contain ~/ or
449 * be relative to the current directory
450 * @returns the full file name,
451 * NULL is returned on error
454 GNUNET_STRINGS_filename_expand (const char *fil)
472 if (fil[0] == DIR_SEPARATOR)
473 /* absolute path, just copy */
474 return GNUNET_strdup (fil);
477 fm = getenv ("HOME");
480 LOG (GNUNET_ERROR_TYPE_WARNING,
481 _("Failed to expand `$HOME': environment variable `HOME' not set"));
484 fm = GNUNET_strdup (fm);
485 /* do not copy '~' */
488 /* skip over dir seperator to be consistent */
489 if (fil_ptr[0] == DIR_SEPARATOR)
500 buffer = GNUNET_malloc (len);
501 if (getcwd (buffer, len) != NULL)
506 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
509 GNUNET_free (buffer);
512 GNUNET_free (buffer);
517 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
518 buffer = getenv ("PWD"); /* alternative */
520 fm = GNUNET_strdup (buffer);
523 fm = GNUNET_strdup ("./"); /* give up */
525 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
526 buffer = GNUNET_malloc (n);
527 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
528 (fm[strlen (fm) - 1] ==
529 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
533 fn = GNUNET_malloc (MAX_PATH + 1);
535 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
537 SetErrnoFromWinError (lRet);
538 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
541 /* is the path relative? */
542 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
544 char szCurDir[MAX_PATH + 1];
546 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
547 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
549 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
550 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
553 buffer = GNUNET_malloc (MAX_PATH + 1);
554 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
565 * Give relative time in human-readable fancy format.
567 * @param delta time in milli seconds
568 * @return time as human-readable string
571 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
573 const char *unit = _( /* time unit */ "ms");
575 uint64_t dval = delta.rel_value;
577 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
578 return GNUNET_strdup (_("eternity"));
582 unit = _( /* time unit */ "s");
586 unit = _( /* time unit */ "m");
590 unit = _( /* time unit */ "h");
594 unit = _( /* time unit */ " days");
599 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
605 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
606 * return value does not include the newline character.
608 * @param t time to convert
609 * @return absolute time in human-readable format
612 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
617 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
618 return GNUNET_strdup (_("end of time"));
619 tt = t.abs_value / 1000;
621 ret = ctime_r (&tt, GNUNET_malloc (32));
623 ret = GNUNET_strdup (ctime (&tt));
625 ret[strlen (ret) - 1] = '\0';
632 * Returns a pointer to a part of filename (allocates nothing)!
634 * @param filename filename to extract basename from
635 * @return short (base) name of the file (that is, everything following the
636 * last directory separator in filename. If filename ends with a
637 * directory separator, the result will be a zero-length string.
638 * If filename has no directory separators, the result is filename
642 GNUNET_STRINGS_get_short_name (const char *filename)
644 const char *short_fn = filename;
646 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR))
654 * Get the numeric value corresponding to a character.
656 * @param a a character
657 * @return corresponding numeric value
660 getValue__ (unsigned char a)
662 if ((a >= '0') && (a <= '9'))
664 if ((a >= 'A') && (a <= 'V'))
665 return (a - 'A' + 10);
671 * Convert binary data to ASCII encoding. The ASCII encoding is rather
672 * GNUnet specific. It was chosen such that it only uses characters
673 * in [0-9A-V], can be produced without complex arithmetics and uses a
674 * small number of characters.
675 * Does not append 0-terminator, but returns a pointer to the place where
676 * it should be placed, if needed.
678 * @param data data to encode
679 * @param size size of data (in bytes)
680 * @param out buffer to fill
681 * @param out_size size of the buffer. Must be large enough to hold
682 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
683 * @return pointer to the next byte in 'out' or NULL on error.
686 GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out, size_t out_size)
689 * 32 characters for encoding
691 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
697 GNUNET_assert (data != NULL);
698 GNUNET_assert (out != NULL);
699 if (out_size < (((size*8) + ((size*8) % 5)) % 5))
708 while ((rpos < size) || (vbit > 0))
710 if ((rpos < size) && (vbit < 5))
712 bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */
717 bits <<= (5 - vbit); /* zero-padding */
718 GNUNET_assert (vbit == ((size * 8) % 5));
721 if (wpos >= out_size)
726 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
729 if (wpos != out_size)
734 GNUNET_assert (vbit == 0);
740 * Convert ASCII encoding back to data
741 * out_size must match exactly the size of the data before it was encoded.
743 * @param enc the encoding
744 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
745 * @param out location where to store the decoded data
746 * @param out_size sizeof the output buffer
747 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
750 GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
751 unsigned char *out, size_t out_size)
759 int encoded_len = out_size * 8;
760 if (encoded_len % 5 > 0)
762 vbit = encoded_len % 5; /* padding! */
770 if ((encoded_len + shift) / 5 != enclen)
771 return GNUNET_SYSERR;
775 bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
777 return GNUNET_SYSERR;
780 GNUNET_assert (rpos > 0);
781 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
783 return GNUNET_SYSERR;
787 out[--wpos] = (unsigned char) bits;
792 GNUNET_assert (rpos == 0);
793 GNUNET_assert (vbit == 0);
799 * Parse a path that might be an URI.
801 * @param path path to parse. Must be NULL-terminated.
802 * @param scheme_part a pointer to 'char *' where a pointer to a string that
803 * represents the URI scheme will be stored. Can be NULL. The string is
804 * allocated by the function, and should be freed by GNUNET_free() when
805 * it is no longer needed.
806 * @param path_part a pointer to 'const char *' where a pointer to the path
807 * part of the URI will be stored. Can be NULL. Points to the same block
808 * of memory as 'path', and thus must not be freed. Might point to '\0',
809 * if path part is zero-length.
810 * @return GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not
811 * an URI, '* scheme_part' and '*path_part' will remain unchanged
812 * (if they weren't NULL).
815 GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part,
816 const char **path_part)
821 const char *post_scheme_part = NULL;
823 for (end = 0, i = 0; !end && i < len; i++)
828 if (path[i] == ':' && i > 0)
833 if (!((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z')
834 || (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || path[i] == '-'
835 || (path[i] == '.')))
848 post_scheme_part = &path[i];
855 if (post_scheme_part == NULL)
859 *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
860 memcpy (*scheme_part, path, post_scheme_part - path);
861 (*scheme_part)[post_scheme_part - path] = '\0';
864 *path_part = post_scheme_part;
870 * Check whether 'filename' is absolute or not, and if it's an URI
872 * @param filename filename to check
873 * @param can_be_uri GNUNET_YES to check for being URI, GNUNET_NO - to
874 * assume it's not URI
875 * @param r_is_uri a pointer to an int that is set to GNUNET_YES if 'filename'
876 * is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
877 * not GNUNET_YES, *r_is_uri is set to GNUNET_NO.
878 * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
879 * The string is allocated by the function, and should be freed with
880 * GNUNET_free (). Can be NULL.
881 * @return GNUNET_YES if 'filename' is absolute, GNUNET_NO otherwise.
884 GNUNET_STRINGS_path_is_absolute (const char *filename, int can_be_uri,
885 int *r_is_uri, char **r_uri_scheme)
890 const char *post_scheme_path;
893 /* consider POSIX paths to be absolute too, even on W32,
894 * as plibc expansion will fix them for us.
896 if (filename[0] == '/')
900 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
908 GNUNET_free_non_null (uri);
910 len = strlen(post_scheme_path);
911 /* Special check for file:///c:/blah
912 * We want to parse 'c:/', not '/c:/'
914 if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':')
915 post_scheme_path = &post_scheme_path[1];
917 return GNUNET_STRINGS_path_is_absolute (post_scheme_path, GNUNET_NO, NULL, NULL);
924 *r_is_uri = GNUNET_NO;
927 len = strlen (filename);
929 ((filename[0] >= 'A' && filename[0] <= 'Z')
930 || (filename[0] >= 'a' && filename[0] <= 'z'))
931 && filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\'))
938 #define _IFMT 0170000 /* type of file */
939 #define _IFLNK 0120000 /* symbolic link */
940 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
944 * Perform 'checks' on 'filename'
946 * @param filename file to check
947 * @param checks checks to perform
948 * @return GNUNET_YES if all checks pass, GNUNET_NO if at least one of them
949 * fails, GNUNET_SYSERR when a check can't be performed
952 GNUNET_STRINGS_check_filename (const char *filename,
953 enum GNUNET_STRINGS_FilenameCheck checks)
956 if (filename == NULL || filename[0] == '\0')
957 return GNUNET_SYSERR;
958 if (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)
959 if (!GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
961 if (checks & (GNUNET_STRINGS_CHECK_EXISTS
962 | GNUNET_STRINGS_CHECK_IS_DIRECTORY
963 | GNUNET_STRINGS_CHECK_IS_LINK))
965 if (STAT (filename, &st))
967 if (checks & GNUNET_STRINGS_CHECK_EXISTS)
970 return GNUNET_SYSERR;
973 if (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)
974 if (!S_ISDIR (st.st_mode))
976 if (checks & GNUNET_STRINGS_CHECK_IS_LINK)
977 if (!S_ISLNK (st.st_mode))
982 #define MAX_IPV6_ADDRLEN 47
983 #define MAX_IPV4_ADDRLEN 21
984 #define MAX_IP_ADDRLEN MAX_IPV6_ADDRLEN
988 * Tries to convert 'zt_addr' string to an IPv6 address.
990 * @param zt_addr 0-terminated string. May be mangled by the function.
991 * @param addrlen length of zt_addr (not counting 0-terminator).
992 * @param r_buf a buffer to fill. Initially gets filled with zeroes,
993 * then its sin6_port, sin6_family and sin6_addr are set appropriately.
994 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
995 * case the contents of r_buf are undefined.
998 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1000 struct sockaddr_in6 *r_buf)
1007 return GNUNET_SYSERR;
1009 port_colon = strrchr (zt_addr, ':');
1010 if (port_colon == NULL)
1011 return GNUNET_SYSERR;
1012 ret = SSCANF (port_colon, ":%u", &port);
1013 if (ret != 1 || port > 65535)
1014 return GNUNET_SYSERR;
1015 port_colon[0] = '\0';
1016 memset (r_buf, 0, sizeof (struct sockaddr_in6));
1017 ret = inet_pton (AF_INET6, zt_addr, &r_buf->sin6_addr);
1019 return GNUNET_SYSERR;
1020 r_buf->sin6_port = htons (port);
1021 r_buf->sin6_family = AF_INET6;
1027 * Tries to convert 'zt_addr' string to an IPv4 address.
1029 * @param zt_addr 0-terminated string. May be mangled by the function.
1030 * @param addrlen length of zt_addr (not counting 0-terminator).
1031 * @param r_buf a buffer to fill.
1032 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case
1033 * the contents of r_buf are undefined.
1036 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
1037 struct sockaddr_in *r_buf)
1039 unsigned int temps[5];
1044 return GNUNET_SYSERR;
1046 cnt = SSCANF (zt_addr, "%u.%u.%u.%u:%u", &temps[0], &temps[1], &temps[2], &temps[3], &port);
1048 return GNUNET_SYSERR;
1050 for (cnt = 0; cnt < 4; cnt++)
1051 if (temps[cnt] > 0xFF)
1052 return GNUNET_SYSERR;
1054 return GNUNET_SYSERR;
1058 r_buf->sin_family = AF_INET;
1059 r_buf->sin_port = htons (port);
1060 r_buf->sin_addr.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
1061 (temps[2] << 8) + temps[3]);
1066 * Tries to convert 'addr' string to an IP (v4 or v6) address.
1067 * IPv6 address must have its address part enclosed in '()' parens
1069 * Will automatically decide whether to treat 'addr' as v4 or v6 address.
1071 * @param addr a string, may not be 0-terminated.
1072 * @param addrlen number of bytes in addr (if addr is 0-terminated,
1073 * 0-terminator should not be counted towards addrlen).
1074 * @param r_buf a buffer to fill.
1075 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
1076 * case the contents of r_buf are undefined.
1079 GNUNET_STRINGS_to_address_ip (const char *addr,
1081 struct sockaddr_storage *r_buf)
1084 char zt_addr[MAX_IP_ADDRLEN + 1];
1085 uint16_t zt_len = addrlen <= MAX_IP_ADDRLEN ? addrlen : MAX_IP_ADDRLEN;
1088 return GNUNET_SYSERR;
1090 memset (zt_addr, 0, MAX_IP_ADDRLEN + 1);
1091 strncpy (zt_addr, addr, zt_len);
1093 /* For URIs we use '(' and ')' instead of '[' and ']'. Do the substitution
1094 * now, as GNUNET_STRINGS_to_address_ipv6() takes a proper []-enclosed IPv6
1097 if (zt_addr[0] == '(')
1099 for (i = 0; i < zt_len; i++)
1113 return GNUNET_STRINGS_to_address_ipv6 (zt_addr, zt_len, (struct sockaddr_in6 *) r_buf);
1115 return GNUNET_STRINGS_to_address_ipv4 (zt_addr, zt_len, (struct sockaddr_in *) r_buf);
1118 /* end of strings.c */