]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/locationmodule.c
downloader: Check DNS for most recent version
[people/ms/libloc.git] / src / python / locationmodule.c
CommitLineData
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
f4fef543
MT
20#include <loc/resolv.h>
21
62ffafdd 22#include "locationmodule.h"
fadebc89 23#include "as.h"
af208e26 24#include "country.h"
9cdf6c53 25#include "database.h"
39967361 26#include "network.h"
d688e569 27#include "writer.h"
9cdf6c53 28
51173f45
MT
29/* Declare global context */
30struct loc_ctx* loc_ctx;
31
18e2b14c
MT
32PyMODINIT_FUNC PyInit_location(void);
33
62ffafdd
MT
34static void location_free(void) {
35 // Release context
36 if (loc_ctx)
37 loc_unref(loc_ctx);
38}
39
c941dd31
MT
40static PyObject* set_log_level(PyObject* m, PyObject* args) {
41 int priority = LOG_INFO;
42
43 if (!PyArg_ParseTuple(args, "i", &priority))
44 return NULL;
45
46 loc_set_log_priority(loc_ctx, priority);
47
48 Py_RETURN_NONE;
49}
50
f4fef543
MT
51static PyObject* discover_latest_version(PyObject* m, PyObject* args) {
52 const char* domain = NULL;
53
54 if (!PyArg_ParseTuple(args, "|s", &domain))
55 return NULL;
56
57 time_t t = 0;
58
59 int r = loc_discover_latest_version(loc_ctx, domain, &t);
60 if (r) {
61 PyErr_SetFromErrno(PyExc_OSError);
62 return NULL;
63 }
64
65 return PyLong_FromUnsignedLong(t);
66}
67
18e2b14c 68static PyMethodDef location_module_methods[] = {
f4fef543
MT
69 {
70 "discover_latest_version",
71 (PyCFunction)discover_latest_version,
72 METH_VARARGS,
73 NULL,
74 },
c941dd31
MT
75 {
76 "set_log_level",
77 (PyCFunction)set_log_level,
78 METH_VARARGS,
79 NULL,
80 },
18e2b14c
MT
81 { NULL },
82};
83
84static struct PyModuleDef location_module = {
85 .m_base = PyModuleDef_HEAD_INIT,
86 .m_name = "location",
87 .m_size = -1,
88 .m_doc = "Python module for libloc",
89 .m_methods = location_module_methods,
62ffafdd 90 .m_free = (freefunc)location_free,
18e2b14c
MT
91};
92
93PyMODINIT_FUNC PyInit_location(void) {
62ffafdd
MT
94 // Initialise loc context
95 int r = loc_new(&loc_ctx);
96 if (r)
97 return NULL;
98
18e2b14c
MT
99 PyObject* m = PyModule_Create(&location_module);
100 if (!m)
101 return NULL;
102
fadebc89
MT
103 // AS
104 if (PyType_Ready(&ASType) < 0)
105 return NULL;
106
107 Py_INCREF(&ASType);
108 PyModule_AddObject(m, "AS", (PyObject *)&ASType);
109
af208e26
MT
110 // Country
111 if (PyType_Ready(&CountryType) < 0)
112 return NULL;
113
114 Py_INCREF(&CountryType);
115 PyModule_AddObject(m, "Country", (PyObject *)&CountryType);
116
9cdf6c53
MT
117 // Database
118 if (PyType_Ready(&DatabaseType) < 0)
119 return NULL;
120
121 Py_INCREF(&DatabaseType);
122 PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType);
123
afb426df
MT
124 // Database Enumerator
125 if (PyType_Ready(&DatabaseEnumeratorType) < 0)
126 return NULL;
127
128 Py_INCREF(&DatabaseEnumeratorType);
129 //PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
130
39967361
MT
131 // Network
132 if (PyType_Ready(&NetworkType) < 0)
133 return NULL;
134
135 Py_INCREF(&NetworkType);
136 PyModule_AddObject(m, "Network", (PyObject *)&NetworkType);
137
d688e569
MT
138 // Writer
139 if (PyType_Ready(&WriterType) < 0)
140 return NULL;
141
142 Py_INCREF(&WriterType);
143 PyModule_AddObject(m, "Writer", (PyObject *)&WriterType);
144
ddb184be 145 // Add constants
0a56a83e
MT
146 if (PyModule_AddStringConstant(m, "__version__", VERSION))
147 return NULL;
ddb184be 148
24ca7992 149 // Add flags
c1467f55 150 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY))
24ca7992
MT
151 return NULL;
152
c1467f55 153 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER))
24ca7992
MT
154 return NULL;
155
c1467f55 156 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST))
24ca7992
MT
157 return NULL;
158
18e2b14c
MT
159 return m;
160}