1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
package Net::DNS::RR::LOC;
# $Id: LOC.pm,v 1.5 2000/11/19 06:04:45 mfuhr Exp mfuhr $
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK @poweroften $reference_alt
$reference_latlon $conv_sec $conv_min $conv_deg
$default_min $default_sec $default_size
$default_horiz_pre $default_vert_pre);
require Exporter;
use Net::DNS;
use Net::DNS::Packet;
@ISA = qw(Net::DNS::RR Exporter);
@EXPORT = qw();
@EXPORT_OK = qw();
# Powers of 10 from 0 to 9 (used to speed up calculations).
@poweroften = (1, 10, 100, 1_000, 10_000, 100_000, 1_000_000,
10_000_000, 100_000_000, 1_000_000_000);
# Reference altitude in centimeters (see RFC 1876).
$reference_alt = 100_000 * 100;
# Reference lat/lon (see RFC 1876).
$reference_latlon = 2**31;
# Conversions to/from thousandths of a degree.
$conv_sec = 1000;
$conv_min = 60 * $conv_sec;
$conv_deg = 60 * $conv_min;
# Defaults (from RFC 1876, Section 3).
$default_min = 0;
$default_sec = 0;
$default_size = 1;
$default_horiz_pre = 10_000;
$default_vert_pre = 10;
sub new {
my ($class, $self, $data, $offset) = @_;
if ($self->{"rdlength"} > 0) {
my ($version) = unpack("\@$offset C", $$data);
++$offset;
$self->{"version"} = $version;
if ($version == 0) {
my ($size) = unpack("\@$offset C", $$data);
$size = precsize_ntoval($size);
++$offset;
my ($horiz_pre) = unpack("\@$offset C", $$data);
$horiz_pre = precsize_ntoval($horiz_pre);
++$offset;
my ($vert_pre) = unpack("\@$offset C", $$data);
$vert_pre = precsize_ntoval($vert_pre);
++$offset;
my ($latitude) = unpack("\@$offset N", $$data);
$offset += &Net::DNS::INT32SZ;
my ($longitude) = unpack("\@$offset N", $$data);
$offset += &Net::DNS::INT32SZ;
my ($altitude) = unpack("\@$offset N", $$data);
$offset += &Net::DNS::INT32SZ;
$self->{"size"} = $size;
$self->{"horiz_pre"} = $horiz_pre;
$self->{"vert_pre"} = $vert_pre;
$self->{"latitude"} = $latitude;
$self->{"longitude"} = $longitude;
$self->{"altitude"} = $altitude;
}
else {
# What to do for unsupported versions?
}
}
return bless $self, $class;
}
sub new_from_string {
my ($class, $self, $string) = @_;
if ($string &&
$string =~ /^ (\d+) \s+ # deg lat
((\d+) \s+)? # min lat
(([\d.]+) \s+)? # sec lat
(N|S) \s+ # hem lat
(\d+) \s+ # deg lon
((\d+) \s+)? # min lon
(([\d.]+) \s+)? # sec lon
(E|W) \s+ # hem lon
(-?[\d.]+) m? # altitude
(\s+ ([\d.]+) m?)? # size
(\s+ ([\d.]+) m?)? # horiz precision
(\s+ ([\d.]+) m?)? # vert precision
/ix) {
# What to do for other versions?
my $version = 0;
my ($latdeg, $latmin, $latsec, $lathem) = ($1, $3, $5, $6);
my ($londeg, $lonmin, $lonsec, $lonhem) = ($7, $9, $11, $12);
my ($alt, $size, $horiz_pre, $vert_pre) = ($13, $15, $17, $19);
$latmin = $default_min unless $latmin;
$latsec = $default_sec unless $latsec;
$lathem = uc($lathem);
$lonmin = $default_min unless $lonmin;
$lonsec = $default_sec unless $lonsec;
$lonhem = uc($lonhem);
$size = $default_size unless $size;
$horiz_pre = $default_horiz_pre unless $horiz_pre;
$vert_pre = $default_vert_pre unless $vert_pre;
$self->{"version"} = $version;
$self->{"size"} = $size * 100;
$self->{"horiz_pre"} = $horiz_pre * 100;
$self->{"vert_pre"} = $vert_pre * 100;
$self->{"latitude"} = dms2latlon($latdeg, $latmin, $latsec,
$lathem);
$self->{"longitude"} = dms2latlon($londeg, $lonmin, $lonsec,
$lonhem);
$self->{"altitude"} = $alt * 100 + $reference_alt;
}
return bless $self, $class;
}
sub rdatastr {
my $self = shift;
my $rdatastr;
if (exists $self->{"version"}) {
if ($self->{"version"} == 0) {
my $lat = $self->{"latitude"};
my $lon = $self->{"longitude"};
my $altitude = $self->{"altitude"};
my $size = $self->{"size"};
my $horiz_pre = $self->{"horiz_pre"};
my $vert_pre = $self->{"vert_pre"};
$altitude = ($altitude - $reference_alt) / 100;
$size /= 100;
$horiz_pre /= 100;
$vert_pre /= 100;
$rdatastr = latlon2dms($lat, "NS") . " " .
latlon2dms($lon, "EW") . " " .
sprintf("%.2fm", $altitude) . " " .
sprintf("%.2fm", $size) . " " .
sprintf("%.2fm", $horiz_pre) . " " .
sprintf("%.2fm", $vert_pre);
}
else {
$rdatastr = "; version " . $self->{"version"} .
" not supported";
}
}
else {
$rdatastr = "; no data";
}
return $rdatastr;
}
sub rr_rdata {
my $self = shift;
my $rdata = "";
if (exists $self->{"version"}) {
$rdata .= pack("C", $self->{"version"});
if ($self->{"version"} == 0) {
$rdata .= pack("C3", precsize_valton($self->{"size"}),
precsize_valton($self->{"horiz_pre"}),
precsize_valton($self->{"vert_pre"}));
$rdata .= pack("N3", $self->{"latitude"},
$self->{"longitude"},
$self->{"altitude"});
}
else {
# What to do for other versions?
}
}
return $rdata;
}
sub precsize_ntoval {
my $prec = shift;
my $mantissa = (($prec >> 4) & 0x0f) % 10;
my $exponent = ($prec & 0x0f) % 10;
return $mantissa * $poweroften[$exponent];
}
sub precsize_valton {
my $val = shift;
my $exponent = 0;
while ($val > 10) {
$val /= 10;
++$exponent;
}
return (int($val) << 4) | ($exponent & 0x0f);
}
sub latlon2dms {
my ($rawmsec, $hems) = @_;
# Tried to use modulus here, but Perl dumped core if
# the value was >= 2**31.
my ($abs, $deg, $min, $sec, $msec, $hem);
$abs = abs($rawmsec - $reference_latlon);
$deg = int($abs / $conv_deg);
$abs -= $deg * $conv_deg;
$min = int($abs / $conv_min);
$abs -= $min * $conv_min;
$sec = int($abs / $conv_sec);
$abs -= $sec * $conv_sec;
$msec = $abs;
$hem = substr($hems, ($rawmsec >= $reference_latlon ? 0 : 1), 1);
return sprintf("%d %02d %02d.%03d %s", $deg, $min, $sec, $msec, $hem);
}
sub dms2latlon {
my ($deg, $min, $sec, $hem) = @_;
my ($retval);
$retval = ($deg * $conv_deg) + ($min * $conv_min) + ($sec * $conv_sec);
$retval = -$retval if ($hem eq "S") || ($hem eq "W");
$retval += $reference_latlon;
return $retval;
}
sub latlon {
my $self = shift;
my ($retlat, $retlon);
if ($self->{"version"} == 0) {
$retlat = latlon2deg($self->{"latitude"});
$retlon = latlon2deg($self->{"longitude"});
}
else {
$retlat = $retlon = undef;
}
return ($retlat, $retlon);
}
sub latlon2deg {
my $rawmsec = shift;
my $deg;
$deg = ($rawmsec - $reference_latlon) / $conv_deg;
return $deg;
}
1;
__END__
=head1 NAME
Net::DNS::RR::LOC - DNS LOC resource record
=head1 SYNOPSIS
C<use Net::DNS::RR>;
=head1 DESCRIPTION
Class for DNS Location (LOC) resource records. See RFC 1876 for
details.
=head1 METHODS
=head2 version
print "version = ", $rr->version, "\n";
Returns the version number of the representation; programs should
always check this. C<Net::DNS> currently supports only version 0.
=head2 size
print "size = ", $rr->size, "\n";
Returns the diameter of a sphere enclosing the described entity,
in centimeters.
=head2 horiz_pre
print "horiz_pre = ", $rr->horiz_pre, "\n";
Returns the horizontal precision of the data, in centimeters.
=head2 vert_pre
print "vert_pre = ", $rr->vert_pre, "\n";
Returns the vertical precision of the data, in centimeters.
=head2 latitude
print "latitude = ", $rr->latitude, "\n";
Returns the latitude of the center of the sphere described by
the C<size> method, in thousandths of a second of arc. 2**31
represents the equator; numbers above that are north latitude.
=head2 longitude
print "longitude = ", $rr->longitude, "\n";
Returns the longitude of the center of the sphere described by
the C<size> method, in thousandths of a second of arc. 2**31
represents the prime meridian; numbers above that are east
longitude.
=head2 latlon
($lat, $lon) = $rr->latlon;
system("xearth", "-pos", "fixed $lat $lon");
Returns the latitude and longitude as floating-point degrees.
Positive numbers represent north latitude or east longitude;
negative numbers represent south latitude or west longitude.
=head2 altitude
print "altitude = ", $rr->altitude, "\n";
Returns the altitude of the center of the sphere described by
the C<size> method, in centimeters, from a base of 100,000m
below the WGS 84 reference spheroid used by GPS.
=head1 COPYRIGHT
Copyright (c) 1997-1998 Michael Fuhr. All rights reserved. This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Some of the code and documentation is based on RFC 1876 and on code
contributed by Christopher Davis.
=head1 SEE ALSO
L<perl(1)>, L<Net::DNS>, L<Net::DNS::Resolver>, L<Net::DNS::Packet>,
L<Net::DNS::Header>, L<Net::DNS::Question>, L<Net::DNS::RR>,
RFC 1876
=cut
|