-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathCookieOption.java
More file actions
125 lines (112 loc) · 3.09 KB
/
Copy pathCookieOption.java
File metadata and controls
125 lines (112 loc) · 3.09 KB
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
// SPDX-License-Identifier: BSD-3-Clause
package org.xbill.DNS;
import java.io.IOException;
import java.util.Optional;
import org.xbill.DNS.utils.base16;
/**
* Cookie EDNS0 Option.
*
* @see OPTRecord
* @see <a href="https://datatracker.ietf.org/doc/html/rfc7873">RFC 7873</a>
* @author Klaus Malorny
*/
public class CookieOption extends EDNSOption {
/** client cookie */
private byte[] clientCookie;
/** server cookie */
private byte[] serverCookie;
/** Default constructor for constructing instance from binary representation. */
CookieOption() {
super(EDNSOption.Code.COOKIE);
}
/**
* Constructor.
*
* @param clientCookie the client cookie, which must consist of eight bytes
*/
public CookieOption(byte[] clientCookie) {
this(clientCookie, null);
}
/**
* Constructor.
*
* @param clientCookie the client cookie, which must consist of eight bytes
* @param serverCookie the server cookie, which must consist of 8 to 32 bytes if present
*/
public CookieOption(byte[] clientCookie, byte[] serverCookie) {
this();
if (clientCookie == null) {
throw new IllegalArgumentException("client cookie must not be null");
}
if (clientCookie.length != 8) {
throw new IllegalArgumentException("client cookie must consist of eight bytes");
}
this.clientCookie = clientCookie;
if (serverCookie != null) {
int length = serverCookie.length;
if (length < 8 || length > 32) {
throw new IllegalArgumentException("server cookie must consist of 8 to 32 bytes");
}
}
this.serverCookie = serverCookie;
}
/**
* Returns the client cookie.
*
* @return the client cookie
*/
public byte[] getClientCookie() {
return clientCookie;
}
/**
* Returns the server cookie.
*
* @return the server cookie
*/
public Optional<byte[]> getServerCookie() {
return Optional.ofNullable(serverCookie);
}
/**
* Converts the wire format of an EDNS Option (the option data only) into the type-specific
* format.
*
* @param in The input stream.
*/
@Override
void optionFromWire(DNSInput in) throws IOException {
int length = in.remaining();
if (length < 8) {
throw new WireParseException("invalid length of client cookie");
}
clientCookie = in.readByteArray(8);
if (length > 8) {
if (length < 16 || length > 40) {
throw new WireParseException("invalid length of server cookie");
}
serverCookie = in.readByteArray();
}
}
/**
* Converts an EDNS Option (the type-specific option data only) into wire format.
*
* @param out The output stream.
*/
@Override
void optionToWire(DNSOutput out) {
out.writeByteArray(clientCookie);
if (serverCookie != null) {
out.writeByteArray(serverCookie);
}
}
/**
* Returns a string representation of the option parameters
*
* @return the string representation
*/
@Override
String optionToString() {
return serverCookie != null
? base16.toString(clientCookie) + " " + base16.toString(serverCookie)
: base16.toString(clientCookie);
}
}