1/**
2 *
3 */
4package javax.jmdns.impl.constants;
5
6/**
7 * DNS result code.
8 *
9 * @author Arthur van Hoff, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Rick Blair
10 */
11public enum DNSResultCode {
12    /**
13     * Token
14     */
15    Unknown("Unknown", 65535),
16    /**
17     * No Error [RFC1035]
18     */
19    NoError("No Error", 0),
20    /**
21     * Format Error [RFC1035]
22     */
23    FormErr("Format Error", 1),
24    /**
25     * Server Failure [RFC1035]
26     */
27    ServFail("Server Failure", 2),
28    /**
29     * Non-Existent Domain [RFC1035]
30     */
31    NXDomain("Non-Existent Domain", 3),
32    /**
33     * Not Implemented [RFC1035]
34     */
35    NotImp("Not Implemented", 4),
36    /**
37     * Query Refused [RFC1035]
38     */
39    Refused("Query Refused", 5),
40    /**
41     * Name Exists when it should not [RFC2136]
42     */
43    YXDomain("Name Exists when it should not", 6),
44    /**
45     * RR Set Exists when it should not [RFC2136]
46     */
47    YXRRSet("RR Set Exists when it should not", 7),
48    /**
49     * RR Set that should exist does not [RFC2136]
50     */
51    NXRRSet("RR Set that should exist does not", 8),
52    /**
53     * Server Not Authoritative for zone [RFC2136]]
54     */
55    NotAuth("Server Not Authoritative for zone", 9),
56    /**
57     * Name not contained in zone [RFC2136]
58     */
59    NotZone("NotZone Name not contained in zone", 10),
60
61    ;
62
63    // 0 NoError No Error [RFC1035]
64    // 1 FormErr Format Error [RFC1035]
65    // 2 ServFail Server Failure [RFC1035]
66    // 3 NXDomain Non-Existent Domain [RFC1035]
67    // 4 NotImp Not Implemented [RFC1035]
68    // 5 Refused Query Refused [RFC1035]
69    // 6 YXDomain Name Exists when it should not [RFC2136]
70    // 7 YXRRSet RR Set Exists when it should not [RFC2136]
71    // 8 NXRRSet RR Set that should exist does not [RFC2136]
72    // 9 NotAuth Server Not Authoritative for zone [RFC2136]
73    // 10 NotZone Name not contained in zone [RFC2136]
74    // 11-15 Unassigned
75    // 16 BADVERS Bad OPT Version [RFC2671]
76    // 16 BADSIG TSIG Signature Failure [RFC2845]
77    // 17 BADKEY Key not recognized [RFC2845]
78    // 18 BADTIME Signature out of time window [RFC2845]
79    // 19 BADMODE Bad TKEY Mode [RFC2930]
80    // 20 BADNAME Duplicate key name [RFC2930]
81    // 21 BADALG Algorithm not supported [RFC2930]
82    // 22 BADTRUNC Bad Truncation [RFC4635]
83    // 23-3840 Unassigned
84    // 3841-4095 Reserved for Private Use [RFC5395]
85    // 4096-65534 Unassigned
86    // 65535 Reserved, can be allocated by Standards Action [RFC5395]
87
88    /**
89     * DNS Result Code types are encoded on the last 4 bits
90     */
91    final static int     RCode_MASK         = 0x0F;
92    /**
93     * DNS Extended Result Code types are encoded on the first 8 bits
94     */
95    final static int     ExtendedRCode_MASK = 0xFF;
96
97    private final String _externalName;
98
99    private final int    _index;
100
101    DNSResultCode(String name, int index) {
102        _externalName = name;
103        _index = index;
104    }
105
106    /**
107     * Return the string representation of this type
108     *
109     * @return String
110     */
111    public String externalName() {
112        return _externalName;
113    }
114
115    /**
116     * Return the numeric value of this type
117     *
118     * @return String
119     */
120    public int indexValue() {
121        return _index;
122    }
123
124    /**
125     * @param flags
126     * @return label
127     */
128    public static DNSResultCode resultCodeForFlags(int flags) {
129        int maskedIndex = flags & RCode_MASK;
130        for (DNSResultCode aCode : DNSResultCode.values()) {
131            if (aCode._index == maskedIndex) return aCode;
132        }
133        return Unknown;
134    }
135
136    public static DNSResultCode resultCodeForFlags(int flags, int extendedRCode) {
137        int maskedIndex = ((extendedRCode >> 28) & ExtendedRCode_MASK) | (flags & RCode_MASK);
138        for (DNSResultCode aCode : DNSResultCode.values()) {
139            if (aCode._index == maskedIndex) return aCode;
140        }
141        return Unknown;
142    }
143
144    @Override
145    public String toString() {
146        return this.name() + " index " + this.indexValue();
147    }
148
149}
150