Class: Diameter::AVP
- Inherits:
-
Object
- Object
- Diameter::AVP
- Includes:
- AVPParser, Constants::AVPType, Internals
- Defined in:
- lib/diameter/avp.rb
Overview
Represents a Diameter AVP. Use this for non-vendor-specific AVPs, and its subclass VendorSpecificAVP for ones defined for a particular vendor.
Constant Summary
Constants included from Constants::AVPType
Constants::AVPType::GROUPED, Constants::AVPType::IPADDR, Constants::AVPType::OCTETSTRING, Constants::AVPType::U32
Instance Attribute Summary collapse
-
#code ⇒ Fixnum
readonly
The AVP Code.
-
#mandatory ⇒ true, false
readonly
Whether this AVP is mandatory (i.e. its M flag is set).
-
#vendor_id ⇒ Object
readonly
Returns the value of attribute vendor_id.
- #vendor_specific? ⇒ Boolean readonly
Data getters/setters for different AVP types collapse
-
#float32 ⇒ Float
Returns this AVP’s byte data, interpreted as a Float32.
-
#float32=(value) ⇒ void
Sets this AVP’s byte data to a Float32.
-
#float64 ⇒ Float
Returns this AVP’s byte data, interpreted as a Float64.
-
#float64=(value) ⇒ void
Sets this AVP’s byte data to a Float64.
-
#grouped_value ⇒ Array<AVP>
Returns this AVP’s byte data, interpreted as a Grouped AVP.
-
#grouped_value=(avps) ⇒ void
Sets this AVP’s byte data to a Grouped AVP.
-
#inner_avp(name) ⇒ AVP?
(also: #[])
For a grouped AVP, returns the first AVP with this name it contains.
-
#inner_avps(name) ⇒ Array<AVP>
For a grouped AVP, returns all AVPs it contains with this name.
-
#int32 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Integer32.
-
#int32=(value) ⇒ void
Sets this AVP’s byte data to an Integer32.
-
#int64 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Integer64.
-
#int64=(value) ⇒ void
Sets this AVP’s byte data to an Integer64.
-
#ip_address ⇒ IPAddr
Returns this AVP’s byte data, interpreted as an Address.
-
#ip_address=(value) ⇒ void
Sets this AVP’s byte data to an Address.
-
#octet_string ⇒ String
Returns this AVP’s byte data, interpreted as an OctetString.
-
#octet_string=(value) ⇒ void
Sets this AVP’s byte data to an OctetString.
-
#uint32 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Unsigned32.
-
#uint32=(value) ⇒ void
Sets this AVP’s byte data to an Unsigned32.
-
#uint64 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Unsigned64.
-
#uint64=(value) ⇒ void
Sets this AVP’s byte data to an Unsigned64.
Class Method Summary collapse
-
.create(name, val, options = {}) ⇒ AVP
Creates an AVP by name, and assigns it a value.
-
.define(name, code, type, vendor = nil) ⇒ void
Defines a new AVP that can subsequently be created/retrieved by name.
Instance Method Summary collapse
-
#initialize(code, options = {}) ⇒ AVP
constructor
private
Prefer AVP.create where possible.
-
#to_s ⇒ Object
Returns a string representation of this AVP.
-
#to_wire ⇒ String
Returns this AVP encoded properly as bytes in network byte order, suitable for sending over a TCP or SCTP connection.
Constructor Details
#initialize(code, options = {}) ⇒ AVP
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prefer create where possible.
57 58 59 60 61 62 |
# File 'lib/diameter/avp.rb', line 57 def initialize(code, = {}) @code = code @vendor_id = [:vendor_id] || 0 @content = [:content] || '' @mandatory = .fetch(:mandatory, true) end |
Instance Attribute Details
#code ⇒ Fixnum (readonly)
Returns The AVP Code.
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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/diameter/avp.rb', line 47 class AVP include Internals include Constants::AVPType attr_reader :code, :mandatory, :vendor_id include AVPParser # @api private # # Prefer {AVP.create} where possible. def initialize(code, = {}) @code = code @vendor_id = [:vendor_id] || 0 @content = [:content] || '' @mandatory = .fetch(:mandatory, true) end # Creates an AVP by name, and assigns it a value. # # @param name The name of the AVP, e.g. "Origin-Host" # @param val The value of the AVP. Must be of the type defined for # that AVP - e.g. a Fixnum for an AVP defined as Unsigned32, a # String for an AVP defined as OctetString, or an IPAddr for an AVP # defined as IPAddress. # @option opts [true, false] mandatory # Whether understanding this AVP is mandatory within this # application. # # @return [AVP] The AVP that was created. def self.create(name, val, = {}) code, type, vendor = AVPNames.get(name) [:vendor_id] = vendor avp = AVP.new(code, ) set_content(avp, type, val) avp end # Defines a new AVP that can subsequently be created/retrieved by # name. # # @param name [String] The AVP name # @param code [Fixnum] The AVP Code # @param type [AVPType] The type of this AVP's value # @param vendor [Fixnum] Optional vendor ID for a vendor-specific # AVP. # @return [void] def self.define(name, code, type, vendor=nil) AVPNames.add(name, code, type, vendor) end # Returns this AVP encoded properly as bytes in network byte order, # suitable for sending over a TCP or SCTP connection. See # {http://tools.ietf.org/html/rfc6733#section-4.1} for the # format. # # @return [String] The bytes representing this AVP def to_wire if vendor_specific? to_wire_vendor else to_wire_novendor end end # Guessing the type of an AVP and displaying it sensibly is complex, # so this is a complex method (but one that has a unity of purpose, # so can't easily be broken down). Disable several Rubocop # complexity metrics to reflect this. # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity # Returns a string representation of this AVP. Makes a best-effort # attempt to guess the type of the content (even for unknown AVPs) # and display it sensibly. # # @example # avp.to_s => "AVP 267, mandatory: true, content as int32: 1" def to_s has_all_ascii_values = @content.bytes.reject { |c| (32 < c && c < 126) }.empty? could_be_32bit_num = (@content.length == 4) could_be_64bit_num = (@content.length == 8) could_be_ip = ((@content.length == 6 && @content[0..1] == "\x00\x01") || (@content.length == 18 && @content[0..1] == "\x00\x02")) maybe_grouped = !(has_all_ascii_values || could_be_64bit_num || could_be_32bit_num || could_be_ip) s = vendor_specific? ? "AVP #{@code}, Vendor-ID #{@vendor_id}, mandatory: #{@mandatory}" : "AVP #{@code}, mandatory: #{@mandatory}" s += ", content as string: #{@content}" if has_all_ascii_values s += ", content as int32: #{uint32}" if could_be_32bit_num s += ", content as int64: #{uint64}" if could_be_64bit_num s += ", content as ip: #{ip_address}" if could_be_ip s += ", grouped AVP, #{grouped_value.collect(&:to_s)}" if maybe_grouped s end # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity # @!attribute [r] vendor_specific? # @return [true, false] Whether this AVP is mandatory # (i.e. its M flag is set) def vendor_specific? @vendor_id != 0 end # @!group Data getters/setters for different AVP types # Returns this AVP's byte data, interpreted as a # {http://tools.ietf.org/html/rfc6733#section-4.4 Grouped AVP}. # # @return [Array<AVP>] The contained AVPs. def grouped_value AVPParser.parse_avps_int(@content) end # Sets this AVP's byte data to a # {http://tools.ietf.org/html/rfc6733#section-4.4 Grouped AVP}. # # @param [Array<AVP>] avps The AVPs that should be contained within # this AVP. # @return [void] def grouped_value=(avps) new_content = '' avps.each { |a| new_content += a.to_wire } @content = new_content end # For a grouped AVP, returns the first AVP with this name it # contains. # # @param [String] name The AVP name # @return [AVP] if this AVP is found inside the Grouped AVP # @return [nil] if this AVP is not found inside the Grouped AVP def inner_avp(name) avps = inner_avps(name) if avps.empty? nil else avps[0] end end # For a grouped AVP, returns all AVPs it contains with this name. # # @param [String] name The AVP name # @return [Array<AVP>] def inner_avps(name) code, _type, _vendor = AVPNames.get(name) grouped_value.select { |a| a.code == code } end alias_method :[], :inner_avp # Even though it is just "the raw bytes in the content", # octet_string is only one way of interpreting the AVP content and # shouldn't be treated differently to the others, so disable the # TrivialAccessors warning. # rubocop:disable Style/TrivialAccessors # Returns this AVP's byte data, interpreted as an OctetString. # # @return [String] The contained OctetString. def octet_string @content end # Sets this AVP's byte data to an OctetString. # # @param [String] value The octets to use as the value. # @return [void] def octet_string=(value) @content = value end # rubocop:enable Style/TrivialAccessors # Returns this AVP's byte data, interpreted as an Integer32. # # @return [Fixnum] The contained Integer32. def int32 @content.unpack('l>')[0] end # Sets this AVP's byte data to an Integer32. # # @param [Fixnum] value # @return [void] def int32=(value) @content = [value].pack('l>') end # Returns this AVP's byte data, interpreted as an Integer64. # # @return [Fixnum] The contained Integer64. def int64 @content.unpack('q>')[0] end # Sets this AVP's byte data to an Integer64. # # @param [Fixnum] value # @return [void] def int64=(value) @content = [value].pack('q>') end # Returns this AVP's byte data, interpreted as an Unsigned32. # # @return [Fixnum] The contained Unsigned32. def uint32 @content.unpack('N')[0] end # Sets this AVP's byte data to an Unsigned32. # # @param [Fixnum] value # @return [void] def uint32=(value) @content = [value].pack('N') end # Returns this AVP's byte data, interpreted as an Unsigned64. # # @return [Fixnum] The contained Unsigned64. def uint64 @content.unpack('Q>')[0] end # Sets this AVP's byte data to an Unsigned64. # # @param [Fixnum] value # @return [void] def uint64=(value) @content = [value].pack('Q>') end # Returns this AVP's byte data, interpreted as a Float32. # # @return [Float] The contained Float32. def float32 @content.unpack('g')[0] end # Sets this AVP's byte data to a Float32. # # @param [Float] value # @return [void] def float32=(value) @content = [value].pack('g') end # Returns this AVP's byte data, interpreted as a Float64. # # @return [Float] The contained Float64. def float64 @content.unpack('G')[0] end # Sets this AVP's byte data to a Float64. # # @param [Float] value # @return [void] def float64=(value) @content = [value].pack('G') end # Returns this AVP's byte data, interpreted as an # {http://tools.ietf.org/html/rfc6733#section-4.3.1 Address}. # # @return [IPAddr] The contained # {http://tools.ietf.org/html/rfc6733#section-4.3.1 Address}. def ip_address IPAddr.new_ntoh(@content[2..-1]) end # Sets this AVP's byte data to an Address. # # @param [IPAddr] value # @return [void] def ip_address=(value) bytes = if value.ipv4? [1].pack('n') else [2].pack('n') end bytes += value.hton @content = bytes end # @!endgroup private def self.set_content(avp, type, val) case type when GROUPED avp.grouped_value = val when U32 avp.uint32 = val when OCTETSTRING avp.octet_string = val when IPADDR avp.ip_address = val end end def to_wire_novendor length_8, length_16 = UInt24.to_u8_and_u16(@content.length + 8) avp_flags = @mandatory ? '01000000' : '00000000' header = [@code, avp_flags, length_8, length_16].pack('NB8Cn') header + padded_content end def to_wire_vendor length_8, length_16 = UInt24.to_u8_and_u16(@content.length + 12) avp_flags = @mandatory ? '11000000' : '10000000' header = [@code, avp_flags, length_8, length_16, @vendor_id].pack('NB8CnN') header + padded_content end protected def padded_content wire_content = @content wire_content += "\x00" while ((wire_content.length % 4) != 0) wire_content end end |
#mandatory ⇒ true, false (readonly)
Returns Whether this AVP is mandatory (i.e. its M flag is set).
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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/diameter/avp.rb', line 47 class AVP include Internals include Constants::AVPType attr_reader :code, :mandatory, :vendor_id include AVPParser # @api private # # Prefer {AVP.create} where possible. def initialize(code, = {}) @code = code @vendor_id = [:vendor_id] || 0 @content = [:content] || '' @mandatory = .fetch(:mandatory, true) end # Creates an AVP by name, and assigns it a value. # # @param name The name of the AVP, e.g. "Origin-Host" # @param val The value of the AVP. Must be of the type defined for # that AVP - e.g. a Fixnum for an AVP defined as Unsigned32, a # String for an AVP defined as OctetString, or an IPAddr for an AVP # defined as IPAddress. # @option opts [true, false] mandatory # Whether understanding this AVP is mandatory within this # application. # # @return [AVP] The AVP that was created. def self.create(name, val, = {}) code, type, vendor = AVPNames.get(name) [:vendor_id] = vendor avp = AVP.new(code, ) set_content(avp, type, val) avp end # Defines a new AVP that can subsequently be created/retrieved by # name. # # @param name [String] The AVP name # @param code [Fixnum] The AVP Code # @param type [AVPType] The type of this AVP's value # @param vendor [Fixnum] Optional vendor ID for a vendor-specific # AVP. # @return [void] def self.define(name, code, type, vendor=nil) AVPNames.add(name, code, type, vendor) end # Returns this AVP encoded properly as bytes in network byte order, # suitable for sending over a TCP or SCTP connection. See # {http://tools.ietf.org/html/rfc6733#section-4.1} for the # format. # # @return [String] The bytes representing this AVP def to_wire if vendor_specific? to_wire_vendor else to_wire_novendor end end # Guessing the type of an AVP and displaying it sensibly is complex, # so this is a complex method (but one that has a unity of purpose, # so can't easily be broken down). Disable several Rubocop # complexity metrics to reflect this. # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity # Returns a string representation of this AVP. Makes a best-effort # attempt to guess the type of the content (even for unknown AVPs) # and display it sensibly. # # @example # avp.to_s => "AVP 267, mandatory: true, content as int32: 1" def to_s has_all_ascii_values = @content.bytes.reject { |c| (32 < c && c < 126) }.empty? could_be_32bit_num = (@content.length == 4) could_be_64bit_num = (@content.length == 8) could_be_ip = ((@content.length == 6 && @content[0..1] == "\x00\x01") || (@content.length == 18 && @content[0..1] == "\x00\x02")) maybe_grouped = !(has_all_ascii_values || could_be_64bit_num || could_be_32bit_num || could_be_ip) s = vendor_specific? ? "AVP #{@code}, Vendor-ID #{@vendor_id}, mandatory: #{@mandatory}" : "AVP #{@code}, mandatory: #{@mandatory}" s += ", content as string: #{@content}" if has_all_ascii_values s += ", content as int32: #{uint32}" if could_be_32bit_num s += ", content as int64: #{uint64}" if could_be_64bit_num s += ", content as ip: #{ip_address}" if could_be_ip s += ", grouped AVP, #{grouped_value.collect(&:to_s)}" if maybe_grouped s end # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity # @!attribute [r] vendor_specific? # @return [true, false] Whether this AVP is mandatory # (i.e. its M flag is set) def vendor_specific? @vendor_id != 0 end # @!group Data getters/setters for different AVP types # Returns this AVP's byte data, interpreted as a # {http://tools.ietf.org/html/rfc6733#section-4.4 Grouped AVP}. # # @return [Array<AVP>] The contained AVPs. def grouped_value AVPParser.parse_avps_int(@content) end # Sets this AVP's byte data to a # {http://tools.ietf.org/html/rfc6733#section-4.4 Grouped AVP}. # # @param [Array<AVP>] avps The AVPs that should be contained within # this AVP. # @return [void] def grouped_value=(avps) new_content = '' avps.each { |a| new_content += a.to_wire } @content = new_content end # For a grouped AVP, returns the first AVP with this name it # contains. # # @param [String] name The AVP name # @return [AVP] if this AVP is found inside the Grouped AVP # @return [nil] if this AVP is not found inside the Grouped AVP def inner_avp(name) avps = inner_avps(name) if avps.empty? nil else avps[0] end end # For a grouped AVP, returns all AVPs it contains with this name. # # @param [String] name The AVP name # @return [Array<AVP>] def inner_avps(name) code, _type, _vendor = AVPNames.get(name) grouped_value.select { |a| a.code == code } end alias_method :[], :inner_avp # Even though it is just "the raw bytes in the content", # octet_string is only one way of interpreting the AVP content and # shouldn't be treated differently to the others, so disable the # TrivialAccessors warning. # rubocop:disable Style/TrivialAccessors # Returns this AVP's byte data, interpreted as an OctetString. # # @return [String] The contained OctetString. def octet_string @content end # Sets this AVP's byte data to an OctetString. # # @param [String] value The octets to use as the value. # @return [void] def octet_string=(value) @content = value end # rubocop:enable Style/TrivialAccessors # Returns this AVP's byte data, interpreted as an Integer32. # # @return [Fixnum] The contained Integer32. def int32 @content.unpack('l>')[0] end # Sets this AVP's byte data to an Integer32. # # @param [Fixnum] value # @return [void] def int32=(value) @content = [value].pack('l>') end # Returns this AVP's byte data, interpreted as an Integer64. # # @return [Fixnum] The contained Integer64. def int64 @content.unpack('q>')[0] end # Sets this AVP's byte data to an Integer64. # # @param [Fixnum] value # @return [void] def int64=(value) @content = [value].pack('q>') end # Returns this AVP's byte data, interpreted as an Unsigned32. # # @return [Fixnum] The contained Unsigned32. def uint32 @content.unpack('N')[0] end # Sets this AVP's byte data to an Unsigned32. # # @param [Fixnum] value # @return [void] def uint32=(value) @content = [value].pack('N') end # Returns this AVP's byte data, interpreted as an Unsigned64. # # @return [Fixnum] The contained Unsigned64. def uint64 @content.unpack('Q>')[0] end # Sets this AVP's byte data to an Unsigned64. # # @param [Fixnum] value # @return [void] def uint64=(value) @content = [value].pack('Q>') end # Returns this AVP's byte data, interpreted as a Float32. # # @return [Float] The contained Float32. def float32 @content.unpack('g')[0] end # Sets this AVP's byte data to a Float32. # # @param [Float] value # @return [void] def float32=(value) @content = [value].pack('g') end # Returns this AVP's byte data, interpreted as a Float64. # # @return [Float] The contained Float64. def float64 @content.unpack('G')[0] end # Sets this AVP's byte data to a Float64. # # @param [Float] value # @return [void] def float64=(value) @content = [value].pack('G') end # Returns this AVP's byte data, interpreted as an # {http://tools.ietf.org/html/rfc6733#section-4.3.1 Address}. # # @return [IPAddr] The contained # {http://tools.ietf.org/html/rfc6733#section-4.3.1 Address}. def ip_address IPAddr.new_ntoh(@content[2..-1]) end # Sets this AVP's byte data to an Address. # # @param [IPAddr] value # @return [void] def ip_address=(value) bytes = if value.ipv4? [1].pack('n') else [2].pack('n') end bytes += value.hton @content = bytes end # @!endgroup private def self.set_content(avp, type, val) case type when GROUPED avp.grouped_value = val when U32 avp.uint32 = val when OCTETSTRING avp.octet_string = val when IPADDR avp.ip_address = val end end def to_wire_novendor length_8, length_16 = UInt24.to_u8_and_u16(@content.length + 8) avp_flags = @mandatory ? '01000000' : '00000000' header = [@code, avp_flags, length_8, length_16].pack('NB8Cn') header + padded_content end def to_wire_vendor length_8, length_16 = UInt24.to_u8_and_u16(@content.length + 12) avp_flags = @mandatory ? '11000000' : '10000000' header = [@code, avp_flags, length_8, length_16, @vendor_id].pack('NB8CnN') header + padded_content end protected def padded_content wire_content = @content wire_content += "\x00" while ((wire_content.length % 4) != 0) wire_content end end |
#vendor_id ⇒ Object (readonly)
Returns the value of attribute vendor_id.
50 51 52 |
# File 'lib/diameter/avp.rb', line 50 def vendor_id @vendor_id end |
#vendor_specific? ⇒ Boolean (readonly)
158 159 160 |
# File 'lib/diameter/avp.rb', line 158 def vendor_specific? @vendor_id != 0 end |
Class Method Details
.create(name, val, options = {}) ⇒ AVP
Creates an AVP by name, and assigns it a value.
76 77 78 79 80 81 82 83 84 |
# File 'lib/diameter/avp.rb', line 76 def self.create(name, val, = {}) code, type, vendor = AVPNames.get(name) [:vendor_id] = vendor avp = AVP.new(code, ) set_content(avp, type, val) avp end |
Instance Method Details
#float32 ⇒ Float
Returns this AVP’s byte data, interpreted as a Float32.
299 300 301 |
# File 'lib/diameter/avp.rb', line 299 def float32 @content.unpack('g')[0] end |
#float32=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to a Float32.
307 308 309 |
# File 'lib/diameter/avp.rb', line 307 def float32=(value) @content = [value].pack('g') end |
#float64 ⇒ Float
Returns this AVP’s byte data, interpreted as a Float64.
314 315 316 |
# File 'lib/diameter/avp.rb', line 314 def float64 @content.unpack('G')[0] end |
#float64=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to a Float64.
322 323 324 |
# File 'lib/diameter/avp.rb', line 322 def float64=(value) @content = [value].pack('G') end |
#grouped_value ⇒ Array<AVP>
Returns this AVP’s byte data, interpreted as a Grouped AVP.
168 169 170 |
# File 'lib/diameter/avp.rb', line 168 def grouped_value AVPParser.parse_avps_int(@content) end |
#grouped_value=(avps) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to a Grouped AVP.
178 179 180 181 182 |
# File 'lib/diameter/avp.rb', line 178 def grouped_value=(avps) new_content = '' avps.each { |a| new_content += a.to_wire } @content = new_content end |
#inner_avp(name) ⇒ AVP? Also known as: []
For a grouped AVP, returns the first AVP with this name it contains.
190 191 192 193 194 195 196 197 198 |
# File 'lib/diameter/avp.rb', line 190 def inner_avp(name) avps = inner_avps(name) if avps.empty? nil else avps[0] end end |
#inner_avps(name) ⇒ Array<AVP>
For a grouped AVP, returns all AVPs it contains with this name.
204 205 206 207 208 |
# File 'lib/diameter/avp.rb', line 204 def inner_avps(name) code, _type, _vendor = AVPNames.get(name) grouped_value.select { |a| a.code == code } end |
#int32 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Integer32.
239 240 241 |
# File 'lib/diameter/avp.rb', line 239 def int32 @content.unpack('l>')[0] end |
#int32=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to an Integer32.
247 248 249 |
# File 'lib/diameter/avp.rb', line 247 def int32=(value) @content = [value].pack('l>') end |
#int64 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Integer64.
254 255 256 |
# File 'lib/diameter/avp.rb', line 254 def int64 @content.unpack('q>')[0] end |
#int64=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to an Integer64.
262 263 264 |
# File 'lib/diameter/avp.rb', line 262 def int64=(value) @content = [value].pack('q>') end |
#ip_address ⇒ IPAddr
Returns this AVP’s byte data, interpreted as an Address.
331 332 333 |
# File 'lib/diameter/avp.rb', line 331 def ip_address IPAddr.new_ntoh(@content[2..-1]) end |
#ip_address=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to an Address.
339 340 341 342 343 344 345 346 347 348 |
# File 'lib/diameter/avp.rb', line 339 def ip_address=(value) bytes = if value.ipv4? [1].pack('n') else [2].pack('n') end bytes += value.hton @content = bytes end |
#octet_string ⇒ String
Returns this AVP’s byte data, interpreted as an OctetString.
222 223 224 |
# File 'lib/diameter/avp.rb', line 222 def octet_string @content end |
#octet_string=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to an OctetString.
230 231 232 |
# File 'lib/diameter/avp.rb', line 230 def octet_string=(value) @content = value end |
#to_s ⇒ Object
Returns a string representation of this AVP. Makes a best-effort attempt to guess the type of the content (even for unknown AVPs) and display it sensibly.
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 |
# File 'lib/diameter/avp.rb', line 127 def to_s has_all_ascii_values = @content.bytes.reject { |c| (32 < c && c < 126) }.empty? could_be_32bit_num = (@content.length == 4) could_be_64bit_num = (@content.length == 8) could_be_ip = ((@content.length == 6 && @content[0..1] == "\x00\x01") || (@content.length == 18 && @content[0..1] == "\x00\x02")) maybe_grouped = !(has_all_ascii_values || could_be_64bit_num || could_be_32bit_num || could_be_ip) s = vendor_specific? ? "AVP #{@code}, Vendor-ID #{@vendor_id}, mandatory: #{@mandatory}" : "AVP #{@code}, mandatory: #{@mandatory}" s += ", content as string: #{@content}" if has_all_ascii_values s += ", content as int32: #{uint32}" if could_be_32bit_num s += ", content as int64: #{uint64}" if could_be_64bit_num s += ", content as ip: #{ip_address}" if could_be_ip s += ", grouped AVP, #{grouped_value.collect(&:to_s)}" if maybe_grouped s end |
#to_wire ⇒ String
Returns this AVP encoded properly as bytes in network byte order, suitable for sending over a TCP or SCTP connection. See http://tools.ietf.org/html/rfc6733#section-4.1 for the format.
105 106 107 108 109 110 111 |
# File 'lib/diameter/avp.rb', line 105 def to_wire if vendor_specific? to_wire_vendor else to_wire_novendor end end |
#uint32 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Unsigned32.
269 270 271 |
# File 'lib/diameter/avp.rb', line 269 def uint32 @content.unpack('N')[0] end |
#uint32=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to an Unsigned32.
277 278 279 |
# File 'lib/diameter/avp.rb', line 277 def uint32=(value) @content = [value].pack('N') end |
#uint64 ⇒ Fixnum
Returns this AVP’s byte data, interpreted as an Unsigned64.
284 285 286 |
# File 'lib/diameter/avp.rb', line 284 def uint64 @content.unpack('Q>')[0] end |
#uint64=(value) ⇒ void
This method returns an undefined value.
Sets this AVP’s byte data to an Unsigned64.
292 293 294 |
# File 'lib/diameter/avp.rb', line 292 def uint64=(value) @content = [value].pack('Q>') end |