Acquire and release the GIL around select callbacks.
[mLib-python] / defs.pxi
1 # -*-pyrex-*-
2 #
3 # $Id$
4 #
5 # Basic definitions, used all over
6 #
7 # (c) 2005 Straylight/Edgeware
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of the Python interface to mLib.
13 #
14 # mLib/Python is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # mLib/Python is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with mLib/Python; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28 #----- C library ------------------------------------------------------------
29
30 cdef extern from 'errno.h':
31 int errno
32 enum:
33 EINTR
34 EAGAIN
35 cdef extern from 'limits.h':
36 enum:
37 LONG_MAX
38 cdef extern from 'math.h':
39 double modf(double x, double *i)
40 cdef extern from 'stddef.h':
41 ctypedef int size_t
42 cdef extern from 'string.h':
43 void memcpy(void *p, void *q, size_t n)
44 char *strerror(int err)
45
46 #----- Unix interface -------------------------------------------------------
47
48 cdef extern from 'sys/types.h':
49 pass
50 cdef extern from 'sys/time.h':
51 struct timeval:
52 int tv_sec
53 int tv_usec
54
55 cdef extern from 'sys/socket.h':
56 struct sockaddr:
57 int sa_family
58 enum:
59 AF_INET
60 int getsockname(int fd, sockaddr *pa, size_t *psz)
61 int getpeername(int fd, sockaddr *pa, size_t *psz)
62 cdef extern from 'arpa/inet.h':
63 struct in_addr:
64 int s_addr
65 int inet_aton(char *rp, in_addr *ia)
66 char *inet_ntoa(in_addr ia)
67 cdef extern from 'netinet/in.h':
68 struct sockaddr_in:
69 int sin_family
70 in_addr sin_addr
71 int sin_port
72 int htons(int x)
73 int htonl(int x)
74 int ntohs(int x)
75 int ntohl(int x)
76 cdef extern from 'netdb.h':
77 struct hostent:
78 char *h_name
79 char **h_aliases
80 int h_addrtype
81 int h_length
82 char **h_addr_list
83 char *h_addr
84 int h_errno
85
86 #----- Python ---------------------------------------------------------------
87
88 cdef extern from 'Python.h':
89
90 object PyString_FromStringAndSize(char *p, int len)
91 int PyString_AsStringAndSize(obj, char **p, int *len) except -1
92 int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
93 int PyObject_TypeCheck(obj, ty)
94 object PyInt_FromLong(long i)
95 object PyLong_FromUnsignedLong(unsigned long i)
96
97 void PyEval_AcquireLock()
98 void PyEval_ReleaseLock()
99
100 ctypedef struct PyObject:
101 pass
102 ctypedef struct PyTypeObject:
103 pass
104 void Py_INCREF(PyObject *obj)
105 void Py_DECREF(PyObject *obj)
106
107 #----- mLib basic stuff -----------------------------------------------------
108
109 cdef extern from 'mLib/alloc.h':
110 char *xstrdup(char *p)
111 void *xmalloc(size_t sz)
112 void xfree(void *p)
113
114 cdef extern from 'mLib/bits.h':
115 ctypedef int uint32
116
117 cdef extern from 'mLib/dstr.h':
118 ctypedef struct dstr:
119 char *buf
120 int len
121 void DCREATE(dstr *d)
122 void dstr_destroy(dstr *d)
123
124 #----- CRC32 ----------------------------------------------------------------
125
126 cdef extern from 'mLib/crc32.h':
127 uint32 c_crc32 "crc32" (uint32 a, void *p, int sz)
128
129 #----- Universal hashing ----------------------------------------------------
130
131 cdef extern from 'mLib/unihash.h':
132 ctypedef struct unihash_info:
133 pass
134 void unihash_setkey(unihash_info *i, uint32 k)
135 uint32 UNIHASH_INIT(unihash_info *i)
136 uint32 unihash_hash(unihash_info *i, uint32 a, void *p, int sz)
137 unihash_info unihash_global
138
139 #----- Symbol tables --------------------------------------------------------
140
141 cdef extern from 'mLib/sym.h':
142 ctypedef struct sym_table:
143 pass
144 ctypedef struct sym_base:
145 pass
146 ctypedef struct sym_iter:
147 pass
148 void sym_create(sym_table *t)
149 void sym_destroy(sym_table *t)
150 void *sym_find(sym_table *t, char *n, long l, int sz, unsigned *f)
151 void sym_remove(sym_table *t, void *b)
152 char *SYM_NAME(void *b)
153 int SYM_LEN(void *b)
154 void sym_mkiter(sym_iter *i, sym_table *t)
155 void *sym_next(sym_iter *i)
156
157 #----- Atom stuff -----------------------------------------------------------
158
159 # --- Atoms ---
160 #
161 # Partly written in `real' C.
162
163 cdef extern from 'atom.h':
164 ctypedef struct atom:
165 pass
166 ctypedef struct atom_iter:
167 pass
168 ctypedef struct atom_table:
169 pass
170 atom_table *ATOM_GLOBAL
171 void atom_mkiter(atom_iter *i, atom_table *t)
172 atom *atom_next(atom_iter *)
173 void atom_pysetup()
174 atom_pywrap(atom *a)
175 atom_pyintern(obj)
176 atom *ATOM_A(obj)
177 PyTypeObject atom_pytype
178
179 # --- Association tables ---
180
181 cdef extern from 'mLib/assoc.h':
182 ctypedef struct assoc_table:
183 pass
184 ctypedef struct assoc_base:
185 pass
186 ctypedef struct assoc_iter:
187 pass
188 void assoc_create(assoc_table *t)
189 void assoc_destroy(assoc_table *t)
190 void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f)
191 void assoc_remove(assoc_table *t, void *b)
192 atom *ASSOC_ATOM(void *b)
193 void assoc_mkiter(assoc_iter *i, assoc_table *t)
194 void *assoc_next(assoc_iter *i)
195
196 #----- Double-ended arrays --------------------------------------------------
197
198 cdef extern from 'array.h':
199 void da_pysetup()
200 PyTypeObject da_pytype
201 PyTypeObject daiter_pytype
202
203 #----- Line buffer ----------------------------------------------------------
204
205 cdef extern from 'mLib/lbuf.h':
206 cdef struct lbuf:
207 int f
208 int delim
209 size_t sz
210 enum:
211 LBUF_ENABLE
212 _LBUF_CRLF "LBUF_CRLF"
213 _LBUF_STRICTCRLF "LBUF_STRICTCRLF"
214 void lbuf_flush(lbuf *b, char *p, size_t len)
215 void lbuf_close(lbuf *b)
216 size_t lbuf_free(lbuf *b, char **p)
217 void lbuf_setsize(lbuf *b, size_t sz)
218 void lbuf_enable(lbuf *b)
219 void lbuf_disable(lbuf *b)
220 void lbuf_init(lbuf *b,
221 void (*func)(char *s, size_t len, void *arg), void *arg)
222 void lbuf_destroy(lbuf *b)
223
224 #----- Packet buffer --------------------------------------------------------
225
226 cdef extern from 'mLib/pkbuf.h':
227 ctypedef struct pkbuf:
228 int f
229 int want
230 enum:
231 PKBUF_ENABLE
232 void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len)
233 void pkbuf_close(pkbuf *pk)
234 size_t pkbuf_free(pkbuf *pk, unsigned char **p)
235 void pkbuf_want(pkbuf *pk, size_t sz)
236 void pkbuf_init(pkbuf *b,
237 void (*func)(unsigned char *s, size_t len,
238 pkbuf *pk, size_t *keep, void *arg),
239 void *arg)
240 void pkbuf_destroy(pkbuf *b)
241
242 #----- Select stuff ---------------------------------------------------------
243
244 # --- Basics ---
245
246 cdef extern from 'mLib/sel.h':
247 ctypedef struct sel_state:
248 pass
249 ctypedef struct sel_file:
250 int fd
251 ctypedef struct sel_timer:
252 pass
253 enum:
254 _SEL_READ "SEL_READ"
255 _SEL_WRITE "SEL_WRITE"
256 _SEL_EXC "SEL_EXC"
257 void sel_init(sel_state *s)
258 void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode,
259 void (*func)(int fd, unsigned mode, void *arg),
260 void *arg)
261 void sel_force(sel_file *f)
262 void sel_addfile(sel_file *f)
263 void sel_rmfile(sel_file *f)
264 void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv,
265 void (*func)(timeval *tv, void *arg),
266 void *arg)
267 void sel_rmtimer(sel_timer *t)
268 int sel_select(sel_state *s) except *
269
270 # --- Non-blocking connection ---
271
272 cdef extern from 'mLib/conn.h':
273 ctypedef struct conn:
274 pass
275 int conn_fd(conn *c, sel_state *s, int fd,
276 void (*func)(int fd, void *arg), void *arg)
277 void conn_kill(conn *c)
278
279 # --- Background name resolver ---
280
281 cdef extern from 'mLib/bres.h':
282 ctypedef struct bres_client:
283 pass
284 void bres_byname(bres_client *r, char *name,
285 void (*func)(hostent *h, void *arg), void *arg)
286 void bres_byaddr(bres_client *r, in_addr addr,
287 void (*func)(hostent *h, void *arg), void *arg)
288 void bres_abort(bres_client *r)
289 void bres_exec(char *null)
290 void bres_init(sel_state *s)
291
292 # --- In-band signal handling ---
293
294 cdef extern from 'mLib/sig.h':
295 ctypedef struct sig:
296 pass
297 void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg)
298 void sig_remove(sig *s)
299 void sig_init(sel_state *s)
300
301 # --- Line buffer ---
302
303 cdef extern from 'mLib/selbuf.h':
304 ctypedef struct selbuf:
305 sel_file reader
306 lbuf b
307 void selbuf_enable(selbuf *b)
308 void selbuf_disable(selbuf *b)
309 void selbuf_setsize(selbuf *b, size_t sz)
310 void selbuf_init(selbuf *b, sel_state *s, int fd,
311 void (*func)(char *s, size_t len, void *arg), void *arg)
312 void selbuf_destroy(selbuf *b)
313
314 # --- Packet buffer ---
315
316 cdef extern from 'mLib/selpk.h':
317 ctypedef struct selpk:
318 sel_file reader
319 pkbuf pk
320 void selpk_enable(selpk *b)
321 void selpk_disable(selpk *b)
322 void selpk_want(selpk *b, size_t sz)
323 void selpk_init(selpk *b, sel_state *s, int fd,
324 void (*func)(unsigned char *p, size_t n,
325 pkbuf *pk, size_t *keep, void *arg),
326 void *arg)
327 void selpk_destroy(selpk *b)
328
329 # --- Ident client ---
330
331 cdef extern from 'mLib/ident.h':
332 ctypedef struct ident_request:
333 pass
334 enum:
335 IDENT_USERID
336 IDENT_ERROR
337 IDENT_BAD
338 struct ident_userid:
339 char *os
340 char *user
341 union ident_u:
342 ident_userid userid
343 char *error
344 ctypedef struct ident_reply:
345 int sport
346 int dport
347 int type
348 ident_u u
349 void ident(ident_request *rq, sel_state *s,
350 sockaddr_in *local, sockaddr_in *remote,
351 void (*func)(ident_reply *r, void *arg),
352 void *arg)
353 void ident_abort(ident_request *rq)
354
355 #----- Error reporting ------------------------------------------------------
356
357 cdef extern from 'mLib/quis.h':
358 void _ego "ego"(char *prog)
359 char *_quis "quis"()
360 cdef extern from 'mLib/report.h':
361 void _moan "moan"(char *f, char *msg)
362
363 #----- Internal utilities ---------------------------------------------------
364
365 cdef extern from 'grim.h':
366 int PSIZEOF(void *x)
367
368 #----- That's all, folks ----------------------------------------------------