summaryrefslogtreecommitdiffstats
path: root/patches/Python-2.6.2/generic/0001-Add-support-for-socketcan-to-the-python-socket-modul.patch
blob: a1a0c9dcf17d2ab87456149dc843d1ccfbf8b08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
From 7f8972018c739fedf709f6d25405fa5505d226c7 Mon Sep 17 00:00:00 2001
From: Michael Olbrich <m.olbrich@pengutronix.de>
Date: Tue, 5 May 2009 15:17:20 +0200
Subject: [PATCH] Add support for socketcan to the python socket module

This patch add support for the protocol family AF_CAN. It contains all the
necessary code to use the python socket module for socketcan.

Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 Lib/plat-linux2/IN.py  |    2 +
 Modules/socketmodule.c |   90 ++++++++++++++++++++++++++++++++++++++++++++++++
 Modules/socketmodule.h |   11 ++++++
 configure.in           |   13 +++++++
 4 files changed, 116 insertions(+), 0 deletions(-)

diff --git a/Lib/plat-linux2/IN.py b/Lib/plat-linux2/IN.py
index ad307f6..f72ae88 100644
--- a/Lib/plat-linux2/IN.py
+++ b/Lib/plat-linux2/IN.py
@@ -384,6 +384,7 @@ PF_SNA = 22
 PF_IRDA = 23
 PF_PPPOX = 24
 PF_WANPIPE = 25
+PF_CAN = 29
 PF_BLUETOOTH = 31
 PF_MAX = 32
 AF_UNSPEC = PF_UNSPEC
@@ -414,6 +415,7 @@ AF_SNA = PF_SNA
 AF_IRDA = PF_IRDA
 AF_PPPOX = PF_PPPOX
 AF_WANPIPE = PF_WANPIPE
+AF_CAN = PF_CAN
 AF_BLUETOOTH = PF_BLUETOOTH
 AF_MAX = PF_MAX
 SOL_RAW = 255
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index ca10fb5..02fe1e1 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -417,6 +417,10 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
 
 #define	SAS2SA(x)	((struct sockaddr *)(x))
 
+#ifdef ENABLE_CAN
+#include <linux/can/raw.h>
+#endif
+
 /*
  * Constants for getnameinfo()
  */
@@ -1041,6 +1045,22 @@ makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
 	}
 #endif
 
+#ifdef ENABLE_CAN
+	case AF_CAN:
+	{
+		struct sockaddr_can *a = (struct sockaddr_can *)addr;
+		char *ifname = "";
+		struct ifreq ifr;
+		/* need to look up interface name give index */
+		if (a->can_ifindex) {
+			ifr.ifr_ifindex = a->can_ifindex;
+			if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
+				ifname = ifr.ifr_name;
+		}
+		return Py_BuildValue("s", ifname);
+	}
+#endif
+
 #ifdef USE_BLUETOOTH
 	case AF_BLUETOOTH:
 		switch (proto) {
@@ -1300,6 +1320,29 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
 	}
 #endif
 
+#ifdef ENABLE_CAN
+	case AF_CAN:
+	{
+		struct sockaddr_can* addr;
+		struct ifreq ifr;
+		char *interfaceName;
+		addr=&(s->sock_addr.can);
+		if (!PyArg_Parse(args, "s", &interfaceName))
+			return 0;
+		strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
+		ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
+		if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
+		        s->errorhandler();
+			return 0;
+		}
+		addr->can_family = AF_CAN;
+		addr->can_ifindex = ifr.ifr_ifindex;
+		*addr_ret = (struct sockaddr *) addr;
+		*len_ret = sizeof *addr;
+		return 1;
+	}
+#endif
+
 #ifdef USE_BLUETOOTH
 	case AF_BLUETOOTH:
 	{
@@ -1530,6 +1573,14 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
 	}
 #endif
 
