| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
import struct |
| 19 |
|
| 20 |
import dns.exception |
| 21 |
import dns.immutable |
| 22 |
import dns.rdata |
| 23 |
import dns.tokenizer |
| 24 |
|
| 25 |
|
| 26 |
def _validate_float_string(what): |
| 27 |
if len(what) == 0: |
| 28 |
raise dns.exception.FormError |
| 29 |
if what[0] == b"-"[0] or what[0] == b"+"[0]: |
| 30 |
what = what[1:] |
| 31 |
if what.isdigit(): |
| 32 |
return |
| 33 |
try: |
| 34 |
(left, right) = what.split(b".") |
| 35 |
except ValueError: |
| 36 |
raise dns.exception.FormError |
| 37 |
if left == b"" and right == b"": |
| 38 |
raise dns.exception.FormError |
| 39 |
if not left == b"" and not left.decode().isdigit(): |
| 40 |
raise dns.exception.FormError |
| 41 |
if not right == b"" and not right.decode().isdigit(): |
| 42 |
raise dns.exception.FormError |
| 43 |
|
| 44 |
|
| 45 |
@dns.immutable.immutable |
| 46 |
class GPOS(dns.rdata.Rdata): |
| 47 |
"""GPOS record""" |
| 48 |
|
| 49 |
# see: RFC 1712 |
| 50 |
|
| 51 |
__slots__ = ["latitude", "longitude", "altitude"] |
| 52 |
|
| 53 |
def __init__(self, rdclass, rdtype, latitude, longitude, altitude): |
| 54 |
super().__init__(rdclass, rdtype) |
| 55 |
if isinstance(latitude, float) or isinstance(latitude, int): |
| 56 |
latitude = str(latitude) |
| 57 |
if isinstance(longitude, float) or isinstance(longitude, int): |
| 58 |
longitude = str(longitude) |
| 59 |
if isinstance(altitude, float) or isinstance(altitude, int): |
| 60 |
altitude = str(altitude) |
| 61 |
latitude = self._as_bytes(latitude, True, 255) |
| 62 |
longitude = self._as_bytes(longitude, True, 255) |
| 63 |
altitude = self._as_bytes(altitude, True, 255) |
| 64 |
_validate_float_string(latitude) |
| 65 |
_validate_float_string(longitude) |
| 66 |
_validate_float_string(altitude) |
| 67 |
self.latitude = latitude |
| 68 |
self.longitude = longitude |
| 69 |
self.altitude = altitude |
| 70 |
flat = self.float_latitude |
| 71 |
if flat < -90.0 or flat > 90.0: |
| 72 |
raise dns.exception.FormError("bad latitude") |
| 73 |
flong = self.float_longitude |
| 74 |
if flong < -180.0 or flong > 180.0: |
| 75 |
raise dns.exception.FormError("bad longitude") |
| 76 |
|
| 77 |
def to_text(self, origin=None, relativize=True, **kw): |
| 78 |
return ( |
| 79 |
f"{self.latitude.decode()} {self.longitude.decode()} " |
| 80 |
f"{self.altitude.decode()}" |
| 81 |
) |
| 82 |
|
| 83 |
@classmethod |
| 84 |
def from_text( |
| 85 |
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None |
| 86 |
): |
| 87 |
latitude = tok.get_string() |
| 88 |
longitude = tok.get_string() |
| 89 |
altitude = tok.get_string() |
| 90 |
return cls(rdclass, rdtype, latitude, longitude, altitude) |
| 91 |
|
| 92 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 93 |
l = len(self.latitude) |
| 94 |
assert l < 256 |
| 95 |
file.write(struct.pack("!B", l)) |
| 96 |
file.write(self.latitude) |
| 97 |
l = len(self.longitude) |
| 98 |
assert l < 256 |
| 99 |
file.write(struct.pack("!B", l)) |
| 100 |
file.write(self.longitude) |
| 101 |
l = len(self.altitude) |
| 102 |
assert l < 256 |
| 103 |
file.write(struct.pack("!B", l)) |
| 104 |
file.write(self.altitude) |
| 105 |
|
| 106 |
@classmethod |
| 107 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 108 |
latitude = parser.get_counted_bytes() |
| 109 |
longitude = parser.get_counted_bytes() |
| 110 |
altitude = parser.get_counted_bytes() |
| 111 |
return cls(rdclass, rdtype, latitude, longitude, altitude) |
| 112 |
|
| 113 |
@property |
| 114 |
def float_latitude(self): |
| 115 |
"latitude as a floating point value" |
| 116 |
return float(self.latitude) |
| 117 |
|
| 118 |
@property |
| 119 |
def float_longitude(self): |
| 120 |
"longitude as a floating point value" |
| 121 |
return float(self.longitude) |
| 122 |
|
| 123 |
@property |
| 124 |
def float_altitude(self): |
| 125 |
"altitude as a floating point value" |
| 126 |
return float(self.altitude) |