diff --git a/scapy/contrib/ppi_geotag.py b/scapy/contrib/ppi_geotag.py index bb7f321dd36b1952e266808f55d8e79ff4782671..4235031d1e84cb501b98e7410878f3c8f5e2200c 100644 --- a/scapy/contrib/ppi_geotag.py +++ b/scapy/contrib/ppi_geotag.py @@ -10,11 +10,14 @@ """ PPI-GEOLOCATION tags """ -import struct +import struct, time from scapy.packet import * from scapy.fields import * from scapy.contrib.ppi import PPIGenericFldHdr,addPPIType +# On windows, epoch is 01/02/1970 at 00:00 +EPOCH = time.mktime((1970, 1, 2, 0, 0, 0, 0, 0, 0))-86400 + CURR_GEOTAG_VER = 2 #Major revision of specification PPI_GPS = 30002 @@ -154,10 +157,15 @@ class NSCounter_Field(LEIntField): return "%1.9f"%(y) class UTCTimeField(IntField): - def __init__(self, name, default, epoch=time.gmtime(0), strf="%a, %d %b %Y %H:%M:%S +0000"): + __slots__ = ["epoch", "delta", "strf"] + def __init__(self, name, default, epoch=None, strf="%a, %d %b %Y %H:%M:%S +0000"): IntField.__init__(self, name, default) - self.epoch = epoch - self.delta = time.mktime(epoch) - time.mktime(time.gmtime(0)) + if epoch is None: + mk_epoch = EPOCH + else: + mk_epoch = time.mktime(epoch) + self.epoch = mk_epoch + self.delta = mk_epoch - EPOCH self.strf = strf def i2repr(self, pkt, x): if x is None: @@ -168,11 +176,9 @@ class UTCTimeField(IntField): class LETimeField(UTCTimeField,LEIntField): __slots__ = ["epoch", "delta", "strf"] - def __init__(self, name, default, epoch=time.gmtime(0), strf="%a, %d %b %Y %H:%M:%S +0000"): + def __init__(self, name, default, epoch=None, strf="%a, %d %b %Y %H:%M:%S +0000"): LEIntField.__init__(self, name, default) - self.epoch = epoch - self.delta = time.mktime(epoch) - time.mktime(time.gmtime(0)) - self.strf = strf + UTCTimeField.__init__(self, name, default, epoch=epoch, strf=strf) class SignedByteField(Field): def __init__(self, name, default): diff --git a/scapy/contrib/ppi_geotag.uts b/scapy/contrib/ppi_geotag.uts new file mode 100644 index 0000000000000000000000000000000000000000..96ea30fc6f86f055cfc3f1bba24e5baa23b36807 --- /dev/null +++ b/scapy/contrib/ppi_geotag.uts @@ -0,0 +1,45 @@ +# PPI_Geotag tests + +############ +############ ++ PPI Geotags tests + += Import PPI Geotag + +from scapy.contrib.ppi_geotag import * + += Test GPS dissection + +assert str(GPS()) == '2u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00' + += Test Vector dissection + +assert str(Vector()) == '3u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00' + += Test Sensor dissection + +assert str(Sensor()) == '4u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00' + += Test Antenna dissection + +assert str(Antenna()) == '5u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00' + += Test GPSTime_Field time handling + +assert GPSTime_Field("GPSTime", None).delta == 0.0 + += Test UTCTimeField with time values + +local_time = time.localtime() +utc_time = UTCTimeField("Test", None, epoch=local_time) +assert time.localtime(utc_time.epoch) == time.localtime() +assert time.gmtime(utc_time.delta) == time.localtime() +assert utc_time.i2repr(None, None) == (time.strftime("%a, %d %b %Y %H:%M:%S +0000", local_time) + " (" + str(int(utc_time.delta)) + ")") + += Test LETimeField with time values + +local_time = time.localtime() +lme_time = LETimeField("Test", None, epoch=local_time) +assert time.localtime(lme_time.epoch) == local_time +assert time.gmtime(lme_time.delta) == local_time +assert lme_time.i2repr(None, None) == (time.strftime("%a, %d %b %Y %H:%M:%S +0000", local_time) + " (" + str(int(lme_time.delta)) + ")")