Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

colinbs
Copy link
Member

@colinbs colinbs commented Oct 28, 2016

Closes issue #81
Uses same if-statements used in rtr.c
There still are return values for failed checks needed, suggestions are welcome.

@colinbs colinbs changed the title rtrlib/rtr: adds range checks for interval values in packets.c #81 rtrlib/rtr: adds range checks for interval values in packets.c Oct 28, 2016
@colinbs colinbs force-pushed the interval_range_checks branch from 5161af2 to a77d9a6 Compare October 28, 2016 12:58
@codecov-io
Copy link

codecov-io commented Oct 28, 2016

Codecov Report

Merging #98 into master will decrease coverage by 0.06%.
The diff coverage is 59.74%.

@@            Coverage Diff             @@
##           master      #98      +/-   ##
==========================================
- Coverage   65.72%   65.65%   -0.07%     
==========================================
  Files          16       16              
  Lines        2150     2213      +63     
==========================================
+ Hits         1413     1453      +40     
- Misses        737      760      +23

@waehlisch
Copy link
Member

waehlisch commented Oct 28, 2016

Please, do not merge. There are several open items:

  • There is no appropriate error handling.
  • There is no error log message in case that these values overwrite pre-configured values (see rtr.c). Actually, it is unclear to me if overwriting pre-configured values is a good option at all. I contacted Randy and Rob.

PS: Please, don't use verb conjugation (e.g., use "add ..." instead "adds ...") in commity message.

@colinbs colinbs changed the title rtrlib/rtr: adds range checks for interval values in packets.c rtrlib/rtr: add range checks for interval values in packets.c Oct 31, 2016
@smlng
Copy link
Member

smlng commented Nov 1, 2016

@colinbs, I agree with @waehlisch, before we merge we need real error handling. Further, though haven't had the time for a careful review, I think those values have to be passed through ntohl see here.

[EDIT] for the latter see #87, too.

@waehlisch
Copy link
Member

Some more feedback after talking with Rob.

(1) Should the router send an error PDU to the cache server if the cache server sends a timer value exceeding the spec values?

He suggests that the "client could just enforce the minimum and maximum values (eg, if the cache says something larger than max, router treats it as if cache had said max). I suppose one might have a knob to control whether the router does this or instead generates an Error."

Personally, I like the idea but it should be very well documented. The only error PDU that currently might fit is Corrupt Data (fatal).

(2) If the admin configured explicitly other values compared to the values sent by the cache, can the cache overwrite these values?

There is a clear statement that the router should not harm the cache. (Note that there is a trust relation between cache and router.)

By default, the router should accept the values communicated by the cache. However, in any case, a log message should be fired.

Furthermore, there should be a configuration knob that allows the router to keep the explicitly configured expiration interval.

@smlng
Copy link
Member

smlng commented Nov 3, 2016

