On Sat, Dec 15, 2007 at 04:13:23PM -0300, Alberto Bertogli wrote:
> I also wrote a patch for the official socket module, which I will post
> soon.

Here's the patch for the Python's socket module.

If it's ok with you, intend to send it to the python's development
mailing list; so it'd be great if somebody with more TIPC experience
than myself reviews it.

The patch itself it's quite simple: it adds some TIPC constants at the
end, and adds a way to convert a struct sockaddr_tipc into a Python
tuple, and viceversa.

The tuple is composed of four integer values:
 - TIPC_ADDR_NAME or TIPC_ADDR_NAMESEQ
 - A server type
 if TIPC_ADDR_NAME:
   - A port number
   - Ignored
 else:
   - Lower port number
   - Upper port number

The scope is hardcoded to cluster; but if you think it's worth it, I can
make it to be an optional fifth value.

If this patch gets applied, it will be extremely hard to change the
address representation in the future, so it'd be nice to get it right =)

I selected a tuple because it is:
 - a simple, easy to construct data structure,
 - extensible if we need to add more fields in the future,
 - used as a way to express addresses for all the other network
   protocols.

I know there are two more address types (TIPC_ADDR_MCAST and
TIPC_ADDR_ID), but I have never used them, so I don't know the
semantics well enough.

.From the tipc examples I can assume TIPC_ADDR_MCAST uses nameseq and
TIPC_ADDR_ID uses the id part of the sockaddr_tipc union. If you think
it's worth it, I can add code to handle those too (they will fit into
the 4-tuple described above).


Thanks a lot,
                Alberto

From: Alberto Bertogli <[EMAIL PROTECTED]>
Date: Wed, 5 Dec 2007 18:39:18 -0300
Subject: [PATCH] Make socket support TIPC.

TIPC (http://tipc.sf.net) is an open protocol designed for use in
clustered computer environments. It currently has an open source
implementation for Linux (>= 2.6.16), and VxWorks.

This patch adds optional Linux-only support for TIPC in the socket module.
---
 Modules/socketmodule.c |  112 +++++++++++++++++++++++++++++++++++++++++++++++-
 Modules/socketmodule.h |    4 ++
 configure.in           |    2 +-
 3 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 5d38753..3d76957 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -7,7 +7,8 @@ This module provides an interface to Berkeley socket IPC.
 Limitations:
 
 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
-  portable manner, though AF_PACKET and AF_NETLINK are supported under Linux.
+  portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
+  under Linux.
 - No read/write operations (use sendall/recv or makefile instead).
 - Additional restrictions apply on some non-Unix platforms (compensated
   for by socket.py).
@@ -52,6 +53,10 @@ Module interface:
   the Ethernet protocol number to be received. For example:
   ("eth0",0x1234).  Optional 3rd,4th,5th elements in the tuple
   specify packet-type and ha-type/addr.
+- an AF_TIPC socket address is a tuple containing four integers: the address
+  type (must be either TIPC_ADDR_NAME or TIPC_ADDR_NAMESEQ), the server type,
+  and then if the address type is TIPC_ADDR_NAME a port number and an ignored
+  integer, otherwise a lower and an upper port number.
 
 Local naming conventions:
 
@@ -1094,6 +1099,26 @@ makesockaddr(int sockfd, struct sockaddr *addr, int 
addrlen, int proto)
        }
 #endif
 
+#ifdef HAVE_LINUX_TIPC_H
+       case AF_TIPC:
+       {
+               struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
+               if (a->addrtype == TIPC_ADDR_NAMESEQ) {
+                       return Py_BuildValue("iiii",
+                                       a->addrtype,
+                                       a->addr.nameseq.type,
+                                       a->addr.nameseq.lower,
+                                       a->addr.nameseq.upper);
+               } else {
+                       return Py_BuildValue("iiii",
+                                       a->addrtype,
+                                       a->addr.name.name.type,
+                                       a->addr.name.name.instance,
+                                       a->addr.name.name.instance);
+               }
+       }
+#endif
+
        /* More cases here... */
 
        default:
