1 17 package org.alfresco.filesys.netbios.win32; 18 19 import java.net.InetAddress ; 20 import java.net.NetworkInterface ; 21 import java.net.SocketException ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 25 import org.alfresco.filesys.netbios.NetBIOSName; 26 import org.alfresco.filesys.util.DataBuffer; 27 import org.alfresco.filesys.util.IPAddress; 28 29 32 public class Win32NetBIOS 33 { 34 35 39 protected final static int FindNameBufferLen = 33; 40 41 43 private static Throwable m_loadDLLException; 44 45 50 public static final boolean isInitialized() 51 { 52 return m_loadDLLException == null ? true : false; 53 } 54 55 60 public static final Throwable getInitializationException() 61 { 62 return m_loadDLLException; 63 } 64 65 70 public static final boolean isAvailable() { 71 72 74 if ( isInitialized() == false) 75 return false; 76 77 80 int[] lanas = LanaEnum(); 81 if ( lanas != null && lanas.length > 0) 82 return true; 83 return false; 84 } 85 86 93 public static native int AddName(int lana, byte[] name); 94 95 102 public static native int AddGroupName(int lana, byte[] name); 103 104 113 public static native int FindNameRaw(int lana, byte[] name, byte[] nameBuf, int bufLen); 114 115 122 public static int FindName(int lana, NetBIOSName nbName) 123 { 124 125 127 byte[] nameBuf = new byte[nbName.isGroupName() ? 65535 : 4096]; 128 129 131 int sts = FindNameRaw(lana, nbName.getNetBIOSName(), nameBuf, nameBuf.length); 132 133 if (sts != NetBIOS.NRC_GoodRet) 134 return -sts; 135 136 138 DataBuffer buf = new DataBuffer(nameBuf, 0, nameBuf.length); 139 140 int nodeCount = buf.getShort(); 141 buf.skipBytes(1); 142 boolean isGroupName = buf.getByte() == 0 ? false : true; 143 144 146 int curPos = buf.getPosition(); 147 148 for (int i = 0; i < nodeCount; i++) 149 { 150 151 159 161 buf.skipBytes(9); 162 163 165 if (buf.getByte() == 0 && buf.getByte() == 0) 166 { 167 168 170 byte[] ipAddr = new byte[4]; 171 172 ipAddr[0] = (byte) buf.getByte(); 173 ipAddr[1] = (byte) buf.getByte(); 174 ipAddr[2] = (byte) buf.getByte(); 175 ipAddr[3] = (byte) buf.getByte(); 176 177 179 nbName.addIPAddress(ipAddr); 180 181 183 curPos += FindNameBufferLen; 184 buf.setPosition(curPos); 185 } 186 } 187 188 190 return nodeCount; 191 } 192 193 200 public static native int DeleteName(int lana, byte[] name); 201 202 207 public static int[] LanaEnumerate() 208 { 209 212 boolean adapterAvail = false; 213 214 try 215 { 216 219 Enumeration <NetworkInterface > nis = NetworkInterface.getNetworkInterfaces(); 220 221 while ( nis.hasMoreElements() && adapterAvail == false) 222 { 223 NetworkInterface ni = nis.nextElement(); 224 if ( ni.getName().equals("lo") == false) 225 { 226 228 Enumeration <InetAddress > addrs = ni.getInetAddresses(); 229 if ( addrs.hasMoreElements()) 230 adapterAvail = true; 231 } 232 } 233 234 } 235 catch ( SocketException ex) 236 { 237 } 238 239 241 if ( adapterAvail == false) 242 return null; 243 244 246 return LanaEnum(); 247 } 248 249 254 private static native int[] LanaEnum(); 255 256 262 public static native int Reset(int lana); 263 264 273 public static native int Listen(int lana, byte[] toName, byte[] fromName, byte[] callerName); 274 275 285 public static native int Receive(int lana, int lsn, byte[] buf, int off, int maxLen); 286 287 297 public static native int Send(int lana, int lsn, byte[] buf, int off, int len); 298 299 310 public static native int SendDatagram(int lana, int srcNum, byte[] destName, byte[] buf, int off, int len); 311 312 321 public static native int SendBroadcastDatagram(int lana, byte[] buf, int off, int len); 322 323 333 public static native int ReceiveDatagram(int lana, int nameNum, byte[] buf, int off, int maxLen); 334 335 345 public static native int ReceiveBroadcastDatagram(int lana, int nameNum, byte[] buf, int off, int maxLen); 346 347 353 public static native int Hangup(int lana, int lsn); 354 355 360 public static native String GetLocalNetBIOSName(); 361 362 367 public static native String GetLocalDomainName(); 368 369 375 public static native String getWINSServerList(); 376 377 383 public static final String getIPAddressForLANA(int lana) 384 { 385 386 388 String localName = GetLocalNetBIOSName(); 389 if (localName == null) 390 return null; 391 392 394 NetBIOSName nbName = new NetBIOSName(localName, NetBIOSName.WorkStation, false); 395 396 398 int sts = FindName(lana, nbName); 399 400 if (sts == -NetBIOS.NRC_EnvNotDef) 401 { 402 403 405 Reset(lana); 406 sts = FindName(lana, nbName); 407 } 408 409 411 String ipAddr = null; 412 413 if (sts >= 0) 414 { 415 416 418 ipAddr = nbName.getIPAddressString(0); 419 } 420 421 423 return ipAddr; 424 } 425 426 432 public static final String getAdapterNameForLANA(int lana) 433 { 434 435 437 String ipAddr = getIPAddressForLANA(lana); 438 if (ipAddr == null) 439 return null; 440 441 443 Hashtable <String , NetworkInterface > adapters = getNetworkAdapterList(); 444 String adapterName = null; 445 446 if (adapters != null) 447 { 448 449 451 NetworkInterface ni = adapters.get(ipAddr); 452 if (ni != null) 453 adapterName = ni.getDisplayName(); 454 } 455 456 458 return adapterName; 459 } 460 461 467 public static final int getLANAForIPAddress(String addr) 468 { 469 470 472 if (IPAddress.isNumericAddress(addr) == false) 473 return -1; 474 475 477 int[] lanas = LanaEnum(); 478 if (lanas == null || lanas.length == 0) 479 return -1; 480 481 483 for (int i = 0; i < lanas.length; i++) 484 { 485 486 488 String curAddr = getIPAddressForLANA(lanas[i]); 489 if (curAddr != null && curAddr.equals(addr)) 490 return lanas[i]; 491 } 492 493 495 return -1; 496 } 497 498 504 public static final int getLANAForAdapterName(String name) 505 { 506 507 509 Hashtable <String , NetworkInterface > niList = getNetworkAdapterList(); 510 511 513 Enumeration <String > niEnum = niList.keys(); 514 515 while (niEnum.hasMoreElements()) 516 { 517 518 520 String ipAddr = niEnum.nextElement(); 521 NetworkInterface ni = niList.get(ipAddr); 522 523 if (ni.getDisplayName().equalsIgnoreCase(name)) 524 { 525 526 528 return getLANAForIPAddress(ipAddr); 529 } 530 } 531 532 534 return -1; 535 } 536 537 542 private static final Hashtable <String , NetworkInterface > getNetworkAdapterList() 543 { 544 545 547 Hashtable <String , NetworkInterface > niList = new Hashtable <String , NetworkInterface >(); 548 549 try 550 { 551 552 554 Enumeration <NetworkInterface > niEnum = NetworkInterface.getNetworkInterfaces(); 555 556 while (niEnum.hasMoreElements()) 557 { 558 559 561 NetworkInterface ni = niEnum.nextElement(); 562 Enumeration <InetAddress > addrEnum = ni.getInetAddresses(); 563 564 while (addrEnum.hasMoreElements()) 565 { 566 567 570 InetAddress addr = addrEnum.nextElement(); 571 niList.put(addr.getHostAddress(), ni); 572 } 573 } 574 } 575 catch (Exception ex) 576 { 577 } 578 579 581 return niList; 582 } 583 584 586 591 protected static native void InitializeSockets() 592 throws WinsockNetBIOSException; 593 594 597 protected static native void ShutdownSockets(); 598 599 606 protected static native int CreateSocket(int lana) 607 throws WinsockNetBIOSException; 608 609 616 protected static native int CreateDatagramSocket(int lana) 617 throws WinsockNetBIOSException; 618 619 626 protected static native int BindSocket(int sockPtr, byte[] name) 627 throws WinsockNetBIOSException; 628 629 637 protected static native int ListenSocket(int sockPtr, byte[] callerName) 638 throws WinsockNetBIOSException; 639 640 645 protected static native void CloseSocket(int sockPtr); 646 647 657 protected static native int SendSocket(int sockPtr, byte[] buf, int off, int len) 658 throws WinsockNetBIOSException; 659 660 671 protected static native int ReceiveSocket(int sockPtr, byte[] buf, int off, int maxLen) 672 throws WinsockNetBIOSException; 673 674 685 protected static native int SendSocketDatagram(int sockPtr, byte[] toName, byte[] buf, int off, int len) 686 throws WinsockNetBIOSException; 687 688 692 public static native void waitForNetworkAddressChange(); 693 694 697 static 698 { 699 700 702 try 703 { 704 System.loadLibrary("Win32NetBIOS"); 705 } 706 catch (Throwable ex) 707 { 708 710 m_loadDLLException = ex; 711 } 712 } 713 } 714 | Popular Tags |