@@ -29,24 +29,17 @@ using Addresses = std::vector<ip4::Addr>;
2929template <typename T>
3030Addresses parse_iface (T& obj)
3131{
32- Expects (obj.IsArray ());
32+ Expects (obj.HasMember (" address" ));
33+ Expects (obj.HasMember (" netmask" ));
34+ Expects (obj.HasMember (" gateway" ));
3335
34- // Iface can either be empty: [] or contain 3 to 4 IP's (see Inet static config)
35- if (not obj.Empty ())
36- {
37- Expects (obj.Size () >= 3 and obj.Size () <= 4 );
38-
39- Addresses addresses;
40-
41- for (auto & addr : obj.GetArray ())
42- {
43- addresses.emplace_back (std::string{addr.GetString ()});
44- }
45-
46- return addresses;
47- }
36+ ip4::Addr address{obj[" address" ].GetString ()};
37+ ip4::Addr netmask{obj[" netmask" ].GetString ()};
38+ ip4::Addr gateway{obj[" gateway" ].GetString ()};
39+ ip4::Addr dns = (not obj.HasMember (" dns" )) ? gateway : ip4::Addr{obj[" dns" ].GetString ()};
4840
49- return {};
41+ Addresses addresses{address, netmask, gateway, dns};
42+ return addresses;
5043}
5144
5245inline void config_stack (Inet<IP4>& stack, const Addresses& addrs)
@@ -69,23 +62,45 @@ void configure(const rapidjson::Value& net)
6962
7063 Expects (net.IsArray () && " Member net is not an array" );
7164
72- int N = 0 ;
73- for (auto & val : net.GetArray ())
65+ auto configs = net.GetArray ();
66+ if (configs.Size () > Super_stack::inet ().ip4_stacks ().size ())
67+ MYINFO (" ! WARNING: Found more configs than there are interfaces" );
68+ // Iterate all interfaces in config
69+ for (auto & val : configs)
7470 {
75- if (N >= static_cast <int >(Super_stack::inet ().ip4_stacks ().size ()))
76- break ;
71+ Expects (val.HasMember (" iface" )
72+ && " Missing member iface - don't know which interface to configure" );
73+
74+ auto N = val[" iface" ].GetInt ();
7775
7876 auto & stack = Super_stack::get<IP4>(N);
79- // static configs
80- if (val.IsArray ())
77+
78+ // if config is not set, just ignore
79+ if (not val.HasMember (" config" ))
80+ continue ;
81+
82+ std::string method = val[" config" ].GetString ();
83+
84+ if (method == " dhcp" )
85+ {
86+ stack.negotiate_dhcp (5.0 );
87+ }
88+ else if (method == " static" )
8189 {
8290 config_stack (stack, parse_iface (val));
8391 }
84- else if (val. IsString () and strcmp (val. GetString (), " dhcp" ) == 0 )
92+ else if (method == " dhcp-with-fallback " )
8593 {
86- stack.negotiate_dhcp (5.0 );
94+ auto addresses = parse_iface (val);
95+ auto static_cfg = [addresses, &stack] (bool timeout)
96+ {
97+ if (timeout) {
98+ MYINFO (" DHCP timeout (%s) - falling back to static configuration" , stack.ifname ().c_str ());
99+ config_stack (stack, addresses);
100+ }
101+ };
102+ stack.negotiate_dhcp (5.0 , static_cfg);
87103 }
88- ++N;
89104 }
90105
91106 MYINFO (" Configuration complete" );
0 commit comments