NBNS.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package de.tudarmstadt.informatik.hostage.protocol.SMBUtils;
  2. import java.nio.ByteBuffer;
  3. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  4. public class NBNS {
  5. private byte[] transactID;
  6. private byte[] flags;
  7. private byte[] questions;
  8. private byte[] answerRRs;
  9. private byte[] authorityRRs;
  10. private byte[] additionalRRs;
  11. private byte[] payload;
  12. private byte[] additional;
  13. private byte[] addr;
  14. private byte[] name;
  15. private int type;
  16. private int service;
  17. public NBNS(byte[] packet) {
  18. transactID = new byte[]{packet[0], packet[1]};
  19. flags = new byte[]{packet[2], packet[3]};
  20. questions = new byte[]{packet[4], packet[5]};
  21. answerRRs = new byte[]{packet[6], packet[7]};
  22. authorityRRs = new byte[]{packet[8], packet[9]};
  23. additionalRRs = new byte[]{packet[10], packet[11]};
  24. int length = 0;
  25. for (int i = 12; i < packet.length; i++, length++) {
  26. if (packet[i] == 0x01) {
  27. length++;
  28. break;
  29. }
  30. }
  31. payload = new byte[length];
  32. for (int i = 0; i < payload.length; i++) {
  33. payload[i] = packet[i+12];
  34. }
  35. additional = new byte[packet.length-12-length];
  36. for (int i = 0; i < additional.length; i++) {
  37. additional[i] = packet[i+12+length];
  38. }
  39. }
  40. public NBNS(byte[] transactID, String addr) {
  41. this.transactID = transactID;
  42. addressToBytes(addr);
  43. }
  44. public NBNS(byte[] transactID, int type, int service, String name, String addr) {
  45. this(transactID, addr);
  46. this.type = type;
  47. this.service = service;
  48. setName(name);
  49. }
  50. private void preparePacket() {
  51. switch (type) {
  52. case Type.REGISTRATION_UNIQUE:
  53. prepareRegistrationPacket();
  54. break;
  55. case Type.REGISTRATION_GROUP:
  56. prepareRegistrationPacket();
  57. break;
  58. case Type.NAME_QUERY:
  59. prepareNameQueryPacket();
  60. break;
  61. default:
  62. }
  63. }
  64. private void prepareRegistrationPacket() {
  65. flags = new byte[]{0x29, 0x10};
  66. questions = new byte[]{0x00, 0x01};
  67. answerRRs = new byte[]{0x00, 0x00};
  68. authorityRRs = new byte[]{0x00, 0x00};
  69. additionalRRs = new byte[]{0x00, 0x01};
  70. payload = getPayload();
  71. additional = getAdditionalRecords();
  72. }
  73. private void prepareNameQueryPacket() {
  74. flags = new byte[] {0x01, 0x10};
  75. questions = new byte[]{0x00, 0x01};
  76. answerRRs = new byte[]{0x00, 0x00};
  77. authorityRRs = new byte[]{0x00, 0x00};
  78. additionalRRs = new byte[]{0x00, 0x00};
  79. payload = getPayload();
  80. }
  81. private byte[] getPayload() {
  82. byte[] payload = NMBStringCoder.wrapNBNSName(this.name, service);
  83. byte[] type = {0x00, 0x20};
  84. byte[] nbnsclass = {0x00, 0x01};
  85. return HelperUtils.concat(payload, type, nbnsclass);
  86. }
  87. private void addressToBytes(String addrString) {
  88. String[] addrParts = addrString.split("\\.");
  89. addr = new byte[4];
  90. addr[0] = (byte)Integer.parseInt(addrParts[0]);
  91. addr[1] = (byte)Integer.parseInt(addrParts[1]);
  92. addr[2] = (byte)Integer.parseInt(addrParts[2]);
  93. addr[3] = (byte)Integer.parseInt(addrParts[3]);
  94. }
  95. public static byte[] getServiceBytes(int service) {
  96. switch (service) {
  97. case Service.SERVER:
  98. return new byte[]{0x43, 0x41};
  99. case Service.MESSENGER:
  100. return new byte[]{0x41, 0x44};
  101. case Service.WORKSTATION:
  102. return new byte[]{0x41, 0x41};
  103. case Service.BROWSER_ELECTION:
  104. return new byte[]{0x42, 0x4f};
  105. case Service.LOCAL_MASTER_BROWSER:
  106. return new byte[]{0x42, 0x4e};
  107. default:
  108. return new byte[]{0x43, 0x41};
  109. }
  110. }
  111. private byte[] getAdditionalRecords() {
  112. byte[] name = {(byte) 0xc0, 0x0c};
  113. byte[] type = {0x00, 0x20};
  114. byte[] nbnsclass = {0x00, 0x01};
  115. byte[] timeToLive = {0x00, 0x00, 0x00, 0x00};
  116. byte[] nameFlags = ((this.type == Type.REGISTRATION_UNIQUE) ? new byte[]{0x00, 0x00} : new byte[]{0x08, 0x00});
  117. byte[] buffer = ByteBuffer.allocate(4).putInt(nameFlags.length + addr.length).array();
  118. byte[] length = {buffer[2], buffer[3]};
  119. return HelperUtils.concat(name, type, nbnsclass, timeToLive, length, nameFlags, addr);
  120. }
  121. public byte[] getNextPacket() {
  122. preparePacket();
  123. transactID[1] += 0x01;
  124. return getBytes();
  125. }
  126. public byte[] getBytes() {
  127. return HelperUtils.concat(transactID, flags, questions, answerRRs, authorityRRs, additionalRRs, payload, additional);
  128. }
  129. public void setName(String name) {
  130. this.name = name.getBytes();
  131. this.name = NMBStringCoder.encodeNBNSName(this.name);
  132. }
  133. public void setType(int type) {
  134. this.type = type;
  135. }
  136. public void setService(int service) {
  137. this.service = service;
  138. }
  139. public byte[] getAndIncrementTransactID() {
  140. byte[] response = new byte[2];
  141. response[0] = transactID[0];
  142. response[1] = transactID[1];
  143. transactID[1] += 0x01;
  144. return response;
  145. }
  146. public byte[] getAddr() {
  147. return addr;
  148. }
  149. public byte[] getName() {
  150. return name;
  151. }
  152. }