summaryrefslogtreecommitdiff
path: root/src/shared/conf-parser.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2020-10-29 00:01:02 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2020-10-29 14:23:49 +0900
commit0ec2a7a125e72edecf43d36c5b117b153c1a4ef3 (patch)
tree17f88a647a0dc5644d7e95ad61d6ead2c08cfbbb /src/shared/conf-parser.c
parentfc27088ae714d50e9703352253f7b89273e7097d (diff)
downloadsystemd-0ec2a7a125e72edecf43d36c5b117b153c1a4ef3.tar.gz
libsystemd-network: move config_parse_hwaddr() and config_parse_hwaddrs()
Diffstat (limited to 'src/shared/conf-parser.c')
-rw-r--r--src/shared/conf-parser.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index 524f57ff80..7798382cd8 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -11,6 +11,7 @@
#include "conf-files.h"
#include "conf-parser.h"
#include "def.h"
+#include "ether-addr-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
@@ -24,6 +25,7 @@
#include "process-util.h"
#include "rlimit-util.h"
#include "sd-id128.h"
+#include "set.h"
#include "signal-util.h"
#include "socket-util.h"
#include "string-util.h"
@@ -1244,4 +1246,106 @@ int config_parse_vlanprotocol(const char* unit,
return 0;
}
+int config_parse_hwaddr(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ _cleanup_free_ struct ether_addr *n = NULL;
+ struct ether_addr **hwaddr = data;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ if (isempty(rvalue)) {
+ *hwaddr = mfree(*hwaddr);
+ return 0;
+ }
+
+ n = new0(struct ether_addr, 1);
+ if (!n)
+ return log_oom();
+
+ r = ether_addr_from_string(rvalue, n);
+ if (r < 0) {
+ log_syntax(unit, LOG_WARNING, filename, line, r,
+ "Not a valid MAC address, ignoring assignment: %s", rvalue);
+ return 0;
+ }
+
+ free_and_replace(*hwaddr, n);
+
+ return 0;
+}
+
+int config_parse_hwaddrs(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ Set **hwaddrs = data;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ if (isempty(rvalue)) {
+ /* Empty assignment resets the list */
+ *hwaddrs = set_free_free(*hwaddrs);
+ return 0;
+ }
+
+ for (const char *p = rvalue;;) {
+ _cleanup_free_ char *word = NULL;
+ _cleanup_free_ struct ether_addr *n = NULL;
+
+ r = extract_first_word(&p, &word, NULL, 0);
+ if (r == 0)
+ return 0;
+ if (r == -ENOMEM)
+ return log_oom();
+ if (r < 0) {
+ log_syntax(unit, LOG_WARNING, filename, line, r,
+ "Invalid syntax, ignoring: %s", rvalue);
+ return 0;
+ }
+
+ n = new(struct ether_addr, 1);
+ if (!n)
+ return log_oom();
+
+ r = ether_addr_from_string(word, n);
+ if (r < 0) {
+ log_syntax(unit, LOG_WARNING, filename, line, r,
+ "Not a valid MAC address, ignoring: %s", word);
+ continue;
+ }
+
+ r = set_ensure_put(hwaddrs, &ether_addr_hash_ops, n);
+ if (r < 0)
+ return log_oom();
+ if (r > 0)
+ TAKE_PTR(n); /* avoid cleanup */
+ }
+}
+
DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent, "Failed to parse percent value");