Class: Diameter::AVP

Inherits:
Object
  • Object
show all
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::DiameterIdentity, Constants::AVPType::DiameterURI, Constants::AVPType::Enumerated, Constants::AVPType::Float32, Constants::AVPType::Float64, Constants::AVPType::Grouped, Constants::AVPType::IPAddress, Constants::AVPType::Integer32, Constants::AVPType::Integer64, Constants::AVPType::OctetString, Constants::AVPType::UTF8String, Constants::AVPType::Unsigned32, Constants::AVPType::Unsigned64

Instance Attribute Summary collapse

Data getters/setters for different AVP types collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



58
59
60
61
62
63
# File 'lib/diameter/avp.rb', line 58

def initialize(code, options = {})
  @code = code
  @vendor_id = options[:vendor_id] || 0
  @content = options[:content] || ''
  @mandatory = options.fetch(:mandatory, true)
end

Instance Attribute Details

#codeFixnum (readonly)

Returns The AVP Code.

Returns:

  • (Fixnum)

    The AVP Code



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
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/diameter/avp.rb', line 48

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, options = {})
    @code = code
    @vendor_id = options[:vendor_id] || 0
    @content = options[:content] || ''
    @mandatory = options.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, options = {})
    code, type, vendor = AVPNames.get(name)
    options[:vendor_id] = vendor
    avp = AVP.new(code, options)

    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 OctetString
      avp.octet_string = val
    when Unsigned32
      avp.uint32 = val
    when Unsigned64
      avp.uint64 = val
    when Integer32
      avp.int32 = val
    when Integer64
      avp.int64 = val
    when Float32
      avp.float32 = val
    when Float64
      avp.float64 = val
    when IPAddress
      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

#mandatorytrue, false (readonly)

Returns Whether this AVP is mandatory (i.e. its M flag is set).

Returns:

  • (true, false)

    Whether this AVP is mandatory (i.e. its M flag is set)



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
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/diameter/avp.rb', line 48

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, options = {})
    @code = code
    @vendor_id = options[:vendor_id] || 0
    @content = options[:content] || ''
    @mandatory = options.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, options = {})
    code, type, vendor = AVPNames.get(name)
    options[:vendor_id] = vendor
    avp = AVP.new(code, options)

    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 OctetString
      avp.octet_string = val
    when Unsigned32
      avp.uint32 = val
    when Unsigned64
      avp.uint64 = val
    when Integer32
      avp.int32 = val
    when Integer64
      avp.int64 = val
    when Float32
      avp.float32 = val
    when Float64
      avp.float64 = val
    when IPAddress
      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_idObject (readonly)

Returns the value of attribute vendor_id.



51
52
53
# File 'lib/diameter/avp.rb', line 51

def vendor_id
  @vendor_id
end

#vendor_specific?Boolean (readonly)

Returns:

  • (Boolean)


159
160
161
# File 'lib/diameter/avp.rb', line 159

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.

Parameters:

  • name

    The name of the AVP, e.g. “Origin-Host”

  • 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.

  • opts (Hash)

    a customizable set of options

Returns:

  • (AVP)

    The AVP that was created.



77
78
79
80
81
82
83
84
85
# File 'lib/diameter/avp.rb', line 77

def self.create(name, val, options = {})
  code, type, vendor = AVPNames.get(name)
  options[:vendor_id] = vendor
  avp = AVP.new(code, options)

  set_content(avp, type, val)

  avp
end

.define(name, code, type, vendor = nil) ⇒ void

This method returns an undefined value.

Defines a new AVP that can subsequently be created/retrieved by name.

Parameters:

  • name (String)

    The AVP name

  • code (Fixnum)

    The AVP Code

  • type (AVPType)

    The type of this AVP’s value

  • vendor (Fixnum) (defaults to: nil)

    Optional vendor ID for a vendor-specific AVP.



96
97
98
# File 'lib/diameter/avp.rb', line 96

def self.define(name, code, type, vendor=nil)
  AVPNames.add(name, code, type, vendor)
end

Instance Method Details

#float32Float

Returns this AVP’s byte data, interpreted as a Float32.

Returns:

  • (Float)

    The contained Float32.



300
301
302
# File 'lib/diameter/avp.rb', line 300

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.

Parameters:

  • value (Float)


308
309
310
# File 'lib/diameter/avp.rb', line 308

def float32=(value)
  @content = [value].pack('g')
end

#float64Float

Returns this AVP’s byte data, interpreted as a Float64.

