2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 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/resolv.h>
22 #include "locationmodule.h"
29 /* Declare global context */
30 struct loc_ctx
* loc_ctx
;
32 PyMODINIT_FUNC
PyInit_location(void);
34 static void location_free(void) {
40 static PyObject
* set_log_level(PyObject
* m
, PyObject
* args
) {
41 int priority
= LOG_INFO
;
43 if (!PyArg_ParseTuple(args
, "i", &priority
))
46 loc_set_log_priority(loc_ctx
, priority
);
51 static PyObject
* discover_latest_version(PyObject
* m
, PyObject
* args
) {
52 const char* domain
= NULL
;
54 if (!PyArg_ParseTuple(args
, "|s", &domain
))
59 int r
= loc_discover_latest_version(loc_ctx
, domain
, &t
);
61 PyErr_SetFromErrno(PyExc_OSError
);
65 return PyLong_FromUnsignedLong(t
);
68 static PyObject
* country_code_is_valid(PyObject
* m
, PyObject
* args
) {
69 const char* country_code
= NULL
;
71 if (!PyArg_ParseTuple(args
, "s", &country_code
))
74 if (loc_country_code_is_valid(country_code
))
80 static PyMethodDef location_module_methods
[] = {
82 "country_code_is_valid",
83 (PyCFunction
)country_code_is_valid
,
88 "discover_latest_version",
89 (PyCFunction
)discover_latest_version
,
95 (PyCFunction
)set_log_level
,
102 static struct PyModuleDef location_module
= {
103 .m_base
= PyModuleDef_HEAD_INIT
,
104 .m_name
= "location",
106 .m_doc
= "Python module for libloc",
107 .m_methods
= location_module_methods
,
108 .m_free
= (freefunc
)location_free
,
111 PyMODINIT_FUNC
PyInit_location(void) {
112 // Initialise loc context
113 int r
= loc_new(&loc_ctx
);
117 PyObject
* m
= PyModule_Create(&location_module
);
122 if (PyType_Ready(&ASType
) < 0)
126 PyModule_AddObject(m
, "AS", (PyObject
*)&ASType
);
129 if (PyType_Ready(&CountryType
) < 0)
132 Py_INCREF(&CountryType
);
133 PyModule_AddObject(m
, "Country", (PyObject
*)&CountryType
);
136 if (PyType_Ready(&DatabaseType
) < 0)
139 Py_INCREF(&DatabaseType
);
140 PyModule_AddObject(m
, "Database", (PyObject
*)&DatabaseType
);
142 // Database Enumerator
143 if (PyType_Ready(&DatabaseEnumeratorType
) < 0)
146 Py_INCREF(&DatabaseEnumeratorType
);
147 //PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
150 if (PyType_Ready(&NetworkType
) < 0)
153 Py_INCREF(&NetworkType
);
154 PyModule_AddObject(m
, "Network", (PyObject
*)&NetworkType
);
157 if (PyType_Ready(&WriterType
) < 0)
160 Py_INCREF(&WriterType
);
161 PyModule_AddObject(m
, "Writer", (PyObject
*)&WriterType
);
164 if (PyModule_AddStringConstant(m
, "__version__", VERSION
))
168 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY
))
171 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER
))
174 if (PyModule_AddIntConstant(m
, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST
))