1 17 package org.alfresco.filesys.netbios; 18 19 import java.io.IOException ; 20 import java.net.DatagramPacket ; 21 import java.net.DatagramSocket ; 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 import org.alfresco.filesys.util.DataPacker; 26 27 30 public class NetBIOSDatagram 31 { 32 34 public static final int DIRECT_UNIQUE = 0x10; 35 public static final int DIRECT_GROUP = 0x11; 36 public static final int BROADCAST = 0x12; 37 public static final int DATAGRAM_ERROR = 0x13; 38 public static final int DATAGRAM_QUERY = 0x14; 39 public static final int POSITIVE_RESP = 0x15; 40 public static final int NEGATIVE_RESP = 0x16; 41 42 44 public static final int FLG_MOREFRAGMENTS = 0x01; 45 public static final int FLG_FIRSTPKT = 0x02; 46 47 49 public static final int DEFBUFSIZE = 4096; 50 51 53 public static final int NB_MSGTYPE = 0; 54 public static final int NB_FLAGS = 1; 55 public static final int NB_DATAGRAMID = 2; 56 public static final int NB_SOURCEIP = 4; 57 public static final int NB_SOURCEPORT = 8; 58 public static final int NB_DATAGRAMLEN = 10; 59 public static final int NB_PKTOFFSET = 12; 60 public static final int NB_FROMNAME = 14; 61 public static final int NB_TONAME = 48; 62 public static final int NB_USERDATA = 82; 63 64 public static final int NB_MINLENGTH = 82; 65 public static final int NB_MINSMBLEN = 100; 66 67 69 protected byte[] m_buf; 70 71 73 private static int m_nextId; 74 75 78 79 public NetBIOSDatagram() 80 { 81 82 84 m_buf = new byte[DEFBUFSIZE]; 85 } 86 87 92 public NetBIOSDatagram(byte[] pkt) 93 { 94 m_buf = pkt; 95 } 96 97 102 public NetBIOSDatagram(int bufSize) 103 { 104 m_buf = new byte[bufSize]; 105 } 106 107 110 public final static synchronized int getNextDatagramId() 111 { 112 113 115 return m_nextId++; 116 } 117 118 123 public final byte[] getBuffer() 124 { 125 return m_buf; 126 } 127 128 133 public final int getDatagramId() 134 { 135 return DataPacker.getIntelShort(m_buf, NB_DATAGRAMID); 136 } 137 138 143 public final NetBIOSName getDestinationName() 144 { 145 146 148 String name = NetBIOSSession.DecodeName(m_buf, NB_TONAME + 1); 149 if (name != null) 150 { 151 152 154 NetBIOSName nbName = new NetBIOSName(name.substring(0, 14), name.charAt(15), false); 155 if (getMessageType() == DIRECT_GROUP) 156 nbName.setGroup(true); 157 return nbName; 158 } 159 return null; 160 } 161 162 167 public final int getFlags() 168 { 169 return m_buf[NB_FLAGS] & 0xFF; 170 } 171 172 177 public final int getLength() 178 { 179 return DataPacker.getShort(m_buf, NB_DATAGRAMLEN); 180 } 181 182 187 public final int getDataLength() 188 { 189 return getLength() - NB_USERDATA; 190 } 191 192 197 public final int getMessageType() 198 { 199 return m_buf[NB_MSGTYPE] & 0xFF; 200 } 201 202 207 public final byte[] getSourceIPAddress() 208 { 209 210 212 byte[] ipaddr = new byte[4]; 213 214 216 for (int i = 0; i < 4; i++) 217 ipaddr[i] = m_buf[NB_SOURCEIP + i]; 218 219 221 return ipaddr; 222 } 223 224 229 public final String getSourceAddress() 230 { 231 232 234 byte[] addr = getSourceIPAddress(); 235 236 238 StringBuffer addrStr = new StringBuffer (); 239 240 addrStr.append(addr[0]); 241 addrStr.append("."); 242 addrStr.append(addr[1]); 243 addrStr.append("."); 244 addrStr.append(addr[2]); 245 addrStr.append("."); 246 addrStr.append(addr[3]); 247 248 return addrStr.toString(); 249 } 250 251 256 public final NetBIOSName getSourceName() 257 { 258 259 261 String name = NetBIOSSession.DecodeName(m_buf, NB_FROMNAME + 1); 262 263 265 if (name != null) 266 { 267 268 270 NetBIOSName nbName = new NetBIOSName(name.substring(0, 14), name.charAt(15), false); 271 return nbName; 272 } 273 return null; 274 } 275 276 281 public final int getSourcePort() 282 { 283 return DataPacker.getIntelShort(m_buf, NB_SOURCEPORT); 284 } 285 286 291 public final boolean isSMBData() 292 { 293 if (m_buf[NB_USERDATA] == (byte) 0xFF && m_buf[NB_USERDATA + 1] == (byte) 'S' 294 && m_buf[NB_USERDATA + 2] == (byte) 'M' && m_buf[NB_USERDATA + 3] == (byte) 'B' 295 && getLength() >= NB_MINSMBLEN) 296 return true; 297 return false; 298 } 299 300 305 306 public final String getMessageTypeString() 307 { 308 309 311 String typ = null; 312 313 switch (getMessageType()) 314 { 315 case DIRECT_GROUP: 316 typ = "DIRECT GROUP"; 317 break; 318 case DIRECT_UNIQUE: 319 typ = "DIRECT UNIQUE"; 320 break; 321 case DATAGRAM_ERROR: 322 typ = "DATAGRAM ERROR"; 323 break; 324 case DATAGRAM_QUERY: 325 typ = "DATAGRAM QUERY"; 326 break; 327 case BROADCAST: 328 typ = "BROADCAST"; 329 break; 330 case POSITIVE_RESP: 331 typ = "POSITIVE RESP"; 332 break; 333 case NEGATIVE_RESP: 334 typ = "NEGATIVE RESP"; 335 break; 336 default: 337 typ = "UNKNOWN"; 338 break; 339 } 340 341 343 return typ; 344 } 345 346 362 public final void SendDatagram(int dgramTyp, String fromName, char fromNameType, String toName, char toNameType, 363 byte[] userData, int userLen, int userOff, InetAddress addr, int port) throws IOException , 364 UnknownHostException 365 { 366 367 369 setMessageType(dgramTyp); 370 setSourceName(fromName, fromNameType); 371 setDestinationName(toName, toNameType); 372 setSourcePort(RFCNetBIOSProtocol.DATAGRAM); 373 setSourceIPAddress(InetAddress.getLocalHost().getAddress()); 374 setFlags(FLG_FIRSTPKT); 375 376 if (m_nextId == 0) 377 m_nextId = (int) (System.currentTimeMillis() & 0x7FFF); 378 setDatagramId(m_nextId++); 379 380 382 setLength(userLen + NB_USERDATA); 383 setUserData(userData, userLen, userOff); 384 385 387 NetBIOSDatagramSocket nbSocket = NetBIOSDatagramSocket.getInstance(); 388 nbSocket.sendDatagram(this, addr, port); 389 } 390 391 405 public final void SendDatagram(int dgramTyp, String fromName, char fromNameType, String toName, char toNameType, 406 byte[] userData, int userLen, int userOff) throws IOException , UnknownHostException 407 { 408 409 411 setMessageType(dgramTyp); 412 setSourceName(fromName, fromNameType); 413 setDestinationName(toName, toNameType); 414 setSourcePort(RFCNetBIOSProtocol.DATAGRAM); 415 setSourceIPAddress(InetAddress.getLocalHost().getAddress()); 416 setFlags(FLG_FIRSTPKT); 417 418 if (m_nextId == 0) 419 m_nextId = (int) (System.currentTimeMillis() & 0x7FFF); 420 setDatagramId(m_nextId++); 421 422 424 setLength(userLen + NB_USERDATA); 425 setUserData(userData, userLen, userOff); 426 427 429 NetBIOSDatagramSocket nbSocket = NetBIOSDatagramSocket.getInstance(); 430 nbSocket.sendBroadcastDatagram(this); 431 } 432 433 446 public final void SendDatagram(int dgramTyp, String fromName, String toName, byte[] userData, int userLen) 447 throws IOException , UnknownHostException 448 { 449 450 452 SendDatagram(dgramTyp, fromName, NetBIOSName.FileServer, toName, NetBIOSName.FileServer, userData, userLen, 0); 453 } 454 455 469 public final void SendDatagram(int dgramTyp, DatagramSocket sock, String fromName, char fromNameType, 470 String toName, char toNameType, byte[] userData, int userLen, int userOff) throws IOException 471 { 472 473 475 setMessageType(dgramTyp); 476 setSourceName(fromName, fromNameType); 477 setDestinationName(toName, toNameType); 478 setSourcePort(RFCNetBIOSProtocol.DATAGRAM); 479 setSourceIPAddress(InetAddress.getLocalHost().getAddress()); 480 setFlags(FLG_FIRSTPKT); 481 482 if (m_nextId == 0) 483 m_nextId = (int) (System.currentTimeMillis() & 0x7FFF); 484 setDatagramId(m_nextId++); 485 486 488 setLength(userLen + NB_USERDATA); 489 setUserData(userData, userLen, userOff); 490 491 493 InetAddress destAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(null)); 494 DatagramPacket dgram = new DatagramPacket (m_buf, userLen + NB_USERDATA, destAddr, RFCNetBIOSProtocol.DATAGRAM); 495 496 498 500 502 sock.send(dgram); 503 } 504 505 514 public final void SendDatagram(int dgramTyp, DatagramSocket sock, String fromName, String toName, byte[] userData, 515 int userLen) throws IOException 516 { 517 518 520 SendDatagram(dgramTyp, sock, fromName, NetBIOSName.FileServer, toName, NetBIOSName.FileServer, userData, 521 userLen, 0); 522 } 523 524 529 public final void setDatagramId(int id) 530 { 531 DataPacker.putIntelShort(id, m_buf, NB_DATAGRAMID); 532 } 533 534 539 public final void setDestinationName(String name) 540 { 541 setDestinationName(name, NetBIOSName.FileServer); 542 } 543 544 549 public final void setDestinationName(String name, char typ) 550 { 551 552 554 NetBIOSSession.EncodeName(name, typ, m_buf, NB_TONAME); 555 } 556 557 562 public final void setFlags(int flg) 563 { 564 m_buf[NB_FLAGS] = (byte) (flg & 0xFF); 565 } 566 567 572 public final void setLength(int len) 573 { 574 DataPacker.putShort((short) len, m_buf, NB_DATAGRAMLEN); 575 } 576 577 582 public final void setMessageType(int msg) 583 { 584 m_buf[NB_MSGTYPE] = (byte) (msg & 0xFF); 585 } 586 587 592 public final void setSourceIPAddress(byte[] ipaddr) 593 { 594 595 597 for (int i = 0; i < 4; i++) 598 m_buf[NB_SOURCEIP + i] = ipaddr[i]; 599 } 600 601 606 public final void setSourceName(String name) 607 { 608 609 611 NetBIOSSession.EncodeName(name, NetBIOSName.FileServer, m_buf, NB_FROMNAME); 612 } 613 614 619 public final void setSourceName(String name, char typ) 620 { 621 622 624 NetBIOSSession.EncodeName(name, typ, m_buf, NB_FROMNAME); 625 } 626 627 632 public final void setSourcePort(int port) 633 { 634 DataPacker.putShort((short) port, m_buf, NB_SOURCEPORT); 635 } 636 637 643 public final void setUserData(byte[] buf, int len) 644 { 645 646 648 System.arraycopy(buf, 0, m_buf, NB_USERDATA, len); 649 } 650 651 658 public final void setUserData(byte[] buf, int len, int off) 659 { 660 661 663 System.arraycopy(buf, off, m_buf, NB_USERDATA, len); 664 } 665 666 669 protected final void CommonInit() 670 { 671 } 672 } | Popular Tags |