1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
|
/* SPDX-FileCopyrightText: 2013-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/**
* @file
* @brief Implementation of an API to handle Hosts objects
*
* This file contains all methods to handle Hosts collections (gvm_hosts_t)
* and single hosts objects (gvm_host_t.)
*
* The module consequently uses glib datatypes.
*/
#include "hosts.h"
#include "networking.h" /* for ipv4_as_ipv6, addr6_as_str, gvm_resolve */
#include <arpa/inet.h> /* for inet_pton, inet_ntop */
#include <assert.h> /* for assert */
#include <ctype.h> /* for isdigit */
#include <malloc.h>
#include <netdb.h> /* for getnameinfo, NI_NAMEREQD */
#include <stdint.h> /* for uint8_t, uint32_t */
#include <stdio.h> /* for sscanf, perror */
#include <stdlib.h> /* for strtol, atoi */
#include <string.h> /* for strchr, memcpy, memcmp, bzero, strcasecmp */
#include <sys/socket.h> /* for AF_INET, AF_INET6, sockaddr */
#include <unistd.h> /* for usleep() */
#undef G_LOG_DOMAIN
/**
* @brief GLib log domain.
*/
#define G_LOG_DOMAIN "libgvm base"
/* Static variables */
gchar *host_type_str[HOST_TYPE_MAX] = {
[HOST_TYPE_NAME] = "Hostname",
[HOST_TYPE_IPV4] = "IPv4",
[HOST_TYPE_IPV6] = "IPv6",
[HOST_TYPE_CIDR_BLOCK] = "IPv4 CIDR block",
[HOST_TYPE_RANGE_SHORT] = "IPv4 short range",
[HOST_TYPE_RANGE_LONG] = "IPv4 long range"};
/* Function definitions */
/**
* @brief Checks if a buffer points to a valid IPv4 address.
* "192.168.11.1" is valid, "192.168.1.300" and "192.168.1.1e" are not.
*
* @param[in] str Buffer to check in.
*
* @return 1 if valid IPv4 address, 0 otherwise.
*/
static int
is_ipv4_address (const char *str)
{
struct sockaddr_in sa;
return inet_pton (AF_INET, str, &(sa.sin_addr)) == 1;
}
/**
* @brief Checks if a buffer points to a valid IPv6 address.
* "0:0:0:0:0:0:0:1", "::1" and "::FFFF:192.168.13.55" are valid "::1g" is not.
*
* @param[in] str Buffer to check in.
*
* @return 1 if valid IPv6 address, 0 otherwise.
*/
static int
is_ipv6_address (const char *str)
{
struct sockaddr_in6 sa6;
return inet_pton (AF_INET6, str, &(sa6.sin6_addr)) == 1;
}
/**
* @brief Checks if a buffer points to an IPv4 CIDR-expressed block.
* "192.168.12.3/24" is valid, "192.168.1.3/31" is not.
*
* @param[in] str Buffer to check in.
*
* @return 1 if valid CIDR-expressed block, 0 otherwise.
*/
static int
is_cidr_block (const char *str)
{
long block;
char *addr_str, *block_str, *p;
addr_str = g_strdup (str);
block_str = strchr (addr_str, '/');
if (block_str == NULL)
{
g_free (addr_str);
return 0;
}
/* Separate the address from the block value. */
*block_str = '\0';
block_str++;
if (!is_ipv4_address (addr_str) || !isdigit (*block_str))
{
g_free (addr_str);
return 0;
}
p = NULL;
block = strtol (block_str, &p, 10);
if (*p || block <= 0 || block > 30)
{
g_free (addr_str);
return 0;
}
g_free (addr_str);
return 1;
}
/**
* @brief Gets the network block value from a CIDR-expressed block string.
* For "192.168.1.1/24" it is 24.
*
* @param[in] str Buffer containing CIDR-expressed block.
* @param[out] block Variable to store block value.
*
* @return -1 if error, 0 otherwise.
*/
static int
cidr_get_block (const char *str, unsigned int *block)
{
if (str == NULL || block == NULL)
return -1;
if (sscanf (str, "%*[0-9.]/%2u", block) != 1)
return -1;
return 0;
}
/**
* @brief Gets the IPv4 value from a CIDR-expressed block.
* eg. For "192.168.1.10/24" it is "192.168.1.10".
*
* @param[in] str String containing CIDR-expressed block.
* @param[out] addr Variable to store the IPv4 address value.
*
* @return -1 if error, 0 otherwise.
*/
static int
cidr_get_ip (const char *str, struct in_addr *addr)
{
gchar *addr_str, *tmp;
if (str == NULL || addr == NULL)
return -1;
addr_str = g_strdup (str);
tmp = strchr (addr_str, '/');
if (tmp == NULL)
{
g_free (addr_str);
return -1;
}
*tmp = '\0';
if (inet_pton (AF_INET, addr_str, addr) != 1)
return -1;
g_free (addr_str);
return 0;
}
/**
* @brief Gets the first and last usable IPv4 addresses from a CIDR-expressed
* block. eg. "192.168.1.0/24" would give 192.168.1.1 as first and 192.168.1.254
* as last.
*
* Both network and broadcast addresses are skipped:
* - They are _never_ used as a host address. Not being included is the expected
* behaviour from users.
* - When needed, short/long ranges (eg. 192.168.1.0-255) are available.
*
* @param[in] str Buffer containing CIDR-expressed block.
* @param[out] first First IPv4 address in block.
* @param[out] last Last IPv4 address in block.
*
* @return -1 if error, 0 else.
*/
static int
cidr_block_ips (const char *str, struct in_addr *first, struct in_addr *last)
{
unsigned int block;
if (str == NULL || first == NULL || last == NULL)
return -1;
/* Get IP and block values. */
if (cidr_get_block (str, &block) == -1)
return -1;
if (cidr_get_ip (str, first) == -1)
return -1;
/* First IP: And with mask and increment. */
first->s_addr &= htonl (0xffffffff ^ ((1 << (32 - block)) - 1));
first->s_addr = htonl (ntohl (first->s_addr) + 1);
/* Last IP: First IP + Number of usable hosts - 1. */
last->s_addr = htonl (ntohl (first->s_addr) + (1 << (32 - block)) - 3);
return 0;
}
/**
* @brief Checks if a buffer points to a valid long range-expressed network.
* "192.168.12.1-192.168.13.50" is valid.
*
* @param[in] str Buffer to check in.
*
* @return 1 if valid long range-expressed network, 0 otherwise.
*/
static int
is_long_range_network (const char *str)
{
char *first_str, *second_str;
int ret;
first_str = g_strdup (str);
second_str = strchr (first_str, '-');
if (second_str == NULL)
{
g_free (first_str);
return 0;
}
/* Separate the addresses. */
*second_str = '\0';
second_str++;
ret = is_ipv4_address (first_str) && is_ipv4_address (second_str);
g_free (first_str);
return ret;
}
/**
* @brief Gets the first and last IPv4 addresses from a long range-expressed
* network. eg. "192.168.1.1-192.168.2.40" would give 192.168.1.1 as first and
* 192.168.2.40 as last.
*
* @param[in] str String containing long range-expressed network.
* @param[out] first First IP address in block.
* @param[out] last Last IP address in block.
*
* @return -1 if error, 0 else.
*/
static int
long_range_network_ips (const char *str, struct in_addr *first,
struct in_addr *last)
{
char *first_str, *last_str;
if (str == NULL || first == NULL || last == NULL)
return -1;
first_str = g_strdup (str);
last_str = strchr (first_str, '-');
if (last_str == NULL)
{
g_free (first_str);
return -1;
}
/* Separate the two IPs. */
*last_str = '\0';
last_str++;
if (inet_pton (AF_INET, first_str, first) != 1
|| inet_pton (AF_INET, last_str, last) != 1)
{
g_free (first_str);
return -1;
}
g_free (first_str);
return 0;
}
/**
* @brief Checks if a buffer points to a valid short range-expressed network.
* "192.168.11.1-50" is valid, "192.168.1.1-50e" and "192.168.1.1-300" are not.
*
* @param str String to check in.
*
* @return 1 if str points to a valid short range-network, 0 otherwise.
*/
static int
is_short_range_network (const char *str)
{
long end;
char *ip_str, *end_str, *p;
ip_str = g_strdup (str);
end_str = strchr (ip_str, '-');
if (end_str == NULL)
{
g_free (ip_str);
return 0;
}
/* Separate the addresses. */
*end_str = '\0';
end_str++;
if (!is_ipv4_address (ip_str) || !isdigit (*end_str))
{
g_free (ip_str);
return 0;
}
p = NULL;
end = strtol (end_str, &p, 10);
if (*p || end < 0 || end > 255)
{
g_free (ip_str);
return 0;
}
g_free (ip_str);
return 1;
}
/**
* @brief Gets the first and last IPv4 addresses from a short range-expressed
* network. "192.168.1.1-40" would give 192.168.1.1 as first and 192.168.1.40 as
* last.
*
* @param[in] str String containing short range-expressed network.
* @param[out] first First IP address in block.
* @param[out] last Last IP address in block.
*
* @return -1 if error, 0 else.
*/
static int
short_range_network_ips (const char *str, struct in_addr *first,
struct in_addr *last)
{
char *first_str, *last_str;
int end;
if (str == NULL || first == NULL || last == NULL)
return -1;
first_str = g_strdup (str);
last_str = strchr (first_str, '-');
if (last_str == NULL)
{
g_free (first_str);
return -1;
}
/* Separate the two IPs. */
*last_str = '\0';
last_str++;
end = atoi (last_str);
/* Get the first IP */
if (inet_pton (AF_INET, first_str, first) != 1)
{
g_free (first_str);
return -1;
}
/* Get the last IP */
last->s_addr = htonl ((ntohl (first->s_addr) & 0xffffff00) + end);
g_free (first_str);
return 0;
}
/**
* @brief Checks if a buffer points to a valid hostname.
*
* @param[in] str String to check.
*
* @return 1 if valid hostname, 0 otherwise.
*/
static int
is_hostname (const char *str)
{
gchar *copy, **point, **split;
/* From
* https://stackoverflow.com/questions/2532053/validate-a-hostname-string. */
/* Remove one dot from the end. */
copy = g_strdup (str);
if (copy[strlen (copy) - 1] == '.')
copy[strlen (copy) - 1] = '\0';
/* Check length. */
if (strlen (copy) == 0 || strlen (copy) > 253)
{
g_free (copy);
return 0;
}
/* Split on dots. */
point = split = g_strsplit (copy, ".", 0);
g_free (copy);
/* Last part (TLD) may not be an integer. */
if (*point)
{
gchar *last;
while (*(point + 1))
point++;
last = *point;
if (strlen (last))
{
while (*last && isdigit (*last))
last++;
if (*last == '\0')
{
g_strfreev (split);
return 0;
}
}
}
/* Check each part. */
point = split;
while (*point)
if (g_regex_match_simple ("^(?!-)[a-z0-9_-]{1,63}(?<!-)$", *point,
G_REGEX_CASELESS, 0)
== 0)
{
g_strfreev (split);
return 0;
}
else
point++;
g_strfreev (split);
return 1;
}
/**
* @brief Checks if a buffer points to an IPv6 CIDR-expressed block.
* "2620:0:2d0:200::7/120" is valid, "2620:0:2d0:200::7/129" is not.
*
* @param[in] str Buffer to check in.
*
* @return 1 if valid IPv6 CIDR-expressed block, 0 otherwise.
*/
static int
is_cidr6_block (const char *str)
{
long block;
char *addr6_str, *block_str, *p;
addr6_str = g_strdup (str);
block_str = strchr (addr6_str, '/');
if (block_str == NULL)
{
g_free (addr6_str);
return 0;
}
/* Separate the address from the block value. */
*block_str = '\0';
block_str++;
if (!is_ipv6_address (addr6_str) || !isdigit (*block_str))
{
g_free (addr6_str);
return 0;
}
p = NULL;
block = strtol (block_str, &p, 10);
if (*p || block <= 0 || block > 128)
{
g_free (addr6_str);
return 0;
}
g_free (addr6_str);
return 1;
}
/**
* @brief Gets the network block value from a CIDR-expressed block string.
* For "192.168.1.1/24" it is 24.
*
* @param[in] str Buffer containing CIDR-expressed block.
* @param[out] block Variable to store block value.
*
* @return -1 if error, 0 otherwise.
*/
static int
cidr6_get_block (const char *str, unsigned int *block)
{
if (str == NULL || block == NULL)
return -1;
if (sscanf (str, "%*[0-9a-fA-F.:]/%3u", block) != 1)
return -1;
return 0;
}
/**
* @brief Gets the IPv4 value from a CIDR-expressed block.
* eg. For "192.168.1.10/24" it is "192.168.1.10".
*
* @param[in] str String containing CIDR-expressed block.
* @param[out] addr6 Variable to store the IPv4 address value.
*
* @return -1 if error, 0 otherwise.
*/
static int
cidr6_get_ip (const char *str, struct in6_addr *addr6)
{
gchar *addr6_str, *tmp;
if (str == NULL || addr6 == NULL)
return -1;
addr6_str = g_strdup (str);
tmp = strchr (addr6_str, '/');
if (tmp == NULL)
{
g_free (addr6_str);
return -1;
}
*tmp = '\0';
if (inet_pton (AF_INET6, addr6_str, addr6) != 1)
return -1;
g_free (addr6_str);
return 0;
}
/**
* @brief Gets the first and last usable IPv4 addresses from a CIDR-expressed
* block. eg. "192.168.1.0/24 would give 192.168.1.1 as first and 192.168.1.254
* as last. Thus, it skips the network and broadcast addresses.
*
* @param[in] str Buffer containing CIDR-expressed block.
* @param[out] first First IPv4 address in block.
* @param[out] last Last IPv4 address in block.
*
* @return -1 if error, 0 else.
*/
static int
cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
{
unsigned int block;
int i, j;
if (str == NULL || first == NULL || last == NULL)
return -1;
/* Get IP and block values. */
if (cidr6_get_block (str, &block) == -1)
return -1;
if (cidr6_get_ip (str, first) == -1)
return -1;
memcpy (&last->s6_addr, &first->s6_addr, 16);
/* /128 => Specified address is the first and last one. */
if (block == 128)
return 0;
/* First IP: And with mask and increment to skip network address. */
j = 15;
for (i = (128 - block) / 8; i > 0; i--)
{
first->s6_addr[j] = 0;
j--;
}
first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
/* Last IP: Broadcast address - 1. */
j = 15;
for (i = (128 - block) / 8; i > 0; i--)
{
last->s6_addr[j] = 0xff;
j--;
}
last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
/* /127 => Only two addresses. Don't skip network / broadcast addresses.*/
if (block == 127)
return 0;
/* Increment first IP. */
for (i = 15; i >= 0; --i)
if (first->s6_addr[i] < 255)
{
first->s6_addr[i]++;
break;
}
else
first->s6_addr[i] = 0;
/* Decrement last IP. */
for (i = 15; i >= 0; --i)
if (last->s6_addr[i] > 0)
{
last->s6_addr[i]--;
break;
}
else
last->s6_addr[i] = 0xff;
return 0;
}
/**
* @brief Checks if a buffer points to a valid long IPv6 range-expressed
* network. "::fee5-::1:530" is valid.
*
* @param[in] str Buffer to check in.
*
* @return 1 if valid long range-expressed network, 0 otherwise.
*/
static int
is_long_range6_network (const char *str)
{
char *first_str, *second_str;
int ret;
first_str = g_strdup (str);
second_str = strchr (first_str, '-');
if (second_str == NULL)
{
g_free (first_str);
return 0;
}
/* Separate the addresses. */
*second_str = '\0';
second_str++;
ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
g_free (first_str);
return ret;
}
/**
* @brief Gets the first and last IPv6 addresses from a long range-expressed
* network. eg. "::1:200:7-::1:205:500" would give ::1:200:7 as first and
* ::1:205:500 as last.
*
* @param[in] str String containing long IPv6 range-expressed network.
* @param[out] first First IPv6 address in range.
* @param[out] last Last IPv6 address in range.
*
* @return -1 if error, 0 else.
*/
static int
long_range6_network_ips (const char *str, struct in6_addr *first,
struct in6_addr *last)
{
char *first_str, *last_str;
if (str == NULL || first == NULL || last == NULL)
return -1;
first_str = g_strdup (str);
last_str = strchr (first_str, '-');
if (last_str == NULL)
{
g_free (first_str);
return -1;
}
/* Separate the two IPs. */
*last_str = '\0';
last_str++;
if (inet_pton (AF_INET6, first_str, first) != 1
|| inet_pton (AF_INET6, last_str, last) != 1)
{
g_free (first_str);
return -1;
}
g_free (first_str);
return 0;
}
/**
* @brief Checks if a buffer points to a valid short IPv6 range-expressed
* network. "::200:ff:1-fee5" is valid.
*
* @param str String to check in.
*
* @return 1 if str points to a valid short-range IPv6 network, 0 otherwise.
*/
static int
is_short_range6_network (const char *str)
{
char *ip_str, *end_str, *p;
ip_str = g_strdup (str);
end_str = strchr (ip_str, '-');
if (end_str == NULL)
{
g_free (ip_str);
return 0;
}
/* Separate the addresses. */
*end_str = '\0';
end_str++;
if (!is_ipv6_address (ip_str) || *end_str == '\0')
{
g_free (ip_str);
return 0;
}
p = end_str;
/* Check that the 2nd part is at most 4 hexadecimal characters. */
while (isxdigit (*p) && p++)
;
if (*p || p - end_str > 4)
{
g_free (ip_str);
return 0;
}
g_free (ip_str);
return 1;
}
/**
* @brief Gets the first and last IPv6 addresses from a short range-expressed
* network. eg. "\::ffee:1:1001-1005" would give \::ffee:1:1001 as first and
* \::ffee:1:1005 as last.
*
* @param[in] str String containing short IPv6 range-expressed network.
* @param[out] first First IPv6 address in range.
* @param[out] last Last IPv6 address in range.
*
* @return -1 if error, 0 else.
*/
static int
short_range6_network_ips (const char *str, struct in6_addr *first,
struct in6_addr *last)
{
char *first_str, *last_str;
long int end;
if (str == NULL || first == NULL || last == NULL)
return -1;
first_str = g_strdup (str);
last_str = strchr (first_str, '-');
if (last_str == NULL)
{
g_free (first_str);
return -1;
}
/* Separate the first IP. */
*last_str = '\0';
last_str++;
if (inet_pton (AF_INET6, first_str, first) != 1)
{
g_free (first_str);
return -1;
}
/* Calculate the last IP. */
memcpy (last, first, sizeof (*last));
end = strtol (last_str, NULL, 16);
memcpy (&last->s6_addr[15], &end, 1);
memcpy (&last->s6_addr[14], ((char *) &end) + 1, 1);
g_free (first_str);
return 0;
}
/**
* @brief Determines the host type in a buffer.
*
* @param[in] str_stripped Buffer that contains host definition, could a be
* hostname, single IPv4 or IPv6, CIDR-expressed block etc,.
*
* @return Host_TYPE_*, -1 if error.
*/
int
gvm_get_host_type (const gchar *str_stripped)
{
/*
* We have a single element with no leading or trailing
* white spaces. This element could represent different host
* definitions: single IPs, host names, CIDR-expressed blocks,
* range-expressed networks, IPv6 addresses.
*/
/* Null or empty string. */
if (str_stripped == NULL || *str_stripped == '\0')
return -1;
/* Check for regular single IPv4 address. */
if (is_ipv4_address (str_stripped))
return HOST_TYPE_IPV4;
/* Check for regular single IPv6 address. */
if (is_ipv6_address (str_stripped))
return HOST_TYPE_IPV6;
/* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
if (is_cidr_block (str_stripped))
return HOST_TYPE_CIDR_BLOCK;
/* Check for short range-expressed networks "192.168.12.5-40" */
if (is_short_range_network (str_stripped))
return HOST_TYPE_RANGE_SHORT;
/* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
if (is_long_range_network (str_stripped))
return HOST_TYPE_RANGE_LONG;
/* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
if (is_cidr6_block (str_stripped))
return HOST_TYPE_CIDR6_BLOCK;
/* Check for short range-expressed networks "::1-ef12" */
if (is_short_range6_network (str_stripped))
return HOST_TYPE_RANGE6_SHORT;
/* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
if (is_long_range6_network (str_stripped))
return HOST_TYPE_RANGE6_LONG;
/* Check for hostname. */
if (is_hostname (str_stripped))
return HOST_TYPE_NAME;
return -1;
}
/**
* @brief Creates a new gvm_vhost_t object.
*
* @param[in] value Vhost value.
* @param[in] source Source of hostname.
*
* @return Pointer to new vhost object.
*/
gvm_vhost_t *
gvm_vhost_new (char *value, char *source)
{
gvm_vhost_t *vhost;
vhost = g_malloc0 (sizeof (gvm_vhost_t));
vhost->value = value;
vhost->source = source;
return vhost;
}
/**
* @brief Frees the memory occupied by an gvm_vhost_t object.
*
* @param[in] vhost Vhost to free.
*/
static void
gvm_vhost_free (gpointer vhost)
{
if (vhost)
{
g_free (((gvm_vhost_t *) vhost)->value);
g_free (((gvm_vhost_t *) vhost)->source);
}
g_free (vhost);
}
/**
* @brief Creates a deep copy of a gvm_vhost_t object.
*
* @param vhost source vhost
* @param data dummy for g_slist_copy_deep
* @return gpointer copy of vhost
*/
gpointer
gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
{
(void) (data);
gvm_vhost_t *ret = NULL;
if (!vhost)
return NULL;
ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
g_strdup (((gvm_vhost_t *) vhost)->source));
return ret;
}
/**
* @brief Creates a new gvm_host_t object.
*
* @return Pointer to new host object, NULL if creation fails.
*/
static gvm_host_t *
gvm_host_new ()
{
gvm_host_t *host;
host = g_malloc0 (sizeof (gvm_host_t));
return host;
}
/**
* @brief Frees the memory occupied by an gvm_host_t object.
*
* @param[in] host Host to free.
*/
void
gvm_host_free (gpointer host)
{
gvm_host_t *h = host;
if (h == NULL)
return;
/* If host of type hostname, free the name buffer, first. */
if (h->type == HOST_TYPE_NAME)
g_free (h->name);
g_slist_free_full (h->vhosts, gvm_vhost_free);
g_free (h);
}
/**
* @brief Inserts a host object at the end of a hosts collection.
*
* @param[in] hosts Hosts in which to insert the host.
* @param[in] host Host to insert.
*/
void
gvm_hosts_add (gvm_hosts_t *hosts, gvm_host_t *host)
{
if (hosts->count == hosts->max_size)
{
hosts->max_size *= 4;
hosts->hosts =
g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
memset (hosts->hosts + hosts->count, '\0',
(hosts->max_size - hosts->count) * sizeof (gvm_host_t *));
}
hosts->hosts[hosts->count] = host;
hosts->count++;
}
/**
* @brief Creates a hosts collection from a hosts string.
*
* @param[in] hosts_str String of hosts.
*
* @return Hosts collection.
*/
static gvm_hosts_t *
gvm_hosts_init (const char *hosts_str)
{
gvm_hosts_t *hosts;
hosts = g_malloc0 (sizeof (gvm_hosts_t));
hosts->max_size = 1024;
hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
hosts->orig_str = g_strdup (hosts_str);
return hosts;
}
/**
* @brief Fill the gaps in the array of a hosts collection, which are caused by
* the removal of host entries.
*
* @param[in] hosts Hosts collection to fill gaps in.
*/
static void
gvm_hosts_fill_gaps (gvm_hosts_t *hosts)
{
size_t i;
if (!hosts)
return;
for (i = 0; i < hosts->max_size; i++)
{
if (!hosts->hosts[i])
{
size_t j;
/* Fill the gap with the closest host entry, in order to keep the
* sequential ordering. */
for (j = i + 1; j < hosts->max_size; j++)
{
if (hosts->hosts[j])
{
hosts->hosts[i] = hosts->hosts[j];
hosts->hosts[j] = NULL;
break;
}
}
/* No more entries left, ie. the empty space between count and
* max_size. */
if (!hosts->hosts[i])
return;
}
}
}
/**
* @brief Removes duplicate hosts values from an gvm_hosts_t structure.
* Also resets the iterator current position.
*
* @param[in] hosts hosts collection from which to remove duplicates.
*/
static void
gvm_hosts_deduplicate (gvm_hosts_t *hosts)
{
/**
* Uses a hash table in order to deduplicate the hosts list in O(N) time.
*/
GHashTable *name_table;
size_t i, duplicates = 0;
if (hosts == NULL)
return;
name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < hosts->count; i++)
{
gchar *name;
name = gvm_host_value_str (hosts->hosts[i]);
if (name)
{
gvm_host_t *host, *removed = hosts->hosts[i];
host = g_hash_table_lookup (name_table, name);
if (host)
{
/* Remove duplicate host. Add its vhosts to the original host. */
host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
removed->vhosts = NULL;
gvm_host_free (removed);
hosts->hosts[i] = NULL;
duplicates++;
g_free (name);
}
else
g_hash_table_insert (name_table, name, hosts->hosts[i]);
}
}
if (duplicates)
gvm_hosts_fill_gaps (hosts);
g_hash_table_destroy (name_table);
hosts->count -= duplicates;
hosts->duplicated += duplicates;
hosts->current = 0;
#ifdef __GLIBC__
malloc_trim (0);
#endif
}
/**
* @brief Creates a new gvm_hosts_t structure and the associated hosts
* objects from the provided hosts_str.
*
* @param[in] hosts_str The hosts string. A copy will be created of this within
* the returned struct.
* @param[in] max_hosts Max number of hosts in hosts_str. 0 means unlimited.
*
* @return NULL if error or hosts_str contains more than max hosts. Otherwise, a
* hosts structure that should be released using @ref gvm_hosts_free.
*/
gvm_hosts_t *
gvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
{
gvm_hosts_t *hosts;
gchar **host_element, **split;
gchar *str;
if (hosts_str == NULL)
return NULL;
/* Normalize separator: Transform newlines into commas. */
hosts = gvm_hosts_init (hosts_str);
str = hosts->orig_str;
while (*str)
{
if (*str == '\n')
*str = ',';
str++;
}
/* Split comma-separated list into single host-specifications */
split = g_strsplit (hosts->orig_str, ",", 0);
/* first element of the split list */
host_element = split;
while (*host_element)
{
int host_type;
gchar *stripped = g_strstrip (*host_element);
if (stripped == NULL || *stripped == '\0')
{
host_element++;
continue;
}
/* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ?
*/
/* -1 if error. */
host_type = gvm_get_host_type (stripped);
switch (host_type)
{
case HOST_TYPE_NAME:
case HOST_TYPE_IPV4:
case HOST_TYPE_IPV6:
{
/* New host. */
gvm_host_t *host = gvm_host_new ();
host->type = host_type;
if (host_type == HOST_TYPE_NAME)
host->name = g_ascii_strdown (stripped, -1);
else if (host_type == HOST_TYPE_IPV4)
{
if (inet_pton (AF_INET, stripped, &host->addr) != 1)
break;
}
else if (host_type == HOST_TYPE_IPV6)
{
if (inet_pton (AF_INET6, stripped, &host->addr6) != 1)
break;
}
gvm_hosts_add (hosts, host);
break;
}
case HOST_TYPE_CIDR_BLOCK:
case HOST_TYPE_RANGE_SHORT:
case HOST_TYPE_RANGE_LONG:
{
struct in_addr first, last;
uint32_t current;
int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
if (host_type == HOST_TYPE_CIDR_BLOCK)
ips_func = cidr_block_ips;
else if (host_type == HOST_TYPE_RANGE_SHORT)
ips_func = short_range_network_ips;
else
ips_func = long_range_network_ips;
if (ips_func (stripped, &first, &last) == -1)
break;
/* Make sure that first actually comes before last */
if (ntohl (first.s_addr) > ntohl (last.s_addr))
break;
/* Add addresses from first to last as single hosts. */
current = first.s_addr;
while (ntohl (current) <= ntohl (last.s_addr))
{
gvm_host_t *host;
if (max_hosts > 0 && hosts->count > max_hosts)
{
g_strfreev (split);
gvm_hosts_free (hosts);
return NULL;
}
host = gvm_host_new ();
host->type = HOST_TYPE_IPV4;
host->addr.s_addr = current;
gvm_hosts_add (hosts, host);
/* Next IP address. */
current = htonl (ntohl (current) + 1);
}
break;
}
case HOST_TYPE_CIDR6_BLOCK:
case HOST_TYPE_RANGE6_LONG:
case HOST_TYPE_RANGE6_SHORT:
{
struct in6_addr first, last;
unsigned char current[16];
int (*ips_func) (const char *, struct in6_addr *,
struct in6_addr *);
if (host_type == HOST_TYPE_CIDR6_BLOCK)
ips_func = cidr6_block_ips;
else if (host_type == HOST_TYPE_RANGE6_SHORT)
ips_func = short_range6_network_ips;
else
ips_func = long_range6_network_ips;
if (ips_func (stripped, &first, &last) == -1)
break;
/* Make sure the first comes before the last. */
if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
break;
/* Add addresses from first to last as single hosts. */
memcpy (current, &first.s6_addr, 16);
while (memcmp (current, &last.s6_addr, 16) <= 0)
{
int i;
gvm_host_t *host;
if (max_hosts > 0 && hosts->count > max_hosts)
{
g_strfreev (split);
gvm_hosts_free (hosts);
return NULL;
}
host = gvm_host_new ();
host->type = HOST_TYPE_IPV6;
memcpy (host->addr6.s6_addr, current, 16);
gvm_hosts_add (hosts, host);
/* Next IPv6 address. */
for (i = 15; i >= 0; --i)
if (current[i] < 255)
{
current[i]++;
break;
}
else
current[i] = 0;
}
break;
}
case -1:
default:
/* Invalid host string. */
g_strfreev (split);
gvm_hosts_free (hosts);
return NULL;
}
host_element++; /* move on to next element of split list */
if (max_hosts > 0 && hosts->count > max_hosts)
{
g_strfreev (split);
gvm_hosts_free (hosts);
return NULL;
}
}
/* No need to check for duplicates when a hosts string contains a
* single (IP/Hostname/Range/Subnetwork) entry. */
if (g_strv_length (split) > 1)
gvm_hosts_deduplicate (hosts);
g_strfreev (split);
#ifdef __GLIBC__
malloc_trim (0);
#endif
return hosts;
}
/**
* @brief Creates a new gvm_hosts_t structure and the associated hosts
* objects from the provided hosts_str.
*
* @param[in] hosts_str The hosts string. A copy will be created of this within
* the returned struct.
*
* @return NULL if error, otherwise, a hosts structure that should be released
* using @ref gvm_hosts_free.
*/
gvm_hosts_t *
gvm_hosts_new (const gchar *hosts_str)
{
return gvm_hosts_new_with_max (hosts_str, 0);
}
/**
* @brief Gets the next gvm_host_t from a gvm_hosts_t structure. The
* state of iteration is kept internally within the gvm_hosts structure.
*
* @param[in] hosts gvm_hosts_t structure to get next host from.
*
* @return Pointer to host. NULL if error or end of hosts.
*/
gvm_host_t *
gvm_hosts_next (gvm_hosts_t *hosts)
{
if (!hosts || hosts->current == hosts->count)
return NULL;
return hosts->hosts[hosts->current++];
}
/**
* @brief Move the current gvm_host_t from a gvm_hosts_t structure to
* the end of the hosts list.
*
* @param[in,out] hosts gvm_hosts_t structure which hosts must be
* rearange. The hosts->current index points to the last used host and
* gvm_hosts_next() must be called to get the next host in the list.
*
*/
void
gvm_hosts_move_current_host_to_end (gvm_hosts_t *hosts)
{
void *host_tmp;
size_t i;
if (!hosts)
return;
// Keep in mind that gvm_hosts_next will return the current host and then
// increment hosts->current.
if (hosts->current == hosts->count)
{
// We're on the last host, just make the previous host current.
// TODO what happens when current is 0?
hosts->current -= 1;
return;
}
// Make the previous host current. This makes sure that gvm_hosts_next will
// return the host that has replaced the current host.
// TODO what happens when current is 0?
hosts->current -= 1;
// Get the host to be moved.
host_tmp = hosts->hosts[hosts->current];
// Shift all the others down. Start from current + 1 because we're assigning
// into the previous slot (i - 1). It's safe to do this because we already
// checked if current == count above.
for (i = hosts->current + 1; i < hosts->count; i++)
hosts->hosts[i - 1] = hosts->hosts[i];
// Put the moved host on the end.
hosts->hosts[hosts->count - 1] = host_tmp;
}
/**
* @brief Frees memory occupied by an gvm_hosts_t structure.
*
* @param[in] hosts The hosts collection to free.
*
*/
void
gvm_hosts_free (gvm_hosts_t *hosts)
{
size_t i;
if (hosts == NULL)
return;
if (hosts->orig_str)
g_free (hosts->orig_str);
for (i = 0; i < hosts->count; i++)
gvm_host_free (hosts->hosts[i]);
g_free (hosts->hosts);
g_free (hosts);
hosts = NULL;
}
/**
* @brief Randomizes the order of the hosts objects in the collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to shuffle.
*/
void
gvm_hosts_shuffle (gvm_hosts_t *hosts)
{
size_t i = 0;
GRand *rand;
if (hosts == NULL)
return;
/* Shuffle the array. */
rand = g_rand_new ();
for (i = 0; i < hosts->count; i++)
{
void *tmp;
int j = g_rand_int_range (rand, 0, hosts->count);
tmp = hosts->hosts[i];
hosts->hosts[i] = hosts->hosts[j];
hosts->hosts[j] = tmp;
}
hosts->current = 0;
g_rand_free (rand);
}
/**
* @brief Reverses the order of the hosts objects in the collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to reverse.
*/
void
gvm_hosts_reverse (gvm_hosts_t *hosts)
{
size_t i, j;
if (hosts == NULL)
return;
for (i = 0, j = hosts->count - 1; i < j; i++, j--)
{
gvm_host_t *tmp = hosts->hosts[i];
hosts->hosts[i] = hosts->hosts[j];
hosts->hosts[j] = tmp;
}
hosts->current = 0;
}
/**
* @brief Resolves host objects of type name in a hosts collection, replacing
* hostnames with IPv4 values.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection from which to exclude.
*
* @return List of unresolved hostnames.
*/
GSList *
gvm_hosts_resolve (gvm_hosts_t *hosts)
{
size_t i, new_entries = 0, resolved = 0;
GSList *unresolved = NULL;
for (i = 0; i < hosts->count; i++)
{
GSList *list, *tmp;
gvm_host_t *host = hosts->hosts[i];
if (host->type != HOST_TYPE_NAME)
continue;
list = tmp = gvm_resolve_list (host->name);
while (tmp)
{
/* Create a new host for each IP address. */
gvm_host_t *new;
struct in6_addr *ip6 = tmp->data;
gvm_vhost_t *vhost;
new = gvm_host_new ();
if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
|| ip6->s6_addr32[2] != htonl (0xffff))
{
new->type = HOST_TYPE_IPV6;
memcpy (&new->addr6, ip6, sizeof (new->addr6));
}
else
{
new->type = HOST_TYPE_IPV4;
memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
}
vhost =
gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
new->vhosts = g_slist_prepend (new->vhosts, vhost);
gvm_hosts_add (hosts, new);
tmp = tmp->next;
new_entries = 1;
}
/* Remove hostname from list, as it was either replaced by IPs, or
* is unresolvable. */
hosts->hosts[i] = NULL;
resolved++;
if (!list)
unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
gvm_host_free (host);
g_slist_free_full (list, g_free);
}
if (resolved)
gvm_hosts_fill_gaps (hosts);
hosts->count -= resolved;
hosts->removed += resolved;
if (new_entries)
gvm_hosts_deduplicate (hosts);
hosts->current = 0;
return unresolved;
}
/**
* @brief Exclude a list of vhosts from a host's vhosts list.
*
* @param[in] host The host whose vhosts are to be excluded from.
* @param[in] excluded_str String of hosts to exclude.
*
* @return Number of excluded vhosts.
*/
int
gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
{
GSList *vhost;
char **excluded;
int ret = 0;
if (!host || !excluded_str)
return ret;
vhost = host->vhosts;
excluded = g_strsplit (excluded_str, ",", 0);
if (!excluded || !*excluded)
{
g_strfreev (excluded);
return ret;
}
while (vhost)
{
char **tmp = excluded;
char *value = ((gvm_vhost_t *) vhost->data)->value;
while (*tmp)
{
if (!strcasecmp (value, g_strstrip (*tmp)))
{
gvm_vhost_free (vhost->data);
host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
ret++;
break;
}
tmp++;
if (!*tmp)
{
vhost = vhost->next;
break;
}
}
}
g_strfreev (excluded);
return ret;
}
/**
* @brief Excludes a set of hosts provided as a string from a hosts collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in,out] hosts The hosts collection from which to exclude.
* @param[in] excluded_str String of hosts to exclude.
* @param[in] max_hosts Max number of hosts in hosts_str. 0 means unlimited.
*
* @return Number of excluded hosts, -1 if error.
*/
int
gvm_hosts_exclude_with_max (gvm_hosts_t *hosts, const char *excluded_str,
unsigned int max_hosts)
{
/**
* Uses a hash table in order to exclude hosts in O(N+M) time.
*/
gvm_hosts_t *excluded_hosts;
GHashTable *name_table;
size_t excluded = 0, i;
if (hosts == NULL || excluded_str == NULL)
return -1;
excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
if (excluded_hosts == NULL)
return -1;
if (gvm_hosts_count (excluded_hosts) == 0)
{
gvm_hosts_free (excluded_hosts);
return 0;
}
/* Hash host values from excluded hosts list. */
name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < excluded_hosts->count; i++)
{
gchar *name;
name = gvm_host_value_str (excluded_hosts->hosts[i]);
if (name)
g_hash_table_insert (name_table, name, hosts);
}
/* Check for hosts values in hash table. */
for (i = 0; i < hosts->count; i++)
{
gchar *name;
name = gvm_host_value_str (hosts->hosts[i]);
if (name)
{
if (g_hash_table_lookup (name_table, name))
{
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
excluded++;
g_free (name);
continue;
}
g_free (name);
}
}
/* Cleanup. */
if (excluded)
gvm_hosts_fill_gaps (hosts);
hosts->count -= excluded;
hosts->removed += excluded;
hosts->current = 0;
g_hash_table_destroy (name_table);
gvm_hosts_free (excluded_hosts);
return excluded;
}
/**
* @brief Returns a list of hosts after a host authorization check.
*
* @param[in,out] hosts The hosts collection from which to exclude.
* @param[in] deny_hosts_str String of denied hosts. This hosts will be
* removed from the hosts list
* @param[in] allow_hosts_str String of allow hosts. This hosts will be kept
* in the hosts list
*
* @return List of non-authorized hosts if any, otherwise Null. The returned
* list must be free()'d by the caller functions.
*/
GSList *
gvm_hosts_allowed_only (gvm_hosts_t *hosts, const char *deny_hosts_str,
const char *allow_hosts_str)
{
/**
* Uses a hash table in order to exclude hosts in O(N+M) time.
*/
gvm_hosts_t *allowed_hosts, *denied_hosts;
GHashTable *name_allow_table = NULL, *name_deny_table = NULL;
GSList *removed = NULL;
size_t excluded = 0, i;
if (hosts == NULL || (deny_hosts_str == NULL && allow_hosts_str == NULL))
return NULL;
// Prepare list of denied and allowed hosts
denied_hosts = gvm_hosts_new_with_max (deny_hosts_str, 0);
allowed_hosts = gvm_hosts_new_with_max (allow_hosts_str, 0);
if (denied_hosts == NULL && allowed_hosts == NULL)
return NULL;
if (gvm_hosts_count (denied_hosts) == 0)
gvm_hosts_free (denied_hosts);
else
{
/* Hash host values from denied hosts list. */
name_deny_table =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < denied_hosts->count; i++)
{
gchar *name;
name = gvm_host_value_str (denied_hosts->hosts[i]);
if (name)
g_hash_table_insert (name_deny_table, name, hosts);
}
}
if (gvm_hosts_count (allowed_hosts) == 0)
gvm_hosts_free (allowed_hosts);
else
{
/* Hash host values from allowed hosts list. */
name_allow_table =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < allowed_hosts->count; i++)
{
gchar *name;
name = gvm_host_value_str (allowed_hosts->hosts[i]);
if (name)
g_hash_table_insert (name_allow_table, name, hosts);
}
}
/* Check for authorized hosts in hash table and create a list of removed
* hosts. */
for (i = 0; i < hosts->count; i++)
{
gchar *name;
name = gvm_host_value_str (hosts->hosts[i]);
if (name)
{
if (denied_hosts != NULL
&& g_hash_table_lookup (name_deny_table, name))
{
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
excluded++;
removed = g_slist_prepend (removed, name);
continue;
}
else if (allowed_hosts != NULL
&& !g_hash_table_lookup (name_allow_table, name))
{
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
excluded++;
removed = g_slist_prepend (removed, name);
continue;
}
g_free (name);
}
}
/* Cleanup. */
if (excluded)
gvm_hosts_fill_gaps (hosts);
hosts->count -= excluded;
hosts->removed += excluded;
hosts->current = 0;
if (name_allow_table != NULL)
g_hash_table_destroy (name_allow_table);
if (name_deny_table != NULL)
g_hash_table_destroy (name_deny_table);
if (allowed_hosts != NULL)
gvm_hosts_free (allowed_hosts);
if (denied_hosts != NULL)
gvm_hosts_free (denied_hosts);
return removed;
}
/**
* @brief Excludes a set of hosts provided as a string from a hosts collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection from which to exclude.
* @param[in] excluded_str String of hosts to exclude.
*
* @return Number of excluded hosts, -1 if error.
*/
int
gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str)
{
return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
}
/**
* @brief Creates a new gvm_host_t from a host string.
*
* @param[in] host_str The host string can consist of a hostname, IPv4 address
* or IPv6 address.
*
* @return NULL if error. Otherwise, a single host structure that should be put
* into a gvm_hosts_t structure for freeing with @ref gvm_hosts_free or
* freed directly via @ref gvm_host_free.
*/
gvm_host_t *
gvm_host_from_str (const gchar *host_str)
{
int host_type;
if (host_str == NULL)
return NULL;
/* IPv4, hostname, IPv6 */
/* -1 if error. */
host_type = gvm_get_host_type (host_str);
switch (host_type)
{
case HOST_TYPE_NAME:
case HOST_TYPE_IPV4:
case HOST_TYPE_IPV6:
{
/* New host. */
gvm_host_t *host = gvm_host_new ();
host->type = host_type;
if (host_type == HOST_TYPE_NAME)
host->name = g_ascii_strdown (host_str, -1);
else if (host_type == HOST_TYPE_IPV4)
{
if (inet_pton (AF_INET, host_str, &host->addr) != 1)
break;
}
else if (host_type == HOST_TYPE_IPV6)
{
if (inet_pton (AF_INET6, host_str, &host->addr6) != 1)
break;
}
return host;
}
case -1:
default:
return NULL;
}
return NULL;
}
/**
* @brief Checks for a host object reverse dns lookup existence.
*
* @param[in] host The host to reverse-lookup.
*
* @return Result of look-up, NULL otherwise.
*/
char *
gvm_host_reverse_lookup (gvm_host_t *host)
{
int retry = 10;
gchar hostname[NI_MAXHOST];
void *addr;
size_t addrlen;
struct sockaddr_in sa;
struct sockaddr_in6 sa6;
if (!host)
return NULL;
if (host->type == HOST_TYPE_IPV4)
{
addr = &sa;
addrlen = sizeof (sa);
memset (addr, '\0', addrlen);
sa.sin_addr = host->addr;
sa.sin_family = AF_INET;
}
else if (host->type == HOST_TYPE_IPV6)
{
addr = &sa6;
addrlen = sizeof (sa6);
memset (&sa6, '\0', addrlen);
memcpy (&sa6.sin6_addr, &host->addr6, 16);
sa6.sin6_family = AF_INET6;
}
else
return NULL;
while (retry--)
{
int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
0, NI_NAMEREQD);
if (!ret)
return g_ascii_strdown (hostname, -1);
if (ret != EAI_AGAIN)
break;
usleep (10000); // 10ms
}
return NULL;
}
/**
* @brief Verifies that hostname value resolves to a host's IP.
*
* @param[in] host The host whose IP is to be checked against.
* @param[in] value Hostname value to verify.
*
* @return 0 if hostname resolves to host's IP, -1 otherwise.
*/
static int
host_name_verify (gvm_host_t *host, const char *value)
{
GSList *list, *tmp;
char *host_str;
int ret = -1;
assert (host);
assert (value);
host_str = gvm_host_value_str (host);
list = tmp = gvm_resolve_list (value);
while (tmp)
{
char buffer[INET6_ADDRSTRLEN];
addr6_to_str (tmp->data, buffer);
if (!strcasecmp (host_str, buffer))
{
ret = 0;
break;
}
tmp = tmp->next;
}
g_free (host_str);
g_slist_free_full (list, g_free);
return ret;
}
/**
* @brief Add a host's reverse-lookup name to the vhosts list.
*
* @param[in] host The host to which we add the vhost.
*/
void
gvm_host_add_reverse_lookup (gvm_host_t *host)
{
GSList *vhosts;
gvm_vhost_t *vhost;
char *value;
if (!host || host->type == HOST_TYPE_NAME)
return;
value = gvm_host_reverse_lookup (host);
if (!value)
return;
if (host_name_verify (host, value))
{
g_free (value);
return;
}
/* Don't add vhost, if already in the list. */
vhosts = host->vhosts;
while (vhosts)
{
if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
{
g_free (value);
return;
}
vhosts = vhosts->next;
}
vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
host->vhosts = g_slist_prepend (host->vhosts, vhost);
}
/**
* @brief Removes hosts that don't reverse-lookup from the hosts collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to filter.
*
* @return list of hosts removed, Null on error.
*/
gvm_hosts_t *
gvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *hosts)
{
size_t i, count = 0;
gvm_hosts_t *excluded = gvm_hosts_new ("");
if (hosts == NULL)
return NULL;
for (i = 0; i < hosts->count; i++)
{
gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
if (name == NULL)
{
gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
count++;
}
else
g_free (name);
}
if (count)
gvm_hosts_fill_gaps (hosts);
hosts->count -= count;
hosts->removed += count;
hosts->current = 0;
return excluded;
}
/**
* @brief Removes hosts that don't reverse-lookup from the hosts collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to filter.
*
* @return Number of hosts removed, -1 if error.
*/
int
gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
{
gvm_hosts_t *excluded;
int count = 0;
if (hosts == NULL)
return -1;
excluded = gvm_hosts_reverse_lookup_only_excluded (hosts);
count = excluded->count;
gvm_hosts_free (excluded);
return count;
}
/**
* @brief Removes hosts duplicates that reverse-lookup to the same value.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to filter.
*
* @return List of hosts removed, Null if error.
*/
gvm_hosts_t *
gvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *hosts)
{
/**
* Uses a hash table in order to unify the hosts list in O(N) time.
*/
size_t i, count = 0;
GHashTable *name_table;
gvm_hosts_t *excluded = NULL;
if (hosts == NULL)
return NULL;
excluded = gvm_hosts_new ("");
name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < hosts->count; i++)
{
gchar *name;
name = gvm_host_reverse_lookup (hosts->hosts[i]);
if (name)
{
if (g_hash_table_lookup (name_table, name))
{
gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
count++;
g_free (name);
}
else
{
/* Insert in the hash table. Value not important. */
g_hash_table_insert (name_table, name, hosts);
}
}
}
if (count)
gvm_hosts_fill_gaps (hosts);
g_hash_table_destroy (name_table);
hosts->removed += count;
hosts->count -= count;
hosts->current = 0;
return excluded;
}
/**
* @brief Removes hosts duplicates that reverse-lookup to the same value.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to filter.
*
* @return Number of hosts removed, -1 if error.
*/
int
gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
{
gvm_hosts_t *excluded = NULL;
int count = 0;
if (hosts == NULL)
return -1;
excluded = gvm_hosts_reverse_lookup_unify_excluded (hosts);
count = excluded->count;
gvm_hosts_free (excluded);
return count;
}
/**
* @brief Gets the count of single hosts objects in a hosts collection.
*
* @param[in] hosts The hosts collection to count hosts of.
*
* @return The number of single hosts.
*/
unsigned int
gvm_hosts_count (const gvm_hosts_t *hosts)
{
return hosts ? hosts->count : 0;
}
/**
* @brief Gets the count of single values in hosts string that were removed
* (duplicates / excluded.)
*
* @param[in] hosts The hosts collection.
*
* @return The number of removed values.
*/
unsigned int
gvm_hosts_removed (const gvm_hosts_t *hosts)
{
return hosts ? hosts->removed : 0;
}
/**
* @brief Gets the count of single values in hosts string that were duplicated
* and therefore removed from the list
*
* @param[in] hosts The hosts collection.
*
* @return The number of duplicated values.
*/
unsigned int
gvm_hosts_duplicated (const gvm_hosts_t *hosts)
{
return hosts ? hosts->duplicated : 0;
}
/**
* @brief Find the gvm_host_t from a gvm_hosts_t structure.
*
* @param[in] host The host object.
* @param[in] addr Optional pointer to ip address. Could be used so that host
* isn't resolved multiple times when type is HOST_TYPE_NAME.
* @param[in] hosts Hosts collection.
*
* @return Pointer to host if found. NULL if error or host not found
*/
gvm_host_t *
gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
const gvm_hosts_t *hosts)
{
char *host_str;
size_t i;
if (host == NULL || hosts == NULL)
return NULL;
host_str = gvm_host_value_str (host);
for (i = 0; i < hosts->count; i++)
{
gvm_host_t *current_host = hosts->hosts[i];
char *tmp = gvm_host_value_str (current_host);
if (strcasecmp (host_str, tmp) == 0)
{
g_free (host_str);
g_free (tmp);
return current_host;
}
g_free (tmp);
/* Hostnames in hosts list shouldn't be resolved. */
if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
{
struct in6_addr tmpaddr;
gvm_host_get_addr6 (current_host, &tmpaddr);
if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
{
g_free (host_str);
return current_host;
}
}
}
g_free (host_str);
return NULL;
}
/**
* @brief Creates a deep copy of a host. gvm_host_free has to be called on it.
*
* @param host source host
* @return gvm_host_t* copy of host
*/
gvm_host_t *
gvm_duplicate_host (gvm_host_t *host)
{
gvm_host_t *ret = NULL;
if (host == NULL)
return NULL;
ret = gvm_host_new ();
ret->type = host->type;
switch (host->type)
{
case HOST_TYPE_NAME:
ret->name = g_strdup (host->name);
break;
case HOST_TYPE_IPV4:
ret->addr.s_addr = host->addr.s_addr;
break;
case HOST_TYPE_IPV6:
ret->addr6.__in6_u = host->addr6.__in6_u;
break;
default:
g_free (ret);
return NULL;
}
ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);
return ret;
}
/**
* @brief Returns whether a host has an equal host in a hosts collection.
* eg. 192.168.10.1 has an equal in list created from
* "192.168.10.1-5, 192.168.10.10-20" string while 192.168.10.7 doesn't.
*
* @param[in] host The host object.
* @param[in] addr Optional pointer to ip address. Could be used so that host
* isn't resolved multiple times when type is HOST_TYPE_NAME.
* @param[in] hosts Hosts collection.
*
* @return 1 if host has equal in hosts, 0 otherwise.
*/
int
gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
const gvm_hosts_t *hosts)
{
if (gvm_host_find_in_hosts (host, addr, hosts))
return 1;
return 0;
}
/**
* @brief Gets a host object's type.
*
* @param[in] host The host object.
*
* @return Host type.
*/
enum host_type
gvm_host_type (const gvm_host_t *host)
{
assert (host);
return host->type;
}
/**
* @brief Gets a host's type in printable format.
*
* @param[in] host The host object.
*
* @return String representing host type. Statically allocated, thus, not to be
* freed.
*/
gchar *
gvm_host_type_str (const gvm_host_t *host)
{
if (host == NULL)
return NULL;
return host_type_str[host->type];
}
/**
* @brief Gets a host's value in printable format.
*
* @param[in] host The host object.
*
* @return String representing host value. To be freed with g_free().
*/
gchar *
gvm_host_value_str (const gvm_host_t *host)
{
if (host == NULL)
return NULL;
switch (host->type)
{
case HOST_TYPE_NAME:
return g_strdup (host->name);
break;
case HOST_TYPE_IPV4:
case HOST_TYPE_IPV6:
/* Handle both cases using inet_ntop(). */
{
int family, size;
gchar *str;
const void *srcaddr;
if (host->type == HOST_TYPE_IPV4)
{
family = AF_INET;
size = INET_ADDRSTRLEN;
srcaddr = &host->addr;
}
else
{
family = AF_INET6;
size = INET6_ADDRSTRLEN;
srcaddr = &host->addr6;
}
str = g_malloc0 (size);
if (inet_ntop (family, srcaddr, str, size) == NULL)
{
perror ("inet_ntop");
g_free (str);
return NULL;
}
return str;
}
default:
return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
}
}
/**
* @brief Resolves a host object's name to an IPv4 or IPv6 address. Host object
* should be of type HOST_TYPE_NAME.
*
* @param[in] host The host object whose name to resolve.
* @param[out] dst Buffer to store resolved address. Size must be at least
* 4 bytes for AF_INET and 16 bytes for AF_INET6.
* @param[in] family Either AF_INET or AF_INET6.
*
* @return -1 if error, 0 otherwise.
*/
int
gvm_host_resolve (const gvm_host_t *host, void *dst, int family)
{
if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
return -1;
return gvm_resolve (host->name, dst, family);
}
/**
* @brief Gives a host object's value as an IPv6 address.
* If the host type is hostname, it resolves the IPv4 address then gives an
* IPv4-mapped IPv6 address (eg. \::ffff:192.168.1.1 .)
* If the host type is IPv4, it gives an IPv4-mapped IPv6 address.
* If the host's type is IPv6, it gives the value directly.
*
* @param[in] host The host object whose value to get as IPv6.
* @param[out] ip6 Buffer to store the IPv6 address.
*
* @return -1 if error, 0 otherwise.
*/
int
gvm_host_get_addr6 (const gvm_host_t *host, struct in6_addr *ip6)
{
if (host == NULL || ip6 == NULL)
return -1;
switch (gvm_host_type (host))
{
case HOST_TYPE_IPV6:
memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
return 0;
case HOST_TYPE_IPV4:
ipv4_as_ipv6 (&host->addr, ip6);
return 0;
case HOST_TYPE_NAME:
{
struct in_addr ip4;
/* Fail if IPv4 and IPv6 both don't resolve. */
if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
ipv4_as_ipv6 (&ip4, ip6);
else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
return -1;
return 0;
}
default:
return -1;
}
}
|