本文整理汇总了Java中org.xbill.DNS.Message.toWire方法的典型用法代码示例。如果您正苦于以下问题:Java Message.toWire方法的具体用法?Java Message.toWire怎么用?Java Message.toWire使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.xbill.DNS.Message
的用法示例。
在下文中一共展示了Message.toWire方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.xbill.DNS.Message; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
try (DatagramSocket socket = new DatagramSocket()) {
Message message = new Message();
Header header = message.getHeader();
header.setOpcode(Opcode.QUERY);
header.setID(1);
header.setRcode(Rcode.NOERROR);
header.setFlag(Flags.RD);
message.addRecord(Record.newRecord(new Name("www.xqbase.com."), Type.A, DClass.IN), Section.QUESTION);
byte[] data = message.toWire();
DatagramPacket packet = new DatagramPacket(data, data.length, new InetSocketAddress("localhost", 53));
socket.send(packet);
data = new byte[65536];
packet = new DatagramPacket(data, data.length);
socket.setSoTimeout(2000);
socket.receive(packet);
Message response = new Message(Bytes.left(data, packet.getLength()));
System.out.println(response);
}
}
示例2: writeMessage
import org.xbill.DNS.Message; //导入方法依赖的package包/类
private void writeMessage(OutputStream outputStream, Message message) throws IOException {
byte[] messageData = message.toWire();
checkArgument(
messageData.length <= MESSAGE_MAXIMUM_LENGTH,
"DNS request message larger than maximum of %s: %s",
MESSAGE_MAXIMUM_LENGTH,
messageData.length);
ByteBuffer buffer = ByteBuffer.allocate(messageData.length + MESSAGE_LENGTH_FIELD_BYTES);
buffer.putShort((short) messageData.length);
buffer.put(messageData);
outputStream.write(buffer.array());
}
示例3: messageToBytesWithLength
import org.xbill.DNS.Message; //导入方法依赖的package包/类
private byte[] messageToBytesWithLength(Message message) {
byte[] bytes = message.toWire();
ByteBuffer buffer =
ByteBuffer.allocate(bytes.length + DnsMessageTransport.MESSAGE_LENGTH_FIELD_BYTES);
buffer.putShort((short) bytes.length);
buffer.put(bytes);
return buffer.array();
}
示例4: sendQuery
import org.xbill.DNS.Message; //导入方法依赖的package包/类
protected static void sendQuery(Connection connection, Message query) {
if (connection != null) {
byte[] out = query.toWire(Message.MAXLENGTH);
connection.send(out);
}
}
示例5: process
import org.xbill.DNS.Message; //导入方法依赖的package包/类
/**
* Processes a DNS query.
*
* @param sock
* Socket to listen to
*/
private void process(DatagramSocket sock) {
try {
byte[] in = new byte[UDP_SIZE];
// Read the question
DatagramPacket indp = new DatagramPacket(in, UDP_SIZE);
indp.setLength(UDP_SIZE);
sock.receive(indp);
Message msg = new Message(in);
Header header = msg.getHeader();
Record question = msg.getQuestion();
// Prepare a response
Message response = new Message(header.getID());
response.getHeader().setFlag(Flags.QR);
response.addRecord(question, Section.QUESTION);
Name name = question.getName();
boolean hasRecords = false;
String txt = txtRecords.get(name.toString(true));
if (question.getType() == Type.TXT && txt != null) {
response.addRecord(new TXTRecord(name, DClass.IN, TTL, txt), Section.ANSWER);
hasRecords = true;
LOG.info("dns-01: {} {} IN TXT \"{}\"", name, TTL, txt);
}
InetAddress a = aRecords.get(name.toString(true));
if (question.getType() == Type.A && a != null) {
response.addRecord(new ARecord(name, DClass.IN, TTL, a), Section.ANSWER);
hasRecords = true;
LOG.info("dns-01: {} {} IN A {}", name, TTL, a.getHostAddress());
}
if (!hasRecords) {
response.getHeader().setRcode(Rcode.NXDOMAIN);
LOG.warn("dns-01: Cannot answer: {}", question);
}
// Send the response
byte[] resp = response.toWire();
DatagramPacket outdp = new DatagramPacket(resp, resp.length, indp.getAddress(), indp.getPort());
sock.send(outdp);
} catch (Exception ex) {
LOG.error("Failed to process query", ex);
}
}