4.3. Network module

The network case is more complex. There are several potential changes in the network configuration that other modules may need to know about. EBox::NetworkObserver has four methods that the network module invokes when different types of changes are made, and two methods that tell modules to remove references to certain network interfaces from their configurations:

staticIfaceAddressChanged

Invoked when the address of an static network interface is going to be changed, this method receives the old and new addresses and masks as arguments. Returning a true value means that this module's configuration would become inconsistent if such a change was made. In that case the network module will not make the change, but warn the user instead.

ifaceMethodChanged

Invoked when the configuration method for a network interface is going to change. Both the old and new methods are passed as arguments to this function. They are strings: static, dhcp, trunk or notset. As with the previous function, a return value of true will prevent the change from being made.

vifaceAdded

Invoked when a new virtual interface is going to be created. Its arguments are the real interface to which it's going to be added, the name of the new interface, its ip address and its netmask. It works the same way: it returns true if the creation of the virtual interface is incompatible with your module's current configuration.

vifaceDelete

Invoked when a virtual interface is going to be removed. Its arguments are the names of the real and virtual interfaces. It works exactly the same way as the three methods above.

freeIface

It tells your module to remove all reference to a network interface from its configuration. It is usually called after you returned true in one of the methods above and the user insisted on doing whatever change he was trying to do.

freeViface

The same as freeIface, but for virtual interfaces.

Example 4.2 shows the implementation of EBox::NetworkObserver methods in the firewall module.

Example 4.2. Implementing a EBox::NetworkObserver

sub ifaceMethodChanged # (iface, oldmethod, newmethod)
{
	 my ($self, $iface, $oldm, $newm) = @_;

	 ($newm eq 'static') and return undef;
	 ($newm eq 'dhcp') and return undef;

	 return $self->usesIface($iface);
}

sub vifaceDelete # (iface, viface)
{
	 my ($self, $iface, $viface) = @_;
	 return $self->usesIface("$iface:$viface");
}

sub usesIface # (iface)
{
	 my ($self, $iface) = @_;
	 my @reds = $self->all_dirs("redirections");
	 foreach (@reds) {
		  if ($self->get_string("$_/iface") eq $iface) {
			   return 1;
		  }
	 }
	 return undef;
}

sub freeIface # (iface)
{
	 my ($self, $iface) = @_;
	 $self->removePortRedirectionOnIface($iface);
}

sub freeViface # (iface, viface)
{
	 my ($self, $iface, $viface) = @_;
	 $self->removePortRedirectionOnIface("$iface:$viface");
}