[Git][NTPsec/ntpsec][master] Ensure that Python code cannot fall out of sync with magic ntp.h defines.

Eric S. Raymond gitlab at mg.gitlab.com
Wed Oct 19 11:07:09 UTC 2016


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


Commits:
bdd3eccb by Eric S. Raymond at 2016-10-19T07:03:03-04:00
Ensure that Python code cannot fall out of sync with magic ntp.h defines.

- - - - -


5 changed files:

- .gitignore
- include/ntp.h
- pylib/packet.py
- pylib/wscript
- wafhelpers/pythonize-header


Changes:

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


=====================================
include/ntp.h
=====================================
--- a/include/ntp.h
+++ b/include/ntp.h
@@ -1,5 +1,7 @@
 /*
  * ntp.h - NTP definitions for the masses
+ *
+ * pythonize-header: start ignoring
  */
 #ifndef GUARD_NTP_H
 #define GUARD_NTP_H
@@ -83,6 +85,8 @@ typedef unsigned int	u_int;
  *		year += 1900;
  */
 
+/* pythonize-header: stop ignoring */
+
 /*
  * NTP protocol parameters.  See section 3.2.6 of the specification.
  */
@@ -90,6 +94,8 @@ typedef unsigned int	u_int;
 #define	NTP_OLDVERSION	((uint8_t)1) /* oldest credible version */
 #define	NTP_PORT	123	/* included for non-unix machines */
 
+/* pythonize-header: start ignoring */
+
 /*
  * Poll interval parameters
  */
