]>
Commit | Line | Data |
---|---|---|
18e2b14c MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 IPFire Development Team <info@ipfire.org> | |
5 | ||
6 | This library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | This library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <Python.h> | |
c941dd31 | 18 | #include <syslog.h> |
18e2b14c | 19 | |
62ffafdd | 20 | #include "locationmodule.h" |
fadebc89 | 21 | #include "as.h" |
af208e26 | 22 | #include "country.h" |
9cdf6c53 | 23 | #include "database.h" |
39967361 | 24 | #include "network.h" |
d688e569 | 25 | #include "writer.h" |
9cdf6c53 | 26 | |
51173f45 MT |
27 | /* Declare global context */ |
28 | struct loc_ctx* loc_ctx; | |
29 | ||
18e2b14c MT |
30 | PyMODINIT_FUNC PyInit_location(void); |
31 | ||
62ffafdd MT |
32 | static void location_free(void) { |
33 | // Release context | |
34 | if (loc_ctx) | |
35 | loc_unref(loc_ctx); | |
36 | } | |
37 | ||
c941dd31 MT |
38 | static PyObject* set_log_level(PyObject* m, PyObject* args) { |
39 | int priority = LOG_INFO; | |
40 | ||
41 | if (!PyArg_ParseTuple(args, "i", &priority)) | |
42 | return NULL; | |
43 | ||
44 | loc_set_log_priority(loc_ctx, priority); | |
45 | ||
46 | Py_RETURN_NONE; | |
47 | } | |
48 | ||
18e2b14c | 49 | static PyMethodDef location_module_methods[] = { |
c941dd31 MT |
50 | { |
51 | "set_log_level", | |
52 | (PyCFunction)set_log_level, | |
53 | METH_VARARGS, | |
54 | NULL, | |
55 | }, | |
18e2b14c MT |
56 | { NULL }, |
57 | }; | |
58 | ||
59 | static struct PyModuleDef location_module = { | |
60 | .m_base = PyModuleDef_HEAD_INIT, | |
61 | .m_name = "location", | |
62 | .m_size = -1, | |
63 | .m_doc = "Python module for libloc", | |
64 | .m_methods = location_module_methods, | |
62ffafdd | 65 | .m_free = (freefunc)location_free, |
18e2b14c MT |
66 | }; |
67 | ||
68 | PyMODINIT_FUNC PyInit_location(void) { | |
62ffafdd MT |
69 | // Initialise loc context |
70 | int r = loc_new(&loc_ctx); | |
71 | if (r) | |
72 | return NULL; | |
73 | ||
18e2b14c MT |
74 | PyObject* m = PyModule_Create(&location_module); |
75 | if (!m) | |
76 | return NULL; | |
77 | ||
fadebc89 MT |
78 | // AS |
79 | if (PyType_Ready(&ASType) < 0) | |
80 | return NULL; | |
81 | ||
82 | Py_INCREF(&ASType); | |
83 | PyModule_AddObject(m, "AS", (PyObject *)&ASType); | |
84 | ||
af208e26 MT |
85 | // Country |
86 | if (PyType_Ready(&CountryType) < 0) | |
87 | return NULL; | |
88 | ||
89 | Py_INCREF(&CountryType); | |
90 | PyModule_AddObject(m, "Country", (PyObject *)&CountryType); | |
91 | ||
9cdf6c53 MT |
92 | // Database |
93 | if (PyType_Ready(&DatabaseType) < 0) | |
94 | return NULL; | |
95 | ||
96 | Py_INCREF(&DatabaseType); | |
97 | PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType); | |
98 | ||
afb426df MT |
99 | // Database Enumerator |
100 | if (PyType_Ready(&DatabaseEnumeratorType) < 0) | |
101 | return NULL; | |
102 | ||
103 | Py_INCREF(&DatabaseEnumeratorType); | |
104 | //PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType); | |
105 | ||
39967361 MT |
106 | // Network |
107 | if (PyType_Ready(&NetworkType) < 0) | |
108 | return NULL; | |
109 | ||
110 | Py_INCREF(&NetworkType); | |
111 | PyModule_AddObject(m, "Network", (PyObject *)&NetworkType); | |
112 | ||
d688e569 MT |
113 | // Writer |
114 | if (PyType_Ready(&WriterType) < 0) | |
115 | return NULL; | |
116 | ||
117 | Py_INCREF(&WriterType); | |
118 | PyModule_AddObject(m, "Writer", (PyObject *)&WriterType); | |
119 | ||
ddb184be | 120 | // Add constants |
0a56a83e MT |
121 | if (PyModule_AddStringConstant(m, "__version__", VERSION)) |
122 | return NULL; | |
ddb184be | 123 | |
24ca7992 | 124 | // Add flags |
c1467f55 | 125 | if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY)) |
24ca7992 MT |
126 | return NULL; |
127 | ||
c1467f55 | 128 | if (PyModule_AddIntConstant(m, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER)) |
24ca7992 MT |
129 | return NULL; |
130 | ||
c1467f55 | 131 | if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST)) |
24ca7992 MT |
132 | return NULL; |
133 | ||
18e2b14c MT |
134 | return m; |
135 | } |