1 17 package org.alfresco.filesys.netbios; 18 19 import java.net.InetAddress ; 20 import java.util.StringTokenizer ; 21 import java.util.Vector ; 22 23 import org.alfresco.filesys.util.IPAddress; 24 25 28 public class NetBIOSName 29 { 30 32 public static final int NameLength = 16; 33 34 36 public static final char WorkStation = 0x00; 37 public static final char Messenger = 0x01; 38 public static final char RemoteMessenger = 0x03; 39 public static final char RASServer = 0x06; 40 public static final char FileServer = 0x20; 41 public static final char RASClientService = 0x21; 42 public static final char MSExchangeInterchange = 0x22; 43 public static final char MSExchangeStore = 0x23; 44 public static final char MSExchangeDirectory = 0x24; 45 public static final char LotusNotesServerService= 0x2B; 46 public static final char ModemSharingService = 0x30; 47 public static final char ModemSharingClient = 0x31; 48 public static final char McCaffeeAntiVirus = 0x42; 49 public static final char SMSClientRemoteControl = 0x43; 50 public static final char SMSAdminRemoteControl = 0x44; 51 public static final char SMSClientRemoteChat = 0x45; 52 public static final char SMSClientRemoteTransfer= 0x46; 53 public static final char DECPathworksService = 0x4C; 54 public static final char MSExchangeIMC = 0x6A; 55 public static final char MSExchangeMTA = 0x87; 56 public static final char NetworkMonitorAgent = 0xBE; 57 public static final char NetworkMonitorApp = 0xBF; 58 59 61 public static final char Domain = 0x00; public static final char DomainMasterBrowser = 0x1B; 63 public static final char DomainControllers = 0x1C; public static final char MasterBrowser = 0x1D; 65 public static final char DomainAnnounce = 0x1E; 66 67 69 public static final char BrowseMasterGroup = 0x01; 70 71 73 public static final String BrowseMasterName = "\u0001\u0002__MSBROWSE__\u0002"; 74 75 77 public static final String SMBServer = "*SMBSERVER"; 78 public static final String SMBServer2 = "*SMBSERV"; 79 80 82 public static final int DefaultTTL = 28800; 84 86 private static final String EncodeConversion = "ABCDEFGHIJKLMNOP"; 87 88 90 private static String _nameConversionCharset = null; 91 92 94 private String m_name; 95 private char m_type; 96 97 99 private String m_scope; 100 101 103 private boolean m_group = false; 104 105 107 private boolean m_local = true; 108 109 111 private Vector <byte[]> m_addrList; 112 113 115 private long m_expiry; 116 private int m_ttl; 118 125 public NetBIOSName(String name, char typ, boolean group) 126 { 127 setName(name); 128 setType(typ); 129 setGroup(group); 130 } 131 132 140 public NetBIOSName(String name, char typ, boolean group, byte[] ipaddr) 141 { 142 setName(name); 143 setType(typ); 144 setGroup(group); 145 addIPAddress(ipaddr); 146 } 147 148 156 public NetBIOSName(String name, char typ, boolean group, Vector <byte[]> ipList) 157 { 158 setName(name); 159 setType(typ); 160 setGroup(group); 161 addIPAddresses(ipList); 162 } 163 164 173 public NetBIOSName(String name, char typ, boolean group, byte[] ipaddr, int ttl) 174 { 175 setName(name); 176 setType(typ); 177 setGroup(group); 178 addIPAddress(ipaddr); 179 setTimeToLive(ttl); 180 } 181 182 191 public NetBIOSName(String name, char typ, boolean group, Vector <byte[]> ipList, int ttl) 192 { 193 setName(name); 194 setType(typ); 195 setGroup(group); 196 addIPAddresses(ipList); 197 setTimeToLive(ttl); 198 } 199 200 206 public NetBIOSName(byte[] buf, int off) 207 { 208 setName(new String (buf, off, NameLength - 1)); 209 setType((char) buf[off + NameLength - 1]); 210 } 211 212 217 public NetBIOSName(String name) 218 { 219 setName(name.substring(0, NameLength - 1).trim()); 220 setType(name.charAt(NameLength - 1)); 221 } 222 223 229 protected NetBIOSName(String name, String scope) 230 { 231 setName(name.substring(0, NameLength - 1).trim()); 232 setType(name.charAt(NameLength - 1)); 233 234 if (scope != null && scope.length() > 0) 235 setNameScope(scope); 236 } 237 238 244 public boolean equals(Object obj) 245 { 246 247 249 if (obj instanceof NetBIOSName) 250 { 251 252 254 NetBIOSName nbn = (NetBIOSName) obj; 255 if (nbn.getName().equals(getName()) && nbn.getType() == getType() && nbn.isLocalName() == isLocalName()) 256 return true; 257 } 258 259 261 return false; 262 } 263 264 269 public final long getExpiryTime() 270 { 271 return m_expiry; 272 } 273 274 279 public final int getTimeToLive() 280 { 281 return m_ttl; 282 } 283 284 289 public final int numberOfAddresses() 290 { 291 return m_addrList != null ? m_addrList.size() : 0; 292 } 293 294 300 public final byte[] getIPAddress(int idx) 301 { 302 if (m_addrList == null || idx < 0 || idx >= m_addrList.size()) 303 return null; 304 return m_addrList.get(idx); 305 } 306 307 313 public final String getIPAddressString(int idx) 314 { 315 if (m_addrList == null || idx < 0 || idx >= m_addrList.size()) 316 return null; 317 318 320 return IPAddress.asString(m_addrList.get(idx)); 321 } 322 323 328 public final String getName() 329 { 330 return m_name; 331 } 332 333 338 public final byte[] getNetBIOSName() 339 { 340 341 343 byte[] nameBuf = new byte[NameLength]; 344 345 347 byte[] nameBytes = null; 348 349 try 350 { 351 if (hasNameConversionCharacterSet()) 352 nameBytes = getName().getBytes(getNameConversionCharacterSet()); 353 else 354 nameBytes = getName().getBytes(); 355 } 356 catch (Exception ex) 357 { 358 ex.printStackTrace(); 359 } 360 361 for (int i = 0; i < nameBytes.length; i++) 362 nameBuf[i] = nameBytes[i]; 363 for (int i = nameBytes.length; i < NameLength; i++) 364 nameBuf[i] = ' '; 365 nameBuf[NameLength - 1] = (byte) (m_type & 0xFF); 366 367 return nameBuf; 368 } 369 370 375 public final boolean hasNameScope() 376 { 377 return m_scope != null ? true : false; 378 } 379 380 385 public final String getNameScope() 386 { 387 return m_scope; 388 } 389 390 395 public final char getType() 396 { 397 return m_type; 398 } 399 400 405 public int hashCode() 406 { 407 return getName().hashCode() + (int) getType(); 408 } 409 410 415 public final boolean isGroupName() 416 { 417 return m_group; 418 } 419 420 425 public final boolean isLocalName() 426 { 427 return m_local; 428 } 429 430 435 public final boolean isUniqueName() 436 { 437 return m_group ? false : true; 438 } 439 440 443 public final void removeAllAddresses() 444 { 445 m_addrList.removeAllElements(); 446 } 447 448 453 public final void setExpiryTime(long expires) 454 { 455 m_expiry = expires; 456 } 457 458 463 public final void setTimeToLive(int ttl) 464 { 465 m_ttl = ttl; 466 } 467 468 473 public final void setGroup(boolean flag) 474 { 475 m_group = flag; 476 } 477 478 483 public final void setNameScope(String scope) 484 { 485 if (scope == null) 486 m_scope = null; 487 else if (scope.length() > 0 && scope.startsWith(".")) 488 m_scope = scope.substring(1); 489 else 490 m_scope = scope; 491 } 492 493 498 public final void addIPAddress(byte[] ipaddr) 499 { 500 if (m_addrList == null) 501 m_addrList = new Vector <byte[]>(); 502 m_addrList.add(ipaddr); 503 } 504 505 510 public final void addIPAddresses(Vector <byte[]> addrList) 511 { 512 if (m_addrList == null) 513 m_addrList = new Vector <byte[]>(); 514 515 517 for (int i = 0; i < addrList.size(); i++) 518 { 519 byte[] addr = addrList.get(i); 520 m_addrList.add(addr); 521 } 522 } 523 524 529 public final void setLocalName(boolean local) 530 { 531 m_local = local; 532 } 533 534 539 public final void setName(String name) 540 { 541 542 544 int pos = name.indexOf("."); 545 if (pos != -1) 546 { 547 548 550 setNameScope(name.substring(pos + 1)); 551 m_name = toUpperCaseName(name.substring(0, pos)); 552 } 553 else 554 { 555 556 558 m_name = toUpperCaseName(name); 559 } 560 } 561 562 567 public final void setType(char typ) 568 { 569 m_type = typ; 570 } 571 572 577 public static String toUpperCaseName(String name) 578 { 579 580 582 if (name.length() > 2 && name.charAt(0) != 0x01 && name.charAt(1) != 0x02) 583 name = name.trim(); 584 585 587 if (name != null && name.length() > 0) 588 { 589 StringBuffer upperName = new StringBuffer (name.length()); 590 591 for (int i = 0; i < name.length(); i++) 592 { 593 char ch = name.charAt(i); 594 if (ch >= 'a' && ch <= 'z') 595 upperName.append(Character.toUpperCase(ch)); 596 else 597 upperName.append(ch); 598 } 599 600 602 return upperName.toString(); 603 } 604 605 607 return ""; 608 } 609 610 615 public final static boolean hasNameConversionCharacterSet() 616 { 617 return _nameConversionCharset != null ? true : false; 618 } 619 620 625 public final static String getNameConversionCharacterSet() 626 { 627 return _nameConversionCharset; 628 } 629 630 635 public final static void setNameConversionCharacterSet(String charSet) 636 { 637 _nameConversionCharset = charSet; 638 } 639 640 645 public String toString() 646 { 647 StringBuffer str = new StringBuffer (); 648 str.append("["); 649 str.append(m_name); 650 651 if (hasNameScope()) 652 { 653 str.append("."); 654 str.append(m_scope); 655 } 656 657 str.append(":"); 658 str.append(TypeAsString(m_type)); 659 str.append(","); 660 if (m_group == true) 661 str.append("Group,"); 662 else 663 str.append("Unique,"); 664 if (numberOfAddresses() > 0) 665 { 666 for (int i = 0; i < numberOfAddresses(); i++) 667 { 668 str.append(getIPAddressString(i)); 669 str.append("|"); 670 } 671 } 672 str.append("]"); 673 return str.toString(); 674 } 675 676 681 public byte[] encodeName() 682 { 683 684 687 StringBuffer nbName = new StringBuffer (getName().toUpperCase()); 688 689 if (nbName.length() > NameLength - 1) 690 nbName.setLength(NameLength - 1); 691 692 694 while (nbName.length() < NameLength - 1) 695 nbName.append(' '); 696 nbName.append(getType()); 697 698 702 int len = 34; 703 if (hasNameScope()) 704 len += getNameScope().length() + 1; 705 706 byte[] encBuf = new byte[len]; 707 708 710 int pos = 0; 711 encBuf[pos++] = (byte) 32; 712 int idx = 0; 713 714 while (idx < nbName.length()) 715 { 716 717 719 char ch = nbName.charAt(idx++); 720 721 if (ch == ' ') 722 { 723 724 726 encBuf[pos++] = (byte) 'C'; 727 encBuf[pos++] = (byte) 'A'; 728 } 729 else 730 { 731 732 734 encBuf[pos++] = (byte) EncodeConversion.charAt((int) ch / 16); 735 encBuf[pos++] = (byte) EncodeConversion.charAt((int) ch % 16); 736 } 737 } 738 739 741 if (hasNameScope()) 742 { 743 744 746 StringTokenizer tokens = new StringTokenizer (getNameScope(), "."); 747 748 while (tokens.hasMoreTokens()) 749 { 750 751 753 String token = tokens.nextToken(); 754 755 757 encBuf[pos++] = (byte) token.length(); 758 for (int i = 0; i < token.length(); i++) 759 encBuf[pos++] = (byte) token.charAt(i); 760 } 761 } 762 763 765 encBuf[pos++] = (byte) 0; 766 767 769 return encBuf; 770 } 771 772 779 public final int findBestMatchAddress(InetAddress [] addrList) 780 { 781 782 784 if (addrList == null || addrList.length == 0 || numberOfAddresses() == 0) 785 return -1; 786 787 789 if (numberOfAddresses() == 1) 790 return 0; 791 792 794 int topCnt = 0; 795 int topIdx = -1; 796 797 for (int localIdx = 0; localIdx < addrList.length; localIdx++) 798 { 799 800 802 byte[] localAddr = addrList[localIdx].getAddress(); 803 804 806 for (int addrIdx = 0; addrIdx < numberOfAddresses(); addrIdx++) 807 { 808 809 811 byte[] remoteAddr = (byte[]) m_addrList.elementAt(addrIdx); 812 int ipIdx = 0; 813 814 while (ipIdx < 4 && remoteAddr[ipIdx] == localAddr[ipIdx]) 815 ipIdx++; 816 817 819 if (ipIdx > topIdx) 820 { 821 822 824 topIdx = addrIdx; 825 topCnt = ipIdx; 826 } 827 } 828 } 829 830 832 return topIdx; 833 } 834 835 842 public static NetBIOSName decodeNetBIOSName(byte[] buf, int off) 843 { 844 845 847 StringBuffer nameBuf = new StringBuffer (16); 848 849 int nameLen = (int) buf[off++]; 850 int idx = 0; 851 char ch1, ch2; 852 853 while (idx < nameLen) 854 { 855 856 858 ch1 = (char) buf[off++]; 859 ch2 = (char) buf[off++]; 860 861 if (ch1 == 'C' && ch2 == 'A') 862 { 863 864 866 nameBuf.append(' '); 867 } 868 else 869 { 870 871 873 int val = EncodeConversion.indexOf(ch1) << 4; 874 val += EncodeConversion.indexOf(ch2); 875 876 878 nameBuf.append((char) (val & 0xFF)); 879 } 880 881 883 idx += 2; 884 885 } 886 887 889 StringBuffer scopeBuf = new StringBuffer (128); 890 nameLen = (int) buf[off++]; 891 892 while (nameLen > 0) 893 { 894 895 897 if (scopeBuf.length() > 0) 898 scopeBuf.append("."); 899 900 902 for (int i = 0; i < nameLen; i++) 903 scopeBuf.append((char) buf[off++]); 904 905 907 nameLen = (int) buf[off++]; 908 } 909 910 912 return new NetBIOSName(nameBuf.toString(), scopeBuf.toString()); 913 } 914 915 922 public static int decodeNetBIOSNameLength(byte[] buf, int off) 923 { 924 925 927 int totLen = 1; 928 int nameLen = (int) buf[off++]; 929 930 while (nameLen > 0) 931 { 932 933 935 totLen += nameLen; 936 off += nameLen; 937 938 940 nameLen = (int) buf[off++]; 941 totLen++; 942 } 943 944 946 return totLen; 947 } 948 949 955 public final static String TypeAsString(char typ) 956 { 957 958 960 String nameTyp = ""; 961 962 switch (typ) 963 { 964 case WorkStation: 965 nameTyp = "WorkStation"; 966 break; 967 case Messenger: 968 nameTyp = "Messenger"; 969 break; 970 case RemoteMessenger: 971 nameTyp = "RemoteMessenger"; 972 break; 973 case RASServer: 974 nameTyp = "RASServer"; 975 break; 976 case FileServer: 977 nameTyp = "FileServer"; 978 break; 979 case RASClientService: 980 nameTyp = "RASClientService"; 981 break; 982 case MSExchangeInterchange: 983 nameTyp = "MSExchangeInterchange"; 984 break; 985 case MSExchangeStore: 986 nameTyp = "MSExchangeStore"; 987 break; 988 case MSExchangeDirectory: 989 nameTyp = "MSExchangeDirectory"; 990 break; 991 case LotusNotesServerService: 992 nameTyp = "LotusNotesServerService"; 993 break; 994 case ModemSharingService: 995 nameTyp = "ModemSharingService"; 996 break; 997 case ModemSharingClient: 998 nameTyp = "ModemSharingClient"; 999 break; 1000 case McCaffeeAntiVirus: 1001 nameTyp = "McCaffeeAntiVirus"; 1002 break; 1003 case SMSClientRemoteControl: 1004 nameTyp = "SMSClientRemoteControl"; 1005 break; 1006 case SMSAdminRemoteControl: 1007 nameTyp = "SMSAdminRemoteControl"; 1008 break; 1009 case SMSClientRemoteChat: 1010 nameTyp = "SMSClientRemoteChat"; 1011 break; 1012 case SMSClientRemoteTransfer: 1013 nameTyp = "SMSClientRemoteTransfer"; 1014 break; 1015 case DECPathworksService: 1016 nameTyp = "DECPathworksService"; 1017 break; 1018 case MSExchangeIMC: 1019 nameTyp = "MSExchangeIMC"; 1020 break; 1021 case MSExchangeMTA: 1022 nameTyp = "MSExchangeMTA"; 1023 break; 1024 case NetworkMonitorAgent: 1025 nameTyp = "NetworkMonitorAgent"; 1026 break; 1027 case NetworkMonitorApp: 1028 nameTyp = "NetworkMonitorApp"; 1029 break; 1030 case DomainMasterBrowser: 1031 nameTyp = "DomainMasterBrowser"; 1032 break; 1033 case MasterBrowser: 1034 nameTyp = "MasterBrowser"; 1035 break; 1036 case DomainAnnounce: 1037 nameTyp = "DomainAnnounce"; 1038 break; 1039 case DomainControllers: 1040 nameTyp = "DomainControllers"; 1041 break; 1042 default: 1043 nameTyp = "0x" + Integer.toHexString((int) typ); 1044 break; 1045 } 1046 1047 return nameTyp; 1048 } 1049} | Popular Tags |