PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/resources/diameter-base/common/events/src/main/java/net/java/slee/resource/diameter/base/events/avp/DiameterAvpType.java

http://mobicents.googlecode.com/
Java | 547 lines | 106 code | 30 blank | 411 comment | 3 complexity | b5d410be29212e4288fe7fb0b9b44dad MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package net.java.slee.resource.diameter.base.events.avp;
  23. import java.io.Serializable;
  24. import java.io.StreamCorruptedException;
  25. /**
  26. * Enumerated type class to identify AVP value types.
  27. *
  28. * <p/>
  29. * From the spec:
  30. * <p/>
  31. * The Data field is zero or more octets and contains information
  32. * specific to the Attribute. The format and length of the Data field
  33. * is determined by the AVP Code and AVP Length fields. The format of
  34. * the Data field MUST be one of the following base data types or a data
  35. * type derived from the base data types. In the event that a new Basic
  36. * AVP Data Format is needed, a new version of this RFC must be created.
  37. *
  38. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  39. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  40. */
  41. public class DiameterAvpType implements Serializable {
  42. private static final long serialVersionUID = 1L;
  43. private DiameterAvpType(int type) {
  44. this.type = type;
  45. }
  46. public String toString() {
  47. return names[type];
  48. }
  49. public static DiameterAvpType fromString(String s) {
  50. for (int i = 0; i < names.length; i++) {
  51. if(s.equals(names[i])) {
  52. return fromInt(i);
  53. }
  54. }
  55. throw new IllegalArgumentException("Unknown type: " + s);
  56. }
  57. public int getType() {
  58. return type;
  59. }
  60. public static DiameterAvpType fromInt(int i)
  61. {
  62. switch (i)
  63. {
  64. case _OCTET_STRING: return OCTET_STRING;
  65. case _INTEGER_32: return INTEGER_32;
  66. case _INTEGER_64: return INTEGER_64;
  67. case _UNSIGNED_32: return UNSIGNED_32;
  68. case _UNSIGNED_64: return UNSIGNED_64;
  69. case _FLOAT_32: return FLOAT_32;
  70. case _FLOAT_64: return FLOAT_64;
  71. case _GROUPED: return GROUPED;
  72. case _ADDRESS: return ADDRESS;
  73. case _TIME: return TIME;
  74. case _UTF8_STRING: return UTF8_STRING;
  75. case _DIAMETER_IDENTITY: return DIAMETER_IDENTITY;
  76. case _DIAMETER_URI: return DIAMETER_URI;
  77. case _ENUMERATED: return ENUMERATED;
  78. case _IP_FILTER_RULE: return IP_FILTER_RULE;
  79. case _QOS_FILTER_RULE: return QOS_FILTER_RULE;
  80. default: throw new IllegalArgumentException("Unknown type: " + i);
  81. }
  82. }
  83. public static final int _OCTET_STRING = 0;
  84. public static final int _INTEGER_32 = 1;
  85. public static final int _INTEGER_64 = 2;
  86. public static final int _UNSIGNED_32 = 3;
  87. public static final int _UNSIGNED_64 = 4;
  88. public static final int _FLOAT_32 = 5;
  89. public static final int _FLOAT_64 = 6;
  90. public static final int _GROUPED = 7;
  91. public static final int _ADDRESS = 8;
  92. public static final int _TIME = 9;
  93. public static final int _UTF8_STRING = 10;
  94. public static final int _DIAMETER_IDENTITY = 11;
  95. public static final int _DIAMETER_URI = 12;
  96. public static final int _ENUMERATED = 13;
  97. public static final int _IP_FILTER_RULE = 14;
  98. public static final int _QOS_FILTER_RULE = 15;
  99. private static final String[] names = {
  100. "OctetString",
  101. "Integer32",
  102. "Integer64",
  103. "Unsigned32",
  104. "Unsigned64",
  105. "Float32",
  106. "Float64",
  107. "Grouped",
  108. "Address",
  109. "Time",
  110. "UTF8String",
  111. "DiameterIdentity",
  112. "DiameterURI",
  113. "Enumerated",
  114. "IPFilterRule",
  115. "QoSFilterRule",
  116. "IPAddress"
  117. };
  118. /**
  119. * The data contains arbitrary data of variable length. Unless
  120. * otherwise noted, the AVP Length field MUST be set to at least 8
  121. * (12 if the 'V' bit is enabled). AVP Values of this type that are
  122. * not a multiple of four-octets in length is followed by the
  123. * necessary padding so that the next AVP (if any) will start on a
  124. * 32-bit boundary.
  125. */
  126. public static final DiameterAvpType OCTET_STRING = new DiameterAvpType(_OCTET_STRING);
  127. /**
  128. * 32 bit signed value, in network byte order. The AVP Length field
  129. * MUST be set to 12 (16 if the 'V' bit is enabled).
  130. */
  131. public static final DiameterAvpType INTEGER_32 = new DiameterAvpType(_INTEGER_32);
  132. /**
  133. * 64 bit signed value, in network byte order. The AVP Length field
  134. * MUST be set to 16 (20 if the 'V' bit is enabled).
  135. */
  136. public static final DiameterAvpType INTEGER_64 = new DiameterAvpType(_INTEGER_64);
  137. /**
  138. * 32 bit unsigned value, in network byte order. The AVP Length
  139. * field MUST be set to 12 (16 if the 'V' bit is enabled).
  140. */
  141. public static final DiameterAvpType UNSIGNED_32 = new DiameterAvpType(_UNSIGNED_32);
  142. /**
  143. * 64 bit unsigned value, in network byte order. The AVP Length
  144. * field MUST be set to 16 (20 if the 'V' bit is enabled).
  145. */
  146. public static final DiameterAvpType UNSIGNED_64 = new DiameterAvpType(_UNSIGNED_64);
  147. /**
  148. * This represents floating point values of single precision as
  149. * described by [FLOATPOINT]. The 32-bit value is transmitted in
  150. * network byte order. The AVP Length field MUST be set to 12 (16 if
  151. * the 'V' bit is enabled).
  152. */
  153. public static final DiameterAvpType FLOAT_32 = new DiameterAvpType(_FLOAT_32);
  154. /**
  155. * This represents floating point values of double precision as
  156. * described by [FLOATPOINT]. The 64-bit value is transmitted in
  157. * network byte order. The AVP Length field MUST be set to 16 (20 if
  158. * the 'V' bit is enabled).
  159. */
  160. public static final DiameterAvpType FLOAT_64 = new DiameterAvpType(_FLOAT_64);
  161. /**
  162. * The Data field is specified as a sequence of AVPs. Each of these
  163. * AVPs follows - in the order in which they are specified -
  164. * including their headers and padding. The AVP Length field is set
  165. * to 8 (12 if the 'V' bit is enabled) plus the total length of all
  166. * included AVPs, including their headers and padding. Thus the AVP
  167. * length field of an AVP of type Grouped is always a multiple of 4.
  168. */
  169. public static final DiameterAvpType GROUPED = new DiameterAvpType(_GROUPED);
  170. /**
  171. * The Address format is derived from the OctetString AVP Base
  172. * Format. It is a discriminated union, representing, for example a
  173. * 32-bit (IPv4) [IPV4] or 128-bit (IPv6) [IPV6] address, most
  174. * significant octet first. The first two octets of the Address
  175. * AVP represents the AddressType, which contains an Address Family
  176. * defined in [IANAADFAM]. The AddressType is used to discriminate
  177. * the content and format of the remaining octets.
  178. */
  179. public static final DiameterAvpType ADDRESS = new DiameterAvpType(_ADDRESS);
  180. /**
  181. * The Time format is derived from the OctetString AVP Base Format.
  182. * The string MUST contain four octets, in the same format as the
  183. * first four bytes are in the NTP timestamp format. The NTP
  184. * Timestamp format is defined in chapter 3 of [SNTP].
  185. * <p/>
  186. * This represents the number of seconds since 0h on 1 January 1900
  187. * with respect to the Coordinated Universal Time (UTC).
  188. * <p/>
  189. * On 6h 28m 16s UTC, 7 February 2036 the time value will overflow.
  190. * SNTP [SNTP] describes a procedure to extend the time to 2104.
  191. * This procedure MUST be supported by all DIAMETER nodes.
  192. */
  193. public static final DiameterAvpType TIME = new DiameterAvpType(_TIME);
  194. /**
  195. * The UTF8String format is derived from the OctetString AVP Base
  196. * Format. This is a human readable string represented using the
  197. * ISO/IEC IS 10646-1 character set, encoded as an OctetString using
  198. * the UTF-8 [UFT8] transformation format described in RFC 2279.
  199. * <p/>
  200. * Since additional code points are added by amendments to the 10646
  201. * standard from time to time, implementations MUST be prepared to
  202. * encounter any code point from 0x00000001 to 0x7fffffff. Byte
  203. * sequences that do not correspond to the valid encoding of a code
  204. * point into UTF-8 charset or are outside this range are prohibited.
  205. * <p/>
  206. * The use of control codes SHOULD be avoided. When it is necessary
  207. * to represent a new line, the control code sequence CR LF SHOULD be
  208. * used.
  209. * <p/>
  210. * The use of leading or trailing white space SHOULD be avoided.
  211. * <p/>
  212. * For code points not directly supported by user interface hardware
  213. * or software, an alternative means of entry and display, such as
  214. * hexadecimal, MAY be provided.
  215. * <p/>
  216. * For information encoded in 7-bit US-ASCII, the UTF-8 charset is
  217. * identical to the US-ASCII charset.
  218. * <p/>
  219. * UTF-8 may require multiple bytes to represent a single character /
  220. * code point; thus the length of an UTF8String in octets may be
  221. * different from the number of characters encoded.
  222. * <p/>
  223. * Note that the AVP Length field of an UTF8String is measured in
  224. * octets, not characters.
  225. */
  226. public static final DiameterAvpType UTF8_STRING = new DiameterAvpType(_UTF8_STRING);
  227. /**
  228. The DiameterIdentity format is derived from the OctetString AVP
  229. Base Format.
  230. <pre>
  231. DiameterIdentity = FQDN
  232. </pre>
  233. DiameterIdentity value is used to uniquely identify a Diameter
  234. node for purposes of duplicate connection and routing loop
  235. detection.
  236. <p/>
  237. The contents of the string MUST be the FQDN of the Diameter node.
  238. If multiple Diameter nodes run on the same host, each Diameter
  239. node MUST be assigned a unique DiameterIdentity. If a Diameter
  240. node can be identified by several FQDNs, a single FQDN should be
  241. picked at startup, and used as the only DiameterIdentity for that
  242. node, whatever the connection it is sent on.
  243. */
  244. public static final DiameterAvpType DIAMETER_IDENTITY = new DiameterAvpType(_DIAMETER_IDENTITY);
  245. /**
  246. The DiameterURI MUST follow the Uniform Resource Identifiers (URI)
  247. syntax [URI] rules specified below.
  248. <pre>
  249. "aaa://" FQDN [ port ] [ transport ] [ protocol ]
  250. ; No transport security
  251. "aaas://" FQDN [ port ] [ transport ] [ protocol ]
  252. ; Transport security used
  253. FQDN = Fully Qualified Host Name
  254. port = ":" 1*DIGIT
  255. ; One of the ports used to listen for
  256. ; incoming connections.
  257. ; If absent,
  258. ; the default Diameter port (3868) is
  259. ; assumed.
  260. transport = ";transport=" transport-protocol
  261. ; One of the transports used to listen
  262. ; for incoming connections. If absent,
  263. ; the default SCTP [SCTP] protocol is
  264. ; assumed. UDP MUST NOT be used when
  265. ; the aaa-protocol field is set to
  266. ; diameter.
  267. transport-protocol = ( "tcp" / "sctp" / "udp" )
  268. protocol = ";protocol=" aaa-protocol
  269. ; If absent, the default AAA protocol
  270. ; is diameter.
  271. aaa-protocol = ( "diameter" / "radius" / "tacacs+" )
  272. The following are examples of valid Diameter host identities:
  273. aaa://host.example.com;transport=tcp
  274. aaa://host.example.com:6666;transport=tcp
  275. aaa://host.example.com;protocol=diameter
  276. aaa://host.example.com:6666;protocol=diameter
  277. aaa://host.example.com:6666;transport=tcp;protocol=diameter
  278. aaa://host.example.com:1813;transport=udp;protocol=radius
  279. </pre>
  280. */
  281. public static final DiameterAvpType DIAMETER_URI = new DiameterAvpType(_DIAMETER_URI);
  282. /**
  283. * Enumerated is derived from the Integer32 AVP Base Format. The
  284. * definition contains a list of valid values and their
  285. * interpretation and is described in the Diameter application
  286. * introducing the AVP.
  287. */
  288. public static final DiameterAvpType ENUMERATED = new DiameterAvpType(_ENUMERATED);
  289. /**
  290. The IPFilterRule format is derived from the OctetString AVP Base
  291. Format. It uses the ASCII charset. Packets may be filtered based
  292. on the following information that is associated with it.
  293. <pre>
  294. Direction (in or out)
  295. Source and destination IP address (possibly masked)
  296. Protocol
  297. Source and destination port (lists or ranges)
  298. TCP flags
  299. IP fragment flag
  300. IP options
  301. ICMP types
  302. </pre>
  303. Rules for the appropriate direction are evaluated in order, with
  304. the first matched rule terminating the evaluation. Each packet is
  305. evaluated once. If no rule matches, the packet is dropped if the
  306. last rule evaluated was a permit, and passed if the last rule was
  307. a deny.
  308. IPFilterRule filters MUST follow the format:
  309. <pre>
  310. action dir proto from src to dst [options]
  311. action permit - Allow packets that match the rule.
  312. deny - Drop packets that match the rule.
  313. dir "in" is from the terminal, "out" is to the
  314. terminal.
  315. proto An IP protocol specified by number. The "ip"
  316. keyword means any protocol will match.
  317. src and dst &lt;address/mask&gt; [ports]
  318. The &lt;address/mask&gt; may be specified as:
  319. ipno An IPv4 or IPv6 number in dotted-
  320. quad or canonical IPv6 form. Only
  321. this exact IP number will match the
  322. rule.
  323. ipno/bits An IP number as above with a mask
  324. width of the form 1.2.3.4/24. In
  325. this case, all IP numbers from
  326. 1.2.3.0 to 1.2.3.255 will match.
  327. The bit width MUST be valid for the
  328. IP version and the IP number MUST
  329. NOT have bits set beyond the mask.
  330. For a match to occur, the same IP
  331. version must be present in the
  332. packet that was used in describing
  333. the IP address. To test for a
  334. particular IP version, the bits part
  335. can be set to zero. The keyword
  336. "any" is 0.0.0.0/0 or the IPv6
  337. equivalent. The keyword "assigned"
  338. is the address or set of addresses
  339. assigned to the terminal. For IPv4,
  340. a typical first rule is often "deny
  341. in ip! assigned"
  342. The sense of the match can be inverted by
  343. preceding an address with the not modifier (!),
  344. causing all other addresses to be matched
  345. instead. This does not affect the selection of
  346. port numbers.
  347. With the TCP, UDP and SCTP protocols, optional
  348. ports may be specified as:
  349. {port/port-port}[,ports[,...]]
  350. The '-' notation specifies a range of ports
  351. (including boundaries).
  352. Fragmented packets that have a non-zero offset
  353. (i.e., not the first fragment) will never match
  354. a rule that has one or more port
  355. specifications. See the frag option for
  356. details on matching fragmented packets.
  357. options:
  358. frag Match if the packet is a fragment and this is not
  359. the first fragment of the datagram. frag may not
  360. be used in conjunction with either tcpflags or
  361. TCP/UDP port specifications.
  362. ipoptions spec
  363. Match if the IP header contains the comma
  364. separated list of options specified in spec. The
  365. supported IP options are:
  366. ssrr (strict source route), lsrr (loose source
  367. route), rr (record packet route) and ts
  368. (timestamp). The absence of a particular option
  369. may be denoted with a '!'.
  370. tcpoptions spec
  371. Match if the TCP header contains the comma
  372. separated list of options specified in spec. The
  373. supported TCP options are:
  374. mss (maximum segment size), window (tcp window
  375. advertisement), sack (selective ack), ts (rfc1323
  376. timestamp) and cc (rfc1644 t/tcp connection
  377. count). The absence of a particular option may
  378. be denoted with a '!'.
  379. established
  380. TCP packets only. Match packets that have the RST
  381. or ACK bits set.
  382. setup TCP packets only. Match packets that have the SYN
  383. bit set but no ACK bit.
  384. tcpflags spec
  385. TCP packets only. Match if the TCP header
  386. contains the comma separated list of flags
  387. specified in spec. The supported TCP flags are:
  388. fin, syn, rst, psh, ack and urg. The absence of a
  389. particular flag may be denoted with a '!'. A rule
  390. that contains a tcpflags specification can never
  391. match a fragmented packet that has a non-zero
  392. offset. See the frag option for details on
  393. matching fragmented packets.
  394. icmptypes types
  395. ICMP packets only. Match if the ICMP type is in
  396. the list types. The list may be specified as any
  397. combination of ranges or individual types
  398. separated by commas. Both the numeric values and
  399. the symbolic values listed below can be used. The
  400. supported ICMP types are:
  401. echo reply (0), destination unreachable (3),
  402. source quench (4), redirect (5), echo request
  403. (8), router advertisement (9), router
  404. solicitation (10), time-to-live exceeded (11), IP
  405. header bad (12), timestamp request (13),
  406. timestamp reply (14), information request (15),
  407. information reply (16), address mask request (17)
  408. and address mask reply (18).
  409. </pre>
  410. There is one kind of packet that the access device MUST always
  411. discard, that is an IP fragment with a fragment offset of one. This
  412. is a valid packet, but it only has one use, to try to circumvent
  413. firewalls.
  414. An access device that is unable to interpret or apply a deny rule
  415. MUST terminate the session. An access device that is unable to
  416. interpret or apply a permit rule MAY apply a more restrictive
  417. rule. An access device MAY apply deny rules of its own before the
  418. supplied rules, for example to protect the access device owner's
  419. infrastructure.
  420. The rule syntax is a modified subset of ipfw(8) from FreeBSD, and the
  421. ipfw.c code may provide a useful base for implementations.
  422. */
  423. public static final DiameterAvpType IP_FILTER_RULE = new DiameterAvpType(_IP_FILTER_RULE);
  424. /**
  425. The QosFilterRule format is derived from the OctetString AVP Base
  426. Format. It uses the ASCII charset. Packets may be marked or
  427. metered based on the following information that is associated with
  428. it:
  429. <pre>
  430. Direction (in or out)
  431. Source and destination IP address (possibly masked)
  432. Protocol
  433. Source and destination port (lists or ranges)
  434. DSCP values (no mask or range)
  435. </pre>
  436. Rules for the appropriate direction are evaluated in order, with
  437. the first matched rule terminating the evaluation. Each packet is
  438. evaluated once. If no rule matches, the packet is treated as best
  439. effort. An access device that is unable to interpret or apply a
  440. QoS rule SHOULD NOT terminate the session.
  441. QoSFilterRule filters MUST follow the format:
  442. <pre>
  443. action dir proto from src to dst [options]
  444. tag - Mark packet with a specific DSCP
  445. [DIFFSERV]. The DSCP option MUST be
  446. included.
  447. meter - Meter traffic. The metering options
  448. MUST be included.
  449. dir The format is as described under IPFilterRule.
  450. proto The format is as described under
  451. IPFilterRule.
  452. src and dst The format is as described under
  453. IPFilterRule.
  454. </pre>
  455. */
  456. public static final DiameterAvpType QOS_FILTER_RULE = new DiameterAvpType(_QOS_FILTER_RULE);
  457. private Object readResolve() throws StreamCorruptedException {
  458. try {
  459. return fromInt(type);
  460. }
  461. catch (IllegalArgumentException iae) {
  462. throw new StreamCorruptedException("Invalid internal state found:" + type);
  463. }
  464. }
  465. private final int type;
  466. }