[Git][NTPsec/ntpsec][master] 5 commits: Break a coincidental cohesion.

Eric S. Raymond gitlab at mg.gitlab.com
Thu Oct 20 12:32:06 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
a9f3b4f8 by Eric S. Raymond at 2016-10-20T08:31:19-04:00
Break a coincidental cohesion.

Initialization for local stuff should not be the same as initialization
for networking. This will enable a later change with visible benefits.

- - - - -
3b85fbdf by Eric S. Raymond at 2016-10-20T08:31:19-04:00
In which we discover that init_lib() is pointless,,,

Because we've sepaerayed out init_network(), and init_systime() turns
out to hav been pointless.

- - - - -
b7c20403 by Eric S. Raymond at 2016-10-20T08:31:19-04:00
Remove unneeded code.

- - - - -
7fdffd0f by Eric S. Raymond at 2016-10-20T08:31:19-04:00
Remove an incorrect byte swap.

- - - - -
40950b91 by Eric S. Raymond at 2016-10-20T08:31:19-04:00
Create a C extension to make libntp functions available to Python.

Initially only has one entry point, statustoa(), tested in pyntpq.

- - - - -


22 changed files:

- .gitignore
- include/ntp_fp.h
- include/ntp_stdlib.h
- + libntp/initnetwork.c
- libntp/lib_strbuf.c
- + libntp/pymodule.c
- + libntp/python_compatibility.h
- libntp/ssl_init.c
- libntp/systime.c
- libntp/wscript
- ntpd/ntpd.c
- ntpdig/main.c
- ntpfrob/main.c
- ntpkeygen/ntpkeygen.c
- ntpq/ntpq.c
- ntpq/pyntpq
- pylib/README
- pylib/packet.py
- tests/common/tests_main.c
- tests/libntp/refidsmear.c
- tests/libntp/timespecops.c
- util/sht.c


Changes:

=====================================
.gitignore
=====================================
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 pylib/ntp_control.py
 pylib/ntp_magic.py
+pylib/libntpc.so
 .lock-waf*
 .waf*
 *.pyc


=====================================
include/ntp_fp.h
=====================================
--- a/include/ntp_fp.h
+++ b/include/ntp_fp.h
@@ -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 *);


=====================================
include/ntp_stdlib.h
=====================================
--- a/include/ntp_stdlib.h
+++ b/include/ntp_stdlib.h
@@ -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);


=====================================
libntp/initnetwork.c
=====================================
--- /dev/null
+++ b/libntp/initnetwork.c
@@ -0,0 +1,22 @@
+/*
+ * 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();
+}


=====================================
libntp/lib_strbuf.c
=====================================
--- a/libntp/lib_strbuf.c
+++ b/libntp/lib_strbuf.c
@@ -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;
-}


=====================================
libntp/pymodule.c
=====================================
--- /dev/null
+++ b/libntp/pymodule.c
@@ -0,0 +1,75 @@
+/*
+ * 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);
+}


=====================================
libntp/python_compatibility.h
=====================================
--- /dev/null
+++ b/libntp/python_compatibility.h
@@ -0,0 +1,44 @@
+/*
+ * 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_ */


=====================================
libntp/ssl_init.c
=====================================
--- a/libntp/ssl_init.c
+++ b/libntp/ssl_init.c
@@ -24,8 +24,6 @@ bool ssl_init_done;
 void
 ssl_init(void)
 {
-	init_lib();
-
 	if (ssl_init_done)
 		return;
 


=====================================
libntp/systime.c
=====================================
--- a/libntp/systime.c
+++ b/libntp/systime.c
@@ -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
@@ -93,13 +86,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


=====================================
libntp/wscript
=====================================
--- a/libntp/wscript
+++ b/libntp/wscript
@@ -1,3 +1,5 @@
+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],
 	)


=====================================
ntpd/ntpd.c
=====================================
--- a/ntpd/ntpd.c
+++ b/ntpd/ntpd.c
@@ -608,7 +608,7 @@ ntpdmain(
 	}
 # endif	/* HAVE_WORKING_FORK */
 
-	init_lib();
+	init_network();
 	/*
 	 * Detach us from the terminal.  May need an #ifndef GIZMO.
 	 */


=====================================
ntpdig/main.c
=====================================
--- a/ntpdig/main.c
+++ b/ntpdig/main.c
@@ -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 "
 		      : "",


=====================================
ntpfrob/main.c
=====================================
--- a/ntpfrob/main.c
+++ b/ntpfrob/main.c
@@ -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':


=====================================
ntpkeygen/ntpkeygen.c
=====================================
--- a/ntpkeygen/ntpkeygen.c
+++ b/ntpkeygen/ntpkeygen.c
@@ -89,8 +89,6 @@ main(
 
 	progname = argv[0];
 
-	init_lib();
-
 #ifdef HAVE_OPENSSL
 	ssl_check_version();
 #endif	/* HAVE_OPENSSL */


=====================================
ntpq/ntpq.c
=====================================
--- a/ntpq/ntpq.c
+++ b/ntpq/ntpq.c
@@ -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)


=====================================
ntpq/pyntpq
=====================================
--- a/ntpq/pyntpq
+++ b/ntpq/pyntpq
@@ -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:


=====================================
pylib/README
=====================================
--- a/pylib/README
+++ b/pylib/README
@@ -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.


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -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.


=====================================
tests/common/tests_main.c
=====================================
--- a/tests/common/tests_main.c
+++ b/tests/common/tests_main.c
@@ -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;


=====================================
tests/libntp/refidsmear.c
=====================================
--- a/tests/libntp/refidsmear.c
+++ b/tests/libntp/refidsmear.c
@@ -11,7 +11,7 @@
 
 TEST_GROUP(refidsmear);
 
-TEST_SETUP(refidsmear) {init_lib();}
+TEST_SETUP(refidsmear) {}
 
 TEST_TEAR_DOWN(refidsmear) {}
 


=====================================
tests/libntp/timespecops.c
=====================================
--- a/tests/libntp/timespecops.c
+++ b/tests/libntp/timespecops.c
@@ -34,7 +34,7 @@ struct lfpfracdata {
 
 TEST_GROUP(timespecops);
 
-TEST_SETUP(timespecops) {init_lib();}
+TEST_SETUP(timespecops) {}
 
 TEST_TEAR_DOWN(timespecops) {}
 


=====================================
util/sht.c
=====================================
--- a/util/sht.c
+++ b/util/sht.c
@@ -73,8 +73,6 @@ main (
 
 	progname = argv[0];
 
-	init_lib();
-
 	if (argc<=1) {
 	  usage:
 		printf ("usage: %s [uu:]{r[c][l]|w|snnn}\n",argv[0]);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/a8badabea162a0d5ed1b9b497c74c89c254dbbf3...40950b91bdd698a301b87b4d368819ff1cf03358
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161020/28ca11ef/attachment.html>


More information about the vc mailing list