summaryrefslogtreecommitdiff
path: root/src/dumm/ext
diff options
context:
space:
mode:
authorRene Mayrhofer <rene@mayrhofer.eu.org>2009-06-23 11:35:38 +0000
committerRene Mayrhofer <rene@mayrhofer.eu.org>2009-06-23 11:35:38 +0000
commit7c52c3f35cdbdff58443b994f2f33d13b4d81f57 (patch)
treee54a27979ea72ec41702bec2984c2eadac3b8862 /src/dumm/ext
parent4ef45ba0404dac3773e83af995a5ec584b23d633 (diff)
downloadvyos-strongswan-7c52c3f35cdbdff58443b994f2f33d13b4d81f57.tar.gz
vyos-strongswan-7c52c3f35cdbdff58443b994f2f33d13b4d81f57.zip
Updated to new upstream version.
Diffstat (limited to 'src/dumm/ext')
-rw-r--r--src/dumm/ext/dumm.c124
-rw-r--r--src/dumm/ext/extconf.rb21
-rw-r--r--src/dumm/ext/extconf.rb.in19
-rw-r--r--src/dumm/ext/lib/dumm.rb45
-rw-r--r--src/dumm/ext/lib/dumm/guest.rb26
5 files changed, 186 insertions, 49 deletions
diff --git a/src/dumm/ext/dumm.c b/src/dumm/ext/dumm.c
index 2610affc3..f7caf252d 100644
--- a/src/dumm/ext/dumm.c
+++ b/src/dumm/ext/dumm.c
@@ -12,8 +12,6 @@
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
- *
- * $Id: dumm.c 4447 2008-10-15 14:47:52Z martin $
*/
#include <stdio.h>
@@ -24,11 +22,13 @@
#include <library.h>
#include <dumm.h>
#include <debug.h>
+#include <utils/linked_list.h>
#undef PACKAGE_NAME
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef PACKAGE_STRING
+#undef PACKAGE_BUGREPORT
#include <ruby.h>
static dumm_t *dumm;
@@ -84,8 +84,6 @@ static void sigchld_handler(int signal, siginfo_t *info, void* ptr)
enumerator->destroy(enumerator);
}
-
-
/**
* Guest bindings
*/
@@ -93,7 +91,9 @@ static VALUE guest_find(VALUE class, VALUE key)
{
enumerator_t *enumerator;
guest_t *guest, *found = NULL;
- if (TYPE(key) == T_SYMBOL) {
+
+ if (TYPE(key) == T_SYMBOL)
+ {
key = rb_convert_type(key, T_STRING, "String", "to_s");
}
enumerator = dumm->create_guest_enumerator(dumm);
@@ -125,19 +125,26 @@ static VALUE guest_get(VALUE class, VALUE key)
static VALUE guest_each(int argc, VALUE *argv, VALUE class)
{
+ linked_list_t *list;
enumerator_t *enumerator;
guest_t *guest;
-
+
if (!rb_block_given_p())
- {
+ {
rb_raise(rb_eArgError, "must be called with a block");
}
+ list = linked_list_create();
enumerator = dumm->create_guest_enumerator(dumm);
while (enumerator->enumerate(enumerator, &guest))
{
- rb_yield(Data_Wrap_Struct(class, NULL, NULL, guest));
+ list->insert_last(list, guest);
}
enumerator->destroy(enumerator);
+ while (list->remove_first(list, (void**)&guest) == SUCCESS)
+ {
+ rb_yield(Data_Wrap_Struct(class, NULL, NULL, guest));
+ }
+ list->destroy(list);
return class;
}
@@ -249,7 +256,9 @@ static VALUE guest_find_iface(VALUE self, VALUE key)
enumerator_t *enumerator;
iface_t *iface, *found = NULL;
guest_t *guest;
- if (TYPE(key) == T_SYMBOL) {
+
+ if (TYPE(key) == T_SYMBOL)
+ {
key = rb_convert_type(key, T_STRING, "String", "to_s");
}
Data_Get_Struct(self, guest_t, guest);
@@ -283,20 +292,27 @@ static VALUE guest_get_iface(VALUE self, VALUE key)
static VALUE guest_each_iface(int argc, VALUE *argv, VALUE self)
{
enumerator_t *enumerator;
+ linked_list_t *list;
guest_t *guest;
iface_t *iface;
-
+
if (!rb_block_given_p())
- {
+ {
rb_raise(rb_eArgError, "must be called with a block");
}
Data_Get_Struct(self, guest_t, guest);
+ list = linked_list_create();
enumerator = guest->create_iface_enumerator(guest);
while (enumerator->enumerate(enumerator, &iface))
{
- rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
+ list->insert_last(list, iface);
}
enumerator->destroy(enumerator);
+ while (list->remove_first(list, (void**)&iface) == SUCCESS)
+ {
+ rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
+ }
+ list->destroy(list);
return self;
}
@@ -305,6 +321,10 @@ static VALUE guest_delete(VALUE self)
guest_t *guest;
Data_Get_Struct(self, guest_t, guest);
+ if (guest->get_pid(guest))
+ {
+ rb_raise(rb_eRuntimeError, "guest is running");
+ }
dumm->delete_guest(dumm, guest);
return Qnil;
}
@@ -338,11 +358,15 @@ static void guest_init()
/**
* Bridge binding
*/
-static VALUE bridge_get(VALUE class, VALUE key)
+static VALUE bridge_find(VALUE class, VALUE key)
{
enumerator_t *enumerator;
bridge_t *bridge, *found = NULL;
+ if (TYPE(key) == T_SYMBOL)
+ {
+ key = rb_convert_type(key, T_STRING, "String", "to_s");
+ }
enumerator = dumm->create_bridge_enumerator(dumm);
while (enumerator->enumerate(enumerator, &bridge))
{
@@ -355,26 +379,43 @@ static VALUE bridge_get(VALUE class, VALUE key)
enumerator->destroy(enumerator);
if (!found)
{
- rb_raise(rb_eRuntimeError, "bridge not found");
+ return Qnil;
}
return Data_Wrap_Struct(class, NULL, NULL, found);
}
+static VALUE bridge_get(VALUE class, VALUE key)
+{
+ VALUE bridge = bridge_find(class, key);
+ if (NIL_P(bridge))
+ {
+ rb_raise(rb_eRuntimeError, "bridge not found");
+ }
+ return bridge;
+}
+
static VALUE bridge_each(int argc, VALUE *argv, VALUE class)
{
enumerator_t *enumerator;
+ linked_list_t *list;
bridge_t *bridge;
-
+
if (!rb_block_given_p())
- {
+ {
rb_raise(rb_eArgError, "must be called with a block");
}
+ list = linked_list_create();
enumerator = dumm->create_bridge_enumerator(dumm);
while (enumerator->enumerate(enumerator, &bridge))
{
- rb_yield(Data_Wrap_Struct(class, NULL, NULL, bridge));
+ list->insert_last(list, bridge);
}
enumerator->destroy(enumerator);
+ while (list->remove_first(list, (void**)&bridge) == SUCCESS)
+ {
+ rb_yield(Data_Wrap_Struct(class, NULL, NULL, bridge));
+ }
+ list->destroy(list);
return class;
}
@@ -402,20 +443,27 @@ static VALUE bridge_to_s(VALUE self)
static VALUE bridge_each_iface(int argc, VALUE *argv, VALUE self)
{
enumerator_t *enumerator;
+ linked_list_t *list;
bridge_t *bridge;
iface_t *iface;
-
+
if (!rb_block_given_p())
- {
+ {
rb_raise(rb_eArgError, "must be called with a block");
}
Data_Get_Struct(self, bridge_t, bridge);
+ list = linked_list_create();
enumerator = bridge->create_iface_enumerator(bridge);
while (enumerator->enumerate(enumerator, &iface))
{
- rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
+ list->insert_last(list, iface);
}
enumerator->destroy(enumerator);
+ while (list->remove_first(list, (void**)&iface) == SUCCESS)
+ {
+ rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
+ }
+ list->destroy(list);
return self;
}
@@ -437,6 +485,8 @@ static void bridge_init()
rb_define_singleton_method(rbc_bridge, "[]", bridge_get, 1);
rb_define_singleton_method(rbc_bridge, "each", bridge_each, -1);
rb_define_singleton_method(rbc_bridge, "new", bridge_new, 1);
+ rb_define_singleton_method(rbc_bridge, "include?", bridge_find, 1);
+ rb_define_singleton_method(rbc_bridge, "bridge?", bridge_find, 1);
rb_define_method(rbc_bridge, "to_s", bridge_to_s, 0);
rb_define_method(rbc_bridge, "each", bridge_each_iface, -1);
@@ -509,22 +559,29 @@ static VALUE iface_add_addr(VALUE self, VALUE name)
static VALUE iface_each_addr(int argc, VALUE *argv, VALUE self)
{
enumerator_t *enumerator;
+ linked_list_t *list;
iface_t *iface;
host_t *addr;
char buf[64];
-
+
if (!rb_block_given_p())
- {
+ {
rb_raise(rb_eArgError, "must be called with a block");
}
Data_Get_Struct(self, iface_t, iface);
enumerator = iface->create_address_enumerator(iface);
while (enumerator->enumerate(enumerator, &addr))
{
- snprintf(buf, sizeof(buf), "%H", addr);
- rb_yield(rb_str_new2(buf));
+ list->insert_last(list, addr->clone(addr));
}
enumerator->destroy(enumerator);
+ while (list->remove_first(list, (void**)&addr) == SUCCESS)
+ {
+ snprintf(buf, sizeof(buf), "%H", addr);
+ addr->destroy(addr);
+ rb_yield(rb_str_new2(buf));
+ }
+ list->destroy(list);
return self;
}
@@ -595,12 +652,31 @@ static VALUE template_unload(VALUE class)
return class;
}
+static VALUE template_each(int argc, VALUE *argv, VALUE class)
+{
+ enumerator_t *enumerator;
+ char *template;
+
+ if (!rb_block_given_p())
+ {
+ rb_raise(rb_eArgError, "must be called with a block");
+ }
+ enumerator = dumm->create_template_enumerator(dumm);
+ while (enumerator->enumerate(enumerator, &template))
+ {
+ rb_yield(rb_str_new2(template));
+ }
+ enumerator->destroy(enumerator);
+ return class;
+}
+
static void template_init()
{
rbc_template = rb_define_class_under(rbm_dumm , "Template", rb_cObject);
rb_define_singleton_method(rbc_template, "load", template_load, 1);
rb_define_singleton_method(rbc_template, "unload", template_unload, 0);
+ rb_define_singleton_method(rbc_template, "each", template_each, -1);
}
/**
diff --git a/src/dumm/ext/extconf.rb b/src/dumm/ext/extconf.rb
deleted file mode 100644
index 136be5c2c..000000000
--- a/src/dumm/ext/extconf.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-#
-# DUMM for Ruby
-#
-
-require "mkmf"
-
-dir_config("dumm")
-
-unless find_header('library.h', '../../libstrongswan') and
- find_header('dumm.h', '..')
- puts "... failed: one or more header files not found!"
- exit
-end
-
-unless find_library('dumm', 'dumm_create')
- puts "... failed: 'libdumm' not found!"
- exit
-end
-
-create_makefile("dumm")
-
diff --git a/src/dumm/ext/extconf.rb.in b/src/dumm/ext/extconf.rb.in
new file mode 100644
index 000000000..36536ec52
--- /dev/null
+++ b/src/dumm/ext/extconf.rb.in
@@ -0,0 +1,19 @@
+#
+# DUMM for Ruby
+#
+
+require 'mkmf'
+
+$defs << " @DEFS@"
+$CFLAGS << " -Wno-format"
+
+dir_config('dumm', '@top_srcdir@/src/dumm', '../.libs')
+dir_config('strongswan', '@top_srcdir@/src/libstrongswan', '../../libstrongswan/.libs')
+
+unless find_library('dumm', 'dumm_create')
+ puts "... failed: 'libdumm' not found!"
+ exit
+end
+
+create_makefile('dumm', '@top_srcdir@/src/dumm/ext')
+
diff --git a/src/dumm/ext/lib/dumm.rb b/src/dumm/ext/lib/dumm.rb
index 2e860ae9f..25939e9f4 100644
--- a/src/dumm/ext/lib/dumm.rb
+++ b/src/dumm/ext/lib/dumm.rb
@@ -11,11 +11,52 @@
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
-
- $Id: dumm.rb 4295 2008-08-27 07:35:20Z tobias $
=end
require 'dumm.so'
require 'dumm/guest'
+module Dumm
+
+ # use guest/bridge indentifiers directly
+ def method_missing(id, *args)
+ if Guest.guest? id
+ return Guest[id]
+ end
+ if Bridge.bridge? id
+ return Bridge[id]
+ end
+ super(id, *args)
+ end
+
+ # shortcut for Template loading
+ def template(name = nil)
+ if name
+ Template.load name
+ else
+ Template.each {|t| puts t }
+ end
+ end
+
+ # unload templates, reset all guests and delete bridges
+ def reset
+ Template.unload
+ Guest.each { |guest|
+ guest.reset if guest.running?
+ }
+ Bridge.each { |bridge|
+ bridge.delete
+ }
+ return Dumm
+ end
+
+ # wait until all running guests have booted up
+ def boot
+ Guest.each {|g|
+ g.boot if g.running?
+ }
+ return Dumm
+ end
+end
+
# vim:sw=2 ts=2 et
diff --git a/src/dumm/ext/lib/dumm/guest.rb b/src/dumm/ext/lib/dumm/guest.rb
index bdd0c33d8..936f512dd 100644
--- a/src/dumm/ext/lib/dumm/guest.rb
+++ b/src/dumm/ext/lib/dumm/guest.rb
@@ -11,8 +11,6 @@
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
-
- $Id: guest.rb 4295 2008-08-27 07:35:20Z tobias $
=end
module Dumm
@@ -34,6 +32,30 @@ module Dumm
end
self[id]
end
+
+ # delete all interfaces
+ def reset
+ each {|i|
+ i.delete
+ }
+ end
+
+ # has the guest booted up?
+ def booted?
+ begin
+ exec("pgrep getty")
+ rescue
+ return false
+ end
+ return true
+ end
+
+ # wait until the guest has booted
+ def boot
+ while not booted?
+ sleep(1)
+ end
+ end
end
end