Class: Dnsruby::RR::LOC

Inherits:
Dnsruby::RR show all
Defined in:
lib/dnsruby/resource/LOC.rb

Overview

Class for DNS Location (LOC) resource records. See RFC 1876 for details.

Constant Summary collapse

ClassValue =

:nodoc: all

nil
TypeValue =

:nodoc: all

Types::LOC
POWEROFTEN =

Powers of 10 from 0 to 9 (used to speed up calculations).

[1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000]
REFERENCE_ALT =

Reference altitude in centimeters (see RFC 1876).

100_000 * 100
REFERENCE_LATLON =

Reference lat/lon (see RFC 1876).

2**31
CONV_SEC =

Conversions to/from thousandths of a degree.

1000
CONV_MIN =
60 * CONV_SEC
CONV_DEG =
60 * CONV_MIN
DEFAULT_MIN =

Defaults (from RFC 1876, Section 3).

0
DEFAULT_SEC =
0
DEFAULT_SIZE =
1
DEFAULT_HORIZ_PRE =
10_000
DEFAULT_VERT_PRE =
10

Constants inherited from Dnsruby::RR

ClassInsensitiveTypes

Instance Attribute Summary collapse

Attributes inherited from Dnsruby::RR

#klass, #name, #rdata, #ttl, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dnsruby::RR

#<=>, #==, #clone, create, #eql?, find_class, get_class, get_num, #hash, implemented_rrs, #init_defaults, new_from_data, new_from_hash, new_from_string, #rdlength, #sameRRset, #to_s

Instance Attribute Details

#altitudeObject

The altitude of the center of the sphere described by the size method, in centimeters, from a base of 100,000m below the WGS 84 reference spheroid used by GPS.



48
49
50
# File 'lib/dnsruby/resource/LOC.rb', line 48

def altitude
  @altitude
end

#horiz_preObject

The horizontal precision of the data, in centimeters.



33
34
35
# File 'lib/dnsruby/resource/LOC.rb', line 33

def horiz_pre
  @horiz_pre
end

#latitudeObject

The latitude of the center of the sphere described by the size method, in thousandths of a second of arc. 2**31 represents the equator; numbers above that are north latitude.



39
40
41
# File 'lib/dnsruby/resource/LOC.rb', line 39

def latitude
  @latitude
end

#longitudeObject

The longitude of the center of the sphere described by the size method, in thousandths of a second of arc. 2**31 represents the prime meridian; numbers above that are east longitude.



44
45
46
# File 'lib/dnsruby/resource/LOC.rb', line 44

def longitude
  @longitude
end

#sizeObject

The diameter of a sphere enclosing the described entity, in centimeters.



31
32
33
# File 'lib/dnsruby/resource/LOC.rb', line 31

def size
  @size
end

#versionObject

The version number of the representation; programs should always check this. Dnsruby currently supports only version 0.



26
27
28
# File 'lib/dnsruby/resource/LOC.rb', line 26

def version
  @version
end

#vert_preObject

The vertical precision of the data, in centimeters.



35
36
37
# File 'lib/dnsruby/resource/LOC.rb', line 35

def vert_pre
  @vert_pre
end

Class Method Details

.decode_rdata(msg) ⇒ Object

:nodoc: all



227
228
229
230
231
232
233
234
235
236
# File 'lib/dnsruby/resource/LOC.rb', line 227

def self.decode_rdata(msg) #:nodoc: all
  version, = msg.get_unpack("C")
  if (version == 0)
    size, horiz_pre, vert_pre, latitude, longitude, altitude = msg.get_unpack('CCCNNN')
    size = precsize_ntoval(size)
    horiz_pre = precsize_ntoval(horiz_pre)
    vert_pre = precsize_ntoval(vert_pre)
    return self.new([version, size, horiz_pre, vert_pre, latitude, longitude, altitude])
  end
end

.precsize_ntoval(prec) ⇒ Object



247
248
249
250
251
# File 'lib/dnsruby/resource/LOC.rb', line 247

def self.precsize_ntoval(prec)
  mantissa = ((prec >> 4) & 0x0f) % 10;
  exponent = (prec & 0x0f) % 10;
  return mantissa * POWEROFTEN[exponent];
end

Instance Method Details

#dms2latlon(deg, min, sec, hem) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/dnsruby/resource/LOC.rb', line 89

def dms2latlon(deg, min, sec, hem)
  retval=0

  retval = (deg * CONV_DEG) + (min * CONV_MIN) + (sec * CONV_SEC).round;
  retval = -retval if ((hem != nil) && ((hem == "S") || (hem == "W")));
  retval += REFERENCE_LATLON;
  return retval;
end

#encode_rdata(msg, canonical = false) ⇒ Object

:nodoc: all



238
239
240
241
242
243
244
245
# File 'lib/dnsruby/resource/LOC.rb', line 238

