From 73d125b250b0e1741f8f7298226107566b33de61 Mon Sep 17 00:00:00 2001 From: ficus Date: Tue, 25 Sep 2012 19:01:13 +0200 Subject: dhcp settings for lan and wifi --- torouterui/netif.py | 76 ++++++++++++++++++++++++++++++++++++------ torouterui/templates/lan.html | 5 ++- torouterui/templates/lib.html | 13 ++++++-- torouterui/templates/wifi.html | 2 +- torouterui/views.py | 23 ++++++++----- 5 files changed, 97 insertions(+), 22 deletions(-) (limited to 'torouterui') diff --git a/torouterui/netif.py b/torouterui/netif.py index 9c9b08a..4d08f14 100644 --- a/torouterui/netif.py +++ b/torouterui/netif.py @@ -175,6 +175,8 @@ def parse_uaputl(): d['tx_power'] = l[11:].strip() elif l.startswith("Tx data rate ="): d['data_rate'] = l[15:].strip() + if d['radio_state'] == 'on': + d['state'] = "ENABLED" return d def read_augeas_ifinfo(ifname): @@ -239,7 +241,8 @@ def write_augeas_ifinfo(ifname, settings, method='disabled'): aug.set(path + "/method", 'static') aug.set(path + "/address", str(settings['ipv4addr'])) aug.set(path + "/netmask", str(settings['ipv4netmask'])) - aug.set(path + "/gateway", str(settings['ipv4gateway'])) + if settings.has_key('ipv4gateway'): + aug.set(path + "/gateway", str(settings['ipv4gateway'])) else: raise ValueError("unrecognized network interface method: " + method) print "committing with augeas..." @@ -247,6 +250,44 @@ def write_augeas_ifinfo(ifname, settings, method='disabled'): print "augeas errors: %s" % aug.get("/augeas/error") aug.close() +def read_augeas_dnsmasq(interface): + """ + interface arg should be one of lan or wifi + """ + d = dict() + aug = augeas.Augeas(flags=augeas.Augeas.NO_MODL_AUTOLOAD) + aug.set("/augeas/load/Interfaces/lens", "Dnsmasq.lns") + aug.set("/augeas/load/Interfaces/incl", "/etc/dnsmasq.d/%s" % interface) + aug.load() + dhcp_range = aug.get("/files/etc/dnsmasq.d/%s/dhcp-range" % interface) + if dhcp_range and len(dhcp_range.split(',')) == 4: + d['dhcpbase'] = dhcp_range.split(',')[0] + d['dhcptop'] = dhcp_range.split(',')[1] + d['dhcpnetmask'] = dhcp_range.split(',')[2] + d['dhcptime'] = dhcp_range.split(',')[3] + aug.close() + return d + +def write_augeas_dnsmasq(interface, form): + """ + interface arg should be one of lan or wifi + """ + aug = augeas.Augeas(flags=augeas.Augeas.NO_MODL_AUTOLOAD) + aug.set("/augeas/load/Interfaces/lens", "Dnsmasq.lns") + aug.set("/augeas/load/Interfaces/incl", "/etc/dnsmasq.d/%s" % interface) + aug.load() + dhcp_range = ','.join([form['dhcpbase'], + form['dhcptop'], + form['dhcpnetmask'], + form['dhcptime']]) + print dhcp_range + aug.set("/files/etc/dnsmasq.d/%s/dhcp-range" % interface, str(dhcp_range)) + print "committing with augeas..." + aug.save() + print "augeas errors: %s" % aug.get("/augeas/error") + aug.close() + return + def get_wan_status(ifname=None): if not ifname: # grab configuration at run time, not earlier @@ -308,39 +349,54 @@ def get_lan_settings(ifname=None): # grab configuration at run time, not earlier ifname = app.config['LAN_IF'] d = read_augeas_ifinfo(ifname) + d['ipv4enable'] = bool(d['ipv4method'] != 'manual') and 'true' + d.update(read_augeas_dnsmasq('lan')) return d def save_lan_settings(form, ifname=None): if not ifname: # grab configuration at run time, not earlier ifname = app.config['LAN_IF'] - write_augeas_ifinfo(ifname, method=form['ipv4method'], settings=form) - if form['ipv4method'] == 'disabled': + if form.get('ipv4enable') != 'true': + write_augeas_ifinfo(ifname, method='disabled', settings=form) print "ifdown..." os.system("ifdown %s" % ifname) else: + write_augeas_ifinfo(ifname, method='static', settings=form) print "ifup..." os.system("ifdown %s" % ifname) os.system("ifup %s &" % ifname) + write_augeas_dnsmasq('lan', form) + os.system("/etc/init.d/dnsmasq reload &") def get_wifi_settings(ifname=None): if not ifname: # grab configuration at run time, not earlier ifname = app.config['WIFI_IF'] - #d = read_augeas_ifinfo(ifname) d = dict() - if not d: - return d - if ifname.startswith('wlan'): - d.update(dict()) # extra wireless settings? - else: - d.update(dict()) # extra wireless settings? + ifinfo = read_augeas_ifinfo(ifname) + print "ifinfo: %s" % ifinfo + if ifinfo: + d.update(ifinfo) + d.update(read_augeas_dnsmasq('wifi')) return d def save_wifi_settings(ifname=None): if not ifname: # grab configuration at run time, not earlier ifname = app.config['WIFI_IF'] + # TODO: need to go in to deep interfaces action here... + if form.get('wifienable') != 'true': + write_augeas_ifinfo(ifname, method='disabled', settings=form) + print "ifdown..." + os.system("ifdown %s" % ifname) + else: + write_augeas_ifinfo(ifname, method='static', settings=form) + print "ifup..." + os.system("ifdown %s" % ifname) + os.system("ifup %s &" % ifname) + write_augeas_dnsmasq('wifi', form) + os.system("/etc/init.d/dnsmasq reload &") pass def is_valid_ipv4(s): diff --git a/torouterui/templates/lan.html b/torouterui/templates/lan.html index 09bcf0a..9b260bf 100644 --- a/torouterui/templates/lan.html +++ b/torouterui/templates/lan.html @@ -14,11 +14,14 @@ Address Configuration {{ lib.forminput(form, formerr, 'ipv4addr', 'IPv4 Address', '0.0.0.0') }} {{ lib.forminput(form, formerr, 'ipv4netmask', 'Netmask', '255.255.0.0') }} -{{ lib.forminput(form, formerr, 'ipv4gateway', 'Gateway IP Address', '0.0.0.0') }} DHCP Configuration +Make sure that the assigned addresses all fall within the netmask range +specified above! + {{ lib.forminput(form, formerr, 'dhcpbase', 'DHCP Range Base', '192.168.1.100') }} {{ lib.forminput(form, formerr, 'dhcptop', 'DHCP Range Top', '192.168.1.200') }} +{{ lib.forminput(form, formerr, 'dhcpnetmask', 'DHCP Netmask', '255.255.255.0') }} {{ lib.forminput(form, formerr, 'dhcptime', 'Lease Time', '12h') }}
diff --git a/torouterui/templates/lib.html b/torouterui/templates/lib.html index 6ac5e12..03a671f 100644 --- a/torouterui/templates/lib.html +++ b/torouterui/templates/lib.html @@ -39,8 +39,17 @@ Network hardware not detected at all! SSID {{ ifstatus.ssid }} - Frequency - {{ ifstatus.freq }} + SSID Broadcast? + {{ ifstatus.ssid_broadcast }} + + Channel + {{ ifstatus.channel }} + + TX Power + {{ ifstatus.tx_power }} + + Auth Mode + {{ ifstatus.auth_mode }} Signal Strength {{ ifstatus.signal_dbm }} diff --git a/torouterui/templates/wifi.html b/torouterui/templates/wifi.html index 3e41788..c8f17f0 100644 --- a/torouterui/templates/wifi.html +++ b/torouterui/templates/wifi.html @@ -20,11 +20,11 @@ Network Address {{ lib.forminput(form, formerr, 'ipv4addr', 'IPv4 Address', '0.0.0.0') }} {{ lib.forminput(form, formerr, 'ipv4netmask', 'Netmask', '255.255.0.0') }} -{{ lib.forminput(form, formerr, 'ipv4gateway', 'Gateway IP Address', '0.0.0.0') }} DHCP Server {{ lib.forminput(form, formerr, 'dhcpbase', 'DHCP Range Base', '192.168.1.100') }} {{ lib.forminput(form, formerr, 'dhcptop', 'DHCP Range Top', '192.168.1.200') }} +{{ lib.forminput(form, formerr, 'dhcpnetmask', 'DHCP Range Top', '255.255.255.0') }} {{ lib.forminput(form, formerr, 'dhcptime', 'Lease Time', '12h') }}
diff --git a/torouterui/views.py b/torouterui/views.py index 12efb1d..d3daf77 100644 --- a/torouterui/views.py +++ b/torouterui/views.py @@ -108,19 +108,19 @@ def lanpage(): formerr=None) # Got this far, need to validate form formerr = dict() - if request.form['ipv4method'] == 'disabled': + if request.form.get('ipv4enable') != 'true': pass # no further validation - elif request.form['ipv4method'] == 'static': + else: if not netif.is_valid_ipv4(request.form['ipv4addr']): formerr['ipv4addr'] = "Not a valid IPv4 address" if not netif.is_valid_ipv4mask(request.form['ipv4netmask']): formerr['ipv4netmask'] = "Not a valid IPv4 netmask" - if not netif.is_valid_ipv4(request.form['ipv4gateway']): - formerr['ipv4gateway'] = "Not a valid IPv4 address" - else: - ke = KeyError("Invalid method: %s" % form['ipv4method']) - print ke - raise ke + if not netif.is_valid_ipv4(request.form['dhcpbase']): + formerr['dhcpbase'] = "Not a valid IPv4 address" + if not netif.is_valid_ipv4(request.form['dhcptop']): + formerr['dhcptop'] = "Not a valid IPv4 address" + if not netif.is_valid_ipv4mask(request.form['dhcpnetmask']): + formerr['dhcpnetmask'] = "Not a valid IPv4 netmask" if len(formerr.keys()) > 0: msg.append(("error", "Please correct the validation issues below"),) @@ -134,6 +134,10 @@ def lanpage(): msg.append(("error", "Was unable to commit changes... permissions problem? \"%s\"" % ioerr)) + except Exception, err: + msg.append(("error", + "Was unable to commit changes... \"%s\"" % err)) + raise err return render_template('lan.html', form=request.form, status=status, formerr=formerr, messages=msg) @@ -179,6 +183,9 @@ def wifipage(): msg.append(("error", "Was unable to commit changes... permissions problem? \"%s\"" % ioerr)) + except Exception, e: + msg.append(("error", + "Was unable to commit changes... \"%s\"" % e)) return render_template('wifi.html', form=request.form, status=status, formerr=formerr, messages=msg) return render_template('wifi.html', settings=None, status=None) -- cgit v1.2.3