1 17 package org.alfresco.filesys.smb.server; 18 19 import java.io.IOException ; 20 21 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol; 22 import org.alfresco.filesys.smb.PacketType; 23 import org.alfresco.filesys.smb.TransactBuffer; 24 import org.alfresco.filesys.util.DataBuffer; 25 import org.alfresco.filesys.util.DataPacker; 26 27 30 class SMBSrvTransPacket extends SMBTransPacket 31 { 32 33 35 private static final int StandardParamsResponse = 10; 36 37 39 protected static final int SetupOffsetResponse = PARAMWORDS + (StandardParamsResponse * 2); 40 41 46 47 public SMBSrvTransPacket(byte[] buf) 48 { 49 super(buf); 50 } 51 52 57 58 public SMBSrvTransPacket(int siz) 59 { 60 super(siz); 61 62 64 setMultiplexId(getNextMultiplexId()); 65 } 66 67 76 public final static void initTransactReply(SMBSrvPacket pkt, int prmCnt, int prmPos, int dataCnt, int dataPos) 77 { 78 79 81 pkt.setParameterCount(10); 82 83 85 pkt.setParameter(0, prmCnt); 86 pkt.setParameter(1, dataCnt); 87 88 90 pkt.setParameter(2, 0); 91 92 94 pkt.setParameter(3, prmCnt); 95 pkt.setParameter(4, prmPos - RFCNetBIOSProtocol.HEADER_LEN); 96 97 99 pkt.setParameter(5, 0); 100 101 103 pkt.setParameter(6, dataCnt); 104 pkt.setParameter(7, dataPos - RFCNetBIOSProtocol.HEADER_LEN); 105 106 108 pkt.setParameter(8, 0); 109 110 112 pkt.setParameter(9, 0); 113 } 114 115 121 protected final static int CalculateDataItemSize(String desc) 122 { 123 124 126 int len = 0; 127 int pos = 0; 128 129 while (pos < desc.length()) 130 { 131 132 134 char dtype = desc.charAt(pos++); 135 int dlen = 1; 136 137 139 if (pos < desc.length() && Character.isDigit(desc.charAt(pos))) 140 { 141 142 144 int numlen = 1; 145 int numpos = pos + 1; 146 while (numpos < desc.length() && Character.isDigit(desc.charAt(numpos++))) 147 numlen++; 148 149 151 dlen = Integer.parseInt(desc.substring(pos, pos + numlen)); 152 153 155 pos = numpos - 1; 156 } 157 158 160 switch (dtype) 161 { 162 163 165 case 'W': 166 len += 2; 167 break; 168 169 171 case 'D': 172 len += 4; 173 break; 174 175 177 case 'B': 178 len += dlen; 179 break; 180 181 183 case 'z': 184 len += 4; 185 break; 186 187 189 case '.': 190 len += dlen; 191 break; 192 193 195 case 'T': 196 len += 4; 197 break; 198 199 } 201 } 203 205 return len; 206 } 207 208 216 public final int getDataBlockOffset(int prmLen) 217 { 218 219 221 int pos = getParameterBlockOffset() + prmLen; 222 if ((pos & 0x01) != 0) 223 pos++; 224 return pos; 225 } 226 227 232 public final int getRxDataBlock() 233 { 234 return getParameter(12) + RFCNetBIOSProtocol.HEADER_LEN; 235 } 236 237 242 public final int getRxDataBlockLength() 243 { 244 return getParameter(11); 245 } 246 247 253 public final int getRxParameter(int prmIdx) 254 { 255 256 258 int pos = getRxParameterBlock(); 259 260 262 pos += prmIdx * 2; return DataPacker.getIntelShort(getBuffer(), pos); 264 } 265 266 271 272 public final int getRxParameterBlock() 273 { 274 275 278 return getParameter(10) + RFCNetBIOSProtocol.HEADER_LEN; 279 } 280 281 286 public final int getRxParameterBlockLength() 287 { 288 return getParameter(9); 289 } 290 291 296 public final int getRxParameterCount() 297 { 298 return getParameterCount() - STD_PARAMS; 299 } 300 301 307 public final int getRxParameterInt(int prmIdx) 308 { 309 310 312 int pos = getRxParameterBlock(); 313 314 316 pos += prmIdx * 2; return DataPacker.getIntelInt(getBuffer(), pos); 318 } 319 320 327 public final String getRxParameterString(int pos, boolean uni) 328 { 329 330 332 pos += getRxParameterBlock(); 333 334 336 byte[] buf = getBuffer(); 337 int len = (buf[pos++] & 0x00FF); 338 return DataPacker.getString(buf, pos, len, uni); 339 } 340 341 349 public final String getRxParameterString(int pos, int len, boolean uni) 350 { 351 352 354 pos += getRxParameterBlock(); 355 356 358 byte[] buf = getBuffer(); 359 return DataPacker.getString(buf, pos, len, uni); 360 } 361 362 367 public final String getRxTransactName() 368 { 369 370 372 if (getCommand() == PacketType.Transaction2) 373 return ""; 374 375 377 int pos = getByteOffset(); 378 return DataPacker.getString(getBuffer(), pos, getByteCount()); 379 } 380 381 386 public final int getSetupCount() 387 { 388 return getParameter(13) & 0xFF; 389 } 390 391 396 public final int getSetupOffset() 397 { 398 return WORDCNT + 29; } 400 401 407 408 public final int getSetupParameter(int idx) 409 { 410 411 413 if (idx >= getRxParameterCount()) 414 throw new java.lang.ArrayIndexOutOfBoundsException (); 415 416 418 return getParameter(idx + STD_PARAMS); 419 } 420 421 426 public final int getMaximumReturnParameterCount() 427 { 428 return getParameter(2); 429 } 430 431 436 public final int getMaximumReturnDataCount() 437 { 438 return getParameter(3); 439 } 440 441 446 public final int getMaximumReturnSetupCount() 447 { 448 return getParameter(4); 449 } 450 451 457 458 public final int getSetupParameterInt(int idx) 459 { 460 461 463 if (idx >= getRxParameterCount()) 464 throw new java.lang.ArrayIndexOutOfBoundsException (); 465 466 468 return getParameterLong(idx + STD_PARAMS); 469 } 470 471 476 public final void setTotalParameterCount(int cnt) 477 { 478 setParameter(0, cnt); 479 } 480 481 486 public final void setTotalDataCount(int cnt) 487 { 488 setParameter(1, cnt); 489 } 490 491 496 public final void setParameterBlockCount(int len) 497 { 498 setParameter(3, len); 499 } 500 501 506 public final void setParameterBlockOffset(int off) 507 { 508 setParameter(4, off != 0 ? off - RFCNetBIOSProtocol.HEADER_LEN : 0); 509 } 510 511 516 public final void setParameterBlockDisplacement(int disp) 517 { 518 setParameter(5, disp); 519 } 520 521 526 public final void setDataBlockCount(int len) 527 { 528 setParameter(6, len); 529 } 530 531 536 public final void setDataBlockOffset(int off) 537 { 538 setParameter(7, off != 0 ? off - RFCNetBIOSProtocol.HEADER_LEN : 0); 539 } 540 541 546 public final void setDataBlockDisplacement(int disp) 547 { 548 setParameter(8, disp); 549 } 550 551 558 protected final void doTransactionResponse(SMBSrvSession sess, TransactBuffer tbuf) throws IOException 559 { 560 561 563 setCommand(tbuf.isType()); 564 565 567 tbuf.setEndOfBuffer(); 568 569 DataBuffer setupBuf = tbuf.getSetupBuffer(); 570 DataBuffer paramBuf = tbuf.getParameterBuffer(); 571 DataBuffer dataBuf = tbuf.getDataBuffer(); 572 573 575 if (tbuf.hasSetupBuffer()) 576 setParameterCount(StandardParamsResponse + setupBuf.getLengthInWords()); 577 else 578 setParameterCount(StandardParamsResponse); 579 580 582 for (int i = 0; i < getParameterCount(); i++) 583 setParameter(i, 0); 584 585 587 int totParamLen = paramBuf != null ? paramBuf.getLength() : 0; 588 int totDataLen = dataBuf != null ? dataBuf.getLength() : 0; 589 590 592 setTotalParameterCount(totParamLen); 593 setTotalDataCount(totDataLen); 594 595 597 int availBuf = getAvailableLength(); 598 int clientLen = getAvailableLength(sess.getClientMaximumBufferSize()); 599 if (availBuf > clientLen) 600 availBuf = clientLen; 601 602 605 int plen = totParamLen; 606 int dlen = totDataLen; 607 608 if ((plen + dlen) > availBuf) 609 { 610 611 613 if (plen > 0) 614 { 615 616 618 if (plen <= availBuf) 619 { 620 621 624 if (dlen > 0) 625 dlen = availBuf - plen; 626 } 627 else 628 { 629 630 632 plen = availBuf / 2; 633 dlen = plen; 634 } 635 } 636 else if (dlen > availBuf) 637 { 638 639 641 dlen = availBuf; 642 } 643 } 644 645 647 setParameterBlockCount(plen); 648 setDataBlockCount(dlen); 649 650 652 if (setupBuf != null) 653 setupBuf.copyData(getBuffer(), SetupOffsetResponse); 654 655 657 int pos = DataPacker.wordAlign(getByteOffset()); 658 setPosition(pos); 659 660 662 setParameterBlockCount(plen); 663 setParameterBlockOffset(pos); 664 665 int packLen = -1; 666 667 if (paramBuf != null) 668 { 669 670 672 packLen = paramBuf.copyData(getBuffer(), pos, plen); 673 674 676 pos = DataPacker.longwordAlign(pos + packLen); 677 setPosition(pos); 678 } 679 680 682 setDataBlockCount(dlen); 683 setDataBlockOffset(pos); 684 685 687 if (dataBuf != null) 688 { 689 690 692 packLen = dataBuf.copyData(getBuffer(), pos, dlen); 693 694 696 setPosition(pos + packLen); 697 } 698 699 701 setByteCount(); 702 703 705 sess.sendResponseSMB(this); 706 707 709 availBuf = getAvailableLength(); 710 if (availBuf > clientLen) 711 availBuf = clientLen; 712 713 715 TransactBuffer rxBuf = null; 716 717 while ((paramBuf != null && paramBuf.getAvailableLength() > 0) 718 || (dataBuf != null && dataBuf.getAvailableLength() > 0)) 719 { 720 721 723 setCommand(tbuf.isType()); 724 725 727 plen = paramBuf != null ? paramBuf.getAvailableLength() : 0; 728 dlen = dataBuf != null ? dataBuf.getAvailableLength() : 0; 729 730 if ((plen + dlen) > availBuf) 731 { 732 733 735 if (plen > 0) 736 { 737 738 740 if (plen <= availBuf) 741 { 742 743 746 if (dlen > 0) 747 dlen = availBuf - plen; 748 } 749 else 750 { 751 752 754 plen = availBuf / 2; 755 dlen = plen; 756 } 757 } 758 else if (dlen > availBuf) 759 { 760 761 763 dlen = availBuf; 764 } 765 } 766 767 769 resetBytePointerAlign(); 770 771 packLen = -1; 772 pos = getPosition(); 773 774 if (plen > 0 && paramBuf != null) 775 { 776 777 779 setParameterBlockOffset(pos); 780 setParameterBlockCount(plen); 781 setParameterBlockDisplacement(paramBuf.getDisplacement()); 782 783 785 packLen = paramBuf.copyData(getBuffer(), pos, plen); 786 787 789 pos = DataPacker.wordAlign(pos + packLen); 790 setPosition(pos); 791 } 792 else 793 { 794 795 797 setParameterBlockCount(0); 798 setParameterBlockOffset(pos); 799 } 800 801 803 if (dlen > 0 && dataBuf != null) 804 { 805 806 808 setDataBlockOffset(pos); 809 setDataBlockCount(dlen); 810 setDataBlockDisplacement(dataBuf.getDisplacement()); 811 812 814 packLen = dataBuf.copyData(getBuffer(), pos, dlen); 815 816 818 setPosition(pos + packLen); 819 } 820 else 821 { 822 823 825 setDataBlockCount(0); 826 setDataBlockOffset(pos); 827 } 828 829 831 setByteCount(); 832 833 835 sess.sendResponseSMB(this); 836 } 837 } 838 } | Popular Tags |