def encode_rdata(msg, canonical=false) #:nodoc: all
  msg.put_pack('C', @version)
  if (@version == 0)
    msg.put_pack('CCCNNN', precsize_valton(@size),
    precsize_valton(@horiz_pre), precsize_valton(@vert_pre),
    @latitude, @longitude, @altitude)
  end
end

#from_data(data) ⇒ Object

:nodoc: all



123
124
125
# File 'lib/dnsruby/resource/LOC.rb', line 123

def from_data(data) #:nodoc: all
  @version, @size, @horiz_pre, @vert_pre, @latitude, @longitude, @altitude = data
end

#from_hash(hash) ⇒ Object

:nodoc: all



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dnsruby/resource/LOC.rb', line 181

def from_hash(hash) #:nodoc: all
  super(hash)
  if (@size == nil)
    @size = DEFAULT_SIZE * 100
  end
  if @horiz_pre == nil
    @horiz_pre = DEFAULT_HORIZ_PRE * 100
  end
  if @vert_pre == nil
    @vert_pre = DEFAULT_VERT_PRE * 100
  end
end

#from_string(string) ⇒ Object

:nodoc: all



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
# File 'lib/dnsruby/resource/LOC.rb', line 127

def from_string(string) #:nodoc: all
  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)  #

    size = DEFAULT_SIZE

    #  What to do for other versions?
    version = 0;

    horiz_pre = DEFAULT_HORIZ_PRE
    vert_pre  = DEFAULT_VERT_PRE
    latdeg, latmin, latsec, lathem = $1.to_i, $3.to_i, $5.to_f, $6;
    londeg, lonmin, lonsec, lonhem = $7.to_i, $9.to_i, $11.to_f, $12
    alt = $13.to_i
    if ($15)
      size = $15.to_f
    end
    if ($17)
    horiz_pre = $17.to_f
    end
    if ($19)
      vert_pre = $19.to_f
    end

    latmin    = DEFAULT_MIN       unless latmin;
    latsec    = DEFAULT_SEC       unless latsec;
    lathem    = lathem.upcase;

    lonmin    = DEFAULT_MIN       unless lonmin;
    lonsec    = DEFAULT_SEC       unless lonsec;
    lonhem    = lonhem.upcase

    @version   = version;
    @size      = size * 100;
    @horiz_pre = horiz_pre * 100;
    @vert_pre  = vert_pre * 100;
    @latitude  = dms2latlon(latdeg, latmin, latsec, lathem);
    @longitude = dms2latlon(londeg, lonmin, lonsec, lonhem);
    @altitude  = alt * 100 + REFERENCE_ALT;
  end
end

#latlonObject

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.

lat, lon = rr.latlon
system("xearth", "-pos", "fixed #{lat} #{lon}")


105
106
107
108
109
110
111
112
113
114
# File 'lib/dnsruby/resource/LOC.rb', line 105

def latlon
  retlat, retlon = nil

  if (@version == 0)
    retlat = latlon2deg(@latitude);
    retlon = latlon2deg(@longitude);
  end

  return retlat, retlon
end

#latlon2deg(rawmsec) ⇒ Object



116
117
118
119
120
121
# File 'lib/dnsruby/resource/LOC.rb', line 116

def latlon2deg(rawmsec)
  deg=0;

  deg = (rawmsec - reference_latlon) / CONV_DEG;
  return deg;
end

#latlon2dms(rawmsec, hems) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dnsruby/resource/LOC.rb', line 71

def latlon2dms(rawmsec, hems)
  #  Tried to use modulus here, but Perl dumped core if
  #  the value was >= 2**31.

  abs  = (rawmsec - REFERENCE_LATLON).abs;
  deg  = (abs / CONV_DEG).round;
  abs  -= deg * CONV_DEG;
  min  = (abs / CONV_MIN).round;
  abs -= min * CONV_MIN;
  sec  = (abs / CONV_SEC).round;  # $conv_sec
  abs -= sec * CONV_SEC;
  msec = abs;

  hem = hems[(rawmsec >= REFERENCE_LATLON ? 0 : 1), 1]

  return sprintf("%d %02d %02d.%03d %s", deg, min, sec, msec, hem);
end

#precsize_valton(val) ⇒ Object



253
254
255
256
257
258
259
260
# File 'lib/dnsruby/resource/LOC.rb', line 253

def precsize_valton(val)
  exponent = 0;
  while (val >= 10)
    val /= 10;
    exponent+=1
  end
  return (val.round << 4) | (exponent & 0x0f);
end

#rdata_to_stringObject

:nodoc: all



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
# File 'lib/dnsruby/resource/LOC.rb', line 194

def rdata_to_string #:nodoc: all
  rdatastr=""

  if (defined?@version)
    if (@version == 0)
      lat       = @latitude;
      lon       = @longitude;
      altitude  = @altitude;
      size      = @size;
      horiz_pre = @horiz_pre;
      vert_pre  = @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 " + @version + " not supported";
    end
  else
    rdatastr = '';
  end

  return rdatastr;
end