配置文件如下:
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.10/24
nameservers:
addresses:
- 8.8.8.8
- 114.114.114.114
routes:
- to: default
via: 192.168.1.1
table: 101
eth1:
dhcp4: false
addresses: [192.168.137.147/24]
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
routes:
- to: default
via: 192.168.137.1
table: 102
eth2:
dhcp4: false
addresses: [192.168.3.10/24]
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
routes:
- to: default
via: 192.168.3.1
table: 103
enP1p17s0:
dhcp4: false
addresses: [192.168.137.150/24]
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
routes:
- to: default
via: 192.168.137.1
table: 104
enP2p33s0:
dhcp4: false
addresses: [192.168.5.10/24]
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
routes:
- to: default
via: 192.168.5.1
table: 105
对应的数据结构如下:
struct nameservers {
char **addresses;
unsigned n_addresses;
};
struct routes_ethx {
char *to;
char *via;
char *table;
};
struct ethx {
char *dhcp4;
char **addresses;
unsigned n_addresses;
struct nameservers *nameserver;
struct routes_ethx *route;
uint64_t route_count;
};
struct ethernets {
struct ethx *eth0;
struct ethx *eth1;
struct ethx *eth2;
struct ethx *enP1p17s0;
struct ethx *enP2p33s0;
};
struct networks {
char *version;
struct ethernets *ethernet;
};
struct netcfg {
struct networks *network;
};
static const cyaml_schema_value_t string_ptr_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char, 0, CYAML_UNLIMITED),
};
static const cyaml_schema_field_t routes_fields_schema[] = {
CYAML_FIELD_STRING_PTR(
"to", CYAML_FLAG_POINTER,
struct routes_ethx, to, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR(
"via", CYAML_FLAG_POINTER,
struct routes_ethx, via, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR(
"table", CYAML_FLAG_POINTER,
struct routes_ethx, table, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const cyaml_schema_field_t nameserver_fields_schema[] = {
CYAML_FIELD_SEQUENCE_COUNT(
"addresses", CYAML_FLAG_POINTER,
struct nameservers, addresses, n_addresses,
&string_ptr_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const cyaml_schema_value_t route_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT,
struct routes_ethx, routes_fields_schema),
};
static const cyaml_schema_field_t ethx_fields_schema[] = {
CYAML_FIELD_STRING_PTR(
"dhcp4", CYAML_FLAG_POINTER,
struct ethx, dhcp4, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE_COUNT(
"addresses", CYAML_FLAG_POINTER,
struct ethx, addresses, n_addresses,
&string_ptr_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_MAPPING_PTR(
"nameservers", CYAML_FLAG_POINTER,
struct ethx, nameserver, nameserver_fields_schema),
CYAML_FIELD_SEQUENCE(
"routes", CYAML_FLAG_POINTER,
struct ethx, route,
&route_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const cyaml_schema_field_t ethernets_fields_schema[] = {
CYAML_FIELD_MAPPING_PTR(
"eth0", CYAML_FLAG_POINTER,
struct ethernets, eth0, ethx_fields_schema),
CYAML_FIELD_MAPPING_PTR(
"eth1", CYAML_FLAG_POINTER,
struct ethernets, eth1, ethx_fields_schema),
CYAML_FIELD_MAPPING_PTR(
"eth2", CYAML_FLAG_POINTER,
struct ethernets, eth2, ethx_fields_schema),
CYAML_FIELD_MAPPING_PTR(
"enP1p17s0", CYAML_FLAG_POINTER,
struct ethernets, enP1p17s0, ethx_fields_schema),
CYAML_FIELD_MAPPING_PTR(
"enP2p33s0", CYAML_FLAG_POINTER,
struct ethernets, enP2p33s0, ethx_fields_schema),
CYAML_FIELD_END
};
static const cyaml_schema_field_t network_fields_schema[] = {
CYAML_FIELD_STRING_PTR(
"version", CYAML_FLAG_POINTER,
struct networks, version, 0, CYAML_UNLIMITED),
CYAML_FIELD_MAPPING_PTR(
"ethernets", CYAML_FLAG_POINTER,
struct networks, ethernet, ethernets_fields_schema),
CYAML_FIELD_END
};
static const cyaml_schema_field_t netcfg_fields_schema[] = {
CYAML_FIELD_MAPPING_PTR(
"network", CYAML_FLAG_POINTER,
struct netcfg, network, network_fields_schema),
CYAML_FIELD_END
};
static const cyaml_schema_value_t netcfg_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct netcfg, netcfg_fields_schema),
};
具体的解析过程按照官方示例代码修改即可