Sends a message to a single server and waits for a response. No checking is done to ensure that the response is associated with the query. @param query The query to send. @return The response. @throws IOException An error occurred while sending or receiving.
(Message query)
| 224 | * @throws IOException An error occurred while sending or receiving. |
| 225 | */ |
| 226 | public Message |
| 227 | send(Message query) throws IOException { |
| 228 | if (Options.check("verbose")) |
| 229 | System.err.println("Sending to " + |
| 230 | address.getAddress().getHostAddress() + |
| 231 | ":" + address.getPort()); |
| 232 | |
| 233 | if (query.getHeader().getOpcode() == Opcode.QUERY) { |
| 234 | Record question = query.getQuestion(); |
| 235 | if (question != null && question.getType() == Type.AXFR) |
| 236 | return sendAXFR(query); |
| 237 | } |
| 238 | |
| 239 | query = (Message) query.clone(); |
| 240 | applyEDNS(query); |
| 241 | if (tsig != null) |
| 242 | tsig.apply(query, null); |
| 243 | |
| 244 | byte [] out = query.toWire(Message.MAXLENGTH); |
| 245 | int udpSize = maxUDPSize(query); |
| 246 | boolean tcp = false; |
| 247 | long endTime = System.currentTimeMillis() + timeoutValue; |
| 248 | do { |
| 249 | byte [] in; |
| 250 | |
| 251 | if (useTCP || out.length > udpSize) |
| 252 | tcp = true; |
| 253 | if (tcp) |
| 254 | in = TCPClient.sendrecv(localAddress, address, out, |
| 255 | endTime); |
| 256 | else |
| 257 | in = UDPClient.sendrecv(localAddress, address, out, |
| 258 | udpSize, endTime); |
| 259 | |
| 260 | /* |
| 261 | * Check that the response is long enough. |
| 262 | */ |
| 263 | if (in.length < Header.LENGTH) { |
| 264 | throw new WireParseException("invalid DNS header - " + |
| 265 | "too short"); |
| 266 | } |
| 267 | /* |
| 268 | * Check that the response ID matches the query ID. We want |
| 269 | * to check this before actually parsing the message, so that |
| 270 | * if there's a malformed response that's not ours, it |
| 271 | * doesn't confuse us. |
| 272 | */ |
| 273 | int id = ((in[0] & 0xFF) << 8) + (in[1] & 0xFF); |
| 274 | int qid = query.getHeader().getID(); |
| 275 | if (id != qid) { |
| 276 | String error = "invalid message id: expected " + qid + |
| 277 | "; got id " + id; |
| 278 | if (tcp) { |
| 279 | throw new WireParseException(error); |
| 280 | } else { |
| 281 | if (Options.check("verbose")) { |
| 282 | System.err.println(error); |
| 283 | } |
nothing calls this directly
no test coverage detected