@@ -55,13 +55,15 @@ def choose_primary_address_type(nic):
5555 tui .screen .popWindow ()
5656 return direction , address_type
5757
58- def get_ipv4_configuration (nic , txt , defaults , include_dns ):
58+ def get_ip_configuration (nic , txt , defaults , include_dns , iface_class ):
5959 def use_vlan_cb_change ():
6060 vlan_field .setFlags (FLAG_DISABLED , vlan_cb .value ())
6161
6262 def dhcp_change ():
6363 for x in [ ip_field , gateway_field , subnet_field , dns_field ]:
64- x .setFlags (FLAG_DISABLED , not dhcp_rb .selected ())
64+ x .setFlags (FLAG_DISABLED , static_rb .selected ())
65+
66+ ipv6 = iface_class == NetInterfaceV6
6567
6668 gf = GridFormHelp (tui .screen , 'Networking' , 'ifconfig' , 1 , 8 )
6769 if txt is None :
@@ -70,160 +72,41 @@ def dhcp_change():
7072 b = [("Ok" , "ok" ), ("Back" , "back" )]
7173 buttons = ButtonBar (tui .screen , b )
7274
75+ #TODO? Change size for IPv6? If so which size?
7376 ip_field = Entry (16 )
7477 subnet_field = Entry (16 )
7578 gateway_field = Entry (16 )
7679 dns_field = Entry (16 )
7780 vlan_field = Entry (16 )
7881
79- if defaults and defaults .mode == NetInterface .Static :
80- # static configuration defined previously
81- dhcp_rb = SingleRadioButton ("Automatic configuration (DHCP)" , None , 0 )
82- dhcp_rb .setCallback (dhcp_change , ())
83- static_rb = SingleRadioButton ("Static configuration:" , dhcp_rb , 1 )
84- static_rb .setCallback (dhcp_change , ())
85- if defaults .ipaddr :
86- ip_field .set (defaults .ipaddr )
87- if defaults .netmask :
88- subnet_field .set (defaults .netmask )
89- if defaults .gateway :
90- gateway_field .set (defaults .gateway )
91- if defaults .dns :
92- dns_field .set (defaults .dns [0 ])
93- else :
94- dhcp_rb = SingleRadioButton ("Automatic configuration (DHCP)" , None , 1 )
95- dhcp_rb .setCallback (dhcp_change , ())
96- static_rb = SingleRadioButton ("Static configuration:" , dhcp_rb , 0 )
97- static_rb .setCallback (dhcp_change , ())
98- ip_field .setFlags (FLAG_DISABLED , False )
99- subnet_field .setFlags (FLAG_DISABLED , False )
100- gateway_field .setFlags (FLAG_DISABLED , False )
101- dns_field .setFlags (FLAG_DISABLED , False )
102-
103- vlan_cb = Checkbox ("Use VLAN:" , defaults .isVlan () if defaults else False )
104- vlan_cb .setCallback (use_vlan_cb_change , ())
105- if defaults and defaults .isVlan ():
106- vlan_field .set (str (defaults .vlan ))
107- else :
108- vlan_field .setFlags (FLAG_DISABLED , False )
109-
110- ip_text = Textbox (15 , 1 , "IP Address:" )
111- subnet_text = Textbox (15 , 1 , "Subnet mask:" )
112- gateway_text = Textbox (15 , 1 , "Gateway:" )
113- dns_text = Textbox (15 , 1 , "Nameserver:" )
114- vlan_text = Textbox (15 , 1 , "VLAN (1-4094):" )
115-
116- entry_grid = Grid (2 , include_dns and 4 or 3 )
117- entry_grid .setField (ip_text , 0 , 0 )
118- entry_grid .setField (ip_field , 1 , 0 )
119- entry_grid .setField (subnet_text , 0 , 1 )
120- entry_grid .setField (subnet_field , 1 , 1 )
121- entry_grid .setField (gateway_text , 0 , 2 )
122- entry_grid .setField (gateway_field , 1 , 2 )
123- if include_dns :
124- entry_grid .setField (dns_text , 0 , 3 )
125- entry_grid .setField (dns_field , 1 , 3 )
126-
127- vlan_grid = Grid (2 , 1 )
128- vlan_grid .setField (vlan_text , 0 , 0 )
129- vlan_grid .setField (vlan_field , 1 , 0 )
130-
131- gf .add (text , 0 , 0 , padding = (0 , 0 , 0 , 1 ))
132- gf .add (dhcp_rb , 0 , 2 , anchorLeft = True )
133- gf .add (static_rb , 0 , 3 , anchorLeft = True )
134- gf .add (entry_grid , 0 , 4 , padding = (0 , 0 , 0 , 1 ))
135- gf .add (vlan_cb , 0 , 5 , anchorLeft = True )
136- gf .add (vlan_grid , 0 , 6 , padding = (0 , 0 , 0 , 1 ))
137- gf .add (buttons , 0 , 7 , growx = 1 )
138-
139- loop = True
140- while loop :
141- result = gf .run ()
142-
143- if buttons .buttonPressed (result ) in ['ok' , None ]:
144- # validate input
145- msg = ''
146- if static_rb .selected ():
147- if not netutil .valid_ipv4_addr (ip_field .value ()):
148- msg = 'IP Address'
149- elif not netutil .valid_ipv4_addr (subnet_field .value ()):
150- msg = 'Subnet mask'
151- elif gateway_field .value () != '' and not netutil .valid_ipv4_addr (gateway_field .value ()):
152- msg = 'Gateway'
153- elif dns_field .value () != '' and not netutil .valid_ipv4_addr (dns_field .value ()):
154- msg = 'Nameserver'
155- if vlan_cb .selected ():
156- if not netutil .valid_vlan (vlan_field .value ()):
157- msg = 'VLAN'
158- if msg != '' :
159- tui .progress .OKDialog ("Networking" , "Invalid %s, please check the field and try again." % msg )
160- else :
161- loop = False
162- else :
163- loop = False
164-
165- tui .screen .popWindow ()
166-
167- if buttons .buttonPressed (result ) == 'back' : return LEFT_BACKWARDS , None
168-
169- vlan_value = int (vlan_field .value ()) if vlan_cb .selected () else None
170- if bool (dhcp_rb .selected ()):
171- answers = NetInterface (NetInterface .DHCP , nic .hwaddr , vlan = vlan_value )
172- else :
173- answers = NetInterface (NetInterface .Static , nic .hwaddr , ip_field .value (),
174- subnet_field .value (), gateway_field .value (),
175- dns_field .value (), vlan = vlan_value )
176- return RIGHT_FORWARDS , answers
177-
178- def get_ipv6_configuration (nic , txt , defaults , include_dns ):
179- def dhcp_change ():
180- for x in [ ip_field , gateway_field , subnet_field , dns_field ]:
181- x .setFlags (FLAG_DISABLED , static_rb .selected ())
182-
183- def use_vlan_cb_change ():
184- vlan_field .setFlags (FLAG_DISABLED , vlan_cb .value ())
185-
186- gf = GridFormHelp (tui .screen , 'Networking' , 'ifconfigv6' , 1 , 10 )
187- if txt is None :
188- txt = "Configuration for %s (%s)" % (nic .name , nic .hwaddr )
189- text = TextboxReflowed (45 , txt )
190- b = [("Ok" , "ok" ), ("Back" , "back" )]
191- buttons = ButtonBar (tui .screen , b )
192-
193- ip_field = Entry (25 )
194- subnet_field = Entry (25 )
195- gateway_field = Entry (25 )
196- dns_field = Entry (25 )
197- vlan_field = Entry (25 )
198-
199- if defaults and defaults .modev6 == NetInterface .Static :
200- # static configuration defined previously
201- dhcp_rb = SingleRadioButton ("Automatic configuration (DHCP)" , None , 0 )
202- dhcp_rb .setCallback (dhcp_change , ())
203- autoconf_rb = SingleRadioButton ("Automatic configuration (Autoconf)" , dhcp_rb , 0 )
82+ static = defaults and (defaults .modev6 if ipv6 else defaults .mode ) == NetInterface .Static
83+ dhcp_rb = SingleRadioButton ("Automatic configuration (DHCP)" , None , not static )
84+ dhcp_rb .setCallback (dhcp_change , ())
85+ static_rb = SingleRadioButton ("Static configuration:" , dhcp_rb , static )
86+ static_rb .setCallback (dhcp_change , ())
87+ if ipv6 :
88+ autoconf_rb = SingleRadioButton ("Automatic configuration (Autoconf)" , autoconf_rb , 0 )
20489 autoconf_rb .setCallback (dhcp_change , ())
205- static_rb = SingleRadioButton ("Static configuration:" , autoconf_rb , 1 )
206- static_rb .setCallback (dhcp_change , ())
207- if defaults :
90+ dhcp_change ()
91+
92+ if defaults :
93+ if ipv6 :
20894 if defaults .ipv6addr :
20995 ip6addr , netmask = defaults .ipv6addr .split ("/" )
21096 ip_field .set (ip6addr )
21197 subnet_field .set (netmask )
21298 if defaults .ipv6_gateway :
21399 gateway_field .set (defaults .ipv6_gateway )
214- if defaults .dns :
215- dns_field .set (defaults .dns [0 ])
216- else :
217- dhcp_rb = SingleRadioButton ("Automatic configuration (DHCP)" , None , 1 )
218- dhcp_rb .setCallback (dhcp_change , ())
219- autoconf_rb = SingleRadioButton ("Automatic configuration (Autoconf)" , dhcp_rb , 0 )
220- autoconf_rb .setCallback (dhcp_change , ())
221- static_rb = SingleRadioButton ("Static configuration:" , autoconf_rb , 0 )
222- static_rb .setCallback (dhcp_change , ())
223- ip_field .setFlags (FLAG_DISABLED , False )
224- subnet_field .setFlags (FLAG_DISABLED , False )
225- gateway_field .setFlags (FLAG_DISABLED , False )
226- dns_field .setFlags (FLAG_DISABLED , False )
100+ else :
101+ if defaults .ipaddr :
102+ ip_field .set (defaults .ipaddr )
103+ if defaults .netmask :
104+ subnet_field .set (defaults .netmask )
105+ if defaults .gateway :
106+ gateway_field .set (defaults .gateway )
107+
108+ if defaults .dns :
109+ dns_field .set (defaults .dns [0 ])
227110
228111 vlan_cb = Checkbox ("Use VLAN:" , defaults .isVlan () if defaults else False )
229112 vlan_cb .setCallback (use_vlan_cb_change , ())
@@ -232,8 +115,10 @@ def use_vlan_cb_change():
232115 else :
233116 vlan_field .setFlags (FLAG_DISABLED , False )
234117
235- ip_text = Textbox (15 , 1 , "IPv6 Address:" )
236- subnet_text = Textbox (15 , 1 , "CIDR (4-128):" )
118+ ip_msg = "IPv6 Address" if ipv6 else "IP Address"
119+ mask_msg = "CIDR (4-128)" if ipv6 else "Subnet mask"
120+ ip_text = Textbox (15 , 1 , "%s:" % ip_msg )
121+ subnet_text = Textbox (15 , 1 , "%s:" % mask_msg )
237122 gateway_text = Textbox (15 , 1 , "Gateway:" )
238123 dns_text = Textbox (15 , 1 , "Nameserver:" )
239124 vlan_text = Textbox (15 , 1 , "VLAN (1-4094):" )
@@ -255,14 +140,17 @@ def use_vlan_cb_change():
255140
256141 gf .add (text , 0 , 0 , padding = (0 , 0 , 0 , 1 ))
257142 gf .add (dhcp_rb , 0 , 2 , anchorLeft = True )
258- gf .add (autoconf_rb , 0 , 3 , anchorLeft = True )
259- gf .add (static_rb , 0 , 4 , anchorLeft = True )
260- gf .add (entry_grid , 0 , 5 , padding = (0 , 0 , 0 , 1 ))
261- gf .add (vlan_cb , 0 , 6 , anchorLeft = True )
262- gf .add (vlan_grid , 0 , 7 , padding = (0 , 0 , 0 , 1 ))
263- gf .add (buttons , 0 , 8 , growx = 1 )
143+ gf .add (static_rb , 0 , 3 , anchorLeft = True )
144+ if ipv6 :
145+ gf .add (autoconf_rb , 0 , 4 , anchorLeft = True )
146+ # One more line for IPv6 autoconf
147+ gf .add (entry_grid , 0 , 4 + ipv6 , padding = (0 , 0 , 0 , 1 ))
148+ gf .add (vlan_cb , 0 , 5 + ipv6 , anchorLeft = True )
149+ gf .add (vlan_grid , 0 , 6 + ipv6 , padding = (0 , 0 , 0 , 1 ))
150+ gf .add (buttons , 0 , 7 + ipv6 , growx = 1 )
264151
265152 loop = True
153+ ip_family = socket .AF_INET6 if ipv6 else socket .AF_INET
266154 while loop :
267155 result = gf .run ()
268156
@@ -271,13 +159,14 @@ def use_vlan_cb_change():
271159 msg = ''
272160 if static_rb .selected ():
273161 subnet_value = int (subnet_field .value ())
274- if not netutil .valid_ipv6_addr (ip_field .value ()):
275- msg = 'IPv6 Address'
276- elif subnet_value > 128 or subnet_value < 4 :
277- msg = 'CIDR'
278- elif gateway_field .value () != '' and not netutil .valid_ipv6_addr (gateway_field .value ()):
162+ invalid_subnet = subnet_value > 128 or subnet_value < 4 if ipv6 else not netutil .valid_ipv4_addr (subnet_field .value ())
163+ if not netutil .valid_ip_address_family (ip_field .value (), ip_family ):
164+ msg = ip_msg
165+ elif invalid_subnet :
166+ msg = mask_msg
167+ elif gateway_field .value () != '' and not netutil .valid_ip_address_family (gateway_field .value (), ip_family ):
279168 msg = 'Gateway'
280- elif dns_field .value () != '' and not netutil .valid_ipv6_addr (dns_field .value ()):
169+ elif dns_field .value () != '' and not netutil .valid_ip_address_family (dns_field .value (), ip_family ):
281170 msg = 'Nameserver'
282171 if vlan_cb .selected ():
283172 if not netutil .valid_vlan (vlan_field .value ()):
@@ -295,11 +184,11 @@ def use_vlan_cb_change():
295184
296185 vlan_value = int (vlan_field .value ()) if vlan_cb .selected () else None
297186 if bool (dhcp_rb .selected ()):
298- answers = NetInterfaceV6 (NetInterface .DHCP , nic .hwaddr , vlan = vlan_value )
299- elif bool (autoconf_rb .selected ()):
300- answers = NetInterfaceV6 (NetInterface .Autoconf , nic .hwaddr , vlan = vlan_value )
187+ answers = iface_class (NetInterface .DHCP , nic .hwaddr , vlan = vlan_value )
188+ elif ipv6 and bool (autoconf_rb .selected ()):
189+ answers = iface_class (NetInterface .Autoconf , nic .hwaddr , vlan = vlan_value )
301190 else :
302- answers = NetInterfaceV6 (NetInterface .Static , nic .hwaddr , ip_field .value (),
191+ answers = iface_class (NetInterface .Static , nic .hwaddr , ip_field .value (),
303192 subnet_field .value (), gateway_field .value (),
304193 dns_field .value (), vlan = vlan_value )
305194
@@ -311,12 +200,12 @@ def use_vlan_cb_change():
311200
312201 answers = None
313202 if address_type in ["ipv4" , "dual" ]:
314- direction , answers = get_ipv4_configuration (nic , txt , defaults , include_dns )
203+ direction , answers = get_ip_configuration (nic , txt , defaults , include_dns , NetInterface )
315204 if direction == LEFT_BACKWARDS :
316205 return LEFT_BACKWARDS , None
317206
318207 if address_type in ["ipv6" , "dual" ]:
319- direction , answers_ipv6 = get_ipv6_configuration (nic , txt , defaults , include_dns )
208+ direction , answers_ipv6 = get_ip_configuration (nic , txt , defaults , include_dns , NetInterfaceV6 )
320209 if direction == LEFT_BACKWARDS :
321210 return LEFT_BACKWARDS , None
322211
0 commit comments