Commits (5)
pylib/ntp_control.py
pylib/ntp_magic.py
pylib/libntpc.so
.lock-waf*
.waf*
*.pyc
......
......@@ -363,7 +363,6 @@ extern char * rfc3339date (l_fp *);
extern void mfp_mul (int32_t *, uint32_t *, int32_t, uint32_t, int32_t, uint32_t);
extern void set_sys_fuzz (double);
extern void init_systime (void);
extern void get_ostime (struct timespec *tsp);
extern void normalize_time (struct timespec, long, l_fp *);
extern void get_systime (l_fp *);
......
......@@ -76,7 +76,7 @@ extern uint32_t caldaystart (uint32_t ntptime, const time_t *pivot);
extern const char *clockname (int);
extern int clocktime (int, int, int, int, int, uint32_t, uint32_t *, uint32_t *);
extern void init_auth (void);
extern void init_lib (void);
extern void init_network (void);
extern struct savekey *auth_findkey (keyid_t);
extern void auth_moremem (int);
extern void auth_prealloc_symkeys(int);
......
/*
* lib_strbuf - library string storage
*/
#include <config.h>
#include <isc/net.h>
/*
* Storage declarations
*/
bool ipv4_works;
bool ipv6_works;
/*
* initialization routine. Might be needed if the code is ROMized.
*/
void
init_network(void)
{
ipv4_works = isc_net_probeipv4_bool();
ipv6_works = isc_net_probeipv6_bool();
}
......@@ -16,22 +16,4 @@
int debug;
libbufstr lib_stringbuf[LIB_NUMBUF];
int lib_nextbuf;
bool ipv4_works;
bool ipv6_works;
/*
* initialization routine. Might be needed if the code is ROMized.
*/
void
init_lib(void)
{
static bool lib_inited;
if (lib_inited)
return;
ipv4_works = isc_net_probeipv4_bool();
ipv6_works = isc_net_probeipv6_bool();
init_systime();
lib_inited = true;
}
/*
* This file is Copyright (c) 2010 by the GPSD project
* BSD terms apply: see the file COPYING in the distribution root for details.
*
* Python binding for selected libntp library functions
*/
#include <Python.h>
#include <config.h>
#include "ntp_machine.h"
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_stdlib.h"
#include "ntp_random.h"
#include "ntp_config.h"
#include "ntp_assert.h"
#include "isc/error.h"
#include "isc/formatcheck.h"
#include "ntp_control.h"
#include "python_compatibility.h"
/*
* Client utility functions
*/
static PyObject *
libntpc_statustoa(PyObject *self, PyObject *args)
{
int status1, status2;
char *gs;
UNUSED_ARG(self);
if (!PyArg_ParseTuple(args, "ii", &status1, &status2))
return NULL;
gs = statustoa(status1, status2);
return Py_BuildValue("s", gs);
}
/* List of functions defined in the module */
static PyMethodDef libntpc_methods[] = {
{"statustoa", libntpc_statustoa, METH_VARARGS,
PyDoc_STR("Status string display from peer status word.")},
{NULL, NULL, 0, NULL} /* sentinel */
};
PyDoc_STRVAR(module_doc,
"Python wrapper for selected libntp C library routines.\n\
");
/* banishes a pointless compiler warning */
extern PyMODINIT_FUNC initlibntpc(void);
// cppcheck-suppress unusedFunction
NTPSEC_PY_MODULE_INIT(libntpc)
{
PyObject *m;
/* Create the module and add the functions */
NTPSEC_PY_MODULE_DEF(m, "libntpc", module_doc, libntpc_methods)
/* for statustoa() */
PyModule_AddIntConstant(m, "TYPE_SYS", TYPE_SYS);
PyModule_AddIntConstant(m, "TYPE_PEER", TYPE_PEER);
PyModule_AddIntConstant(m, "TYPE_CLOCK", TYPE_CLOCK);
if (m == NULL)
return NTPSEC_PY_MODULE_ERROR_VAL;
return NTPSEC_PY_MODULE_SUCCESS_VAL(m);
}
/*
* python_compatibility.h -- macros for Python 2/3 compatibility
*
* This file is Copyright (c) 2016 by the NTPsec project
* BSD terms apply: see the file COPYING in the distribution root for details.
*
* Definitions based on examples in "Supporting Python 3 - The Book Site"
* http://python3porting.com/cextensions.html
*/
#ifndef _PYTHON_COMPATIBILITY_H_
#define _PYTHON_COMPATIBILITY_H_
#include <Python.h>
#if PY_MAJOR_VERSION >= 3
#define NTPSEC_PY_MODULE_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define NTPSEC_PY_MODULE_DEF(mod, name, doc, methods) \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
mod = PyModule_Create(&moduledef);
#define NTPSEC_PY_MODULE_ERROR_VAL NULL
#define NTPSEC_PY_MODULE_SUCCESS_VAL(val) val
#define NTPSEC_PY_BYTE_FORMAT "y#"
#else /* !Python 3 */
#define NTPSEC_PY_MODULE_INIT(name) PyMODINIT_FUNC init##name(void)
#define NTPSEC_PY_MODULE_DEF(mod, name, doc, methods) \
mod = Py_InitModule3(name, methods, doc);
#define NTPSEC_PY_MODULE_ERROR_VAL
#define NTPSEC_PY_MODULE_SUCCESS_VAL(val)
#define NTPSEC_PY_BYTE_FORMAT "s#"
#endif /* !Python 3 */
#endif /* _PYTHON_COMPATIBILITY_H_ */
......@@ -24,8 +24,6 @@ bool ssl_init_done;
void
ssl_init(void)
{
init_lib();
if (ssl_init_done)
return;
......
......@@ -73,13 +73,6 @@ time_stepped_callback step_callback;
*/
static bool lamport_violated; /* clock was stepped back */
#ifdef DEBUG
static bool systime_init_done;
# define DONE_SYSTIME_INIT() systime_init_done = true
#else
# define DONE_SYSTIME_INIT() do {} while (false)
#endif
void
set_sys_fuzz(
double fuzz_val
......@@ -92,13 +85,6 @@ set_sys_fuzz(
}
void
init_systime(void)
{
DONE_SYSTIME_INIT();
}
void
get_ostime(
struct timespec * tsp
......@@ -156,8 +142,6 @@ normalize_time(
l_fp lfpfuzz;
l_fp lfpdelta;
DEBUG_REQUIRE(systime_init_done);
/* First check if here was a Lamport violation, that is, two
* successive calls to 'get_ostime()' resulted in negative
* time difference. Use a few milliseconds of permissible
......
import sys
def build(ctx):
srcnode = ctx.srcnode.abspath()
......@@ -16,6 +18,7 @@ def build(ctx):
"getopt.c",
"hextolfp.c",
"humandate.c",
"initnetwork.c",
"lib_strbuf.c",
"machines.c",
"modetoa.c",
......@@ -37,7 +40,6 @@ def build(ctx):
"socktoa.c",
"socktohost.c",
"ssl_init.c",
"statestr.c",
"strl_obsd.c",
"syssignal.c",
"timetoa.c",
......@@ -46,18 +48,36 @@ def build(ctx):
"ymd2yd.c",
]
libntp_source_sharable = [
"lib_strbuf.c",
"statestr.c",
]
libntp_source_systime = [
"systime.c"
]
includes = [
"%s/libisc/include/" % srcnode,
"%s/libisc/unix/include/" % srcnode, # XXX: platform: requires unix/win32 switch.
"%s/libsodium/include/" % srcnode,
]
# C library
ctx(
target = "ntp",
features = "c cstlib bld_include src_include",
use = "sodium",
source = libntp_source + libntp_source_systime,
includes = [
"%s/libisc/include/" % srcnode,
"%s/libisc/unix/include/" % srcnode, # XXX: platform: requires unix/win32 switch.
"%s/libsodium/include/" % srcnode
],
source = libntp_source + libntp_source_sharable + libntp_source_systime,
includes = includes,
)
pyversion = ".".join([str(x) for x in sys.version_info[:2]])
# Loadable Python extension
ctx(
target = "ntpc",
features = "c cshlib bld_include src_include",
source = ["pymodule.c"] + libntp_source_sharable,
includes = includes + ["/usr/include/python" + pyversion],
)
......@@ -608,7 +608,7 @@ ntpdmain(
}
# endif /* HAVE_WORKING_FORK */
init_lib();
init_network();
/*
* Detach us from the terminal. May need an #ifndef GIZMO.
*/
......
......@@ -214,8 +214,8 @@ ntpdig_main (
if (!libevent_version_ok())
exit(2);
init_lib();
init_auth();
init_network();
while ((op = ntp_getopt_long(argc, argv,
ALL_OPTIONS, longoptions, NULL)) != -1) {
......@@ -310,7 +310,7 @@ ntpdig_main (
argc -= optct;
argv += optct;
TRACE(2, ("init_lib() done, %s%s\n",
TRACE(2, ("init_network() done, %s%s\n",
(ipv4_works)
? "ipv4_works "
: "",
......
......@@ -10,14 +10,11 @@
#include "config.h"
#include "ntpfrob.h"
void init_lib(void);
int
main(int argc, char **argv)
{
int ch;
iomode mode = plain_text;
init_lib();
while ((ch = getopt(argc, argv, "a:Ab:cejp:r")) != EOF) {
switch (ch) {
case 'A':
......
......@@ -89,8 +89,6 @@ main(
progname = argv[0];
init_lib();
#ifdef HAVE_OPENSSL
ssl_check_version();
#endif /* HAVE_OPENSSL */
......
......@@ -473,8 +473,8 @@ ntpqmain(
delay_time.l_ui = 0;
delay_time.l_uf = DEFDELAY;
init_lib(); /* sets up ipv4_works, ipv6_works */
init_auth();
init_network();
/* Check to see if we have IPv6. Otherwise default to IPv4 */
if (!ipv6_works)
......
......@@ -15,6 +15,7 @@ import os, sys, getopt, cmd, errno, curses, curses.ascii
import socket, select, struct, shlex
from ntp.packet import *
from ntp.libntpc import *
# This import only works on Unixes. The intention is to enable
# Ctrl-P, Ctrl-N, and friends in Cmd.
......@@ -181,10 +182,6 @@ tstflagnames = (
"peer_unreach" # BOGON13
)
def statustoa(_is_sys, n):
# FIXME: see libntp/statestr.c
return n
def prettydate(d):
# FIXME: we need to unpack this date.
return d[2:]
......@@ -453,10 +450,14 @@ usage: timeout [ msec ]
print(e.message)
return
if decodestatus:
if associd == 0:
statype = TYPE_SYS
else:
statype = TYPE_PEER
sys.stdout.write("associd=%u status=%04x %s,\n" %
(associd,
self.session.rstatus,
statustoa(associd == 0, self.session.rstatus)))
statustoa(statype, self.session.rstatus)))
for (name, legend, fmt) in variables:
value = msg[name]
if fmt in (NTP_STR, NTP_UINT, NTP_INT, NTP_ADD, NTP_ADP):
......@@ -1275,8 +1276,6 @@ USAGE: ntpq [-46dphinOV] [-c str] [-D lvl] [ host ...]
if __name__ == '__main__':
#delay_time.l_ui = 0
#delay_time.l_uf = DEFDELAY
#init_lib() # sets up ipv4_works, ipv6_works
#init_auth()
try:
......
......@@ -4,3 +4,7 @@ This directory contains helper classes for NTP utilities written in
Python. It should be installed under the name 'ntp'. It is locally
symlinked under that name at other places in the tree so that various
utilities can be run before the package has been installed.
Do not delete the magic symlink to the build directory! That makes
testing the libntpc extension in place possible, rather than requiring
it to have been installed in rootspace.
......@@ -467,7 +467,7 @@ class Mode6Session:
# Record status info out of the last packet.
if not rpkt.more():
seenlastfrag = True
self.rstatus = socket.ntohs(rpkt.status)
self.rstatus = rpkt.status
# If we've seen the last fragment, look for holes in the sequence.
# If there aren't any, we're done.
......
......@@ -76,8 +76,8 @@ static void RunAllTests(void)
int main(int argc, const char * argv[]) {
init_lib();
init_auth();
init_network();
args_argc = argc;
args_argv = argv;
......
......@@ -11,7 +11,7 @@
TEST_GROUP(refidsmear);
TEST_SETUP(refidsmear) {init_lib();}
TEST_SETUP(refidsmear) {}
TEST_TEAR_DOWN(refidsmear) {}
......