If we want to have the behavior to be configurable this will atleast introduce a new call to the API, we should be careful there. Thus, for now I would recommend to just extend the API by a separate, additional call. If we change/uptdate/optimize the rtrlib API in the future (see #4, #86), this can be put into the config of the rtr_mgr.

@colinbs
Copy link
Member Author

colinbs commented Nov 7, 2016

So I guess it should be left as it is for now and be picked up again as soon as the API receives changes.

@smlng
Copy link
Member

smlng commented Nov 7, 2016

@colinbs: as I said above, an additional call -that is, no modification to existing API calls- should be safe right now. However, it might not be optimal but that can be fixed as soon as we update/cleanup the API.

@colinbs
Copy link
Member Author

colinbs commented Nov 16, 2016

Several things were changed/added:
To avoid magic numbers for the min/max values for interval boundaries they are now defined in rtr.h

There now is a (temporary) solution for different options regarding interval handling:
This is still WIP and right now only for demonstration/discussion purposes.

  • IGNORE_ANY: no interval values will be applied at all
  • ACCEPT_ANY: all interval values, in range or not, will be applied
  • DEFAULT_MIN_MAX: if interval values are not in range, apply min/max values
  • IGNORE_ON_FAILURE: if interval values are not in range, do nothing

A user should be able to apply one of these values to INTERVAL_OPTION. How he does this is not yet implemented and object of discussion.

@colinbs colinbs force-pushed the interval_range_checks branch from dd6e808 to 0f2f628 Compare November 21, 2016 13:41
rtrlib/rtr/rtr.h Outdated
#define RTR_DBG1(a) lrtr_dbg("RTR Socket: " a)

// min and max values for expiration time
static const uint16_t EXPIRATION_MIN = 600; // ten minutes
Copy link
Member

Choose a reason for hiding this comment

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

Use prefix RTR_ for all #defines

rtrlib/rtr/rtr.c Outdated
rtr_socket->tr_socket = tr;

if(refresh_interval > 86400 || refresh_interval < 1) {
if(refresh_interval > REFRESH_MAX || refresh_interval < REFRESH_MIN) {
Copy link
Member

@smlng smlng Dec 9, 2016

Choose a reason for hiding this comment

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

why not use the (new) rtr_check_interval_range function here and below?

return *((char *) pdu + 1);
}

static int rtr_check_interval_range(uint32_t interval, const int interval_type)
Copy link
Member

Choose a reason for hiding this comment

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

move this function to rtrlib/rtr.c to use it in other code parts as well, see below.


#define TEMPORARY_PDU_STORE_INCREMENT_VALUE 100
#define MAX_SUPPORTED_PDU_TYPE 10
#define INTERVAL_OPTION -1
Copy link
Member

Choose a reason for hiding this comment

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

this will not work, should be static int interval_option = SOME_DEFAULT_VALUE, and there should be a API call to set this parameter.

if (interv_retval == INSIDE_INTERVAL_RANGE) {
rtr_socket->expire_interval = ((struct pdu_end_of_data_v1 *) pdu)->expire_interval;
} else {
switch (INTERVAL_OPTION) {
Copy link
Member

Choose a reason for hiding this comment

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

won't work, see above. Use static variable.

rtr_socket->expire_interval = ((struct pdu_end_of_data_v1 *) pdu)->expire_interval;
break;
case IGNORE_ON_FAILURE:
// if interval values are out of bounds, ignore them without error PDU.
Copy link
Member

Choose a reason for hiding this comment

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

add debug output here, too

if (interv_retval == INSIDE_INTERVAL_RANGE) {
rtr_socket->retry_interval = ((struct pdu_end_of_data_v1 *) pdu)->retry_interval;
} else {
switch (INTERVAL_OPTION) {
Copy link
Member

Choose a reason for hiding this comment

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

Here is a lot of code duplication to handle the different intervals according to the interval_option.

Either write another helper function that just returns the correct value corresponding to interval_option and interval_type or expand/rewrite rtr_check_interval_range to return the value instead of just -1, 0, or 1,


// These modes let the user configure how
// received intervals should be handled.
enum interval_mode {
Copy link
Member

Choose a reason for hiding this comment

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

this enum might be needed for a future config API call, so might be good to move it into a header file.
Further, please add doxygen conform documentation for this enum then.

@colinbs colinbs force-pushed the interval_range_checks branch 12 times, most recently from 4c6b38b to e241131 Compare February 2, 2017 12:17
#define TEMPORARY_PDU_STORE_INCREMENT_VALUE 100
#define MAX_SUPPORTED_PDU_TYPE 10

static int interval_option = DEFAULT_MIN_MAX;
Copy link
Member

Choose a reason for hiding this comment

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

This is only visible here, but you also define it in rtr.c, hence it should be globally defined (and set) somewhere

rtrlib/rtr/rtr.c Outdated
#include "rtrlib/rtr/rtr.h"
#include "rtrlib/lib/utils.h"

static int interval_option = DEFAULT_MIN_MAX;
Copy link
Member

Choose a reason for hiding this comment

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

see comment above

rtrlib/rtr/rtr.c Outdated
minimum = RTR_RETRY_MIN;
maximum = RTR_RETRY_MAX;
break;
default:
Copy link
Member

Choose a reason for hiding this comment

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

add error message and return here?

rtrlib/rtr/rtr.c Outdated

int interv_retval = rtr_check_interval_range(interval, type);

if (interv_retval == INSIDE_INTERVAL_RANGE || interval_mode == ACCEPT_ANY) {
Copy link
Member

Choose a reason for hiding this comment

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

I would move the switch() case here, and check for INSIDE_INTERVAL_RANGE where required

rtrlib/rtr/rtr.c Outdated
} else {
int interv_retval = rtr_check_interval_range(refresh_interval, REFRESH);

if(interv_retval == INSIDE_INTERVAL_RANGE) {
Copy link
Member

@smlng smlng Feb 3, 2017

Choose a reason for hiding this comment

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

you could summarize the checks into one like:

if (rtr_check_interval_range(refresh_interval, REFRESH) != INSIDE_INTERVAL_RANGE || 
    rtr_check_interval_range(refresh_interval, EXPIRATION) != INSIDE_INTERVAL_RANGE || 
    rtr_check_interval_range(refresh_interval, RETRY) != INSIDE_INTERVAL_RANGE) {
    /* optional: print some error message? */
    return RTR_INVALID_PARAM;
}
/* otherwise not error, hence set all as given */

rtrlib/rtr/rtr.h Outdated
*/
enum interval_mode {
/** Ignore appliance of interval values at all. */
IGNORE_ANY = 0,
Copy link
Member

Choose a reason for hiding this comment

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

add comments for doxygen like this:

IGNORE_ANY = 0,     /*< Ignore appliance of interval values at all. */

Copy link
Member Author

Choose a reason for hiding this comment

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

Should I adjust the comments for the rtr_socket_state enum as well, while I'm at it? Or are these not relevant for Doxygen?

@fho fho closed this Feb 3, 2017
@fho fho reopened this Feb 3, 2017
@colinbs colinbs force-pushed the interval_range_checks branch from e580c25 to 281de67 Compare February 8, 2017 15:59
rtrlib/rtr/rtr.c Outdated
RTR_DBG1("Invalid interval mode. Mode remains unchanged.");
return;
}
RTR_DBG("Interval mode set to %s", txt);
Copy link
Member

Choose a reason for hiding this comment

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

This functions complexity is only related to this debug output, otherwise it should be just:

void set_interval_mode(int option)
{
    switch(option) {
    case IGNORE_ANY:
    case ACCEPT_ANY:
    case DEFAULT_MIN_MAX:
    case IGNORE_ON_FAILURE:
        interval_option = option;
        break;
    default:
         RTR_DBG1("Invalid interval mode. Mode remains unchanged.");
    }
}

rtrlib/rtr/rtr.c Outdated

int rtr_check_interval_range(uint32_t interval, int16_t minimum, int32_t maximum)
{
if(interval < minimum) {
Copy link
Member

Choose a reason for hiding this comment

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

add space if ( and braces are not discouraged by kernel coding style

rtrlib/rtr/rtr.c Outdated
switch (type) {
case EXPIRATION:
minimum = RTR_EXPIRATION_MIN;
maximum = RTR_EXPIRATION_MAX;
Copy link
Member

Choose a reason for hiding this comment

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

minimum, and maximum can be initialized before switch?

Copy link
Member Author

Choose a reason for hiding this comment

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

To prevent confusion on my side: should minimum and maximum be initialized before the switch statement like this?
uint16_t minimum = 0;
uint32_t maximum = 0;

Copy link
Member

Choose a reason for hiding this comment

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

ah sorry, forget about it thought min/max are always the same, but they are not :/ my bad.

rtrlib/rtr/rtr.h Outdated
*/
enum interval_mode {
IGNORE_ANY = 0, /*< Ignore appliance of interval values at all. */

Copy link
Member

Choose a reason for hiding this comment

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

remove empty lines, here and below

Copy link
Member

Choose a reason for hiding this comment

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

and same here, enums start with 0 and increment by 1 as a default.

rtrlib/rtr/rtr.c Outdated

if (interv_retval == INSIDE_INTERVAL_RANGE || interval_mode == ACCEPT_ANY) {
apply_interval_value(rtr_socket, interval, type);
} else {
Copy link
Member

@smlng smlng Feb 27, 2017

Choose a reason for hiding this comment

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

IMHO this would be [more] readable without switch () case rewrite for instance like:

if (interv_retval == INSIDE_INTERVAL_RANGE || interval_mode == ACCEPT_ANY) {
    apply_interval_value(rtr_socket, interval, type);
}
if else (interval_mode == DEFAULT_MIN_MAX) {
    if (interv_retval == BELOW_INTERVAL_RANGE)
        apply_interval_value(rtr_socket, minimum, type);
    else
        apply_interval_value(rtr_socket, maximum, type);
}
else {
    RTR_DBG();
}

assert_int_equal(rtr_socket->expire_interval, pdu_eod->expire_interval);

/* test checks that determine if value is inside range */
retval = 2;
Copy link
Member

Choose a reason for hiding this comment

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

why is retval set here, or below, when it is replaced by function return value afterwards?

rtrlib/rtr/rtr.h Outdated
};

enum interval_type {
EXPIRATION = 0,
Copy link
Member

Choose a reason for hiding this comment

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

explicite values are not needed here, enums start with 0 by default and increment by 1, too.

rtrlib/rtr/rtr.h Outdated
* @return ABOVE_INTERVAL_RANGE If the given interval is above the specified range.
* @return INSIDE_INTERVAL_RANGE If the given interval is inside the specified range.
*/
int rtr_check_interval_range(uint32_t interval, int16_t minimum, int32_t maximum);
Copy link
Member

Choose a reason for hiding this comment

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

I think its a bit strange that min and max have different types, int16_t and int32_t specifically, which could be unsigned by the way, right?!

Copy link
Member Author

@colinbs colinbs Aug 15, 2017

Choose a reason for hiding this comment

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

Yeah, it looks kind of strange. I used int16_t since the minimum values for the intervals fit into it. Not sure if this is even necessary.
Anyway, I will change it to uint32_t.

Copy link
Member

Choose a reason for hiding this comment

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

the intervals are stored as uint32_t in the socket struct anyway - so makes sense to use unsigned

@smlng
Copy link
Member

smlng commented Jan 26, 2018

please rebase

@smlng smlng added this to the release 0.6.0 milestone Feb 8, 2018
@smlng
Copy link
Member

smlng commented Feb 8, 2018

@colinbs ping?

Use new rtr_check_interval_range function in rtr.c
Refactor code duplication.
@colinbs colinbs force-pushed the interval_range_checks branch from f4e2040 to d91a225 Compare February 14, 2018 10:48
Copy link
Member

@mroethke mroethke left a comment

Choose a reason for hiding this comment

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

Furthermore there are a few style issues in packets.[ch] and rtr.[ch] however enforcing coding style in those files probably does not make much sense, considering their current state.

UNUSED(state);

struct rtr_socket *rtr_socket = malloc(1024);
struct pdu_end_of_data_v1 *pdu_eod = malloc(1024);
Copy link
Member

Choose a reason for hiding this comment

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

There is no need here to allocate 2*1K of memory here, either use sizeof or allocate static memory. preferably the later.

{
UNUSED(state);

struct rtr_socket *rtr_socket = malloc(1024);
Copy link
Member

Choose a reason for hiding this comment

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

dito


struct rtr_socket rtr_socket;

set_interval_mode(&rtr_socket, IGNORE_ANY);
Copy link
Member

Choose a reason for hiding this comment

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

you need to add an include for rtr_mgr.h, Travis reports compiler warning:

/rtrlib/rtrlib/tests/unittests/test_packets_static.c:478:2: warning: implicit declaration of function ‘set_interval_mode’ [-Wimplicit-function-declaration]
  set_interval_mode(&rtr_socket, IGNORE_ANY);
  ^

@smlng
Copy link
Member

smlng commented Mar 9, 2018

please squash the 2 unittest commits into one

@colinbs colinbs force-pushed the interval_range_checks branch from b801306 to 4d2998e Compare March 9, 2018 13:24
@smlng
Copy link
Member

smlng commented Mar 9, 2018

@mroethke if you agree, please merge (without squash)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants