-
Notifications
You must be signed in to change notification settings - Fork 24
dhcp: add client implementation #416
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2025 Anthony Harivel | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <gr_api.h> | ||
| #include <gr_infra.h> | ||
| #include <gr_net_types.h> | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| typedef enum dhcp_state : uint8_t { | ||
| DHCP_STATE_INIT = 0, | ||
| DHCP_STATE_SELECTING, | ||
| DHCP_STATE_REQUESTING, | ||
| DHCP_STATE_BOUND, | ||
| DHCP_STATE_RENEWING, | ||
| DHCP_STATE_REBINDING, | ||
| } dhcp_state_t; | ||
|
|
||
| struct gr_dhcp_status { | ||
| uint16_t iface_id; | ||
| dhcp_state_t state; | ||
| ip4_addr_t server_ip; | ||
| ip4_addr_t assigned_ip; | ||
| uint32_t lease_time; | ||
| uint32_t renewal_time; // T1 | ||
| uint32_t rebind_time; // T2 | ||
| }; | ||
|
|
||
| #define GR_DHCP_MODULE 0xd4c9 | ||
|
|
||
| // list //////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #define GR_DHCP_LIST REQUEST_TYPE(GR_DHCP_MODULE, 0x01) | ||
|
|
||
| // struct gr_dhcp_list_req { }; | ||
|
|
||
| // STREAM(struct gr_dhcp_status); | ||
|
|
||
| // start /////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #define GR_DHCP_START REQUEST_TYPE(GR_DHCP_MODULE, 0x02) | ||
|
|
||
| struct gr_dhcp_start_req { | ||
| uint16_t iface_id; | ||
| }; | ||
|
|
||
| // struct gr_dhcp_start_resp { }; | ||
|
|
||
| // stop //////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #define GR_DHCP_STOP REQUEST_TYPE(GR_DHCP_MODULE, 0x03) | ||
|
|
||
| struct gr_dhcp_stop_req { | ||
| uint16_t iface_id; | ||
| }; | ||
|
|
||
| // struct gr_dhcp_stop_resp { }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # Copyright (c) 2025 Anthony Harivel | ||
|
|
||
| api_headers += files('gr_dhcp.h') | ||
| api_inc += include_directories('.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2025 Anthony Harivel | ||
|
|
||
| #include <gr_cli.h> | ||
| #include <gr_cli_iface.h> | ||
| #include <gr_dhcp.h> | ||
| #include <gr_net_types.h> | ||
| #include <gr_table.h> | ||
|
|
||
| #include <ecoli.h> | ||
| #include <libsmartcols.h> | ||
|
|
||
| #include <errno.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| static cmd_status_t dhcp_enable_cmd(struct gr_api_client *c, const struct ec_pnode *p) { | ||
| const char *iface_name = arg_str(p, "IFACE"); | ||
| struct gr_dhcp_start_req req; | ||
| struct gr_iface *iface; | ||
|
|
||
| iface = iface_from_name(c, iface_name); | ||
| if (iface == NULL) | ||
| return CMD_ERROR; | ||
|
|
||
| req.iface_id = iface->id; | ||
| free(iface); | ||
|
|
||
| if (gr_api_client_send_recv(c, GR_DHCP_START, sizeof(req), &req, NULL) < 0) | ||
| return CMD_ERROR; | ||
|
|
||
| return CMD_SUCCESS; | ||
| } | ||
|
|
||
| static cmd_status_t dhcp_disable_cmd(struct gr_api_client *c, const struct ec_pnode *p) { | ||
| const char *iface_name = arg_str(p, "IFACE"); | ||
| struct gr_dhcp_stop_req req; | ||
| struct gr_iface *iface; | ||
|
|
||
| iface = iface_from_name(c, iface_name); | ||
| if (iface == NULL) | ||
| return CMD_ERROR; | ||
|
|
||
| req.iface_id = iface->id; | ||
| free(iface); | ||
|
|
||
| if (gr_api_client_send_recv(c, GR_DHCP_STOP, sizeof(req), &req, NULL) < 0) | ||
| return CMD_ERROR; | ||
|
|
||
| return CMD_SUCCESS; | ||
| } | ||
|
|
||
| static const char *dhcp_state_str(enum dhcp_state state) { | ||
| switch (state) { | ||
| case DHCP_STATE_INIT: | ||
| return "INIT"; | ||
| case DHCP_STATE_SELECTING: | ||
| return "SELECTING"; | ||
| case DHCP_STATE_REQUESTING: | ||
| return "REQUESTING"; | ||
| case DHCP_STATE_BOUND: | ||
| return "BOUND"; | ||
| case DHCP_STATE_RENEWING: | ||
| return "RENEWING"; | ||
| case DHCP_STATE_REBINDING: | ||
| return "REBINDING"; | ||
| default: | ||
| return "UNKNOWN"; | ||
| } | ||
| } | ||
|
|
||
| static cmd_status_t dhcp_show_cmd(struct gr_api_client *c, const struct ec_pnode *) { | ||
| const struct gr_dhcp_status *status; | ||
| struct libscols_table *table; | ||
| int ret; | ||
|
|
||
| table = scols_new_table(); | ||
| if (table == NULL) | ||
| return CMD_ERROR; | ||
|
|
||
| scols_table_new_column(table, "INTERFACE", 0, 0); | ||
| scols_table_new_column(table, "STATE", 0, 0); | ||
| scols_table_new_column(table, "ADDRESS", 0, 0); | ||
| scols_table_new_column(table, "SERVER", 0, 0); | ||
| scols_table_new_column(table, "LEASE", 0, SCOLS_FL_RIGHT); | ||
|
|
||
| gr_api_client_stream_foreach (status, ret, c, GR_DHCP_LIST, 0, NULL) { | ||
| struct libscols_line *line = scols_table_new_line(table, NULL); | ||
| struct gr_iface *iface = iface_from_id(c, status->iface_id); | ||
|
|
||
| if (iface != NULL) { | ||
| scols_line_sprintf(line, 0, "%s", iface->name); | ||
| free(iface); | ||
| } else { | ||
| scols_line_sprintf(line, 0, "%u", status->iface_id); | ||
| } | ||
|
|
||
| scols_line_sprintf(line, 1, "%s", dhcp_state_str(status->state)); | ||
|
|
||
| if (status->assigned_ip != 0) { | ||
| scols_line_sprintf(line, 2, IP4_F, &status->assigned_ip); | ||
| } else { | ||
| scols_line_sprintf(line, 2, "-"); | ||
| } | ||
|
|
||
| if (status->server_ip != 0) { | ||
| scols_line_sprintf(line, 3, IP4_F, &status->server_ip); | ||
| } else { | ||
| scols_line_sprintf(line, 3, "-"); | ||
| } | ||
|
|
||
| if (status->lease_time != 0) | ||
| scols_line_sprintf(line, 4, "%us", status->lease_time); | ||
| else | ||
| scols_line_sprintf(line, 4, "-"); | ||
| } | ||
|
|
||
| if (ret < 0) { | ||
| scols_unref_table(table); | ||
| return CMD_ERROR; | ||
| } | ||
|
|
||
| scols_print_table(table); | ||
| scols_unref_table(table); | ||
|
|
||
| return CMD_SUCCESS; | ||
| } | ||
|
|
||
| static int ctx_init(struct ec_node *root) { | ||
| int ret; | ||
|
|
||
| ret = CLI_COMMAND( | ||
| CLI_CONTEXT(root, CTX_ARG("dhcp", "DHCP client.")), | ||
| "enable IFACE", | ||
| dhcp_enable_cmd, | ||
| "Enable DHCP on interface.", | ||
| with_help( | ||
| "Interface name.", | ||
| ec_node_dyn("IFACE", complete_iface_names, INT2PTR(GR_IFACE_TYPE_UNDEF)) | ||
| ) | ||
| ); | ||
| if (ret < 0) | ||
| return ret; | ||
|
|
||
| ret = CLI_COMMAND( | ||
| CLI_CONTEXT(root, CTX_ARG("dhcp", "DHCP client.")), | ||
| "disable IFACE", | ||
| dhcp_disable_cmd, | ||
| "Disable DHCP on interface.", | ||
| with_help( | ||
| "Interface name.", | ||
| ec_node_dyn("IFACE", complete_iface_names, INT2PTR(GR_IFACE_TYPE_UNDEF)) | ||
| ) | ||
| ); | ||
| if (ret < 0) | ||
| return ret; | ||
|
|
||
| ret = CLI_COMMAND( | ||
| CLI_CONTEXT(root, CTX_ARG("dhcp", "DHCP client.")), | ||
| "show", | ||
| dhcp_show_cmd, | ||
| "Show DHCP client status." | ||
| ); | ||
| if (ret < 0) | ||
| return ret; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static struct cli_context ctx = { | ||
| .name = "dhcp", | ||
| .init = ctx_init, | ||
| }; | ||
|
|
||
| static void __attribute__((constructor, used)) init(void) { | ||
| cli_context_register(&ctx); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # Copyright (c) 2025 Anthony Harivel | ||
|
|
||
| cli_src += files( | ||
| 'dhcp.c', | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using a more specific type, such as
clock_t?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clock_tis for measuring CPU time (clock()), NOT calendar time. Using it for lease timestamps would be incorrect.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
time_tmay be more appropriate ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time_tis for absolute timestamps (calendar time), not durations. uint32_t max = 4,294,967,295 seconds ≈ 136 years (way more than any DHCP lease lol). I usetime_tfor timestamps (lease_start) butuint32_tfor durations (lease_time, renewal_time, rebind_time)