aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2024-01-09 19:49:09 +0100
committerChristian Grothoff <christian@grothoff.org>2024-01-09 19:49:09 +0100
commit9746a780b7804ae88fa8a2bd9c17661054e1081e (patch)
tree61e109277c405e279a5bd4905c50f019ca63b2c8
parent9ef19ec60337441212d70965549416eecffd4576 (diff)
downloadgnunet-9746a780b7804ae88fa8a2bd9c17661054e1081e.tar.gz
gnunet-9746a780b7804ae88fa8a2bd9c17661054e1081e.zip
fix #8050 (plus more code cleanup)
-rw-r--r--src/lib/util/strings.c68
1 files changed, 40 insertions, 28 deletions
diff --git a/src/lib/util/strings.c b/src/lib/util/strings.c
index 7751c7aa9..7039496f7 100644
--- a/src/lib/util/strings.c
+++ b/src/lib/util/strings.c
@@ -1478,20 +1478,13 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1478struct GNUNET_STRINGS_IPv6NetworkPolicy * 1478struct GNUNET_STRINGS_IPv6NetworkPolicy *
1479GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) 1479GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1480{ 1480{
1481 unsigned int count; 1481 size_t count;
1482 unsigned int i; 1482 size_t len;
1483 unsigned int len; 1483 size_t pos;
1484 unsigned int pos;
1485 int start;
1486 int slash;
1487 int ret; 1484 int ret;
1488 char *routeList; 1485 char *routeList;
1489 struct GNUNET_STRINGS_IPv6NetworkPolicy *result; 1486 struct GNUNET_STRINGS_IPv6NetworkPolicy *result;
1490 unsigned int bits;
1491 unsigned int off; 1487 unsigned int off;
1492 int save;
1493 int colon;
1494 char dummy[2];
1495 1488
1496 if (NULL == routeListX) 1489 if (NULL == routeListX)
1497 return NULL; 1490 return NULL;
@@ -1500,8 +1493,8 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1500 return NULL; 1493 return NULL;
1501 routeList = GNUNET_strdup (routeListX); 1494 routeList = GNUNET_strdup (routeListX);
1502 count = 0; 1495 count = 0;
1503 for (i = 0; i < len; i++) 1496 for (size_t j = 0; j < len; j++)
1504 if (';' == routeList[i]) 1497 if (';' == routeList[j])
1505 count++; 1498 count++;
1506 if (';' != routeList[len - 1]) 1499 if (';' != routeList[len - 1])
1507 { 1500 {
@@ -1511,27 +1504,34 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1511 GNUNET_free (routeList); 1504 GNUNET_free (routeList);
1512 return NULL; 1505 return NULL;
1513 } 1506 }
1514 1507 GNUNET_assert (count < UINT_MAX);
1515 result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy) 1508 result = GNUNET_new_array (count + 1,
1516 * (count + 1)); 1509 struct GNUNET_STRINGS_IPv6NetworkPolicy);
1517 i = 0;
1518 pos = 0; 1510 pos = 0;
1519 while (i < count) 1511 for (size_t i = 0; i < count; i++)
1520 { 1512 {
1513 size_t start;
1514 size_t slash;
1515
1521 start = pos; 1516 start = pos;
1522 while (';' != routeList[pos]) 1517 while (';' != routeList[pos])
1523 pos++; 1518 pos++;
1524 slash = pos; 1519 slash = pos;
1525 while ((slash >= start) && (routeList[slash] != '/')) 1520 while ( (slash >= start) &&
1521 (routeList[slash] != '/') )
1526 slash--; 1522 slash--;
1527 1523
1528 if (slash < start) 1524 if (slash < start)
1529 { 1525 {
1530 memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr)); 1526 memset (&result[i].netmask,
1527 0xFF,
1528 sizeof(struct in6_addr));
1531 slash = pos; 1529 slash = pos;
1532 } 1530 }
1533 else 1531 else
1534 { 1532 {
1533 size_t colon;
1534
1535 routeList[pos] = '\0'; 1535 routeList[pos] = '\0';
1536 for (colon = pos; ':' != routeList[colon]; colon--) 1536 for (colon = pos; ':' != routeList[colon]; colon--)
1537 if ('/' == routeList[colon]) 1537 if ('/' == routeList[colon])
@@ -1540,19 +1540,28 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1540 { 1540 {
1541 routeList[colon] = '\0'; 1541 routeList[colon] = '\0';
1542 if (GNUNET_OK != 1542 if (GNUNET_OK !=
1543 parse_port_policy (&routeList[colon + 1], &result[i].pp)) 1543 parse_port_policy (&routeList[colon + 1],
1544 &result[i].pp))
1544 { 1545 {
1545 GNUNET_free (result); 1546 GNUNET_free (result);
1546 GNUNET_free (routeList); 1547 GNUNET_free (routeList);
1547 return NULL; 1548 return NULL;
1548 } 1549 }
1549 } 1550 }
1550 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); 1551 ret = inet_pton (AF_INET6,
1552 &routeList[slash + 1],
1553 &result[i].netmask);
1551 if (ret <= 0) 1554 if (ret <= 0)
1552 { 1555 {
1553 save = errno; 1556 char dummy;
1554 if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) || 1557 unsigned int bits;
1555 (bits > 128)) 1558 int save = errno;
1559
1560 if ( (1 != sscanf (&routeList[slash + 1],
1561 "%u%c",
1562 &bits,
1563 &dummy)) ||
1564 (bits > 128) )
1556 { 1565 {
1557 if (0 == ret) 1566 if (0 == ret)
1558 LOG (GNUNET_ERROR_TYPE_WARNING, 1567 LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -1561,7 +1570,8 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1561 else 1570 else
1562 { 1571 {
1563 errno = save; 1572 errno = save;
1564 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton"); 1573 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1574 "inet_pton");
1565 } 1575 }
1566 GNUNET_free (result); 1576 GNUNET_free (result);
1567 GNUNET_free (routeList); 1577 GNUNET_free (routeList);
@@ -1582,7 +1592,9 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1582 } 1592 }
1583 } 1593 }
1584 routeList[slash] = '\0'; 1594 routeList[slash] = '\0';
1585 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); 1595 ret = inet_pton (AF_INET6,
1596 &routeList[start],
1597 &result[i].network);
1586 if (ret <= 0) 1598 if (ret <= 0)
1587 { 1599 {
1588 if (0 == ret) 1600 if (0 == ret)
@@ -1590,13 +1602,13 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1590 _ ("Wrong format `%s' for network\n"), 1602 _ ("Wrong format `%s' for network\n"),
1591 &routeList[slash + 1]); 1603 &routeList[slash + 1]);
1592 else 1604 else
1593 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); 1605 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1606 "inet_pton");
1594 GNUNET_free (result); 1607 GNUNET_free (result);
1595 GNUNET_free (routeList); 1608 GNUNET_free (routeList);
1596 return NULL; 1609 return NULL;
1597 } 1610 }
1598 pos++; 1611 pos++;
1599 i++;
1600 } 1612 }
1601 GNUNET_free (routeList); 1613 GNUNET_free (routeList);
1602 return result; 1614 return result;