Skip to content

Commit ff374b0

Browse files
committed
apply suggestions
1 parent 515c877 commit ff374b0

6 files changed

Lines changed: 60 additions & 47 deletions

File tree

base/hosts.c

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ is_hostname (const char *str)
466466
*
467467
* @return 1 if valid IPv6 CIDR-expressed block, 0 otherwise.
468468
*/
469-
static int
470-
is_cidr6_block (const char *str)
469+
int
470+
gvm_is_cidr6_block (const char *str)
471471
{
472472
long block;
473473
char *addr6_str, *block_str, *p;
@@ -503,12 +503,6 @@ is_cidr6_block (const char *str)
503503
return 1;
504504
}
505505

506-
int
507-
gvm_is_cidr6_block (const char *str)
508-
{
509-
return is_cidr6_block (str);
510-
}
511-
512506
/**
513507
* @brief Gets the network block value from a CIDR-expressed block string.
514508
* For "192.168.1.1/24" it is 24.
@@ -518,8 +512,8 @@ gvm_is_cidr6_block (const char *str)
518512
*
519513
* @return -1 if error, 0 otherwise.
520514
*/
521-
static int
522-
cidr6_get_block (const char *str, unsigned int *block)
515+
int
516+
gvm_cidr6_get_block (const char *str, unsigned int *block)
523517
{
524518
if (str == NULL || block == NULL)
525519
return -1;
@@ -530,12 +524,6 @@ cidr6_get_block (const char *str, unsigned int *block)
530524
return 0;
531525
}
532526

533-
int
534-
gvm_cidr6_get_block (const char *str, unsigned int *block)
535-
{
536-
return cidr6_get_block (str, block);
537-
}
538-
539527
/**
540528
* @brief Gets the IPv4 value from a CIDR-expressed block.
541529
* eg. For "192.168.1.10/24" it is "192.168.1.10".
@@ -545,8 +533,8 @@ gvm_cidr6_get_block (const char *str, unsigned int *block)
545533
*
546534
* @return -1 if error, 0 otherwise.
547535
*/
548-
static int
549-
cidr6_get_ip (const char *str, struct in6_addr *addr6)
536+
int
537+
gvm_cidr6_get_ip (const char *str, struct in6_addr *addr6)
550538
{
551539
gchar *addr6_str, *tmp;
552540

@@ -569,12 +557,6 @@ cidr6_get_ip (const char *str, struct in6_addr *addr6)
569557
return 0;
570558
}
571559

572-
int
573-
gvm_cidr6_get_ip (const char *str, struct in6_addr *addr6)
574-
{
575-
return cidr6_get_ip (str, addr6);
576-
}
577-
578560
/**
579561
* @brief Gets the first and last usable IPv4 addresses from a CIDR-expressed
580562
* block. eg. "192.168.1.0/24 would give 192.168.1.1 as first and 192.168.1.254
@@ -586,8 +568,9 @@ gvm_cidr6_get_ip (const char *str, struct in6_addr *addr6)
586568
*
587569
* @return -1 if error, 0 else.
588570
*/
589-
static int
590-
cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
571+
int
572+
gvm_cidr6_block_ips (const char *str, struct in6_addr *first,
573+
struct in6_addr *last)
591574
{
592575
unsigned int block;
593576
int i, j;
@@ -596,9 +579,9 @@ cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
596579
return -1;
597580

598581
/* Get IP and block values. */
599-
if (cidr6_get_block (str, &block) == -1)
582+
if (gvm_cidr6_get_block (str, &block) == -1)
600583
return -1;
601-
if (cidr6_get_ip (str, first) == -1)
584+
if (gvm_cidr6_get_ip (str, first) == -1)
602585
return -1;
603586
memcpy (&last->s6_addr, &first->s6_addr, 16);
604587

@@ -650,13 +633,6 @@ cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
650633
return 0;
651634
}
652635

653-
int
654-
gvm_cidr6_block_ips (const char *str, struct in6_addr *first,
655-
struct in6_addr *last)
656-
{
657-
return cidr6_block_ips (str, first, last);
658-
}
659-
660636
/**
661637
* @brief Checks if a buffer points to a valid long IPv6 range-expressed
662638
* network. "::fee5-::1:530" is valid.
@@ -869,7 +845,7 @@ gvm_get_host_type (const gchar *str_stripped)
869845
return HOST_TYPE_RANGE_LONG;
870846

871847
/* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
872-
if (is_cidr6_block (str_stripped))
848+
if (gvm_is_cidr6_block (str_stripped))
873849
return HOST_TYPE_CIDR6_BLOCK;
874850

875851
/* Check for short range-expressed networks "::1-ef12" */
@@ -1241,7 +1217,7 @@ gvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
12411217
struct in6_addr *);
12421218

12431219
if (host_type == HOST_TYPE_CIDR6_BLOCK)
1244-
ips_func = cidr6_block_ips;
1220+
ips_func = gvm_cidr6_block_ips;
12451221
else if (host_type == HOST_TYPE_RANGE6_SHORT)
12461222
ips_func = short_range6_network_ips;
12471223
else

boreas/alivedetection.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,6 @@ alive_detection_free (void *error)
462462
* gvm_host_t are freed by caller of start_alive_detection()! */
463463
g_hash_table_destroy (scanner.hosts_data->targethosts);
464464
g_free (scanner.hosts_data);
465-
g_free (&(scanner.ipv6_net)->net);
466-
g_free (scanner.ipv6_net);
467465

468466
/* Set error. */
469467
*(boreas_error_t *) error = error_out;

