-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndrxmodule.c
2861 lines (2262 loc) · 76 KB
/
ndrxmodule.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file implements a Python extension module for accessing the ATMI API of the
BEA ENDUROX Transaction Monitor system. It can be used to build clients or
servers in the Python language.
(c) 1999,2000 Ralf Henschkowski ([email protected])
*/
/* {{{ includes */
#include <stdio.h> /* System header file */
#ifdef USE_THREADS
#include <pthread.h> /* System header file */
#endif /* USE_THREADS */
#include <xa.h> /* ENDUROX Header File */
#include <atmi.h> /* ENDUROX Header File */
#include "ubf.h" /* ENDUROX Header File */
#include <tpadm.h> /* ENDUROX Header File */
#include <userlog.h> /* ENDUROX Header File */
#include <ubf.h> /* ENDUROX Header File */
#include <Python.h>
#include "ndrxconvert.h" /* Needed for some helper functions to convert Python
data types to ENDUROX data types and vice versa */
/* }}} */
/* {{{ defines & typedefs */
#define MAX_PY_SERVICES 100
#define MAX_SVC_NAME_LEN 15 + 1
#define MAX_METHOD_NAME_LEN 1024
typedef struct {
char name[MAX_SVC_NAME_LEN]; /* constant from atmi.h */
char method[MAX_METHOD_NAME_LEN];
} service_entry;
/* }}} */
/* {{{ local function prototypes */
/* Forward declarions of local functions */
#ifndef NDRXWS
void endurox_dispatch(TPSVCINFO * rqst);
#endif /* not NDRXWS */
extern void _set_XA_switch(struct xa_switch_t* new_xa_switch) ;
static PyObject * makeargvobject(int argc, char** argv);
static int find_entry(const char* name);
static int find_free_entry(const char* name);
static char* transform_py_to_ndrx(PyObject* res_py);
static PyObject* transform_ndrx_to_py(char* ndrxbuf);
static PyObject* ndrx_tppost(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpsubscribe(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpunsubscribe(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpnotify(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpbroadcast(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpsetunsol(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpchkunsol(PyObject* self, PyObject* arg);
static void mainloop(int argc, char** argv);
static PyObject * ndrx_tpcall(PyObject * self, PyObject * args);
static PyObject * ndrx_tpacall(PyObject * self, PyObject * args);
#ifndef NDRXWS
static PyObject * ndrx_tpadmcall(PyObject * self, PyObject * args);
static PyObject * ndrx_tpforward(PyObject * self, PyObject * args);
static PyObject * ndrx_mainloop(PyObject * self, PyObject * args);
static PyObject* ndrx_tpadvertise(PyObject* self, PyObject* arg);
static PyObject* ndrx_tpunadvertise(PyObject* self, PyObject* arg);
static PyObject * ndrx_tpopen(PyObject * self, PyObject * args);
static PyObject * ndrx_tpclose(PyObject * self, PyObject * args);
#endif /* NDRXWS */
static PyObject * ndrx_tpgetrply(PyObject * self, PyObject * args);
static PyObject * ndrx_tpconnect(PyObject * self, PyObject * args);
static PyObject * ndrx_tpsend(PyObject * self, PyObject * args);
static PyObject * ndrx_tprecv(PyObject * self, PyObject * args);
static PyObject * ndrx_tpbegin(PyObject * self, PyObject * args);
static PyObject * ndrx_tpcommit(PyObject * self, PyObject * args);
static PyObject * ndrx_tpabort(PyObject * self, PyObject * args);
static PyObject * ndrx_tpsuspend(PyObject * self, PyObject * args);
static PyObject * ndrx_tpresume(PyObject * self, PyObject * args);
static PyObject * ndrx_tpgetlev(PyObject * self, PyObject * args);
static PyObject * ndrx_tpdiscon(PyObject * self, PyObject * args);
static PyObject * ndrx_userlog(PyObject * self, PyObject * args);
static void ins(PyObject *d, char *s, long x);
static PyObject * ndrx_tpinit(PyObject * self, PyObject * args);
#if NDRXVERSION >= 7
static PyObject * ndrx_tpgetctxt(PyObject * self, PyObject * args);
static PyObject * ndrx_tpsetctxt(PyObject * self, PyObject * args);
#endif /* NDRXVERSION */
static PyObject * ndrx_tpchkauth(PyObject * self, PyObject * args);
static PyObject * ndrx_tpterm(PyObject * self, PyObject * args);
static PyObject* ndrx_get_tpurcode(PyObject* self, PyObject * args);
static PyObject* ndrx_set_tpurcode(PyObject* self, PyObject * args);
static PyObject * ndrx_tpenqueue(PyObject * self, PyObject * args);
static PyObject * ndrx_tpdequeue(PyObject * self, PyObject * args);
/* }}} */
/* {{{ local variables */
static PyMethodDef ndrx_methods[] = {
{"tpinit", ndrx_tpinit, METH_VARARGS, "args: {usrname: '', clt=''}"},
#if NDRXVERSION >= 7
{"tpgetctxt", ndrx_tpgetctxt, METH_VARARGS, "args: {} -> context"},
{"tpsetctxt", ndrx_tpsetctxt, METH_VARARGS, "args: {context}"},
#endif /* NDRXVERSION */
{"tpterm", ndrx_tpterm, METH_VARARGS},
{"tpchkauth", ndrx_tpchkauth, METH_VARARGS},
{"tpcall", ndrx_tpcall, METH_VARARGS, "args: ('service', {args}|'flags')"},
{"tpacall", ndrx_tpacall, METH_VARARGS, "args: ('service', {args}|'args') -> handle"},
{"tpconnect", ndrx_tpconnect, METH_VARARGS, "args: ('service', {args}|'args') -> handle"},
{"tpsend", ndrx_tpsend, METH_VARARGS, "args: (handle, input, [flags]) -> revent"},
#ifndef NDRXWS
{"tpadmcall", ndrx_tpadmcall, METH_VARARGS, "args: ({args}|'flags')"},
{"tpopen", ndrx_tpopen, METH_VARARGS, ""},
{"tpclose", ndrx_tpclose, METH_VARARGS, ""},
{"tpadvertise", ndrx_tpadvertise, METH_VARARGS},
{"tpunadvertise", ndrx_tpunadvertise, METH_VARARGS},
{"mainloop", ndrx_mainloop, METH_VARARGS},
{"tpforward", ndrx_tpforward, METH_VARARGS, "args: ('service', {args}|'args')"},
#endif /* NDRXWS */
{"tpcommit", ndrx_tpcommit, METH_VARARGS, ""},
{"tpabort", ndrx_tpabort, METH_VARARGS, ""},
{"tpbegin", ndrx_tpbegin, METH_VARARGS, "args: timeout"},
{"tpsuspend", ndrx_tpsuspend, METH_VARARGS, ""},
{"tpresume", ndrx_tpresume, METH_VARARGS, ""},
{"tpgetlev", ndrx_tpgetlev, METH_VARARGS, ""},
{"tprecv", ndrx_tprecv, METH_VARARGS, ""},
{"tpdiscon", ndrx_tpdiscon, METH_VARARGS, ""},
{"tpgetrply", ndrx_tpgetrply, METH_VARARGS, ""},
{"tpenqueue", ndrx_tpenqueue, METH_VARARGS, "args: ('qspace', 'qname', data, {qctl})"},
{"tpdequeue", ndrx_tpdequeue, METH_VARARGS, "args: ('qspace', 'qname', {qctl})"},
{"tppost", ndrx_tppost, METH_VARARGS},
{"tpsubscribe", ndrx_tpsubscribe, METH_VARARGS, "args: ('expr', 'filter', {evctl}) -> handle"},
{"tpunsubscribe", ndrx_tpunsubscribe, METH_VARARGS, "args: (handle)"},
{"tpnotify", ndrx_tpnotify, METH_VARARGS},
{"tpbroadcast", ndrx_tpbroadcast, METH_VARARGS},
{"tpsetunsol", ndrx_tpsetunsol, METH_VARARGS},
{"tpchkunsol", ndrx_tpchkunsol, METH_VARARGS},
{"userlog", ndrx_userlog, METH_VARARGS},
{"get_tpurcode", ndrx_get_tpurcode, METH_VARARGS},
{"set_tpurcode", ndrx_set_tpurcode, METH_VARARGS},
{NULL, NULL, 0}
};
#ifndef NDRXWS
static struct PyModuleDef atmimodule = {
PyModuleDef_HEAD_INIT,
"atmi", /* name of module */
0, /* doc */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
ndrx_methods
};
#else
static struct PyModuleDef atmimodule = {
PyModuleDef_HEAD_INIT,
"atmiws", /* name of module */
0,
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
ndrx_methods
};
#endif /* NDRXWS */
/* static variables: only used in the server */
/* Flag indicating that a server is running (in a ENDUROX sense) */
static int _server_is_running = 0;
/* Holds the urcode (user return code) to be returned with the next
tpreturn */
static int _set_tpurcode = 0;
/* Table to hold all the services currently advertised by this server */
static service_entry _registered_services[MAX_PY_SERVICES];
/* Reference to the Python object that implements the server */
static PyObject* _server_obj = NULL;
/* Reference to the Python function that eventually reloads a server object
during server runtime (for debugging purposes */
static PyObject* _reloader_function = NULL;
/* Flag indicating that a tpforward() instead of a tpreturn() should be
used. See ndrx_tpforward() for details */
static int _forward = 0;
/* Name of the service to which the request should be forwarded */
static char* _forward_service = NULL;
/* The data that should be forwarded */
static PyObject* _forward_pydata = NULL;
/* Holds the Unsolicited Message Handler function */
static PyObject * py_unsol_handler = NULL;
/* }}} */
/* **************************************** */
/* */
/* Debinitions of local functions */
/* */
/* **************************************** */
/* {{{ makeargvobject */
/* This function is taken from the Python interpreter's source code. It converts the C
argc/argv to a Python argv list */
static PyObject *
makeargvobject(int argc, char** argv)
{
PyObject *av;
if (argc <= 0 || argv == NULL) {
/* Ensure at least one (empty) argument is seen */
static char *empty_argv[1] = {""};
argv = empty_argv;
argc = 1;
}
av = PyList_New(argc);
if (av != NULL) {
int i;
for (i = 0; i < argc; i++) {
PyObject *v = PyUnicode_FromString(argv[i]);
if (v == NULL) {
Py_DECREF(av);
av = NULL;
break;
}
PyList_SetItem(av, i, v);
}
}
return av;
}
/* }}} */
/* {{{ find_entry() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
find_entry() looks up a service in the static services table.
int find_entry Return: index to _registered_services array or -1
if the service could not be found
const char* name Name of the service :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static int find_entry(const char* name) {
int i;
for (i = 0; i < MAX_PY_SERVICES; i++) {
if (!strcmp(name, _registered_services[i].name)) {
return i;
}
}
return -1;
}
/* }}} */
/* {{{ find_free_entry() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
find_free_entry() finds a free slot for a given service in the static
services table. If the service is already listed in the table, that slot
is returned
int find_free_entry Return: index to a free slot in _registered_services
or -1 if no slot could not be found
const char* name Name of the service that should be inserted :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static int find_free_entry(const char* name) {
int i ;
for (i = 0; i < MAX_PY_SERVICES; i++) {
if (_registered_services[i].name[0] == '\0' || (!strcmp(_registered_services[i].name, name)) ) {
#ifdef DEBUG
printf("find returning %d\n", i);
#endif
return i;
}
}
return -1;
}
/* }}} */
/* {{{ delete_entry() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Delete a given service from the static service table
int delete_entry Return: index to the freed slot
const char* name Name of the service to be deleted :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static int delete_entry(const char* name) {
int i;
for (i = 0; i < MAX_PY_SERVICES; i++) {
if (!strcmp(_registered_services[i].name, name)) {
_registered_services[i].name[0] = '\0';
_registered_services[i].method[0] = '\0';
return i;
}
}
return i;
}
/* }}} */
/* {{{ ins() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Helper function to insert a value x / key s into a dictionary d
PyObject *d Dictionary :IN
char *s Key :IN
long x Value :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void
ins(PyObject *d, char *s, long x)
{
PyObject *v = PyLong_FromLong(x);
if (v) {
PyDict_SetItemString(d, s, v);
Py_DECREF(v);
}
}
/* }}} */
/* {{{ transform_py_to_ndrx() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This function checks for the Python-type of its arguments and returns a
corresponding ENDUROX typed buffer. Currently, only strings or
dictionaries are allowed
char* transform_py_to_ndrx Return: pointer to a ENDUROX typed buffer
PyObject* res_py Python object :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static char* transform_py_to_ndrx(PyObject* res_py) {
char* res_ndrx = NULL;
if (PyDict_Check(res_py)) {
res_ndrx = (char*)dict_to_ubf(res_py);
} else if (PyUnicode_Check(res_py)) {
res_ndrx = pystring_to_string(res_py);
} else {
PyErr_SetString(PyExc_RuntimeError, "Only String or Dictionary arguments are allowed");
}
return res_ndrx;
}
/* }}} */
/* {{{ transform_ndrx_to_py() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This function converts a ENDUROX typed buffer to the corresponding Python
types (string or dictionary). Currently, only STRING and UBF buffers
are allowed.
PyObject* transform_ndrx_to_py Return: Python object
char* ndrxbuf pointer to a ENDUROX typed buffer :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static PyObject* transform_ndrx_to_py(char* ndrxbuf) {
char buffer_type[100] = "";
char buffer_subtype[100] = "";
PyObject* obj = NULL;
if (tptypes(ndrxbuf, buffer_type, buffer_subtype) < 0) {
char tmp[200] = "";
sprintf(tmp, "tptypes() : %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
if (!strcmp(buffer_type, "UBF")) {
if ((obj = ubf_to_dict((UBFH*)ndrxbuf)) == NULL) {
#ifdef DEBUG
bprintf(stderr, "no ubf buffer\n");
#endif
goto leave_func;
}
} else if (!strcmp(buffer_type, "STRING")) {
if ((obj = string_to_pystring((char*)ndrxbuf)) == NULL) {
#ifdef DEBUG
bprintf(stderr, "no string buffer\n");
#endif
goto leave_func;
}
}
leave_func:
#ifdef DEBUG
PyObject_Print(obj, stdout, 0);
printf("\n");
#endif
return obj;
}
/* }}} */
#ifndef NDRXWS
/* {{{ mainloop() */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This function is taken from the ENDUROX generated main. When called, the
server is under ENDUROX control and can process requests. This function
returns only when the server is shut down.
int argc # of command line arguments :IN
char** argv command line arguments :IN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void
mainloop(int argc, char** argv) {
#ifdef TMMAINEXIT
#include "mainexit.h"
#endif
int res = 0;
res = _tmstartserver( argc, argv, _tmgetsvrargs());
}
/* }}} */
#endif /* NDRXWS */
/* {{{ ndrx_tpcall() */
static PyObject *
ndrx_tpcall(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * input_py = NULL;
PyObject * flags_py = NULL;
char* service_name;
char* ndrxbuf = NULL;
long outlen = 0;
long flags = 0;
if (PyArg_ParseTuple(args, "sO|O", &service_name, &input_py, &flags_py) == 0) {
goto leave_func;
}
if (!service_name || (strlen(service_name) <= 0)) {
PyErr_SetString(PyExc_RuntimeError, "tpcall(): No service name given");
goto leave_func;
}
if (!input_py) {
PyErr_SetString(PyExc_RuntimeError, "tpcall(): No arguments given");
goto leave_func;
}
if (strlen(service_name) > MAX_SVC_NAME_LEN) {
PyErr_SetString(PyExc_RuntimeError, "tpcall(): Service name length too long");
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tpcall(): Bad flags given");
goto leave_func;
}
}
if ((ndrxbuf = transform_py_to_ndrx(input_py)) == NULL) {
goto leave_func;
}
#ifdef DEBUG
{
char bubfname[200] = "";
tptypes(ndrxbuf, bubfname, NULL);
bprintf(stderr, "calling tpcall(%s, [%s]...)\n", service_name, bubfname);
}
#endif
if (tpcall(service_name, ndrxbuf, 0, &ndrxbuf, &outlen, flags ) < 0) {
char tmp[200] = "";
sprintf(tmp, "tpcall(): %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
if ((result = transform_ndrx_to_py(ndrxbuf)) == NULL) {
goto leave_func;
}
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return result;
}
/* }}} */
#ifndef NDRXWS
/* {{{ ndrx_tpadmcall() */
static PyObject *
ndrx_tpadmcall(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * input_py = NULL;
PyObject * flags_py = NULL;
char bubfname[200] = "";
char* ndrxbuf = NULL;
long flags = 0;
if (PyArg_ParseTuple(args, "O|O", &input_py, &flags_py) < 0) {
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tpadmcall(): Bad flags given");
goto leave_func;
}
}
if ((ndrxbuf = transform_py_to_ndrx(input_py)) == NULL) {
goto leave_func;
}
tptypes(ndrxbuf, bubfname, NULL);
if (strcmp(bubfname, "UBF") != 0) {
PyErr_SetString(PyExc_RuntimeError, "tpadmcall(): Must pass dictionary as the input buffer");
goto leave_func;
}
#ifdef DEBUG
{
char bubfname[200] = "";
tptypes(ndrxbuf, bubfname, NULL);
bprintf(stderr, "calling tpadmcall([%s]...)\n", bubfname);
}
#endif
if (tpadmcall((UBFH*)ndrxbuf, (UBFH**)&ndrxbuf, flags ) < 0) {
char tmp[200] = "";
sprintf(tmp, "tpadmcall(): %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
if ((result = transform_ndrx_to_py(ndrxbuf)) == NULL) {
goto leave_func;
}
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return result;
}
/* }}} */
#endif /* NDRXWS */
/* {{{ ndrx_tpacall() */
static PyObject *
ndrx_tpacall(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * input_py = NULL;
PyObject * flags_py = NULL;
char* service_name;
char* ndrxbuf = NULL;
long flags = 0;
int handle = -1;
if (PyArg_ParseTuple(args, "sO|O", &service_name, &input_py, &flags_py) == 0) {
goto leave_func;
}
if (!service_name || (strlen(service_name) <= 0)) {
PyErr_SetString(PyExc_RuntimeError, "tpcall(): No service name given");
goto leave_func;
}
if (!input_py) {
PyErr_SetString(PyExc_RuntimeError, "tpcall(): No arguments given");
goto leave_func;
}
if (strlen(service_name ) > MAX_SVC_NAME_LEN) {
PyErr_SetString(PyExc_RuntimeError, "tpacall(): Service name length too long");
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tpacall(): Bad flags given");
goto leave_func;
}
}
if ((ndrxbuf = transform_py_to_ndrx(input_py)) == NULL) {
goto leave_func;
}
if ((handle = tpacall(service_name, ndrxbuf, 0, flags)) < 0) {
char tmp[200] = "";
sprintf(tmp, "tpacall(): %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
if (! handle) {
Py_INCREF(Py_None);
result = Py_None;
} else {
result = Py_BuildValue("l", (long)handle);
}
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return result;
}
/* }}} */
/* {{{ ndrx_tpgetrply() */
static PyObject *
ndrx_tpgetrply(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * flags_py = NULL;
char* ndrxbuf = NULL;
int handle = -1;
long outlen = 0;
long flags = 0;
if (PyArg_ParseTuple(args, "i|O", &handle, &flags_py) < 0) {
goto leave_func;
}
/* Buffer type will be changed by tpgetrply() if necessary */
if ((ndrxbuf = tpalloc("UBF", NULL, NDRXBUFSIZE)) == NULL) {
bprintf(stderr, "tpalloc(): %d - %s\n", tperrno, tpstrerror(tperrno));
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tpgetrply(): Bad flags given");
goto leave_func;
}
}
if (Binit((UBFH*)ndrxbuf, NDRXBUFSIZE) < 0) {
bprintf(stderr, "tpgetrply(): Binit(): %s\n", Bstrerror(Berror));
goto leave_func;
}
if (handle == 0) {
flags |= TPGETANY;
}
if (tpgetrply(&handle, &ndrxbuf, &outlen, flags) < 0) {
char tmp[200] = "";
sprintf(tmp, "tpgetrply(): %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
if ((result = transform_ndrx_to_py(ndrxbuf)) == NULL) {
goto leave_func;
}
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return result;
}
/* }}} */
#ifndef NDRXWS
/* {{{ ndrx_tpforward() */
static PyObject *
ndrx_tpforward(PyObject * self, PyObject * args)
{
#ifdef DEBUG
printf("call ndrx_tpforward()\n");
PyObject_Print(args, stdout, 0);
#endif
if (PyArg_ParseTuple(args, "sO", &_forward_service, &_forward_pydata) < 0) {
return NULL;
}
#ifdef DEBUG
printf("forward to %s", _forward_service);
#endif /* DEBUG */
Py_INCREF(_forward_pydata);
_forward++;
Py_INCREF(Py_None);
return Py_None;
}
/* }}} */
#endif /* NDRXWS */
/* {{{ ndrx_tpconnect() */
static PyObject *
ndrx_tpconnect(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * input = NULL;
PyObject * service = NULL;
PyObject * flags_py = NULL;
char* service_name;
char* ndrxbuf = NULL;
int handle = -1;
long flags = 0;
if (PyArg_ParseTuple(args, "OO|O", &service, &input, &flags_py) < 0) {
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tpconnect(): Bad flags given");
goto leave_func;
}
}
if (!service || ((service_name = PyBytes_AsString(service)) == NULL)) {
PyErr_SetString(PyExc_RuntimeError, "tpconnect(): No service name and/or arguments given");
goto leave_func;
}
if (strlen(service_name) > MAX_SVC_NAME_LEN) {
PyErr_SetString(PyExc_RuntimeError, "tpconnect(): Service name length too long");
goto leave_func;
}
if ((ndrxbuf = transform_py_to_ndrx(input)) == NULL) {
goto leave_func;
}
/* int tpconnect(char *svc, char *data, long len, long flags) */
if ((handle = tpconnect(service_name, ndrxbuf, 0, flags)) < 0) {
char tmp[200] = "";
sprintf(tmp, "tpconnect(): %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
result = Py_BuildValue("l", (long)handle);
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return result;
}
/* }}} */
/* {{{ ndrx_tpdiscon() */
static PyObject *
ndrx_tpdiscon(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * handle_py = NULL;
long handle = -1;
if (PyArg_ParseTuple(args, "O", &handle_py) < 0) {
goto leave_func;
}
if (!handle_py || ((handle = PyLong_AsLong(handle_py)) < 0)) {
PyErr_SetString(PyExc_RuntimeError, "tpdiscon(): No handle given");
goto leave_func;
}
/* int tpdiscon(int cd) */
if ((handle = tpdiscon(handle)) < 0) {
char tmp[200] = "";
sprintf(tmp, "tpdiscon(%lu): %d - %s", handle, tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
result = Py_BuildValue("l", (long)handle);
leave_func:
return result;
}
/* }}} */
/* {{{ ndrx_tpsend() */
static PyObject *
ndrx_tpsend(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * input = NULL;
PyObject * handle_py = NULL;
PyObject * flags_py = NULL;
char* ndrxbuf = NULL;
long revent = 0;
int handle = -1;
long flags = 0;
int ret = -1;
if (PyArg_ParseTuple(args, "OO|O", &handle_py, &input, &flags_py) < 0) {
goto leave_func;
}
if (!handle_py || ((handle = PyLong_AsLong(handle_py)) < 0)) {
PyErr_SetString(PyExc_RuntimeError, "tpsend(): No handle given");
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tpsend(): Bad flags given");
goto leave_func;
}
}
if ((ndrxbuf = transform_py_to_ndrx(input)) == NULL) {
goto leave_func;
}
/* int tpsend(int cd, char *data, long len, long flags, long *revent) */
if ((ret = tpsend(handle, ndrxbuf, 0, flags, &revent)) < 0) {
if (tperrno != TPEEVENT) {
char tmp[200] = "";
sprintf(tmp, "tpsend(): %d - %s, revent = %lu", tperrno, tpstrerror(tperrno), revent);
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
}
result = Py_BuildValue("l", (long)revent);
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return result;
}
/* }}} */
/* {{{ ndrx_tprecv() */
static PyObject *
ndrx_tprecv(PyObject * self, PyObject * args)
{
PyObject * result = NULL;
PyObject * res_tuple = NULL;
PyObject * handle_py = NULL;
PyObject * flags_py = NULL;
char* ndrxbuf = NULL;
int handle = -1;
long flags = 0;
long len = 0;
long revent = 0;
int ret = 0;
if (PyArg_ParseTuple(args, "O|O", &handle_py, &flags_py) < 0) {
goto leave_func;
}
if (!handle_py || ((handle = PyLong_AsLong(handle_py)) < 0)) {
PyErr_SetString(PyExc_RuntimeError, "tprecv(): No handle given");
goto leave_func;
}
if (flags_py) {
if ((flags = PyLong_AsLong(flags_py)) < 0) {
PyErr_SetString(PyExc_RuntimeError, "tprecv(): Bad flags given");
goto leave_func;
}
}
/* Buffer type will be changed by tprecv() if necessary */
if ((ndrxbuf = tpalloc("UBF", NULL, NDRXBUFSIZE)) == NULL) {
bprintf(stderr, "tpalloc(): %d - %s\n", tperrno, tpstrerror(tperrno));
goto leave_func;
}
if (Binit((UBFH*)ndrxbuf, NDRXBUFSIZE) < 0) {
bprintf(stderr, "Binit(): %s\n", Bstrerror(Berror));
goto leave_func;
}
/* int tprecv(int cd, char **data, long *len, long flags, long *revent) */
if ((ret = tprecv(handle, &ndrxbuf, &len, flags, &revent)) < 0) {
char tmp[200] = "";
if (tperrno != TPEEVENT) {
sprintf(tmp, "tprecv(): %d - %s", tperrno, tpstrerror(tperrno));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
}
/* If an event exists for the descriptor, cd, then tprecv() will return setting tperrno to TPEEVENT. The
event type is returned in revent. Data can be received along with the TPEV_SVCSUCC,
TPEV_SVCFAIL, and TPEV_SENDONLY events. Valid events for tprecv() are as follows. */
if ((revent & ( TPEV_SVCSUCC | TPEV_SVCFAIL | TPEV_SENDONLY))) {
if ((len > 0) && (result = transform_ndrx_to_py(ndrxbuf)) == NULL) {
goto leave_func;
}
}
res_tuple = PyTuple_New(2);
PyTuple_SetItem(res_tuple, 0, PyLong_FromLong(revent));
if (result)
PyTuple_SetItem(res_tuple, 1, result);
else
PyTuple_SetItem(res_tuple, 1, PyLong_FromLong(revent));
leave_func:
if (ndrxbuf) tpfree(ndrxbuf);
return res_tuple;
}
/* }}} */
#ifndef NDRXWS
/* {{{ ndrx_tpopen() */
static PyObject* ndrx_tpopen(PyObject* self, PyObject* arg) {
PyObject * result = NULL;
int ret = -1;
ret = tpopen();
if (ret == -1) {
char tmp[200] = "";