aboutsummaryrefslogtreecommitdiff
path: root/src/util/strings.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-11-27 14:43:02 +0100
committerChristian Grothoff <christian@grothoff.org>2016-11-27 14:43:02 +0100
commitc30f9fe5daf92a89849f7d6d22eacb603918b42d (patch)
tree70b07926e1170c5b19a3d09a773ec2c1422d84df /src/util/strings.c
parentb035390c73b241ddb513f420671043e9113c237c (diff)
downloadgnunet-c30f9fe5daf92a89849f7d6d22eacb603918b42d.tar.gz
gnunet-c30f9fe5daf92a89849f7d6d22eacb603918b42d.zip
largely completing gnunet-nat tool, using new service API (but untested)
Diffstat (limited to 'src/util/strings.c')
-rw-r--r--src/util/strings.c78
1 files changed, 72 insertions, 6 deletions
diff --git a/src/util/strings.c b/src/util/strings.c
index 6a6cad6fe..46eab856f 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -1209,7 +1209,7 @@ GNUNET_STRINGS_check_filename (const char *filename,
1209 1209
1210 1210
1211/** 1211/**
1212 * Tries to convert 'zt_addr' string to an IPv6 address. 1212 * Tries to convert @a zt_addr string to an IPv6 address.
1213 * The string is expected to have the format "[ABCD::01]:80". 1213 * The string is expected to have the format "[ABCD::01]:80".
1214 * 1214 *
1215 * @param zt_addr 0-terminated string. May be mangled by the function. 1215 * @param zt_addr 0-terminated string. May be mangled by the function.
@@ -1292,7 +1292,8 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1292 * the contents of @a r_buf are undefined. 1292 * the contents of @a r_buf are undefined.
1293 */ 1293 */
1294int 1294int
1295GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen, 1295GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
1296 uint16_t addrlen,
1296 struct sockaddr_in *r_buf) 1297 struct sockaddr_in *r_buf)
1297{ 1298{
1298 unsigned int temps[4]; 1299 unsigned int temps[4];
@@ -1301,7 +1302,13 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
1301 1302
1302 if (addrlen < 9) 1303 if (addrlen < 9)
1303 return GNUNET_SYSERR; 1304 return GNUNET_SYSERR;
1304 cnt = SSCANF (zt_addr, "%u.%u.%u.%u:%u", &temps[0], &temps[1], &temps[2], &temps[3], &port); 1305 cnt = SSCANF (zt_addr,
1306 "%u.%u.%u.%u:%u",
1307 &temps[0],
1308 &temps[1],
1309 &temps[2],
1310 &temps[3],
1311 &port);
1305 if (5 != cnt) 1312 if (5 != cnt)
1306 return GNUNET_SYSERR; 1313 return GNUNET_SYSERR;
1307 for (cnt = 0; cnt < 4; cnt++) 1314 for (cnt = 0; cnt < 4; cnt++)
@@ -1328,8 +1335,8 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
1328 * @param addrlen number of bytes in @a addr (if addr is 0-terminated, 1335 * @param addrlen number of bytes in @a addr (if addr is 0-terminated,
1329 * 0-terminator should not be counted towards addrlen). 1336 * 0-terminator should not be counted towards addrlen).
1330 * @param r_buf a buffer to fill. 1337 * @param r_buf a buffer to fill.
1331 * @return #GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which 1338 * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
1332 * case the contents of r_buf are undefined. 1339 * case the contents of @a r_buf are undefined.
1333 */ 1340 */
1334int 1341int
1335GNUNET_STRINGS_to_address_ip (const char *addr, 1342GNUNET_STRINGS_to_address_ip (const char *addr,
@@ -1347,6 +1354,62 @@ GNUNET_STRINGS_to_address_ip (const char *addr,
1347 1354
1348 1355
1349/** 1356/**
1357 * Parse an address given as a string into a
1358 * `struct sockaddr`.
1359 *
1360 * @param addr the address
1361 * @param[out] af set to the parsed address family (i.e. AF_INET)
1362 * @param[out] sa set to the parsed address
1363 * @return 0 on error, otherwise number of bytes in @a sa
1364 */
1365size_t
1366GNUNET_STRINGS_parse_socket_addr (const char *addr,
1367 uint8_t *af,
1368 struct sockaddr **sa)
1369{
1370 char *cp = GNUNET_strdup (addr);
1371
1372 *af = AF_UNSPEC;
1373 if ('[' == *addr)
1374 {
1375 /* IPv6 */
1376 *sa = GNUNET_malloc (sizeof (struct sockaddr_in6));
1377 if (GNUNET_OK !=
1378 GNUNET_STRINGS_to_address_ipv6 (cp,
1379 strlen (cp),
1380 (struct sockaddr_in6 *) *sa))
1381 {
1382 GNUNET_free (*sa);
1383 *sa = NULL;
1384 GNUNET_free (cp);
1385 return 0;
1386 }
1387 *af = AF_INET6;
1388 GNUNET_free (cp);
1389 return sizeof (struct sockaddr_in6);
1390 }
1391 else
1392 {
1393 /* IPv4 */
1394 *sa = GNUNET_malloc (sizeof (struct sockaddr_in));
1395 if (GNUNET_OK !=
1396 GNUNET_STRINGS_to_address_ipv4 (cp,
1397 strlen (cp),
1398 (struct sockaddr_in *) *sa))
1399 {
1400 GNUNET_free (*sa);
1401 *sa = NULL;
1402 GNUNET_free (cp);
1403 return 0;
1404 }
1405 *af = AF_INET;
1406 GNUNET_free (cp);
1407 return sizeof (struct sockaddr_in);
1408 }
1409}
1410
1411
1412/**
1350 * Makes a copy of argv that consists of a single memory chunk that can be 1413 * Makes a copy of argv that consists of a single memory chunk that can be
1351 * freed with a single call to GNUNET_free(); 1414 * freed with a single call to GNUNET_free();
1352 */ 1415 */
@@ -1388,7 +1451,10 @@ _make_continuous_arg_copy (int argc,
1388 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 1451 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1389 */ 1452 */
1390int 1453int
1391GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv) 1454GNUNET_STRINGS_get_utf8_args (int argc,
1455 char *const *argv,
1456 int *u8argc,
1457 char *const **u8argv)
1392{ 1458{
1393#if WINDOWS 1459#if WINDOWS
1394 wchar_t *wcmd; 1460 wchar_t *wcmd;