@@ -1379,6 +1404,47 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
        }
 #endif
 
+#ifdef HAVE_LINUX_TIPC_H
+       case AF_TIPC:
+       {
+               int atype, stype, lower, upper;
+               struct sockaddr_tipc *addr;
+
+               if (!PyTuple_Check(args)) {
+                       PyErr_Format(
+                               PyExc_TypeError,
+                               "getsockaddrarg: "
+                               "AF_TIPC address must be tuple, not %.500s",
+                               Py_Type(args)->tp_name);
+                       return 0;
+               }
+               if (!PyArg_ParseTuple(args, "iiii",
+                                       &atype, &stype, &lower, &upper))
+                       return 0;
+
+               addr = (struct sockaddr_tipc *) addr_ret;
+               memset(addr, 0, sizeof(struct sockaddr_tipc));
+
+               addr->family = AF_TIPC;
+               addr->scope = TIPC_CLUSTER_SCOPE;
+
+               if (atype == TIPC_ADDR_NAME) {
+                       addr->addrtype = TIPC_ADDR_NAME;
+                       addr->addr.name.name.type = stype;
+                       addr->addr.name.name.instance = lower;
+               } else {
+                       addr->addrtype = TIPC_ADDR_NAMESEQ;
+                       addr->addr.nameseq.type = stype;
+                       addr->addr.nameseq.lower = lower;
+                       addr->addr.nameseq.upper = upper;
+               }
+
+               *len_ret = sizeof(*addr);
+
+               return 1;
+       }
+#endif
+
        /* More cases here... */
 
        default:
@@ -1464,6 +1530,14 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
        }
 #endif
 
+#ifdef HAVE_LINUX_TIPC_H
+       case AF_TIPC:
+       {
+               *len_ret = sizeof (struct sockaddr_tipc);
+               return 1;
+       }
+#endif
+
        /* More cases here... */
 
        default:
@@ -4419,6 +4493,42 @@ init_socket(void)
        PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
 #endif
 
+#ifdef HAVE_LINUX_TIPC_H
+       PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
+
+       /* for addresses */
+       PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
+       PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
+
+       /* for setsockopt() */
+       PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
+       PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
+       PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
+       PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
+                       TIPC_DEST_DROPPABLE);
+       PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
+
+       PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
+                       TIPC_LOW_IMPORTANCE);
+       PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
+                       TIPC_MEDIUM_IMPORTANCE);
+       PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
+                       TIPC_HIGH_IMPORTANCE);
+       PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
+                       TIPC_CRITICAL_IMPORTANCE);
+
+       /* for subscriptions */
+       PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
+       PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
+       PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
+       PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
+       PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
+       PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
+       PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
+       PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
+       PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
+#endif
+
        /* Socket types */
        PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
        PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h
index a4382ab..b4a4b3d 100644
--- a/Modules/socketmodule.h
+++ b/Modules/socketmodule.h
@@ -59,6 +59,10 @@
 # include <netpacket/packet.h>
 #endif
 
+#ifdef HAVE_LINUX_TIPC_H
+# include <linux/tipc.h>
+#endif
+
 #ifndef Py__SOCKET_H
 #define Py__SOCKET_H
 #ifdef __cplusplus
diff --git a/configure.in b/configure.in
index 4964954..503ca4b 100644
--- a/configure.in
+++ b/configure.in
@@ -1106,7 +1106,7 @@ sys/param.h sys/poll.h sys/select.h sys/socket.h 
sys/statvfs.h sys/stat.h \
 sys/time.h \
 sys/times.h sys/types.h sys/un.h sys/utsname.h sys/wait.h pty.h libutil.h \
 sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
-bluetooth/bluetooth.h)
+bluetooth/bluetooth.h linux/tipc.h)
 AC_HEADER_DIRENT
 AC_HEADER_MAJOR
 
-- 
1.5.4.rc0.1096.gcd2a6

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
tipc-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tipc-discussion

Reply via email to