1 /* Copyright (c) 2008, 2009 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <arpa/inet.h>
25 #include <netinet/in.h>
31 #include "dynamic-string.h"
38 #define THIS_MODULE VLM_cfg
41 /* XXX This file really needs a unit test! For a while, cfg_get_string(0,
42 * "bridge.a.controller") would return the value of
43 * "bridge.a.controller.in-band", if it existed, and I'm really not certain
44 * that the fix didn't break other things. */
46 /* Configuration file name. */
47 static char *cfg_name;
49 /* Put the temporary file in the same directory as cfg_name, so that
50 * they are guaranteed to be in the same file system and therefore we can
51 * rename() tmp_name over cfg_name. */
52 static char *tmp_name;
54 /* Lock information. */
55 static char *lock_name;
56 static int lock_fd = -1;
58 /* Flag to indicate whether local modifications have been made. */
61 static uint8_t cfg_cookie[CFG_COOKIE_LEN];
63 /* Current configuration. Maintained in sorted order. */
64 static struct svec cfg = SVEC_EMPTY_INITIALIZER;
66 static bool has_double_dot(const char *key, size_t len);
67 static bool is_valid_key(const char *key, size_t len,
68 const char *file_name, int line_number,
70 static char *parse_section(const char *file_name, int line_number,
72 static void parse_setting(const char *file_name, int line_number,
73 const char *section, const char *);
74 static int compare_key(const char *a, const char *b);
75 static char **find_key_le(const char *key);
76 static char **find_key_ge(const char *key);
77 static char *find_key(const char *);
78 static bool parse_mac(const char *, uint8_t mac[6]);
79 static bool parse_dpid(const char *, uint64_t *);
80 static bool is_key(const char *);
81 static bool is_int(const char *);
82 static bool is_bool(const char *);
83 static const char *extract_value(const char *key);
84 static const char *get_nth_value(int idx, const char *key);
85 static bool is_type(const char *s, enum cfg_flags);
87 #define CC_ALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
88 #define CC_DIGIT "0123456789"
89 #define CC_ALNUM CC_ALPHA CC_DIGIT
90 #define CC_SPACE " \t\r\n\v"
92 #define CC_FILE_NAME CC_ALNUM "._-"
93 #define CC_KEY CC_ALNUM "._-@$:+"
101 /* Sets 'file_name' as the configuration file read by cfg_read(). Returns 0 on
102 * success, otherwise a positive errno value if 'file_name' cannot be opened.
104 * This function does not actually read the named file or directory. Use
105 * cfg_read() to (re)read all the configuration files. */
107 cfg_set_file(const char *file_name)
117 cfg_name = lock_name = tmp_name = NULL;
120 /* Make sure that we can open this file for reading. */
121 fd = open(file_name, O_RDONLY);
127 cfg_name = xstrdup(file_name);
129 /* Put the temporary file in the same directory as cfg_name, so that they
130 * are guaranteed to be in the same file system, to guarantee that
131 * rename(tmp_name, cfg_name) will work. */
132 tmp_name = xasprintf("%s.~tmp~", file_name);
134 /* Put the lock file in the same directory as cfg_name, but prefixed by
135 * a dot so as not to garner administrator interest. */
136 slash = strrchr(file_name, '/');
138 lock_name = xasprintf("%.*s/.%s.~lock~",
139 (int) (slash - file_name), file_name, slash + 1);
141 lock_name = xasprintf(".%s.~lock~", file_name);
144 VLOG_INFO("using \"%s\" as configuration file, \"%s\" as lock file",
145 file_name, lock_name);
152 struct sha1_ctx context;
156 for (i = 0; i < cfg.n; i++) {
157 sha1_update(&context, cfg.names[i], strlen(cfg.names[i]));
158 sha1_update(&context, "\n", 1);
160 sha1_final(&context, cfg_cookie);
165 /* Reads all of the configuration files or directories that have been added
166 * with cfg_add_file(), merges their content. Any previous configuration is
167 * replaced. Returns 0 if successful, otherwise a positive errno value. */
182 /* Save old configuration data and clear the active configuration. */
184 svec_swap(&old_cfg, &cfg);
186 /* Read new configuration. */
187 VLOG_DBG("reading configuration from %s", cfg_name);
189 file = fopen(cfg_name, "r");
191 VLOG_ERR("failed to open \"%s\": %s", cfg_name, strerror(errno));
192 svec_terminate(&cfg);
199 while (!ds_get_line(&ds, file)) {
200 const char *s = ds_cstr(&ds);
201 size_t indent = strspn(s, CC_SPACE);
205 if (*s == '#' || *s == '\0') {
206 /* Ignore comments and lines that contain only white space. */
207 } else if (*s == '[') {
210 section = parse_section(cfg_name, line_number, s);
212 VLOG_ERR("%s:%d: ignoring indented section header",
213 cfg_name, line_number);
215 } else if (indent && !section) {
216 VLOG_ERR("%s:%d: ignoring indented line outside any section",
217 cfg_name, line_number);
223 parse_setting(cfg_name, line_number, section, s);
230 svec_terminate(&cfg);
235 if (VLOG_IS_DBG_ENABLED()) {
236 struct svec removed, added;
239 svec_diff(&old_cfg, &cfg, &removed, NULL, &added);
240 if (removed.n || added.n) {
241 VLOG_DBG("configuration changes:");
242 for (i = 0; i < removed.n; i++) {
243 VLOG_DBG("-%s", removed.names[i]);
245 for (i = 0; i < added.n; i++) {
246 VLOG_DBG("+%s", added.names[i]);
249 VLOG_DBG("configuration unchanged");
251 svec_destroy(&added);
252 svec_destroy(&removed);
254 svec_destroy(&old_cfg);
261 /* Fills 'svec' with the entire configuration file. */
263 cfg_get_all(struct svec *svec)
266 svec_append(svec, &cfg);
270 cfg_get_cookie(uint8_t *cookie)
276 memcpy(cookie, cfg_cookie, sizeof(cfg_cookie));
284 COVERAGE_INC(cfg_unlock);
291 open_lockfile(const char *name)
294 /* Try to open an existing lock file. */
295 int fd = open(name, O_RDWR);
298 } else if (errno != ENOENT) {
299 VLOG_WARN("%s: failed to open lock file: %s",
300 name, strerror(errno));
304 /* Try to create a new lock file. */
305 VLOG_INFO("%s: lock file does not exist, creating", name);
306 fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
309 } else if (errno != EEXIST) {
310 VLOG_WARN("%s: failed to create lock file: %s",
311 name, strerror(errno));
315 /* Someone else created the lock file. Try again. */
320 try_lock(int fd, bool block)
323 memset(&l, 0, sizeof l);
325 l.l_whence = SEEK_SET;
328 return fcntl(fd, block ? F_SETLKW : F_SETLK, &l) == -1 ? errno : 0;
331 /* Locks the configuration file against modification by other processes and
332 * re-reads it from disk.
334 * The 'timeout' specifies the maximum number of milliseconds to wait for the
335 * config file to become free. Use 0 to avoid waiting or INT_MAX to wait
338 * Returns 0 on success, otherwise a positive errno value. */
340 cfg_lock(uint8_t *cookie, int timeout)
343 long long int elapsed = 0;
345 uint8_t curr_cookie[CFG_COOKIE_LEN];
348 COVERAGE_INC(cfg_lock);
355 /* Open lock file. */
356 fd = open_lockfile(lock_name);
361 /* Try to lock it. This will block (if 'timeout' > 0). */
362 error = try_lock(fd, timeout > 0);
364 elapsed = time_msec() - start;
370 /* Lock failed. Close the lock file and reopen it on the next
371 * iteration, just in case someone deletes it underneath us (even
372 * though that should not happen). */
374 if (error != EINTR) {
375 /* Hard error, give up. */
376 COVERAGE_INC(cfg_lock_error);
377 VLOG_WARN("%s: failed to lock file "
378 "(after %lld ms, with %d-ms timeout): %s",
379 lock_name, elapsed, timeout, strerror(error));
383 /* Probably, the periodic timer set up by time_init() woke up us. Just
384 * check whether it's time to give up. */
385 if (timeout != INT_MAX && elapsed >= timeout) {
386 COVERAGE_INC(cfg_lock_timeout);
387 VLOG_WARN("%s: giving up on lock file after %lld ms",
391 COVERAGE_INC(cfg_lock_retry);
394 VLOG_WARN("%s: waited %lld ms for lock file", lock_name, elapsed);
401 cfg_get_cookie(curr_cookie);
403 if (memcmp(curr_cookie, cookie, sizeof *curr_cookie)) {
404 /* Configuration has changed, so reject. */
414 do_write_config(const void *data, size_t len)
419 file = fopen(tmp_name, "w");
421 VLOG_WARN("could not open %s for writing: %s",
422 tmp_name, strerror(errno));
426 fwrite(data, 1, len, file);
428 /* This is essentially equivalent to:
429 * error = ferror(file) || fflush(file) || fclose(file);
430 * but it doesn't short-circuit, so that it always closes 'file'. */
431 error = ferror(file);
432 error = fflush(file) || error;
433 error = fclose(file) || error;
435 VLOG_WARN("problem writing to %s: %s", tmp_name, strerror(errno));
439 if (rename(tmp_name, cfg_name) < 0) {
440 VLOG_WARN("could not rename %s to %s: %s",
441 tmp_name, cfg_name, strerror(errno));
450 /* Write the current configuration into the configuration file. Returns 0 if
451 * successful, otherwise a negative errno value. */
460 ? svec_join(&cfg, "\n", "\n")
461 : xstrdup("# This file intentionally left blank.\n"));
462 retval = do_write_config(content, strlen(content));
469 cfg_write_data(uint8_t *data, size_t len)
471 int retval = do_write_config(data, len);
478 /* Returns true if the configuration has changed since the last time it was
479 * read or written. */
487 cfg_buf_put(struct ofpbuf *buffer)
491 for (i = 0; i < cfg.n; i++) {
492 ofpbuf_put(buffer, cfg.names[i], strlen(cfg.names[i]));
493 ofpbuf_put(buffer, "\n", 1);
497 /* Formats the printf()-style format string in the parameter 'format', which
498 * must be the function's last parameter, into string variable 'dst'. The
499 * function is responsible for freeing 'dst'. */
500 #define FORMAT_KEY(FORMAT, DST) \
503 va_start(args__, FORMAT); \
504 (DST) = xvasprintf(FORMAT, args__); \
508 /* Returns true if the configuration includes a key named 'key'. */
510 cfg_has(const char *key_, ...)
515 FORMAT_KEY(key_, key);
516 retval = find_key(key) != NULL;
522 cfg_is_valid(enum cfg_flags flags, const char *key_, ...)
524 char *key, **first, **last, **p;
528 FORMAT_KEY(key_, key);
529 first = find_key_le(key);
530 last = find_key_ge(key);
532 retval = ((!(flags & CFG_REQUIRED) || n)
533 && (!(flags & CFG_MULTIPLE) || n <= 1));
534 for (p = first; retval && p < last; p++) {
535 retval = is_type(strchr(*p, '=') + 1, flags);
541 /* Returns true if the configuration includes at least one key whose name
542 * begins with 'section' followed by a dot. */
544 cfg_has_section(const char *section_, ...)
552 va_start(args, section_);
553 ds_put_format_valist(§ion, section_, args);
554 ds_put_char(§ion, '.');
557 for (p = cfg.names; *p; p++) { /* XXX this is inefficient */
558 if (!strncmp(section.string, *p, section.length)) {
564 ds_destroy(§ion);
568 /* Returns the number of values for the given 'key'. The return value is 0 if
569 * no values exist for 'key'. */
571 cfg_count(const char *key_, ...)
576 FORMAT_KEY(key_, key);
577 retval = find_key_ge(key) - find_key_le(key);
582 /* Fills 'svec' with all of the immediate subsections of 'section'. For
583 * example, if 'section' is "bridge" and keys bridge.a, bridge.b, bridge.b.c,
584 * and bridge.c.x.y.z exist, then 'svec' would be initialized to a, b, and
585 * c. The caller must first initialize 'svec'. */
587 cfg_get_subsections(struct svec *svec, const char *section_, ...)
594 va_start(args, section_);
595 ds_put_format_valist(§ion, section_, args);
596 ds_put_char(§ion, '.');
600 for (p = cfg.names; *p; p++) { /* XXX this is inefficient */
601 if (!strncmp(section.string, *p, section.length)) {
602 const char *ss = *p + section.length;
603 size_t ss_len = strcspn(ss, ".=");
604 svec_add_nocopy(svec, xmemdup0(ss, ss_len));
608 ds_destroy(§ion);
612 cfg_add_entry(const char *entry_, ...)
616 FORMAT_KEY(entry_, entry);
617 svec_add_nocopy(&cfg, entry);
619 svec_terminate(&cfg);
624 cfg_del_entry(const char *entry_, ...)
628 FORMAT_KEY(entry_, entry);
629 svec_del(&cfg, entry);
630 svec_terminate(&cfg);
636 cfg_del_section(const char *section_, ...)
643 va_start(args, section_);
644 ds_put_format_valist(§ion, section_, args);
645 ds_put_char(§ion, '.');
648 for (p = cfg.names; *p; p++) {
649 if (!strncmp(section.string, *p, section.length)) {
655 svec_terminate(&cfg);
657 ds_destroy(§ion);
662 cfg_del_match(const char *pattern_, ...)
664 bool matched = false;
668 FORMAT_KEY(pattern_, pattern);
670 for (p = cfg.names; *p; p++) {
671 if (!fnmatch(pattern, *p, 0)) {
679 svec_terminate(&cfg);
686 /* Fills 'svec' with all of the key-value pairs that match shell glob pattern
687 * 'pattern'. The caller must first initialize 'svec'. */
689 cfg_get_matches(struct svec *svec, const char *pattern_, ...)
694 FORMAT_KEY(pattern_, pattern);
696 for (p = cfg.names; *p; p++) {
697 if (!fnmatch(pattern, *p, 0)) {
705 /* Fills 'svec' with all of the key-value pairs that have sections that
706 * begin with 'section'. The caller must first initialize 'svec'. */
708 cfg_get_section(struct svec *svec, const char *section_, ...)
715 va_start(args, section_);
716 ds_put_format_valist(§ion, section_, args);
717 ds_put_char(§ion, '.');
720 for (p = cfg.names; *p; p++) { /* XXX this is inefficient */
721 if (!strncmp(section.string, *p, section.length)) {
725 ds_destroy(§ion);
728 /* Returns the value numbered 'idx' of 'key'. Returns a null pointer if 'idx'
729 * is greater than or equal to cfg_count(key). The caller must not modify or
730 * free the returned string or retain its value beyond the next call to
733 cfg_get_string(int idx, const char *key_, ...)
738 FORMAT_KEY(key_, key);
739 retval = get_nth_value(idx, key);
744 /* Returns the value numbered 'idx' of 'key'. Returns a null pointer if 'idx'
745 * is greater than or equal to cfg_count(key) or if the value 'idx' of 'key' is
746 * not a valid key. The caller must not modify or free the returned string or
747 * retain its value beyond the next call to cfg_read(). */
749 cfg_get_key(int idx, const char *key_, ...)
751 const char *value, *retval;
754 FORMAT_KEY(key_, key);
755 value = get_nth_value(idx, key);
756 retval = value && is_key(value) ? value : NULL;
761 /* Returns the value numbered 'idx' of 'key', converted to an integer. Returns
762 * 0 if 'idx' is greater than or equal to cfg_count(key) or if the value 'idx'
763 * of 'key' is not a valid integer. */
765 cfg_get_int(int idx, const char *key_, ...)
771 FORMAT_KEY(key_, key);
772 value = get_nth_value(idx, key);
773 retval = value && is_int(value) ? atoi(value) : 0;
778 /* Returns the value numbered 'idx' of 'key', converted to a boolean value.
779 * Returns false if 'idx' is greater than or equal to cfg_count(key) or if the
780 * value 'idx' of 'key' is not a valid boolean. */
782 cfg_get_bool(int idx, const char *key_, ...)
788 FORMAT_KEY(key_, key);
789 value = get_nth_value(idx, key);
790 retval = value && is_bool(value) ? !strcmp(value, "true") : false;
795 /* Returns the value numbered 'idx' of 'key', converted to an IP address in
796 * network byte order. Returns 0 if 'idx' is greater than or equal to
797 * cfg_count(key) or if the value 'idx' of 'key' is not a valid IP address (as
798 * determined by inet_aton()). */
800 cfg_get_ip(int idx, const char *key_, ...)
806 FORMAT_KEY(key_, key);
807 value = get_nth_value(idx, key);
808 if (!value || !inet_aton(value, &addr)) {
809 addr.s_addr = htonl(0);
815 /* Returns the value numbered 'idx' of 'key', converted to an MAC address in
816 * host byte order. Returns 0 if 'idx' is greater than or equal to
817 * cfg_count(key) or if the value 'idx' of 'key' is not a valid MAC address in
818 * the format "##:##:##:##:##:##". */
820 cfg_get_mac(int idx, const char *key_, ...)
822 uint8_t mac[ETH_ADDR_LEN];
826 FORMAT_KEY(key_, key);
827 value = get_nth_value(idx, key);
828 if (!value || !parse_mac(value, mac)) {
829 memset(mac, 0, sizeof mac);
832 return eth_addr_to_uint64(mac);
835 /* Returns the value numbered 'idx' of 'key', parsed as an datapath ID.
836 * Returns 0 if 'idx' is greater than or equal to cfg_count(key) or if the
837 * value 'idx' of 'key' is not a valid datapath ID consisting of exactly 12
838 * hexadecimal digits. */
840 cfg_get_dpid(int idx, const char *key_, ...)
846 FORMAT_KEY(key_, key);
847 value = get_nth_value(idx, key);
848 if (!value || !parse_dpid(value, &dpid)) {
855 /* Returns the value numbered 'idx' of 'key', converted to an integer. Returns
856 * -1 if 'idx' is greater than or equal to cfg_count(key) or if the value 'idx'
857 * of 'key' is not a valid integer between 0 and 4095. */
859 cfg_get_vlan(int idx, const char *key_, ...)
865 FORMAT_KEY(key_, key);
866 value = get_nth_value(idx, key);
867 if (value && is_int(value)) {
868 retval = atoi(value);
869 if (retval < 0 || retval > 4095) {
879 /* Fills 'svec' with all of the string values of 'key'. The caller must
880 * first initialize 'svec'. */
882 cfg_get_all_strings(struct svec *svec, const char *key_, ...)
887 FORMAT_KEY(key_, key);
889 for (p = find_key_le(key), q = find_key_ge(key); p < q; p++) {
890 svec_add(svec, extract_value(*p));
895 /* Fills 'svec' with all of the values of 'key' that are valid keys.
896 * Values of 'key' that are not valid keys are omitted. The caller
897 * must first initialize 'svec'. */
899 cfg_get_all_keys(struct svec *svec, const char *key_, ...)
904 FORMAT_KEY(key_, key);
906 for (p = find_key_le(key), q = find_key_ge(key); p < q; p++) {
907 const char *value = extract_value(*p);
909 svec_add(svec, value);
916 has_double_dot(const char *key, size_t len)
921 for (i = 0; i < len - 1; i++) {
922 if (key[i] == '.' && key[i + 1] == '.') {
931 is_valid_key(const char *key, size_t len,
932 const char *file_name, int line_number, const char *id)
935 VLOG_ERR("%s:%d: missing %s name", file_name, line_number, id);
937 } else if (key[0] == '.') {
938 VLOG_ERR("%s:%d: %s name \"%.*s\" begins with invalid character '.'",
939 file_name, line_number, id, (int) len, key);
941 } else if (key[len - 1] == '.') {
942 VLOG_ERR("%s:%d: %s name \"%.*s\" ends with invalid character '.'",
943 file_name, line_number, id, (int) len, key);
945 } else if (has_double_dot(key, len)) {
946 VLOG_ERR("%s:%d: %s name \"%.*s\" contains '..', which is not allowed",
947 file_name, line_number, id, (int) len, key);
955 parse_section(const char *file_name, int line_number, const char *s)
962 /* Skip [ and any white space. */
964 s += strspn(s, CC_SPACE);
966 /* Obtain the section name. */
967 len = strspn(s, CC_KEY);
968 if (!is_valid_key(s, len, file_name, line_number, "section")) {
971 ds_put_buffer(§ion, s, len);
974 /* Obtain the subsection name, if any. */
975 s += strspn(s, CC_SPACE);
978 len = strspn(s, CC_KEY);
979 if (!is_valid_key(s, len, file_name, line_number, "subsection")) {
982 ds_put_char(§ion, '.');
983 ds_put_buffer(§ion, s, len);
986 VLOG_ERR("%s:%d: missing '\"' following subsection name",
987 file_name, line_number);
991 s += strspn(s, CC_SPACE);
996 VLOG_ERR("%s:%d: missing ']' following section name",
997 file_name, line_number);
1001 s += strspn(s, CC_SPACE);
1003 VLOG_ERR("%s:%d: trailing garbage following ']'",
1004 file_name, line_number);
1008 return ds_cstr(§ion);
1011 ds_destroy(§ion);
1016 parse_setting(const char *file_name, int line_number, const char *section,
1019 struct ds key = DS_EMPTY_INITIALIZER;
1020 struct ds value = DS_EMPTY_INITIALIZER;
1024 ds_put_format(&key, "%s.", section);
1027 /* Obtain the key. */
1028 len = strspn(s, CC_KEY);
1030 VLOG_ERR("%s:%d: missing key name", file_name, line_number);
1033 if (!is_valid_key(s, len, file_name, line_number, "key")) {
1036 ds_put_buffer(&key, s, len);
1040 s += strspn(s, CC_SPACE);
1042 VLOG_ERR("%s:%d: missing '=' following key", file_name, line_number);
1046 s += strspn(s, CC_SPACE);
1048 /* Obtain the value. */
1049 ds_put_cstr(&value, s);
1050 while (value.length > 0 && strchr(CC_SPACE, ds_last(&value))) {
1054 /* Add the setting. */
1055 svec_add_nocopy(&cfg, xasprintf("%s=%s", ds_cstr(&key), ds_cstr(&value)));
1063 compare_key(const char *a, const char *b)
1066 int ac = *a == '\0' || *a == '=' ? INT_MAX : *a;
1067 int bc = *b == '\0' || *b == '=' ? INT_MAX : *b;
1069 return ac < bc ? -1 : 1;
1070 } else if (ac == INT_MAX) {
1078 /* Returns the address of the greatest configuration string with a key less
1079 * than or equal to 'key'. Returns the address of the null terminator if all
1080 * configuration strings are greater than 'key'. */
1082 find_key_le(const char *key)
1087 int half = len >> 1;
1088 int middle = low + half;
1089 if (compare_key(cfg.names[middle], key) < 0) {
1096 return &cfg.names[low];
1099 /* Returns the address of the least configuration string with a key greater
1100 * than or equal to 'key'. Returns the address of the null terminator if all
1101 * configuration strings are less than 'key'. */
1103 find_key_ge(const char *key)
1108 int half = len >> 1;
1109 int middle = low + half;
1110 if (compare_key(cfg.names[middle], key) > 0) {
1117 return &cfg.names[low];
1121 find_key(const char *key)
1123 char **p = find_key_le(key);
1124 return p < &cfg.names[cfg.n] && !compare_key(*p, key) ? *p : NULL;
1128 parse_mac(const char *s, uint8_t mac[6])
1130 return (sscanf(s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
1131 == ETH_ADDR_SCAN_COUNT);
1135 parse_dpid(const char *s, uint64_t *dpid)
1137 if (strlen(s) == 12 && strspn(s, "0123456789abcdefABCDEF") == 12) {
1138 *dpid = strtoll(s, NULL, 16);
1146 is_key(const char *s)
1148 /* XXX needs to check the same things as is_valid_key() too. */
1149 return *s && s[strspn(s, CC_KEY)] == '\0';
1153 is_int(const char *s)
1155 return *s && s[strspn(s, CC_DIGIT)] == '\0';
1159 is_bool(const char *s)
1161 return !strcmp(s, "true") || !strcmp(s, "false");
1165 extract_value(const char *key)
1167 const char *p = strchr(key, '=');
1168 return p ? p + 1 : NULL;
1172 get_nth_value(int idx, const char *key)
1174 char **p = find_key_le(key);
1175 char **q = find_key_ge(key);
1176 return idx < q - p ? extract_value(p[idx]) : NULL;
1180 is_type(const char *s, enum cfg_flags flags)
1182 uint8_t mac[ETH_ADDR_LEN];
1183 struct in_addr addr;
1186 return (flags & CFG_STRING
1187 || (flags & CFG_KEY && is_key(s))
1188 || (flags & CFG_INT && is_int(s))
1189 || (flags & CFG_BOOL && is_bool(s))
1190 || (flags & CFG_IP && inet_aton(s, &addr))
1191 || (flags & CFG_MAC && parse_mac(s, mac))
1192 || (flags & CFG_DPID && parse_dpid(s, &dpid)));