DUMM: Allow addresses to be configured with net prefix.

This commit is contained in:
Tobias Brunner
2011-07-29 12:14:02 +02:00
parent 0511c93d46
commit 50937e8b80
3 changed files with 21 additions and 15 deletions
+8 -6
View File
@@ -594,21 +594,22 @@ static VALUE iface_add_addr(VALUE self, VALUE name)
{
iface_t *iface;
host_t *addr;
int bits;
addr = host_create_from_string(StringValuePtr(name), 0);
addr = host_create_from_subnet(StringValuePtr(name), &bits);
if (!addr)
{
rb_raise(rb_eArgError, "invalid IP address");
}
Data_Get_Struct(self, iface_t, iface);
if (!iface->add_address(iface, addr))
if (!iface->add_address(iface, addr, bits))
{
addr->destroy(addr);
rb_raise(rb_eRuntimeError, "adding address failed");
}
if (rb_block_given_p()) {
rb_yield(self);
iface->delete_address(iface, addr);
iface->delete_address(iface, addr, bits);
}
addr->destroy(addr);
return self;
@@ -647,21 +648,22 @@ static VALUE iface_del_addr(VALUE self, VALUE vaddr)
{
iface_t *iface;
host_t *addr;
int bits;
addr = host_create_from_string(StringValuePtr(vaddr), 0);
addr = host_create_from_subnet(StringValuePtr(vaddr), &bits);
if (!addr)
{
rb_raise(rb_eArgError, "invalid IP address");
}
Data_Get_Struct(self, iface_t, iface);
if (!iface->delete_address(iface, addr))
if (!iface->delete_address(iface, addr, bits))
{
addr->destroy(addr);
rb_raise(rb_eRuntimeError, "address not found");
}
if (rb_block_given_p()) {
rb_yield(self);
iface->add_address(iface, addr);
iface->add_address(iface, addr, bits);
}
addr->destroy(addr);
return self;
+6 -6
View File
@@ -102,10 +102,10 @@ static char* get_hostif(private_iface_t *this)
/**
* Implementation of iface_t.add_address
*/
static bool add_address(private_iface_t *this, host_t *addr)
static bool add_address(private_iface_t *this, host_t *addr, int bits)
{
return (this->guest->exec(this->guest, NULL, NULL,
"exec ip addr add %H dev %s", addr, this->guestif) == 0);
"exec ip addr add %H/%d dev %s", addr, bits, this->guestif) == 0);
}
/**
@@ -146,10 +146,10 @@ static enumerator_t* create_address_enumerator(private_iface_t *this)
/**
* Implementation of iface_t.delete_address
*/
static bool delete_address(private_iface_t *this, host_t *addr)
static bool delete_address(private_iface_t *this, host_t *addr, int bits)
{
return (this->guest->exec(this->guest, NULL, NULL,
"exec ip addr del %H dev %s", addr, this->guestif) == 0);
"exec ip addr del %H/%d dev %s", addr, bits, this->guestif) == 0);
}
/**
@@ -277,9 +277,9 @@ iface_t *iface_create(char *name, guest_t *guest, mconsole_t *mconsole)
this->public.get_hostif = (char*(*)(iface_t*))get_hostif;
this->public.get_guestif = (char*(*)(iface_t*))get_guestif;
this->public.add_address = (bool(*)(iface_t*, host_t *addr))add_address;
this->public.add_address = (bool(*)(iface_t*,host_t*,int))add_address;
this->public.create_address_enumerator = (enumerator_t*(*)(iface_t*))create_address_enumerator;
this->public.delete_address = (bool(*)(iface_t*, host_t *addr))delete_address;
this->public.delete_address = (bool(*)(iface_t*,host_t*,int))delete_address;
this->public.set_bridge = (void(*)(iface_t*, bridge_t*))set_bridge;
this->public.get_bridge = (bridge_t*(*)(iface_t*))get_bridge;
this->public.get_guest = (guest_t*(*)(iface_t*))get_guest;
+7 -3
View File
@@ -50,10 +50,11 @@ struct iface_t {
/**
* Add an address to the interface.
*
* @param addr address to add to interface
* @param addr address to add to the interface
* @param bits network prefix length in bits
* @return TRUE if address added
*/
bool (*add_address)(iface_t *this, host_t *addr);
bool (*add_address)(iface_t *this, host_t *addr, int bits);
/**
* Create an enumerator over all installed addresses.
@@ -65,10 +66,13 @@ struct iface_t {
/**
* Remove an address from an interface.
*
* @note The network prefix length has to be the same as used in add_address
*
* @param addr address to remove
* @param bits network prefix length in bits
* @return TRUE if address removed
*/
bool (*delete_address)(iface_t *this, host_t *addr);
bool (*delete_address)(iface_t *this, host_t *addr, int bits);
/**
* Set the bridge this interface is attached to.