Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addr pool in rule #550

Merged
merged 153 commits into from
Feb 14, 2024
Merged

Conversation

NikitaUnisikhin
Copy link
Collaborator

@NikitaUnisikhin NikitaUnisikhin commented Dec 11, 2023

-- Added address to the rule. Address supported IPv4/IPv6 with mask and hostname
-- Сhanged the logs taking into account the new parameter
-- Сreated a module for working with addresses (address.c) also used the module in hba
-- Added tests for new functionality

int od_address_range_read_prefix(od_address_range_t *address_range, char *prefix)
{
char *end = NULL;
long len = strtoul(prefix, &end, 10);
Copy link
Contributor

@AndrewOvvv AndrewOvvv Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to write an unsigned long value to a long variable? I think we should either change the type of the variable to unsigned long or use the strtol() function to convert the value to long.

Copy link
Collaborator Author

@NikitaUnisikhin NikitaUnisikhin Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len can be <= 0 (str 55)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why do you use strtoul (to unsigned long)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, thanks for the comment, I'll fix it

if (len > 32)
return -1;
struct sockaddr_in *addr = (struct sockaddr_in *)&address_range->mask;
long mask;
Copy link
Contributor

@AndrewOvvv AndrewOvvv Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to store the mask as a long variable, when it can be perceived as a 32-bit integer? It might be more convenient to save it as an int32 or a uint32 type, since we do so on line 44 and in the call to the function on line 47. In both cases, it would seem more suitable to use a uint32 variable to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is not good idea to used signed type for mask. There're exist some UB moments with signed integer types and bitwise operations

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed

return (int) ch1 - (int) ch2;
}
if (ch1 == 0)
break;
Copy link
Contributor

@AndrewOvvv AndrewOvvv Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we believe that s1 is shorter than s2? Why are we only examining ch1 and not ch2?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's expected that ch1==ch2 at this point...
but, idk, there's strcasecmp() https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcasecmp.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case is considered in the next iteration of the loop

found = false;
for (gai = gai_result; gai; gai = gai->ai_next)
{
if (gai->ai_addr->sa_family == client_sa->ss_family)
Copy link
Contributor

@AndrewOvvv AndrewOvvv Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but doesn't the "od_address_equals" function do all the things that happen inside this "if"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed

Copy link
Collaborator

@x4m x4m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me, however feels very leaky. Let's make sure all allocations are free()ed correctly.

bool od_address_range_equals(od_address_range_t *first, od_address_range_t *second)
{
if (first->is_hostname == second->is_hostname)
return first->string_value == second->string_value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

od_address_strcasecmp()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed

return (int) ch1 - (int) ch2;
}
if (ch1 == 0)
break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's expected that ch1==ch2 at this point...
but, idk, there's strcasecmp() https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcasecmp.html

return 1;
}

uint32 od_address_bswap32(uint32 x)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW we have od_hba_bswap32().
But I'd prefer to have od_bswap32() once and for all :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done

sources/router.c Outdated
@@ -348,14 +349,24 @@ od_router_status_t od_router_route(od_router_t *router, od_client_t *client)

/* match latest version of route rule */
od_rule_t *rule;

struct sockaddr_storage sa;
if (!client->is_watchdog) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we exempt watchdog? Can we exampt all outgoing clients, not just watchdog?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, now we exempt all internal clients

return address_range;
}

int od_address_range_copy(od_address_range_t *src, od_address_range_t *dst)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe free() hostname?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't use malloc

@@ -178,6 +178,8 @@ void od_rules_rule_free(od_rule_t *rule)
free(rule->db_name);
if (rule->user_name)
free(rule->user_name);
if (rule->address_range.string_value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe free address_range too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't use malloc

long mask;
struct sockaddr_in *addr =
(struct sockaddr_in *)&address_range->mask;
uint32 mask;
if (len > 0)
mask = (0xffffffffUL << (32 - (int)len)) & 0xffffffffUL;
Copy link
Contributor

@AndrewOvvv AndrewOvvv Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that you now don't need "& 0xffffffffUL" here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed

@x4m x4m merged commit f60df91 into yandex:master Feb 14, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants