diff --git a/os_tools/linux/config/network_interface_file.anubis b/os_tools/linux/config/network_interface_file.anubis index 69b0538..abc8077 100644 --- a/os_tools/linux/config/network_interface_file.anubis +++ b/os_tools/linux/config/network_interface_file.anubis @@ -11,10 +11,13 @@ read system/logger.anubis read system/string.anubis read tools/basis.anubis read tools/streams.anubis +read types/network_types.anubis +read config/dns_config.anubis +read tools/os.anubis delete_entry find_entry -replace_entry +replace_entry = ok add_entry @@ -31,6 +34,97 @@ public type Network_entry: blank, comment(String comment), section(Network_type net_type, List(Network_Parameter) params). + + +define Maybe(Network_Parameter) + _update_param + ( + Network_Parameter param, + String entry_name, + String value + )= + if param is + { + comment(_) then failure, + conf_nv(name,_) then + if name = entry_name then success(conf_nv(name, value)) + else failure, + conf_nvc(name,_,c) then + if name = entry_name then success(conf_nvc(name, value,c)) + else + failure, + } + . +define List(Network_Parameter) + _update_entry + ( + List(Network_Parameter) params, + String entry_name, + String value, + List(Network_Parameter) so_far, + Bool add, //mean add the value if not found + Bool found // + )= + if params is + { + [] then if add & found = false then + reverse([conf_nv(entry_name, value) . so_far]) + else + reverse(so_far), + + [ h . t ] then + with result = _update_param(h,entry_name,value), + if result is + { + failure then _update_entry(t, entry_name, value, [h . so_far], add, found), + success(entry) then _update_entry(t, entry_name, value, [entry . so_far], add, true), + } + + }. + +define Network_entry + _update_entry_type + ( + Network_type net_type, + List(Network_Parameter) params, + String name_port, // i.e eth0,br0 + String entry_name, + String value, + List(Network_Parameter) so_far, + Bool add + )= + if name_port = net_type.name then + if _update_param(net_type.param,entry_name,value) is + { + failure then section(net_type,_update_entry(params,entry_name,value,so_far, add, false)), + success(param) then section(iface(name_port,param),params) //if the value changed is network_type + } + else section(net_type,params) + . + +define List(Network_entry) + update_entry + ( + List(Network_entry) entries, + String name_port, // i.e eth0,br0 + String entry_name, + String value, + List(Network_entry) so_far, + Bool add //for add an entry or not + )= + if entries is + { + [] then reverse(so_far), + [h . t] then + if h is + { + blank then update_entry(t, name_port, entry_name, value, [h . so_far], add), + comment(_) then update_entry(t, name_port, entry_name, value, [h . so_far], add), + section(net_type,params) then update_entry(t, name_port, entry_name, value, + [_update_entry_type(net_type, params, name_port, entry_name, value, [], add) . so_far], add) + } + } + . define String to_String @@ -106,13 +200,11 @@ define Maybe(Network_Parameter) success(value) then if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is { - failure then + failure then success(conf_nv(name, value)), //println("conf_nv found name="+name+" value="+value); - success(conf_nv(name, value)), - - success(comment) then + success(comment) then success(conf_nvc(name, value, comment)) //println("conf_nvc found name="+name+" value="+value+" comment="+comment); - success(conf_nvc(name, value, comment)) + } } }. @@ -177,7 +269,7 @@ define List(Network_Parameter) if read_line(s) is { failure then reverse(so_far), - success(line) then + success(line) then println(line); if nth((Int)0, line) is { failure then reverse(so_far), @@ -212,16 +304,15 @@ define Network_entry if h = "auto" then if t is { - [] then + [] then blank, //println("WARNING unamed section ["+h+"]"); - blank, [ name . t ] then //println("auto found with name ["+name+"]"); //read the second line of the section where the type is located i.e. iface if read_line(s) is { failure then blank, - success(type_line) then + success(type_line) then //println("get type line for "+name+"["+trim(type_line)+"]"); if get_type(trim(type_line), name) is { @@ -282,11 +373,16 @@ public define List(Network_entry) read_interface_entries(make_stream(f), []) }. +public define String + dump_interface_file + = + to_String(read_interface("/calexium/interfaces"),""). + public define Maybe(One) write_interface ( - String file_name, - List(Network_entry) entries + String file_name, + List(Network_entry) entries )= if (Maybe(RWStream))file(file_name, new) is { @@ -294,6 +390,39 @@ public define Maybe(One) success(f) then success(forget(reliable_write(f, to_byte_array(to_String(entries, ""))))) }. +//ipconfig(String ip, +// String mask, +// String gateway, +// List(String) dns). + + +define One + write_ip_config_linux + ( + IpConfig ip_config, + ) = + with file_name = "/etc/network/interfaces", + interfaces_file = read_interface(file_name), + interfaces_file = update_entry(interfaces_file, "br0", "address",ip_config.ip,[], false), + interfaces_file = update_entry(interfaces_file, "br0", "netmask",ip_config.mask,[], false), + interfaces_file = update_entry(interfaces_file, "br0","gateway",ip_config.gateway,[], false), + forget(write_interface(file_name, interfaces_file)) + . + + + +public define Maybe(One) + write_network_config_linux + ( + IpConfig wan_config + ) = + + write_ip_config_linux(wan_config); + write_dns_list(wan_config.dns); + sync_fs; + success(unique). + + /** here tool for testing * anbexec parse_interface @@ -309,7 +438,7 @@ global define One { [] then print("usage : parse_interface "), [ file_name . t ] then - forget(write_interface(file_name+".dump",read_interface(file_name))) + write_ip_config_linux(ipconfig("192.168.4.0","255.255.0.255","0.0.0.0",[])) } . -- libgit2 0.21.4