NBNS.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 NBNSType.REGISTRATION_UNIQUE:
  53. prepareRegistrationPacket();
  54. break;
  55. case NBNSType.REGISTRATION_GROUP:
  56. prepareRegistrationPacket();
  57. break;
  58. case NBNSType.NAME_QUERY:
  59. prepareNameQueryPacket();
  60. break;
  61. case NBNSType.REGISTRATION_MSBROWSE:
  62. prepareRegistrationMsBrowse();
  63. break;
  64. default:
  65. }
  66. }
  67. private void prepareRegistrationPacket() {
  68. flags = new byte[]{0x29, 0x10};
  69. questions = new byte[]{0x00, 0x01};
  70. answerRRs = new byte[]{0x00, 0x00};
  71. authorityRRs = new byte[]{0x00, 0x00};
  72. additionalRRs = new byte[]{0x00, 0x01};
  73. payload = getPayload();
  74. additional = getAdditionalRecords();
  75. }
  76. private void prepareNameQueryPacket() {
  77. flags = new byte[] {0x01, 0x10};
  78. questions = new byte[]{0x00, 0x01};
  79. answerRRs = new byte[]{0x00, 0x00};
  80. authorityRRs = new byte[]{0x00, 0x00};
  81. additionalRRs = new byte[]{0x00, 0x00};
  82. payload = getPayload();
  83. }
  84. private void prepareRegistrationMsBrowse() {
  85. flags = new byte[]{0x29, 0x10};
  86. questions = new byte[]{0x00, 0x01};
  87. answerRRs = new byte[]{0x00, 0x00};
  88. authorityRRs = new byte[]{0x00, 0x00};
  89. additionalRRs = new byte[]{0x00, 0x01};
  90. payload = HelperUtils.concat(new byte[]{0x20, 0x41, 0x42, 0x41, 0x43}, name,
  91. new byte[]{0x41, 0x43, 0x41, 0x42, 0x00, 0x00, 0x20, 0x00, 0x01});
  92. additional = getAdditionalRecords();
  93. }
  94. private byte[] getPayload() {
  95. byte[] payload = NMBStringCoder.wrapNBNSName(this.name, service);
  96. byte[] type = {0x00, 0x20};
  97. byte[] nbnsclass = {0x00, 0x01};
  98. return HelperUtils.concat(payload, type, nbnsclass);
  99. }
  100. private void addressToBytes(String addrString) {
  101. String[] addrParts = addrString.split("\\.");
  102. addr = new byte[4];
  103. addr[0] = (byte)Integer.parseInt(addrParts[0]);
  104. addr[1] = (byte)Integer.parseInt(addrParts[1]);
  105. addr[2] = (byte)Integer.parseInt(addrParts[2]);
  106. addr[3] = (byte)Integer.parseInt(addrParts[3]);
  107. }
  108. public static byte[] getServiceBytes(int service) {
  109. switch (service) {
  110. case NBNSService.SERVER:
  111. return new byte[]{0x43, 0x41};
  112. case NBNSService.MESSENGER:
  113. return new byte[]{0x41, 0x44};
  114. case NBNSService.WORKSTATION:
  115. return new byte[]{0x41, 0x41};
  116. case NBNSService.BROWSER_ELECTION:
  117. return new byte[]{0x42, 0x4f};
  118. case NBNSService.LOCAL_MASTER_BROWSER:
  119. return new byte[]{0x42, 0x4e};
  120. default:
  121. return new byte[]{0x43, 0x41};
  122. }
  123. }
  124. private byte[] getAdditionalRecords() {
  125. byte[] name = {(byte) 0xc0, 0x0c};
  126. byte[] type = {0x00, 0x20};
  127. byte[] nbnsclass = {0x00, 0x01};
  128. byte[] timeToLive = {0x00, 0x00, 0x00, 0x00};
  129. byte[] nameFlags = ((this.type == NBNSType.REGISTRATION_UNIQUE) ? new byte[]{0x00, 0x00} : new byte[]{(byte) 0x80, 0x00});
  130. byte[] buffer = ByteBuffer.allocate(4).putInt(nameFlags.length + addr.length).array();
  131. byte[] length = {buffer[2], buffer[3]};
  132. return HelperUtils.concat(name, type, nbnsclass, timeToLive, length, nameFlags, addr);
  133. }
  134. public byte[] getNextPacket() {
  135. preparePacket();
  136. transactID[1] += 0x01;
  137. return getBytes();
  138. }
  139. public byte[] getBytes() {
  140. return HelperUtils.concat(transactID, flags, questions, answerRRs, authorityRRs, additionalRRs, payload, additional);
  141. }
  142. public void setName(String name) {
  143. this.name = name.getBytes();
  144. this.name = NMBStringCoder.encodeNBNSName(this.name);
  145. }
  146. public void setType(int type) {
  147. this.type = type;
  148. }
  149. public void setService(int service) {
  150. this.service = service;
  151. }
  152. public byte[] getAndIncTransactID() {
  153. byte[] response = new byte[2];
  154. response[0] = transactID[0];
  155. response[1] = transactID[1];
  156. transactID[1] += 0x01;
  157. return response;
  158. }
  159. public byte[] getAddr() {
  160. return addr;
  161. }
  162. public byte[] getName() {
  163. return name;
  164. }
  165. }