Novell Home

Java Implementation of LDAPNetAddr PHP Function

Novell Cool Solutions: Tip
By Damien Joldersma

Digg This - Slashdot This

Posted: 15 Mar 2006
 

Problem

I needed a Java implementation of Jay Burrell's PHP function LDAPNetAddr ($networkaddress), as found in his Cool Solutions article:
http://www.novell.com/coolsolutions/feature/16370.html

Solution

Here's the Java code:

public static String getLDAPNetAddr (LDAPAttribute attribute) 
{ 
 /*
     Jay Burrell, Systems & Networks, Mississippi State University
 
     NetAddr - extract readable network address from the LDAP encoded networkAddress attribute.
     Novell Docs, see: http://developer.novell.com/ndk/doc/ndslib/schm_enu/data/sdk5624.html#sdk5624 
     for Address types: http://developer.novell.com/ndk/doc/ndslib/index.html?page=/ndk/doc/ndslib/schm_enu/data/sdk4170.html 
     LDAP Format, String:
         taggedData = uint32String "#" octetstring
         byte 0 = uint32String = Address Type: 0= IPX Address; 1 = IP Address
         byte 1 = char = "#" - separator
         byte 2+ = octetstring - the ordinal value of the address
                                                         
     Note: with eDirectory 8.6.2, the IP address (type 1) returns correctly, however, an IPX address does not seem to.  eDir 8.7 may correct this.
 */

 if ( attribute == null || !TextUtil.notNull(attribute.getName()).equals("networkAddress") ) 
         return "Invalid LDAP Attribute: " + attribute;

 String networkaddress = attribute.getStringValue();
 byte[] bytes = attribute.getByteValue();
 String addr = "";
 int addrtype = Integer.parseInt(networkaddress.substring(0, 1));
 String[] addrtypes = {"IPX", "IP", "SDLC", "Token Ring", "OSI", "AppleTalk", "NetBEUI", "Socket", "UDP", "TCP", "UDP6", "TCP6", "Reserved (12)", "URL", "Count"};

 int len = bytes.length;
 if (len > 0) 
 { 
   for (int i=0; i<len; i++) {
                 if (i < 2) continue; // skip first two chunks
       
                 addr += bytes[i] + (bytes[i] < 0 ? 256 : 0);
         if (addrtype == 1){  // dot separate IP addresses...
           addr += ".";
         }
   }
   if (addrtype == 1) {  // strip last period from end of $addr
       addr = addr.substring(0, addr.length()-1);
   }
 }  
 else {
   addr += " address not available."; 
 }
 
 return addr;
}


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell