summaryrefslogtreecommitdiff
path: root/src/javax/jmdns/impl/constants/DNSRecordClass.java
blob: 9fa37457c857f8be7c2aa81e074e89245cd31009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
/**
 *
 */
package javax.jmdns.impl.constants;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * DNS Record Class
 * 
 * @author Arthur van Hoff, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Rick Blair
 */
public enum DNSRecordClass {
    /**
     *
     */
    CLASS_UNKNOWN("?", 0),
    /**
     * static final Internet
     */
    CLASS_IN("in", 1),
    /**
     * CSNET
     */
    CLASS_CS("cs", 2),
    /**
     * CHAOS
     */
    CLASS_CH("ch", 3),
    /**
     * Hesiod
     */
    CLASS_HS("hs", 4),
    /**
     * Used in DNS UPDATE [RFC 2136]
     */
    CLASS_NONE("none", 254),
    /**
     * Not a DNS class, but a DNS query class, meaning "all classes"
     */
    CLASS_ANY("any", 255);

    private static Logger       logger       = Logger.getLogger(DNSRecordClass.class.getName());

    /**
     * Multicast DNS uses the bottom 15 bits to identify the record class...<br/>
     * Except for pseudo records like OPT.
     */
    public static final int     CLASS_MASK   = 0x7FFF;

    /**
     * For answers the top bit indicates that all other cached records are now invalid.<br/>
     * For questions it indicates that we should send a unicast response.
     */
    public static final int     CLASS_UNIQUE = 0x8000;

    /**
     *
     */
    public static final boolean UNIQUE       = true;

    /**
     *
     */
    public static final boolean NOT_UNIQUE   = false;

    private final String        _externalName;

    private final int           _index;

    DNSRecordClass(String name, int index) {
        _externalName = name;
        _index = index;
    }

    /**
     * Return the string representation of this type
     * 
     * @return String
     */
    public String externalName() {
        return _externalName;
    }

    /**
     * Return the numeric value of this type
     * 
     * @return String
     */
    public int indexValue() {
        return _index;
    }

    /**
     * Checks if the class is unique
     * 
     * @param index
     * @return <code>true</code> is the class is unique, <code>false</code> otherwise.
     */
    public boolean isUnique(int index) {
        return (this != CLASS_UNKNOWN) && ((index & CLASS_UNIQUE) != 0);
    }

    /**
     * @param name
     * @return class for name
     */
    public static DNSRecordClass classForName(String name) {
        if (name != null) {
            String aName = name.toLowerCase();
            for (DNSRecordClass aClass : DNSRecordClass.values()) {
                if (aClass._externalName.equals(aName)) return aClass;
            }
        }
        logger.log(Level.WARNING, "Could not find record class for name: " + name);
        return CLASS_UNKNOWN;
    }

    /**
     * @param index
     * @return class for name
     */
    public static DNSRecordClass classForIndex(int index) {
        int maskedIndex = index & CLASS_MASK;
        for (DNSRecordClass aClass : DNSRecordClass.values()) {
            if (aClass._index == maskedIndex) return aClass;
        }
        logger.log(Level.WARNING, "Could not find record class for index: " + index);
        return CLASS_UNKNOWN;
    }

    @Override
    public String toString() {
        return this.name() + " index " + this.indexValue();
    }

}