Commit 5ee747e828adb5743ac3e3fe180abdcdb5f1e6fc
1 parent
521656bc
add ip_config_linux
Showing
1 changed file
with
142 additions
and
13 deletions
Show diff stats
os_tools/linux/config/network_interface_file.anubis
| @@ -11,10 +11,13 @@ read system/logger.anubis | @@ -11,10 +11,13 @@ read system/logger.anubis | ||
| 11 | read system/string.anubis | 11 | read system/string.anubis |
| 12 | read tools/basis.anubis | 12 | read tools/basis.anubis |
| 13 | read tools/streams.anubis | 13 | read tools/streams.anubis |
| 14 | +read types/network_types.anubis | ||
| 15 | +read config/dns_config.anubis | ||
| 16 | +read tools/os.anubis | ||
| 14 | 17 | ||
| 15 | delete_entry | 18 | delete_entry |
| 16 | find_entry | 19 | find_entry |
| 17 | -replace_entry | 20 | +replace_entry = ok |
| 18 | add_entry | 21 | add_entry |
| 19 | 22 | ||
| 20 | 23 | ||
| @@ -31,6 +34,97 @@ public type Network_entry: | @@ -31,6 +34,97 @@ public type Network_entry: | ||
| 31 | blank, | 34 | blank, |
| 32 | comment(String comment), | 35 | comment(String comment), |
| 33 | section(Network_type net_type, List(Network_Parameter) params). | 36 | section(Network_type net_type, List(Network_Parameter) params). |
| 37 | + | ||
| 38 | + | ||
| 39 | +define Maybe(Network_Parameter) | ||
| 40 | + _update_param | ||
| 41 | + ( | ||
| 42 | + Network_Parameter param, | ||
| 43 | + String entry_name, | ||
| 44 | + String value | ||
| 45 | + )= | ||
| 46 | + if param is | ||
| 47 | + { | ||
| 48 | + comment(_) then failure, | ||
| 49 | + conf_nv(name,_) then | ||
| 50 | + if name = entry_name then success(conf_nv(name, value)) | ||
| 51 | + else failure, | ||
| 52 | + conf_nvc(name,_,c) then | ||
| 53 | + if name = entry_name then success(conf_nvc(name, value,c)) | ||
| 54 | + else | ||
| 55 | + failure, | ||
| 56 | + } | ||
| 57 | + . | ||
| 58 | +define List(Network_Parameter) | ||
| 59 | + _update_entry | ||
| 60 | + ( | ||
| 61 | + List(Network_Parameter) params, | ||
| 62 | + String entry_name, | ||
| 63 | + String value, | ||
| 64 | + List(Network_Parameter) so_far, | ||
| 65 | + Bool add, //mean add the value if not found | ||
| 66 | + Bool found // | ||
| 67 | + )= | ||
| 68 | + if params is | ||
| 69 | + { | ||
| 70 | + [] then if add & found = false then | ||
| 71 | + reverse([conf_nv(entry_name, value) . so_far]) | ||
| 72 | + else | ||
| 73 | + reverse(so_far), | ||
| 74 | + | ||
| 75 | + [ h . t ] then | ||
| 76 | + with result = _update_param(h,entry_name,value), | ||
| 77 | + if result is | ||
| 78 | + { | ||
| 79 | + failure then _update_entry(t, entry_name, value, [h . so_far], add, found), | ||
| 80 | + success(entry) then _update_entry(t, entry_name, value, [entry . so_far], add, true), | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + }. | ||
| 84 | + | ||
| 85 | +define Network_entry | ||
| 86 | + _update_entry_type | ||
| 87 | + ( | ||
| 88 | + Network_type net_type, | ||
| 89 | + List(Network_Parameter) params, | ||
| 90 | + String name_port, // i.e eth0,br0 | ||
| 91 | + String entry_name, | ||
| 92 | + String value, | ||
| 93 | + List(Network_Parameter) so_far, | ||
| 94 | + Bool add | ||
| 95 | + )= | ||
| 96 | + if name_port = net_type.name then | ||
| 97 | + if _update_param(net_type.param,entry_name,value) is | ||
| 98 | + { | ||
| 99 | + failure then section(net_type,_update_entry(params,entry_name,value,so_far, add, false)), | ||
| 100 | + success(param) then section(iface(name_port,param),params) //if the value changed is network_type | ||
| 101 | + } | ||
| 102 | + else section(net_type,params) | ||
| 103 | + . | ||
| 104 | + | ||
| 105 | +define List(Network_entry) | ||
| 106 | + update_entry | ||
| 107 | + ( | ||
| 108 | + List(Network_entry) entries, | ||
| 109 | + String name_port, // i.e eth0,br0 | ||
| 110 | + String entry_name, | ||
| 111 | + String value, | ||
| 112 | + List(Network_entry) so_far, | ||
| 113 | + Bool add //for add an entry or not | ||
| 114 | + )= | ||
| 115 | + if entries is | ||
| 116 | + { | ||
| 117 | + [] then reverse(so_far), | ||
| 118 | + [h . t] then | ||
| 119 | + if h is | ||
| 120 | + { | ||
| 121 | + blank then update_entry(t, name_port, entry_name, value, [h . so_far], add), | ||
| 122 | + comment(_) then update_entry(t, name_port, entry_name, value, [h . so_far], add), | ||
| 123 | + section(net_type,params) then update_entry(t, name_port, entry_name, value, | ||
| 124 | + [_update_entry_type(net_type, params, name_port, entry_name, value, [], add) . so_far], add) | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + . | ||
| 34 | 128 | ||
| 35 | define String | 129 | define String |
| 36 | to_String | 130 | to_String |
| @@ -106,13 +200,11 @@ define Maybe(Network_Parameter) | @@ -106,13 +200,11 @@ define Maybe(Network_Parameter) | ||
| 106 | success(value) then | 200 | success(value) then |
| 107 | if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is | 201 | if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is |
| 108 | { | 202 | { |
| 109 | - failure then | 203 | + failure then success(conf_nv(name, value)), |
| 110 | //println("conf_nv found name="+name+" value="+value); | 204 | //println("conf_nv found name="+name+" value="+value); |
| 111 | - success(conf_nv(name, value)), | ||
| 112 | - | ||
| 113 | - success(comment) then | 205 | + success(comment) then success(conf_nvc(name, value, comment)) |
| 114 | //println("conf_nvc found name="+name+" value="+value+" comment="+comment); | 206 | //println("conf_nvc found name="+name+" value="+value+" comment="+comment); |
| 115 | - success(conf_nvc(name, value, comment)) | 207 | + |
| 116 | } | 208 | } |
| 117 | } | 209 | } |
| 118 | }. | 210 | }. |
| @@ -177,7 +269,7 @@ define List(Network_Parameter) | @@ -177,7 +269,7 @@ define List(Network_Parameter) | ||
| 177 | if read_line(s) is | 269 | if read_line(s) is |
| 178 | { | 270 | { |
| 179 | failure then reverse(so_far), | 271 | failure then reverse(so_far), |
| 180 | - success(line) then | 272 | + success(line) then println(line); |
| 181 | if nth((Int)0, line) is | 273 | if nth((Int)0, line) is |
| 182 | { | 274 | { |
| 183 | failure then reverse(so_far), | 275 | failure then reverse(so_far), |
| @@ -212,16 +304,15 @@ define Network_entry | @@ -212,16 +304,15 @@ define Network_entry | ||
| 212 | if h = "auto" then | 304 | if h = "auto" then |
| 213 | if t is | 305 | if t is |
| 214 | { | 306 | { |
| 215 | - [] then | 307 | + [] then blank, |
| 216 | //println("WARNING unamed section ["+h+"]"); | 308 | //println("WARNING unamed section ["+h+"]"); |
| 217 | - blank, | ||
| 218 | [ name . t ] then | 309 | [ name . t ] then |
| 219 | //println("auto found with name ["+name+"]"); | 310 | //println("auto found with name ["+name+"]"); |
| 220 | //read the second line of the section where the type is located i.e. iface | 311 | //read the second line of the section where the type is located i.e. iface |
| 221 | if read_line(s) is | 312 | if read_line(s) is |
| 222 | { | 313 | { |
| 223 | failure then blank, | 314 | failure then blank, |
| 224 | - success(type_line) then | 315 | + success(type_line) then |
| 225 | //println("get type line for "+name+"["+trim(type_line)+"]"); | 316 | //println("get type line for "+name+"["+trim(type_line)+"]"); |
| 226 | if get_type(trim(type_line), name) is | 317 | if get_type(trim(type_line), name) is |
| 227 | { | 318 | { |
| @@ -282,11 +373,16 @@ public define List(Network_entry) | @@ -282,11 +373,16 @@ public define List(Network_entry) | ||
| 282 | read_interface_entries(make_stream(f), []) | 373 | read_interface_entries(make_stream(f), []) |
| 283 | }. | 374 | }. |
| 284 | 375 | ||
| 376 | +public define String | ||
| 377 | + dump_interface_file | ||
| 378 | + = | ||
| 379 | + to_String(read_interface("/calexium/interfaces"),""). | ||
| 380 | + | ||
| 285 | public define Maybe(One) | 381 | public define Maybe(One) |
| 286 | write_interface | 382 | write_interface |
| 287 | ( | 383 | ( |
| 288 | - String file_name, | ||
| 289 | - List(Network_entry) entries | 384 | + String file_name, |
| 385 | + List(Network_entry) entries | ||
| 290 | )= | 386 | )= |
| 291 | if (Maybe(RWStream))file(file_name, new) is | 387 | if (Maybe(RWStream))file(file_name, new) is |
| 292 | { | 388 | { |
| @@ -294,6 +390,39 @@ public define Maybe(One) | @@ -294,6 +390,39 @@ public define Maybe(One) | ||
| 294 | success(f) then success(forget(reliable_write(f, to_byte_array(to_String(entries, ""))))) | 390 | success(f) then success(forget(reliable_write(f, to_byte_array(to_String(entries, ""))))) |
| 295 | }. | 391 | }. |
| 296 | 392 | ||
| 393 | +//ipconfig(String ip, | ||
| 394 | +// String mask, | ||
| 395 | +// String gateway, | ||
| 396 | +// List(String) dns). | ||
| 397 | + | ||
| 398 | + | ||
| 399 | +define One | ||
| 400 | + write_ip_config_linux | ||
| 401 | + ( | ||
| 402 | + IpConfig ip_config, | ||
| 403 | + ) = | ||
| 404 | + with file_name = "/etc/network/interfaces", | ||
| 405 | + interfaces_file = read_interface(file_name), | ||
| 406 | + interfaces_file = update_entry(interfaces_file, "br0", "address",ip_config.ip,[], false), | ||
| 407 | + interfaces_file = update_entry(interfaces_file, "br0", "netmask",ip_config.mask,[], false), | ||
| 408 | + interfaces_file = update_entry(interfaces_file, "br0","gateway",ip_config.gateway,[], false), | ||
| 409 | + forget(write_interface(file_name, interfaces_file)) | ||
| 410 | + . | ||
| 411 | + | ||
| 412 | + | ||
| 413 | + | ||
| 414 | +public define Maybe(One) | ||
| 415 | + write_network_config_linux | ||
| 416 | + ( | ||
| 417 | + IpConfig wan_config | ||
| 418 | + ) = | ||
| 419 | + | ||
| 420 | + write_ip_config_linux(wan_config); | ||
| 421 | + write_dns_list(wan_config.dns); | ||
| 422 | + sync_fs; | ||
| 423 | + success(unique). | ||
| 424 | + | ||
| 425 | + | ||
| 297 | 426 | ||
| 298 | /** here tool for testing | 427 | /** here tool for testing |
| 299 | * anbexec parse_interface <given_file> | 428 | * anbexec parse_interface <given_file> |
| @@ -309,7 +438,7 @@ global define One | @@ -309,7 +438,7 @@ global define One | ||
| 309 | { | 438 | { |
| 310 | [] then print("usage : parse_interface <file>"), | 439 | [] then print("usage : parse_interface <file>"), |
| 311 | [ file_name . t ] then | 440 | [ file_name . t ] then |
| 312 | - forget(write_interface(file_name+".dump",read_interface(file_name))) | 441 | + write_ip_config_linux(ipconfig("192.168.4.0","255.255.0.255","0.0.0.0",[])) |
| 313 | } | 442 | } |
| 314 | . | 443 | . |
| 315 | 444 |