+#ifdef ENABLE_CAN
+	case AF_CAN:
+	{
+		*len_ret = sizeof (struct sockaddr_can);
+		return 1;
+	}
+#endif
+
 #ifdef USE_BLUETOOTH
 	case AF_BLUETOOTH:
 	{
@@ -4559,6 +4610,10 @@ init_socket(void)
 	PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
 #endif
 
+#ifdef ENABLE_CAN
+	PyModule_AddIntConstant(m, "AF_CAN", AF_CAN);
+#endif
+
 #ifdef USE_BLUETOOTH
 	PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
 	PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
@@ -4892,6 +4947,41 @@ init_socket(void)
 	PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
 #endif
 
+#ifdef	CAN_RAW
+	PyModule_AddIntConstant(m, "CAN_RAW", CAN_RAW);
+#endif
+#ifdef	CAN_BCM
+	PyModule_AddIntConstant(m, "CAN_BCM", CAN_BCM);
+#endif
+#ifdef	CAN_TP16
+	PyModule_AddIntConstant(m, "CAN_TP16", CAN_TP16);
+#endif
+#ifdef	CAN_TP20
+	PyModule_AddIntConstant(m, "CAN_TP20", CAN_TP20);
+#endif
+#ifdef	CAN_MCNET
+	PyModule_AddIntConstant(m, "CAN_MCNET", CAN_MCNET);
+#endif
+#ifdef	CAN_ISOTP
+	PyModule_AddIntConstant(m, "CAN_ISOTP", CAN_ISOTP);
+#endif
+#ifdef	CAN_NPROTO
+	PyModule_AddIntConstant(m, "CAN_NPROTO", CAN_NPROTO);
+#endif
+
+#ifdef	SOL_CAN_BASE
+	PyModule_AddIntConstant(m, "SOL_CAN_BASE", SOL_CAN_BASE);
+#endif
+#ifdef	SOL_CAN_RAW
+	PyModule_AddIntConstant(m, "SOL_CAN_RAW", SOL_CAN_RAW);
+#endif
+#ifdef ENABLE_CAN
+	PyModule_AddIntConstant(m, "CAN_RAW_FILTER", CAN_RAW_FILTER);
+	PyModule_AddIntConstant(m, "CAN_RAW_ERR_FILTER", CAN_RAW_ERR_FILTER);
+	PyModule_AddIntConstant(m, "CAN_RAW_LOOPBACK", CAN_RAW_LOOPBACK);
+	PyModule_AddIntConstant(m, "CAN_RAW_RECV_OWN_MSGS", CAN_RAW_RECV_OWN_MSGS);
+#endif
+
 	/* Some port configuration */
 #ifdef	IPPORT_RESERVED
 	PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h
index ef8d0fc..56c0cbc 100644
--- a/Modules/socketmodule.h
+++ b/Modules/socketmodule.h
@@ -55,6 +55,14 @@ typedef int socklen_t;
 #include <bluetooth/hci.h>
 #endif
 
+#define AF_CAN 29
+#define PF_CAN AF_CAN
+
+#ifdef HAVE_LINUX_CAN_H
+#define ENABLE_CAN 1
+#include <linux/can.h>
+#endif
+
 #ifdef HAVE_BLUETOOTH_H
 #include <bluetooth.h>
 #endif
@@ -105,6 +113,9 @@ typedef union sock_addr {
 	struct sockaddr_in6 in6;
 	struct sockaddr_storage storage;
 #endif
+#ifdef ENABLE_CAN
+	struct sockaddr_can can;
+#endif
 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
 	struct sockaddr_l2 bt_l2;
 	struct sockaddr_rc bt_rc;
diff --git a/configure.in b/configure.in
index 106e6a7..311311b 100644
--- a/configure.in
+++ b/configure.in
@@ -1261,6 +1261,19 @@ AC_CHECK_HEADERS(linux/netlink.h,,,[
 #endif
 ])
 
+AC_CHECK_HEADERS(linux/can.h,[],[],[#include <sys/socket.h>])
+# check for AF_CAN
+AC_TRY_COMPILE(
+    [[#include <sys/socket.h>
+      int domain = AF_CAN;]],
+    [[socket(domain, 0, 0);]],
+    [],
+    [
+        AC_DEFINE(AF_CAN, 29, [Define AF_CAN if not defined by sys/socket.h])
+        AC_DEFINE(PF_CAN, 29, [Define PF_CAN if not defined by sys/socket.h])
+    ]
+)
+
 # checks for typedefs
 was_it_defined=no
 AC_MSG_CHECKING(for clock_t in time.h)
-- 
1.5.6.3