2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017-2021 IPFire Development Team <info@ipfire.org>
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.
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.
20 #include <loc/format.h>
21 #include <loc/resolv.h>
23 #include "locationmodule.h"
30 /* Declare global context */
31 struct loc_ctx
* loc_ctx
;
33 PyMODINIT_FUNC
PyInit__location(void);
35 static void location_free(void) {
41 static PyObject
* set_log_level(PyObject
* m
, PyObject
* args
) {
42 int priority
= LOG_INFO
;
44 if (!PyArg_ParseTuple(args
, "i", &priority
))
47 loc_set_log_priority(loc_ctx
, priority
);
52 static PyObject
* discover_latest_version(PyObject
* m
, PyObject
* args
) {
53 unsigned int version
= LOC_DATABASE_VERSION_LATEST
;
55 if (!PyArg_ParseTuple(args
, "|i", &version
))
60 int r
= loc_discover_latest_version(loc_ctx
, version
, &t
);
64 return PyLong_FromUnsignedLong(t
);
67 static PyObject
* country_code_is_valid(PyObject
* m
, PyObject
* args
) {
68 const char* country_code
= NULL
;
70 if (!PyArg_ParseTuple(args
, "s", &country_code
))
73 if (loc_country_code_is_valid(country_code
))
79 static PyMethodDef location_module_methods
[] = {
81 "country_code_is_valid",
82 (PyCFunction
)country_code_is_valid
,
87 "discover_latest_version",
88 (PyCFunction
)discover_latest_version
,
94 (PyCFunction
)set_log_level
,
101 static struct PyModuleDef location_module
= {
102 .m_base
= PyModuleDef_HEAD_INIT
,
103 .m_name
= "_location",
105 .m_doc
= "Python module for libloc",
106 .m_methods
= location_module_methods
,
107 .m_free
= (freefunc
)location_free
,
110 PyMODINIT_FUNC
PyInit__location(void) {
111 // Initialise loc context
112 int r
= loc_new(&loc_ctx
);
116 PyObject
* m
= PyModule_Create(&location_module
);
121 if (PyType_Ready(&ASType
) < 0)
125 PyModule_AddObject(m
, "AS", (PyObject
*)&ASType
);
128 if (PyType_Ready(&CountryType
) < 0)
131 Py_INCREF(&CountryType
);
132 PyModule_AddObject(m
, "Country", (PyObject
*)&CountryType
);
135 if (PyType_Ready(&DatabaseType
) < 0)
138 Py_INCREF(&DatabaseType
);
139 PyModule_AddObject(m
, "Database", (PyObject
*)&DatabaseType
);
141 // Database Enumerator
142 if (PyType_Ready(&DatabaseEnumeratorType
) < 0)
145 Py_INCREF(&DatabaseEnumeratorType
);
146 //PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
149 if (PyType_Ready(&NetworkType
) < 0)
152 Py_INCREF(&NetworkType
);
153 PyModule_AddObject(m
, "Network", (PyObject
*)&NetworkType
);
156 if (PyType_Ready(&WriterType
) < 0)
159 Py_INCREF(&WriterType
);
160 PyModule_AddObject(m
, "Writer", (PyObject
*)&WriterType
);
163 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY
))
166 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER
))
169 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST
))
172 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_DROP", LOC_NETWORK_FLAG_DROP
))
175 // Add latest database version
176 if (PyModule_AddIntConstant(m
, "DATABASE_VERSION_LATEST", LOC_DATABASE_VERSION_LATEST
))