Returns:

  • (Float)

    The contained Float64.



315
316
317
# File 'lib/diameter/avp.rb', line 315

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.

Parameters:

  • value (Float)


323
324
325
# File 'lib/diameter/avp.rb', line 323

def float64=(value)
  @content = [value].pack('G')
end

#grouped_valueArray<AVP>

Returns this AVP’s byte data, interpreted as a Grouped AVP.

Returns:

  • (Array<AVP>)

    The contained AVPs.



169
170
171
# File 'lib/diameter/avp.rb', line 169

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.

Parameters:

  • avps (Array<AVP>)

    The AVPs that should be contained within this AVP.



179
180
181
182
183
# File 'lib/diameter/avp.rb', line 179

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.

Parameters:

  • name (String)

    The AVP name

Returns:

  • (AVP)

    if this AVP is found inside the Grouped AVP

  • (nil)

    if this AVP is not found inside the Grouped AVP



191
192
193
194
195
196
197
198
199
# File 'lib/diameter/avp.rb', line 191

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.

Parameters:

  • name (String)

    The AVP name

Returns:



205
206
207
208
209
# File 'lib/diameter/avp.rb', line 205

def inner_avps(name)
  code, _type, _vendor = AVPNames.get(name)

  grouped_value.select { |a| a.code == code }
end

#int32Fixnum

Returns this AVP’s byte data, interpreted as an Integer32.

Returns:

  • (Fixnum)

    The contained Integer32.



240
241
242
# File 'lib/diameter/avp.rb', line 240

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.

Parameters:

  • value (Fixnum)


248
249
250
# File 'lib/diameter/avp.rb', line 248

def int32=(value)
  @content = [value].pack('l>')
end

#int64Fixnum

Returns this AVP’s byte data, interpreted as an Integer64.

Returns:

  • (Fixnum)

    The contained Integer64.



255
256
257
# File 'lib/diameter/avp.rb', line 255

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.

Parameters:

  • value (Fixnum)


263
264
265
# File 'lib/diameter/avp.rb', line 263

def int64=(value)
  @content = [value].pack('q>')
end

#ip_addressIPAddr

Returns this AVP’s byte data, interpreted as an Address.

Returns:

  • (IPAddr)

    The contained Address.



332
333
334
# File 'lib/diameter/avp.rb', line 332

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.

Parameters:

  • value (IPAddr)


340
341
342
343
344
345
346
347
348
349
# File 'lib/diameter/avp.rb', line 340

def ip_address=(value)
  bytes = if value.ipv4?
            [1].pack('n')
          else
            [2].pack('n')
          end

  bytes += value.hton
  @content = bytes
end

#octet_stringString

Returns this AVP’s byte data, interpreted as an OctetString.

Returns:

  • (String)

    The contained OctetString.



223
224
225
# File 'lib/diameter/avp.rb', line 223

def octet_string
  @content
end

#octet_string=(value) ⇒ void

This method returns an undefined value.

Sets this AVP’s byte data to an OctetString.

Parameters:

  • value (String)

    The octets to use as the value.



231
232
233
# File 'lib/diameter/avp.rb', line 231

def octet_string=(value)
  @content = value
end

#to_sObject

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.

Examples:

avp.to_s => "AVP 267, mandatory: true, content as int32: 1"


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
# File 'lib/diameter/avp.rb', line 128

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_wireString

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.

Returns:

  • (String)

    The bytes representing this AVP



106
107
108
109
110
111
112
# File 'lib/diameter/avp.rb', line 106

def to_wire
  if vendor_specific?
    to_wire_vendor
  else
    to_wire_novendor
  end
end

#uint32Fixnum

Returns this AVP’s byte data, interpreted as an Unsigned32.

Returns:

  • (Fixnum)

    The contained Unsigned32.



270
271
272
# File 'lib/diameter/avp.rb', line 270

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.

Parameters:

  • value (Fixnum)


278
279
280
# File 'lib/diameter/avp.rb', line 278

def uint32=(value)
  @content = [value].pack('N')
end

#uint64Fixnum

Returns this AVP’s byte data, interpreted as an Unsigned64.

Returns:

  • (Fixnum)

    The contained Unsigned64.



285
286
287
# File 'lib/diameter/avp.rb', line 285

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.

Parameters:

  • value (Fixnum)


293
294
295
# File 'lib/diameter/avp.rb', line 293

def uint64=(value)
  @content = [value].pack('Q>')
end