Add support for assigning IP addresses
authorDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Fri, 27 Jul 2007 18:02:08 +0000 (18:02 +0000)
committerDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Fri, 27 Jul 2007 18:02:08 +0000 (18:02 +0000)
python/vserver.py
python/vserverimpl.c

index e923309..50f48a1 100644 (file)
@@ -1,6 +1,6 @@
 # Copyright 2005 Princeton University
 
-#$Id: vserver.py,v 1.62 2007/07/20 19:45:35 dhozac Exp $
+#$Id: vserver.py,v 1.63 2007/07/24 17:22:37 dhozac Exp $
 
 import errno
 import fcntl
@@ -181,6 +181,33 @@ class VServer:
     def get_capabilities_config(self):
         return self.config.get('bcapabilities', '')
 
+    def set_ipaddresses(self, addresses):
+        vserverimpl.netremove(self.ctx, "all")
+        for a in addresses.split(","):
+            vserverimpl.netadd(self.ctx, a)
+
+    def set_ipaddresses_config(self, addresses):
+        i = 0
+        for a in addresses.split(","):
+            self.config.set("interfaces/%d/ip" % i, a)
+            i += 1
+        self.set_ipaddresses(addresses)
+
+    def get_ipaddresses_config(self):
+        i = 0
+        ret = []
+        while True:
+            r = self.config.get("interfaces/%d/ip" % i, '')
+            if r == '':
+                break
+            ret += [r]
+            i += 1
+        return ",".join(ret)
+
+    def get_ipaddresses(self):
+        # No clean way to do this right now.
+        return None
+
     def __do_chroot(self):
 
         os.chroot(self.dir)
index 071a603..49a22c9 100644 (file)
@@ -39,6 +39,8 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
 
 #include "config.h"
 #include "pathconfig.h"
@@ -368,6 +370,79 @@ vserver_bcaps2text(PyObject *self, PyObject *args)
   return list;
 }
 
+static inline int
+convertAddress(const char *str, vc_net_nx_type *type, void *dst)
+{
+  int   ret;
+  if (type) *type = vcNET_IPV4;
+  ret = inet_pton(AF_INET, str, dst);
+  if (ret==0) {
+    if (type) *type = vcNET_IPV6;
+    ret = inet_pton(AF_INET6, str, dst);
+  }
+  return ret > 0 ? 0 : -1;
+}
+
+/* XXX These two functions are really similar */
+static PyObject *
+vserver_net_add(PyObject *self, PyObject *args)
+{
+  struct vc_net_nx addr;
+  nid_t nid;
+  const char *ip;
+
+  if (!PyArg_ParseTuple(args, "Is", &nid, &ip))
+    return NULL;
+
+  if (convertAddress(ip, &addr.type, &addr.ip) == -1)
+    return PyErr_Format(PyExc_ValueError, "%s is not a valid IP address", ip);
+
+  switch (addr.type) {
+  case vcNET_IPV4:     addr.mask[0] = htonl(0xffffff00); break;
+  case vcNET_IPV6:     addr.mask[0] = 64; break;
+  default:             addr.mask[0] = 0; break;
+  }
+  addr.count = 1;
+
+  if (vc_net_add(nid, &addr) == -1 && errno != ESRCH)
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return NONE;
+}
+
+static PyObject *
+vserver_net_remove(PyObject *self, PyObject *args)
+{
+  struct vc_net_nx addr;
+  nid_t nid;
+  const char *ip;
+
+  if (!PyArg_ParseTuple(args, "Is", &nid, &ip))
+    return NULL;
+
+  if (strcmp(ip, "all") == 0)
+    addr.type = vcNET_ANY;
+  else if (strcmp(ip, "all4") == 0)
+    addr.type = vcNET_IPV4A;
+  else if (strcmp(ip, "all6") == 0)
+    addr.type = vcNET_IPV6A;
+  else
+    if (convertAddress(ip, &addr.type, &addr.ip) == -1)
+      return PyErr_Format(PyExc_ValueError, "%s is not a valid IP address", ip);
+
+  switch (addr.type) {
+  case vcNET_IPV4:     addr.mask[0] = htonl(0xffffff00); break;
+  case vcNET_IPV6:     addr.mask[0] = 64; break;
+  default:             addr.mask[0] = 0; break;
+  }
+  addr.count = 1;
+
+  if (vc_net_remove(nid, &addr) == -1 && errno != ESRCH)
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return NONE;
+}
+
 static PyMethodDef  methods[] = {
   { "chcontext", vserver_chcontext, METH_VARARGS,
     "chcontext to vserver with provided flags" },
@@ -397,6 +472,10 @@ static PyMethodDef  methods[] = {
     "Translate a string of capabilities to a bitmap" },
   { "bcaps2text", vserver_bcaps2text, METH_VARARGS,
     "Translate a capability-bitmap into a string" },
+  { "netadd", vserver_net_add, METH_VARARGS,
+    "Assign an IP address to a context" },
+  { "netremove", vserver_net_remove, METH_VARARGS,
+    "Remove IP address(es) from a context" },
   { NULL, NULL, 0, NULL }
 };