boreas/boreas_error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typedef enum
1313
{
1414
BOREAS_OPENING_SOCKET_FAILED = -100,
1515
BOREAS_SETTING_SOCKET_OPTION_FAILED,
16+
BOREAS_BIND_SOCKET_FAILED,
1617
BOREAS_NO_VALID_ALIVE_TEST_SPECIFIED,
1718
BOREAS_CLEANUP_ERROR,
1819
BOREAS_NO_SRC_ADDR_FOUND,

boreas/cli.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ init_cli_for_host_discovery (scanner_t *scanner, const char *net,
104104
error = init_ipv6_net_data (scanner, net);
105105
if (error != 0)
106106
{
107-
printf ("%s:Not possible to initialize data: %s", __func__,
107+
printf ("%s: Not possible to initialize data: %s", __func__,
108108
str_boreas_error (error));
109109
return error;
110110
}
@@ -130,6 +130,12 @@ free_cli (scanner_t *scanner, alive_test_t alive_test)
130130
g_hash_table_destroy (scanner->hosts_data->targethosts);
131131
g_free (scanner->hosts_data);
132132

133+
if (alive_test & ALIVE_TEST_IPV6_HOST_DISCOVERY)
134+
{
135+
g_free (scanner->ipv6_net->net);
136+
g_free (scanner->ipv6_net);
137+
}
138+
133139
return close_err;
134140
}
135141

@@ -262,6 +268,13 @@ run_cli_extended (gvm_hosts_t *hosts, alive_test_t alive_test,
262268
return NO_ERROR;
263269
}
264270

271+
/**
272+
* @brief GHFunc helper function to create a comma separated list of host
273+
*
274+
* @param[in] key host
275+
* @param [in] value Not used
276+
* @param[in/out] userdata comma separated list where new keys are appended to.
277+
*/
265278
static void
266279
create_host_list (gpointer key, gpointer value, gpointer *userdata)
267280
{
@@ -270,8 +283,9 @@ create_host_list (gpointer key, gpointer value, gpointer *userdata)
270283
g_string_append (host_str, key);
271284
g_string_append (host_str, ",");
272285
}
286+
273287
/**
274-
* @brief
288+
* @brief Runs a host discovery for large ipv6 network
275289
*
276290
* @param[in] net IPv6 network
277291
* @param[out] hosts_found Discovered alive hosts in comma separated list
@@ -316,14 +330,14 @@ run_cli_for_ipv6_network (const char *net, char **hosts_found,
316330
g_hash_table_foreach (scanner.hosts_data->alivehosts,
317331
(GHFunc) create_host_list, (gpointer) &host_str);
318332

319-
*hosts_found = g_strdup (host_str->str);
320-
g_string_free (host_str, TRUE);
333+
*hosts_found = g_string_free (host_str, FALSE);
321334
}
322335

323336
free_err = free_cli (&scanner, ALIVE_TEST_IPV6_HOST_DISCOVERY);
324337
if (free_err)
325338
{
326339
printf ("Error freeing scan data.\n");
340+
g_free (*hosts_found);
327341
return free_err;
328342
}
329343

boreas/sniffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ start_sniffer_thread (scanner_t *scanner, pthread_t *sniffer_thread_id)
290290
{
291291
int err;
292292

293-
if (0) // scanner->host_discovery)
293+
if (scanner->host_discovery)
294294
{
295295
char filter[256];
296296
snprintf (filter, sizeof (filter), "ip6 and ip6[40]=129 and dst %s",

boreas/util.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,17 +572,41 @@ init_ipv6_net_data (scanner_t *scanner, const char *net)
572572

573573
int opt_on = 1;
574574
soc = socket (AF_INET6, SOCK_RAW, IPPROTO_RAW);
575-
setsockopt (soc, IPPROTO_IPV6, IP_HDRINCL, (char *) &opt_on, sizeof (opt_on));
575+
if (soc < 0)
576+
{
577+
g_warning ("%s: failed to open ICPMV6 socket: %s", __func__,
578+
strerror (errno));
579+
error = BOREAS_OPENING_SOCKET_FAILED;
580+
}
581+
582+
if (setsockopt (soc, IPPROTO_IPV6, IP_HDRINCL, (char *) &opt_on,
583+
sizeof (opt_on))
584+
< 0)
585+
{
586+
g_warning ("%s: failed to set socket option IP_HDRINCL: %s", __func__,
587+
strerror (errno));
588+
error = BOREAS_SETTING_SOCKET_OPTION_FAILED;
589+
}
576590

577591
// Enable multicast
578-
setsockopt (soc, SOL_SOCKET, SO_BROADCAST, &opt_on, sizeof (opt_on));
592+
if (!error)
593+
{
594+
error = set_broadcast (soc);
595+
if (error != 0)
596+
return error;
597+
}
579598

580599
// Create source address
581600
memset (&socs, 0, sizeof (socs));
582601
socs.sin6_family = AF_INET6;
583602
socs.sin6_addr = scanner->ipv6_net->src;
584603
// and set the source address to the socket
585-
bind (soc, (struct sockaddr *) &socs, sizeof (socs));
604+
if (bind (soc, (struct sockaddr *) &socs, sizeof (socs)) < 0)
605+
{
606+
g_warning ("%s: failed to bind socket to source address: %s", __func__,
607+
strerror (errno));
608+
return BOREAS_BIND_SOCKET_FAILED;
609+
}
586610
scanner->icmpv6soc = soc;
587611

588612
return error;

0 commit comments

Comments
 (0)