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

Move get_config_dhcpinfo() function into src/dhcp/dhcp_config_utils.c #548

Merged
merged 2 commits into from
Mar 28, 2023

Conversation

aloisklink
Copy link
Contributor

In edgesec, we have two slightly different definitions of get_config_dhcpinfo(), in:

  • edgesec/src/config.c

    Lines 32 to 84 in da4f33e

    bool get_config_dhcpinfo(char *info, config_dhcpinfo_t *el) {
    UT_array *info_arr;
    utarray_new(info_arr, &ut_str_icd);
    if (split_string_array(info, ',', info_arr) < 0) {
    goto err;
    }
    if (!utarray_len(info_arr)) {
    goto err;
    }
    char **p = NULL;
    p = (char **)utarray_next(info_arr, p);
    if (*p != NULL) {
    errno = 0;
    el->vlanid = (int)strtol(*p, NULL, 10);
    if (errno == EINVAL)
    goto err;
    } else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    if (*p != NULL) {
    os_strlcpy(el->ip_addr_low, *p, OS_INET_ADDRSTRLEN);
    } else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    if (*p != NULL)
    os_strlcpy(el->ip_addr_upp, *p, OS_INET_ADDRSTRLEN);
    else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    if (*p != NULL)
    os_strlcpy(el->subnet_mask, *p, OS_INET_ADDRSTRLEN);
    else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    if (*p != NULL)
    os_strlcpy(el->lease_time, *p, DHCP_LEASE_TIME_SIZE);
    else
    goto err;
    utarray_free(info_arr);
    return true;
    err:
    utarray_free(info_arr);
    return false;
    }
  • bool get_config_dhcpinfo(char *info, config_dhcpinfo_t *el) {
    UT_array *info_arr;
    utarray_new(info_arr, &ut_str_icd);
    ssize_t count = split_string_array(info, ',', info_arr);
    log_trace("Number of substrings=%zd", count);
    if (!utarray_len(info_arr))
    goto err;
    char **p = NULL;
    p = (char **)utarray_next(info_arr, p);
    log_trace("vlanid=%s", *p);
    if (*p != NULL) {
    errno = 0;
    el->vlanid = (int)strtol(*p, NULL, 10);
    if (errno == EINVAL)
    goto err;
    } else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    log_trace("ip_addr_low=%s", *p);
    if (*p != NULL) {
    strcpy(el->ip_addr_low, *p);
    } else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    log_trace("ip_addr_upp=%s", *p);
    if (*p != NULL)
    strcpy(el->ip_addr_upp, *p);
    else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    log_trace("subnet_mask=%s", *p);
    if (*p != NULL)
    strcpy(el->subnet_mask, *p);
    else
    goto err;
    p = (char **)utarray_next(info_arr, p);
    log_trace("lease_time=%s", *p);
    if (*p != NULL)
    strcpy(el->lease_time, *p);
    else
    goto err;
    utarray_free(info_arr);
    return true;
    err:
    utarray_free(info_arr);
    return false;
    }

This PR proposes replacing these two definitions with a single definition in a new file called src/dhcp/dhcp_config_utils.c.

Minor differences:

  • I've taken the definition from the src/config.c file. The one in tests/dhcp/test_dnsmasq.c was almost exactly the same, except it had a bunch of log_trace() commands, which I removed.
  • I've made the info parameter const since we only read from it.

Move `get_config_dhcpinfo` from `src/config.` and
`tests/dhcp/test_dnsmasq.c` (where it was duplicated) into a new file,
`src/dhcp/dhcp_config_utils.c`.
Make the `info` command of `get_config_dhcpinfo()` const, as we
never modify it.
@aloisklink aloisklink added the refactor Refactoring code label Mar 28, 2023
@codecov
Copy link

codecov bot commented Mar 28, 2023

Codecov Report

Merging #548 (803d702) into main (da4f33e) will decrease coverage by 0.03%.
The diff coverage is 71.79%.

@@            Coverage Diff             @@
##             main     #548      +/-   ##
==========================================
- Coverage   54.14%   54.11%   -0.03%     
==========================================
  Files         144      145       +1     
  Lines       21043    20999      -44     
==========================================
- Hits        11394    11364      -30     
+ Misses       9649     9635      -14     
Impacted Files Coverage Δ
src/config.c 79.65% <ø> (+0.60%) ⬆️
tests/dhcp/test_dnsmasq.c 100.00% <ø> (+4.80%) ⬆️
src/dhcp/dhcp_config_utils.c 71.79% <71.79%> (ø)

... and 2 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@aloisklink aloisklink added this pull request to the merge queue Mar 28, 2023
Merged via the queue into main with commit 5a43f91 Mar 28, 2023
@aloisklink aloisklink deleted the refactor/dhcp_config_utils branch March 28, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactoring code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants