git » pytipc » commit 82c2f18

Support different socket scopes.

author Alberto Bertogli
2007-12-17 03:28:01 UTC
committer Alberto Bertogli
2007-12-17 03:28:01 UTC
parent 12ec04c7625452040e11d9f65c7644f841eeae32

Support different socket scopes.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

tipc.py +4 -2
tipc_ll.c +36 -27

diff --git a/tipc.py b/tipc.py
index 0924a1e..032f0f4 100644
--- a/tipc.py
+++ b/tipc.py
@@ -4,8 +4,9 @@ TIPC python wrapper
 Alberto Bertogli (albertito@gmail.com)
 
 
-Addresses are expressed as (addr_type, srv_type, lower, upper); where
-addr_type can be either TIPC_ADDR_NAME or TIPC_ADDR_NAMESEQ.
+Addresses are expressed as (addr_type, srv_type, lower, upper [, scope]);
+where addr_type can be either TIPC_ADDR_NAME or TIPC_ADDR_NAMESEQ and scope
+can be one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
 
 If addr_type is TIPC_ADDR_NAME, then the "upper" parameter is not used (but
 should be set to an int anyway).
@@ -18,6 +19,7 @@ import tipc_ll
 # Exported constants
 from tipc_ll import AF_TIPC, SOL_TIPC, \
 	TIPC_ADDR_NAME, TIPC_ADDR_NAMESEQ, \
+	TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, TIPC_NODE_SCOPE, \
 	TIPC_IMPORTANCE, \
 	TIPC_SRC_DROPPABLE, TIPC_DEST_DROPPABLE, \
 	TIPC_CONN_TIMEOUT, \
diff --git a/tipc_ll.c b/tipc_ll.c
index f7dcff0..eb14260 100644
--- a/tipc_ll.c
+++ b/tipc_ll.c
@@ -23,13 +23,19 @@
  * Internal useful functions
  */
 
-static void fill_sockaddr(struct sockaddr_tipc *sa,
-		int atype, int stype, int lower, int upper)
+static int fill_sockaddr(struct sockaddr_tipc *sa, PyObject *addr)
 {
+	unsigned int atype, stype, lower, upper;
+	unsigned int scope = TIPC_CLUSTER_SCOPE;
+
+	if (!PyArg_ParseTuple(addr, "iiii|i;Invalid TIPC address format",
+				&atype, &stype, &lower, &upper, &scope))
+		return 0;
+
 	memset(sa, 0, sizeof(struct sockaddr_tipc));
 
 	sa->family = AF_TIPC;
-	sa->scope = TIPC_CLUSTER_SCOPE;
+	sa->scope = scope;
 
 	if (atype == TIPC_ADDR_NAME) {
 		sa->addrtype = TIPC_ADDR_NAME;
@@ -41,23 +47,25 @@ static void fill_sockaddr(struct sockaddr_tipc *sa,
 		sa->addr.nameseq.lower = lower;
 		sa->addr.nameseq.upper = upper;
 	}
-	return;
+	return 1;
 }
 
 static PyObject *sa_to_tuple(const struct sockaddr_tipc *sa)
 {
 	if (sa->addrtype == TIPC_ADDR_NAMESEQ) {
-		return Py_BuildValue("iiii",
+		return Py_BuildValue("iiiii",
 				sa->addrtype,
 				sa->addr.nameseq.type,
 				sa->addr.nameseq.lower,
-				sa->addr.nameseq.upper);
+				sa->addr.nameseq.upper,
+				sa->scope);
 	} else {
-		return Py_BuildValue("iiii",
+		return Py_BuildValue("iiiii",
 				sa->addrtype,
 				sa->addr.name.name.type,
 				sa->addr.name.name.instance,
-				sa->addr.name.name.instance);
+				sa->addr.name.name.instance,
+				sa->scope);
 	}
 }
 
@@ -86,16 +94,15 @@ static PyObject *tipc_socket(PyObject *self, PyObject *args)
 
 static PyObject *tipc_bind(PyObject *self, PyObject *args)
 {
-	int fd, atype, stype, lower, upper;
+	int fd, rv;
 	struct sockaddr_tipc sa;
-	int rv;
+	PyObject *addr;
 
-	if (!PyArg_ParseTuple(args, "i(iiii):tipc_bind",
-				&fd, &atype, &stype, &lower, &upper)) {
+	if (!PyArg_ParseTuple(args, "iO:tipc_bind", &fd, &addr))
 		return NULL;
-	}
 
-	fill_sockaddr(&sa, atype, stype, lower, upper);
+	if (!fill_sockaddr(&sa, addr))
+		return NULL;
 
 	Py_BEGIN_ALLOW_THREADS
 	rv = bind(fd, (struct sockaddr *) &sa, sizeof(sa));
@@ -110,16 +117,15 @@ static PyObject *tipc_bind(PyObject *self, PyObject *args)
 
 static PyObject *tipc_connect(PyObject *self, PyObject *args)
 {
-	int fd, atype, stype, lower, upper;
+	int fd, rv;
 	struct sockaddr_tipc sa;
-	int rv;
+	PyObject *addr;
 
-	if (!PyArg_ParseTuple(args, "i(iiii):tipc_connect",
-				&fd, &atype, &stype, &lower, &upper)) {
+	if (!PyArg_ParseTuple(args, "iO:tipc_connect", &fd, &addr))
 		return NULL;
-	}
 
-	fill_sockaddr(&sa, atype, stype, lower, upper);
+	if (!fill_sockaddr(&sa, addr))
+		return NULL;
 
 	Py_BEGIN_ALLOW_THREADS
 	rv = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
@@ -154,19 +160,17 @@ static PyObject *tipc_accept(PyObject *self, PyObject *args)
 
 static PyObject *tipc_sendto(PyObject *self, PyObject *args)
 {
-	int fd, atype, stype, lower, upper;
+	int fd, rv;
 	char *buf;
 	size_t len;
 	struct sockaddr_tipc sa;
-	int rv;
+	PyObject *addr;
 
-	if (!PyArg_ParseTuple(args, "is#(iiii):tipc_sendto",
-				&fd, &buf, &len,
-				&atype, &stype, &lower, &upper)) {
+	if (!PyArg_ParseTuple(args, "is#O:tipc_sendto", &fd, &buf, &len, &addr))
 		return NULL;
-	}
 
-	fill_sockaddr(&sa, atype, stype, lower, upper);
+	if (!fill_sockaddr(&sa, addr))
+		return NULL;
 
 	Py_BEGIN_ALLOW_THREADS
 	rv = sendto(fd, buf, len, 0, (struct sockaddr *) &sa, sizeof(sa));
@@ -285,6 +289,11 @@ PyMODINIT_FUNC inittipc_ll(void)
 	PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
 	PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
 
+	PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
+	PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
+	PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
+
+
 	/* for setsockopt() */
 	PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
 	PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);