/
gettcp.c
126 lines (103 loc) · 3.19 KB
/
gettcp.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
#include <Python.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#define SHELL_NAME "buildtcp.py"
void hexdump (char *desc, void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
if (desc != NULL)
printf ("%s:\n", desc);
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0)
printf (" %s\n", buff);
printf (" %04x ", i);
}
printf (" %02x", pc[i]);
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
while ((i % 16) != 0) {
printf (" ");
i++;
}
printf (" %s\n", buff);
}
void print_dict(PyObject *dict)
{
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
PyObject* k_str_exc_type = PyObject_Repr(key);
PyObject* k_pyStr = PyUnicode_AsEncodedString(k_str_exc_type, "utf-8", "Error ~");
printf("key:%s ---> ", (const char *)PyBytes_AS_STRING(k_pyStr));
PyObject* v_str_exc_type = PyObject_Repr(value);
PyObject* v_pyStr = PyUnicode_AsEncodedString(v_str_exc_type, "utf-8", "Error ~");
printf("value:%s\n", PyBytes_AsString(v_pyStr));
}
}
int main(int argc, char **argv)
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("import os");
PyRun_SimpleString("import time");
PyRun_SimpleString("import io");
PyRun_SimpleString("import socket");
//PyRun_SimpleString("print(os.getcwd())");
PyRun_SimpleString("sys.path.append(os.getcwd())");
PyRun_SimpleString("import buildtcp");
//int ret = PyRun_SimpleFile(fp, "./buildtcp.py");
PyObject *main_module = PyImport_AddModule("__main__");
PyObject *target = PyImport_AddModule("buildtcp");
if(!main_module) {
printf("failed to PyImport_AddModule()\n");
return 0;
}
PyObject *g_dict = PyModule_GetDict(main_module);
PyObject *l_dict = PyDict_New();
if(!l_dict) {
printf("failed to PyDict_New()\n");
return 0;
}
FILE *fp = fopen("./buildtcp.py", "r");
//PyObject *res = PyRun_File(fp, "./buildtcp.py", Py_file_input, g_dict, l_dict);
//PyObject *res = PyRun_File(fp, "./buildtcp.py", Py_file_input, g_dict, g_dict);
//PyObject *res = PyRun_String("buildtcp.buildtcp()", Py_file_input, g_dict, l_dict);
printf("PyDict_Size(g_dict):%d\n", PyDict_Size(g_dict));
print_dict(g_dict);
printf("PyDict_Size(l_dict):%d\n", PyDict_Size(l_dict));
print_dict(l_dict);
//PyObject *key = PyUnicode_FromString("buildtcp");
//PyObject *func = PyDict_GetItem(l_dict, key);
//PyObject* f_str_exc_type = PyObject_Repr(func);
//PyObject* f_pyStr = PyUnicode_AsEncodedString(f_str_exc_type, "utf-8", "Error ~");
//printf("value:%s\n", PyBytes_AsString(f_pyStr));
//res = PyObject_Call(func, PyTuple_New(), NULL);
//res = PyObject_CallObject(func, NULL);
PyObject *res = PyObject_CallMethodObjArgs(target, PyUnicode_FromString("buildtcp"), NULL);
if(!res) {
printf("failed to PyRun_File()\n");
return 0;
}
if(!PyObject_CheckBuffer(res)) {
printf("res is not Py_buffer\n");
return 0;
}
Py_buffer buf;
int ret = PyObject_GetBuffer(res, &buf, PyBUF_SIMPLE);
if(ret == -1) {
printf("failed to PyObject_GetBuffer()\n");
return 0;
}
hexdump("return val", buf.buf, buf.len);
fclose(fp);
Py_Finalize();
return 0;
}