@@ -384,6 +390,9 @@ struct peer {
 #define	MODE_CLIENT	3	/* client mode */
 #define	MODE_SERVER	4	/* server mode */
 #define	MODE_BROADCAST	5	/* broadcast mode */
+
+/* pythonize-header: stop ignoring */
+
 /*
  * These can appear in packets
  */
@@ -394,6 +403,8 @@ struct peer {
  */
 #define	MODE_BCLIENT	6	/* broadcast client mode */
 
+/* pythonize-header: start ignoring */
+
 /*
  * Values for peer.stratum, sys_stratum
  */
@@ -495,6 +506,8 @@ struct pkt {
 	uint32_t	exten[(MAX_MAC_LEN) / sizeof(uint32_t)];
 };
 
+/* pythonize-header: stop ignoring */
+
 /*
  * Stuff for extracting things from li_vn_mode
  */
@@ -575,6 +588,8 @@ struct pkt {
 #define	CEVNT_BADTIME	6	/* bad time */
 #define CEVNT_MAX	CEVNT_BADTIME
 
+/* pythonize-header: start ignoring */
+
 /*
  * To speed lookups, peers are hashed by the low order bits of the
  * remote IP address. These definitions relate to that.


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -7,59 +7,9 @@
 from __future__ import print_function, division
 import sys, socket, select, struct, curses.ascii, collections
 
+from ntp_magic import *
 from ntp_control import *
 
-# If you don't keep this file in sync with ntp.h, havoc will ensue.
-
-# Things from ntp.h
-
-LEAP_NOWARNING  = 0x0   # leap_none: normal, no leap second warning
-LEAP_ADDSECOND  = 0x1   # leap_add_sec: last minute of day has 61 seconds
-LEAP_DELSECOND  = 0x2   # leap_del_sec: last minute of day has 59 seconds
-LEAP_NOTINSYNC  = 0x3   # leap_alarm: overload, clock is free running
-
-NTP_OLDVERSION  = 1     # C code said "oldest credible version"
-NTP_VERSION     = 4     # Current version
-
-MODE_CONTROL = 6
-
-# Event codes. Used for reporting errors/events to the control module
-PEER_EVENT	= 0x080	# this is a peer event */
-CRPT_EVENT	= 0x100	# this is a crypto event */
-
-# System event codes
-EVNT_UNSPEC	= 0	# unspecified
-EVNT_NSET	= 1	# freq not set
-EVNT_FSET	= 2	# freq set
-EVNT_SPIK	= 3	# spike detect
-EVNT_FREQ	= 4	# freq mode
-EVNT_SYNC	= 5	# clock sync
-EVNT_SYSRESTART	= 6	# restart
-EVNT_SYSFAULT	= 7	# panic stop
-EVNT_NOPEER	= 8	# no sys peer
-EVNT_ARMED	= 9	# leap armed
-EVNT_DISARMED	= 10	# leap disarmed
-EVNT_LEAP	= 11	# leap event
-EVNT_CLOCKRESET	= 12	# clock step
-EVNT_KERN	= 13	# kernel event
-EVNT_TAI	= 14	# TAI
-EVNT_LEAPVAL	= 15	# stale leapsecond values
-
-# Peer event codes
-PEVNT_MOBIL	= (1 | PEER_EVENT) # mobilize
-PEVNT_DEMOBIL	= (2 | PEER_EVENT) # demobilize
-PEVNT_UNREACH	= (3 | PEER_EVENT) # unreachable
-PEVNT_REACH	= (4 | PEER_EVENT) # reachable
-PEVNT_RESTART	= (5 | PEER_EVENT) # restart
-PEVNT_REPLY	= (6 | PEER_EVENT) # no reply
-PEVNT_RATE	= (7 | PEER_EVENT) # rate exceeded
-PEVNT_DENY	= (8 | PEER_EVENT) # access denied
-PEVNT_ARMED	= (9 | PEER_EVENT) # leap armed
-PEVNT_NEWPEER	= (10 | PEER_EVENT) # sys peer
-PEVNT_CLOCK	= (11 | PEER_EVENT) # clock event
-PEVNT_AUTH	= (12 | PEER_EVENT) # bad auth
-PEVNT_POPCORN	= (13 | PEER_EVENT) # popcorn
-
 # From ntpq.h:
 
 # Limit on packets in a single response.  Increasing this value to


=====================================
pylib/wscript
=====================================
--- a/pylib/wscript
+++ b/pylib/wscript
@@ -7,14 +7,23 @@ def configure(conf):
 
 def build(ctx):
     srcnode = ctx.srcnode.make_node('pylib')
-    target = ctx.srcnode.make_node('pylib/ntp_control.py')
+    target1 = ctx.srcnode.make_node('pylib/ntp_control.py')
+    target2 = ctx.srcnode.make_node('pylib/ntp_magic.py')
 
     ctx(
 	before      = ['pyc', 'pyo'],
 	cwd         = srcnode,
 	rule        = '../wafhelpers/pythonize-header ${SRC} >${TGT}',
 	source      = "../include/ntp_control.h",
-	target      = target,
+	target      = target1,
+	)
+
+    ctx(
+	before      = ['pyc', 'pyo'],
+	cwd         = srcnode,
+	rule        = '../wafhelpers/pythonize-header ${SRC} >${TGT}',
+	source      = "../include/ntp.h",
+	target      = target2,
 	)
 
     ctx(features='py', source=ctx.path.ant_glob('*.py'), install_from='.', install_path='${PYTHONDIR}/ntp')


=====================================
wafhelpers/pythonize-header
=====================================
--- a/wafhelpers/pythonize-header
+++ b/wafhelpers/pythonize-header
@@ -38,7 +38,17 @@ def pythonize(rfp, wfp):
     wfp.write("# This file was generated by pythonize-header;\n")
     wfp.write("# DO NOT HAND-HACK IT!\n")
     skipto = False
+    ignoring = False
     for line in rfp:
+        # The ignore logic
+        if "pythonize-header" in line:
+            if "start" in line:
+                ignoring = True
+            elif "stop" in line:
+                ignoring = False
+            continue
+        if ignoring:
+            continue
         # Omit include lines
         skipit = False
         for skipline in skippers:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/bdd3eccb3b341b336c9c011ab2b14a47741f8d5e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161019/d5f4fd6e/attachment.html>


More information about the vc mailing list