"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "base/networking.c" between
gvm-libs-1.0-beta2.tar.gz and gvm-libs-1.0.0.tar.gz

About: GVM Libraries for the Greenbone Vulnerability Management (GVM-10) framework (derived from the former openvas-libraries).

networking.c  (gvm-libs-1.0-beta2):networking.c  (gvm-libs-1.0.0)
/* Copyright (C) 2013-2018 Greenbone Networks GmbH /* Copyright (C) 2013-2019 Greenbone Networks GmbH
* *
* SPDX-License-Identifier: GPL-2.0-or-later * SPDX-License-Identifier: GPL-2.0-or-later
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
skipping to change at line 45 skipping to change at line 45
#include <stdlib.h> /* for atoi, strtol */ #include <stdlib.h> /* for atoi, strtol */
#include <string.h> /* for memcpy, bzero, strchr, strlen, strcmp, strncpy */ #include <string.h> /* for memcpy, bzero, strchr, strlen, strcmp, strncpy */
#include <sys/socket.h> /* for AF_INET, AF_INET6, AF_UNSPEC, sockaddr_storage */ #include <sys/socket.h> /* for AF_INET, AF_INET6, AF_UNSPEC, sockaddr_storage */
#include <unistd.h> /* for close */ #include <unistd.h> /* for close */
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#include <netinet/in.h> #include <netinet/in.h>
#define s6_addr32 __u6_addr.__u6_addr32 #define s6_addr32 __u6_addr.__u6_addr32
#endif #endif
/* Global variables */ /* Global variables */
/* Source interface name eg. eth1. */ /* Source interface name eg. eth1. */
char global_source_iface[IFNAMSIZ] = { '\0' }; char global_source_iface[IFNAMSIZ] = {'\0'};
/* Source IPv4 address. */ /* Source IPv4 address. */
struct in_addr global_source_addr = { .s_addr = 0 }; struct in_addr global_source_addr = {.s_addr = 0};
/* Source IPv6 address. */ /* Source IPv6 address. */
struct in6_addr global_source_addr6 = { .s6_addr32 = { 0, 0, 0, 0 } }; struct in6_addr global_source_addr6 = {.s6_addr32 = {0, 0, 0, 0}};
/* Source Interface/Address related functions. */ /* Source Interface/Address related functions. */
/** /**
* @brief Initializes the source network interface name and related information. * @brief Initializes the source network interface name and related information.
* *
* @param[in] iface Name of network interface to use as source interface. * @param[in] iface Name of network interface to use as source interface.
* *
* @return 0 if success. If error, return 1 and reset source values to default. * @return 0 if success. If error, return 1 and reset source values to default.
*/ */
int int
gvm_source_iface_init (const char *iface) gvm_source_iface_init (const char *iface)
skipping to change at line 88 skipping to change at line 88
if (getifaddrs (&ifaddr) == -1) if (getifaddrs (&ifaddr) == -1)
return ret; return ret;
/* Search for the adequate interface/family. */ /* Search for the adequate interface/family. */
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{ {
if (ifa->ifa_addr && strcmp (iface, ifa->ifa_name) == 0) if (ifa->ifa_addr && strcmp (iface, ifa->ifa_name) == 0)
{ {
if (ifa->ifa_addr->sa_family == AF_INET) if (ifa->ifa_addr->sa_family == AF_INET)
{ {
struct in_addr *addr = &((struct sockaddr_in *) struct in_addr *addr =
ifa->ifa_addr)->sin_addr; &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
memcpy (&global_source_addr, addr, sizeof (global_source_addr)); memcpy (&global_source_addr, addr, sizeof (global_source_addr));
ret = 0; ret = 0;
} }
else if (ifa->ifa_addr->sa_family == AF_INET6) else if (ifa->ifa_addr->sa_family == AF_INET6)
{ {
struct sockaddr_in6 *addr; struct sockaddr_in6 *addr;
addr = (struct sockaddr_in6 *) ifa->ifa_addr; addr = (struct sockaddr_in6 *) ifa->ifa_addr;
memcpy (&global_source_addr6.s6_addr, &addr->sin6_addr, memcpy (&global_source_addr6.s6_addr, &addr->sin6_addr,
skipping to change at line 232 skipping to change at line 232
*/ */
char * char *
gvm_source_addr6_str (void) gvm_source_addr6_str (void)
{ {
char *str = g_malloc0 (INET6_ADDRSTRLEN); char *str = g_malloc0 (INET6_ADDRSTRLEN);
inet_ntop (AF_INET6, &global_source_addr6, str, INET6_ADDRSTRLEN); inet_ntop (AF_INET6, &global_source_addr6, str, INET6_ADDRSTRLEN);
return str; return str;
} }
/* Miscellaneous functions. */ /* Miscellaneous functions. */
/** /**
* @brief Maps an IPv4 address as an IPv6 address. * @brief Maps an IPv4 address as an IPv6 address.
* eg. 192.168.10.20 would map to \::ffff:192.168.10.20. * eg. 192.168.10.20 would map to \::ffff:192.168.10.20.
* *
* @param[in] ip4 IPv4 address to map. * @param[in] ip4 IPv4 address to map.
* @param[out] ip6 Buffer to store the IPv6 address. * @param[out] ip6 Buffer to store the IPv6 address.
*/ */
void void
ipv4_as_ipv6 (const struct in_addr *ip4, struct in6_addr *ip6) ipv4_as_ipv6 (const struct in_addr *ip4, struct in6_addr *ip6)
skipping to change at line 305 skipping to change at line 305
*/ */
void void
sockaddr_as_str (const struct sockaddr_storage *addr, char *str) sockaddr_as_str (const struct sockaddr_storage *addr, char *str)
{ {
if (!addr || !str) if (!addr || !str)
return; return;
if (addr->ss_family == AF_INET) if (addr->ss_family == AF_INET)
{ {
struct sockaddr_in *saddr = (struct sockaddr_in *) addr; struct sockaddr_in *saddr = (struct sockaddr_in *) addr;
inet_ntop (AF_INET, &saddr->sin_addr, str, INET6_ADDRSTRLEN); inet_ntop (AF_INET, &saddr->sin_addr, str, INET6_ADDRSTRLEN);
} }
else if (addr->ss_family == AF_INET6) else if (addr->ss_family == AF_INET6)
{ {
struct sockaddr_in6 *s6addr = (struct sockaddr_in6 *) addr; struct sockaddr_in6 *s6addr = (struct sockaddr_in6 *) addr;
if (IN6_IS_ADDR_V4MAPPED (&s6addr->sin6_addr)) if (IN6_IS_ADDR_V4MAPPED (&s6addr->sin6_addr))
inet_ntop (AF_INET, &s6addr->sin6_addr.s6_addr[12], inet_ntop (AF_INET, &s6addr->sin6_addr.s6_addr[12], str,
str, INET6_ADDRSTRLEN); INET6_ADDRSTRLEN);
else else
inet_ntop (AF_INET6, &s6addr->sin6_addr, str, INET6_ADDRSTRLEN); inet_ntop (AF_INET6, &s6addr->sin6_addr, str, INET6_ADDRSTRLEN);
} }
else if (addr->ss_family == AF_UNIX) else if (addr->ss_family == AF_UNIX)
{ {
g_snprintf (str, INET6_ADDRSTRLEN, "unix_socket"); g_snprintf (str, INET6_ADDRSTRLEN, "unix_socket");
} }
else if (addr->ss_family == AF_UNSPEC) else if (addr->ss_family == AF_UNSPEC)
{ {
g_snprintf (str, INET6_ADDRSTRLEN, "unknown_socket"); g_snprintf (str, INET6_ADDRSTRLEN, "unknown_socket");
} }
else else
{ {
g_snprintf (str, INET6_ADDRSTRLEN, g_snprintf (str, INET6_ADDRSTRLEN, "type_%d_socket", addr->ss_family);
"type_%d_socket", addr->ss_family);
} }
} }
/** /**
* @brief Returns a list of addresses that a hostname resolves to. * @brief Returns a list of addresses that a hostname resolves to.
* *
* @param[in] name Hostname to resolve. * @param[in] name Hostname to resolve.
* *
* @return List of addresses, NULL otherwise. * @return List of addresses, NULL otherwise.
*/ */
skipping to change at line 460 skipping to change at line 459
/** /**
* @brief Validate a port range string. * @brief Validate a port range string.
* *
* Accepts ranges in form of "103,U:200-1024,3000-4000,T:3-4,U:7". * Accepts ranges in form of "103,U:200-1024,3000-4000,T:3-4,U:7".
* *
* @param[in] port_range A port range. * @param[in] port_range A port range.
* *
* @return 0 success, 1 failed. * @return 0 success, 1 failed.
*/ */
int int
validate_port_range (const char* port_range) validate_port_range (const char *port_range)
{ {
gchar **split, **point, *range, *range_start; gchar **split, **point, *range, *range_start;
if (!port_range) if (!port_range)
return 1; return 1;
while (*port_range && isblank (*port_range)) port_range++; while (*port_range && isblank (*port_range))
port_range++;
if (*port_range == '\0') if (*port_range == '\0')
return 1; return 1;
/* Treat newlines like commas. */ /* Treat newlines like commas. */
range = range_start = g_strdup (port_range); range = range_start = g_strdup (port_range);
while (*range) while (*range)
{ {
if (*range == '\n') *range = ','; if (*range == '\n')
*range = ',';
range++; range++;
} }
split = g_strsplit (range_start, ",", 0); split = g_strsplit (range_start, ",", 0);
g_free (range_start); g_free (range_start);
point = split; point = split;
while (*point) while (*point)
{ {
gchar *hyphen, *element; gchar *hyphen, *element;
skipping to change at line 512 skipping to change at line 513
{ {
long int number1, number2; long int number1, number2;
const char *first; const char *first;
char *end; char *end;
hyphen++; hyphen++;
/* Check the first number. */ /* Check the first number. */
first = element; first = element;
while (*first && isblank (*first)) first++; while (*first && isblank (*first))
first++;
if (*first == '-') if (*first == '-')
goto fail; goto fail;
errno = 0; errno = 0;
number1 = strtol (first, &end, 10); number1 = strtol (first, &end, 10);
while (*end && isblank (*end)) end++; while (*end && isblank (*end))
end++;
if (errno || (*end != '-')) if (errno || (*end != '-'))
goto fail; goto fail;
if (number1 == 0) if (number1 == 0)
goto fail; goto fail;
if (number1 > 65535) if (number1 > 65535)
goto fail; goto fail;
/* Check the second number. */ /* Check the second number. */
while (*hyphen && isblank (*hyphen)) hyphen++; while (*hyphen && isblank (*hyphen))
hyphen++;
if (*hyphen == '\0') if (*hyphen == '\0')
goto fail; goto fail;
errno = 0; errno = 0;
number2 = strtol (hyphen, &end, 10); number2 = strtol (hyphen, &end, 10);
while (*end && isblank (*end)) end++; while (*end && isblank (*end))
end++;
if (errno || *end) if (errno || *end)
goto fail; goto fail;
if (number2 == 0) if (number2 == 0)
goto fail; goto fail;
if (number2 > 65535) if (number2 > 65535)
goto fail; goto fail;
if (number1 > number2) if (number1 > number2)
goto fail; goto fail;
} }
else else
{ {
long int number; long int number;
const char *only; const char *only;
char *end; char *end;
/* Check the single number. */ /* Check the single number. */
only = element; only = element;
while (*only && isblank (*only)) only++; while (*only && isblank (*only))
only++;
/* Empty ranges are OK. */ /* Empty ranges are OK. */
if (*only) if (*only)
{ {
errno = 0; errno = 0;
number = strtol (only, &end, 10); number = strtol (only, &end, 10);
while (*end && isblank (*end)) end++; while (*end && isblank (*end))
end++;
if (errno || *end) if (errno || *end)
goto fail; goto fail;
if (number == 0) if (number == 0)
goto fail; goto fail;
if (number > 65535) if (number > 65535)
goto fail; goto fail;
} }
} }
point += 1; point += 1;
} }
g_strfreev (split); g_strfreev (split);
return 0; return 0;
fail: fail:
g_strfreev (split); g_strfreev (split);
return 1; return 1;
} }
/** /**
* @brief Create a range array from a port_range string. * @brief Create a range array from a port_range string.
* *
* @param[in] port_range Valid port_range string. * @param[in] port_range Valid port_range string.
* *
* @return Range array. * @return Range array.
*/ */
array_t* array_t *
port_range_ranges (const char *port_range) port_range_ranges (const char *port_range)
{ {
gchar **split, **point, *range_start, *current; gchar **split, **point, *range_start, *current;
array_t *ranges; array_t *ranges;
int tcp; int tcp;
if (!port_range) if (!port_range)
return NULL; return NULL;
ranges = make_array (); ranges = make_array ();
while (*port_range && isblank (*port_range)) port_range++; while (*port_range && isblank (*port_range))
port_range++;
/* Accepts T: and U: before any of the ranges. This toggles the remaining /* Accepts T: and U: before any of the ranges. This toggles the remaining
* ranges, as in nmap. Treats a leading naked range as TCP, whereas nmap * ranges, as in nmap. Treats a leading naked range as TCP, whereas nmap
* treats it as TCP and UDP. */ * treats it as TCP and UDP. */
/* Treat newlines like commas. */ /* Treat newlines like commas. */
range_start = current = g_strdup (port_range); range_start = current = g_strdup (port_range);
while (*current) while (*current)
{ {
if (*current == '\n') *current = ','; if (*current == '\n')
*current = ',';
current++; current++;
} }
tcp = 1; tcp = 1;
split = g_strsplit (range_start, ",", 0); split = g_strsplit (range_start, ",", 0);
g_free (range_start); g_free (range_start);
point = split; point = split;
while (*point) while (*point)
{ {
skipping to change at line 640 skipping to change at line 649
} }
else if ((element[0] == 'U') && (element[1] == ':')) else if ((element[0] == 'U') && (element[1] == ':'))
{ {
tcp = 0; tcp = 0;
element = element + 2; element = element + 2;
} }
/* Else tcp stays as it is. */ /* Else tcp stays as it is. */
} }
/* Skip any space that followed the type specifier. */ /* Skip any space that followed the type specifier. */
while (*element && isblank (*element)) element++; while (*element && isblank (*element))
element++;
hyphen = strchr (element, '-'); hyphen = strchr (element, '-');
if (hyphen) if (hyphen)
{ {
*hyphen = '\0'; *hyphen = '\0';
hyphen++; hyphen++;
while (*hyphen && isblank (*hyphen)) hyphen++; while (*hyphen && isblank (*hyphen))
assert (*hyphen); /* Validation checks this. */ hyphen++;
assert (*hyphen); /* Validation checks this. */
/* A range. */ /* A range. */
range = (range_t*) g_malloc0 (sizeof (range_t)); range = (range_t *) g_malloc0 (sizeof (range_t));
range->start = atoi (element); range->start = atoi (element);
range->end = atoi (hyphen); range->end = atoi (hyphen);
range->type = tcp ? PORT_PROTOCOL_TCP : PORT_PROTOCOL_UDP; range->type = tcp ? PORT_PROTOCOL_TCP : PORT_PROTOCOL_UDP;
range->exclude = 0; range->exclude = 0;
array_add (ranges, range); array_add (ranges, range);
} }
else if (*element) else if (*element)
{ {
/* A single port. */ /* A single port. */
range = (range_t*) g_malloc0 (sizeof (range_t)); range = (range_t *) g_malloc0 (sizeof (range_t));
range->start = atoi (element); range->start = atoi (element);
range->end = range->start; range->end = range->start;
range->type = tcp ? PORT_PROTOCOL_TCP : PORT_PROTOCOL_UDP; range->type = tcp ? PORT_PROTOCOL_TCP : PORT_PROTOCOL_UDP;
range->exclude = 0; range->exclude = 0;
array_add (ranges, range); array_add (ranges, range);
} }
/* Else skip over empty range. */ /* Else skip over empty range. */
point += 1; point += 1;
 End of changes. 28 change blocks. 
32 lines changed or deleted 43 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)