|
| 1 | +package io.quarkus.vertx.http.proxy.fakedns; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +/* |
| 11 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 12 | + * or more contributor license agreements. See the NOTICE file |
| 13 | + * distributed with this work for additional information |
| 14 | + * regarding copyright ownership. The ASF licenses this file |
| 15 | + * to you under the Apache License, Version 2.0 (the |
| 16 | + * "License"); you may not use this file except in compliance |
| 17 | + * with the License. You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, |
| 22 | + * software distributed under the License is distributed on an |
| 23 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 24 | + * KIND, either express or implied. See the License for the |
| 25 | + * specific language governing permissions and limitations |
| 26 | + * under the License. |
| 27 | + * |
| 28 | + */ |
| 29 | +import org.apache.directory.server.dns.io.encoder.*; |
| 30 | +import org.apache.directory.server.dns.messages.DnsMessage; |
| 31 | +import org.apache.directory.server.dns.messages.MessageType; |
| 32 | +import org.apache.directory.server.dns.messages.OpCode; |
| 33 | +import org.apache.directory.server.dns.messages.QuestionRecord; |
| 34 | +import org.apache.directory.server.dns.messages.RecordType; |
| 35 | +import org.apache.directory.server.dns.messages.ResourceRecord; |
| 36 | +import org.apache.directory.server.dns.messages.ResponseCode; |
| 37 | +import org.apache.directory.server.i18n.I18n; |
| 38 | +import org.apache.mina.core.buffer.IoBuffer; |
| 39 | +import org.slf4j.Logger; |
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +/** |
| 43 | + * An encoder for DNS messages. The primary usage of the DnsMessageEncoder is |
| 44 | + * to call the <code>encode(ByteBuffer, DnsMessage)</code> method which will |
| 45 | + * write the message to the outgoing ByteBuffer according to the DnsMessage |
| 46 | + * encoding in RFC-1035. |
| 47 | + * |
| 48 | + * @author <a href="mailto:[email protected]">Apache Directory Project</a> |
| 49 | + * @version $Rev$, $Date$ |
| 50 | + */ |
| 51 | +public class DnsMessageEncoder { |
| 52 | + /** the log for this class */ |
| 53 | + private static final Logger log = LoggerFactory.getLogger(DnsMessageEncoder.class); |
| 54 | + |
| 55 | + /** |
| 56 | + * A Hashed Adapter mapping record types to their encoders. |
| 57 | + */ |
| 58 | + private static final Map<RecordType, RecordEncoder> DEFAULT_ENCODERS; |
| 59 | + |
| 60 | + static { |
| 61 | + Map<RecordType, RecordEncoder> map = new HashMap<RecordType, RecordEncoder>(); |
| 62 | + |
| 63 | + map.put(RecordType.SOA, new StartOfAuthorityRecordEncoder()); |
| 64 | + map.put(RecordType.A, new AddressRecordEncoder()); |
| 65 | + map.put(RecordType.NS, new NameServerRecordEncoder()); |
| 66 | + map.put(RecordType.CNAME, new CanonicalNameRecordEncoder()); |
| 67 | + map.put(RecordType.PTR, new PointerRecordEncoder()); |
| 68 | + map.put(RecordType.MX, new MailExchangeRecordEncoder()); |
| 69 | + map.put(RecordType.SRV, new ServerSelectionRecordEncoder()); |
| 70 | + map.put(RecordType.TXT, new TextRecordEncoder()); |
| 71 | + map.put(RecordType.DNAME, new DnameRecordEncoder()); |
| 72 | + |
| 73 | + DEFAULT_ENCODERS = Collections.unmodifiableMap(map); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Encodes the {@link DnsMessage} into the {@link IoBuffer}. |
| 78 | + * |
| 79 | + * @param byteBuffer |
| 80 | + * @param message |
| 81 | + */ |
| 82 | + public void encode(IoBuffer byteBuffer, DnsMessage message) { |
| 83 | + byteBuffer.putShort((short) message.getTransactionId()); |
| 84 | + |
| 85 | + byte header = (byte) 0x00; |
| 86 | + header |= encodeMessageType(message.getMessageType()); |
| 87 | + header |= encodeOpCode(message.getOpCode()); |
| 88 | + header |= encodeAuthoritativeAnswer(message.isAuthoritativeAnswer()); |
| 89 | + header |= encodeTruncated(message.isTruncated()); |
| 90 | + header |= encodeRecursionDesired(message.isRecursionDesired()); |
| 91 | + byteBuffer.put(header); |
| 92 | + |
| 93 | + header = (byte) 0x00; |
| 94 | + header |= encodeRecursionAvailable(message.isRecursionAvailable()); |
| 95 | + header |= encodeResponseCode(message.getResponseCode()); |
| 96 | + byteBuffer.put(header); |
| 97 | + |
| 98 | + byteBuffer |
| 99 | + .putShort((short) (message.getQuestionRecords() != null ? message.getQuestionRecords().size() : 0)); |
| 100 | + byteBuffer.putShort((short) (message.getAnswerRecords() != null ? message.getAnswerRecords().size() : 0)); |
| 101 | + byteBuffer.putShort((short) (message.getAuthorityRecords() != null ? message.getAuthorityRecords().size() |
| 102 | + : 0)); |
| 103 | + byteBuffer.putShort((short) (message.getAdditionalRecords() != null ? message.getAdditionalRecords().size() |
| 104 | + : 0)); |
| 105 | + |
| 106 | + putQuestionRecords(byteBuffer, message.getQuestionRecords()); |
| 107 | + putResourceRecords(byteBuffer, message.getAnswerRecords()); |
| 108 | + putResourceRecords(byteBuffer, message.getAuthorityRecords()); |
| 109 | + putResourceRecords(byteBuffer, message.getAdditionalRecords()); |
| 110 | + } |
| 111 | + |
| 112 | + private void putQuestionRecords(IoBuffer byteBuffer, List<QuestionRecord> questions) { |
| 113 | + if (questions == null) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + QuestionRecordEncoder encoder = new QuestionRecordEncoder(); |
| 118 | + |
| 119 | + Iterator<QuestionRecord> it = questions.iterator(); |
| 120 | + |
| 121 | + while (it.hasNext()) { |
| 122 | + QuestionRecord question = it.next(); |
| 123 | + encoder.put(byteBuffer, question); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void putResourceRecords(IoBuffer byteBuffer, List<ResourceRecord> records) { |
| 128 | + if (records == null) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + Iterator<ResourceRecord> it = records.iterator(); |
| 133 | + |
| 134 | + while (it.hasNext()) { |
| 135 | + ResourceRecord record = it.next(); |
| 136 | + |
| 137 | + try { |
| 138 | + put(byteBuffer, record); |
| 139 | + } catch (IOException ioe) { |
| 140 | + log.error(ioe.getLocalizedMessage(), ioe); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void put(IoBuffer byteBuffer, ResourceRecord record) throws IOException { |
| 146 | + RecordType type = record.getRecordType(); |
| 147 | + |
| 148 | + RecordEncoder encoder = DEFAULT_ENCODERS.get(type); |
| 149 | + |
| 150 | + if (encoder == null) { |
| 151 | + throw new IOException(I18n.err(I18n.ERR_597, type)); |
| 152 | + } |
| 153 | + |
| 154 | + encoder.put(byteBuffer, record); |
| 155 | + } |
| 156 | + |
| 157 | + private byte encodeMessageType(MessageType messageType) { |
| 158 | + byte oneBit = (byte) (messageType.convert() & 0x01); |
| 159 | + return (byte) (oneBit << 7); |
| 160 | + } |
| 161 | + |
| 162 | + private byte encodeOpCode(OpCode opCode) { |
| 163 | + byte fourBits = (byte) (opCode.convert() & 0x0F); |
| 164 | + return (byte) (fourBits << 3); |
| 165 | + } |
| 166 | + |
| 167 | + private byte encodeAuthoritativeAnswer(boolean authoritative) { |
| 168 | + if (authoritative) { |
| 169 | + return (byte) ((byte) 0x01 << 2); |
| 170 | + } |
| 171 | + return (byte) 0; |
| 172 | + } |
| 173 | + |
| 174 | + private byte encodeTruncated(boolean truncated) { |
| 175 | + if (truncated) { |
| 176 | + return (byte) ((byte) 0x01 << 1); |
| 177 | + } |
| 178 | + return 0; |
| 179 | + } |
| 180 | + |
| 181 | + private byte encodeRecursionDesired(boolean recursionDesired) { |
| 182 | + if (recursionDesired) { |
| 183 | + return (byte) 0x01; |
| 184 | + } |
| 185 | + return 0; |
| 186 | + } |
| 187 | + |
| 188 | + private byte encodeRecursionAvailable(boolean recursionAvailable) { |
| 189 | + if (recursionAvailable) { |
| 190 | + return (byte) ((byte) 0x01 << 7); |
| 191 | + } |
| 192 | + return 0; |
| 193 | + } |
| 194 | + |
| 195 | + private byte encodeResponseCode(ResponseCode responseCode) { |
| 196 | + return (byte) (responseCode.convert() & 0x0F); |
| 197 | + } |
| 198 | +} |
0 commit comments