Update of /cvsroot/freenet/freenet/src/freenet/interfaces
In directory sc8-pr-cvs1:/tmp/cvs-serv11912/src/freenet/interfaces

Modified Files:
        BaseLocalNIOInterface.java 
Log Message:
Indenting, abstractions, enabled allowedHosts modification

Index: BaseLocalNIOInterface.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/interfaces/BaseLocalNIOInterface.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- BaseLocalNIOInterface.java  18 Sep 2003 17:48:07 -0000      1.6
+++ BaseLocalNIOInterface.java  26 Sep 2003 08:14:06 -0000      1.7
@@ -15,46 +15,66 @@
  */
 public abstract class BaseLocalNIOInterface extends NIOInterface {
 
-    protected int[][] allowedHosts;
+    private int[][] allowedHosts;
     boolean logDEBUG = Core.logger.shouldLog(Logger.DEBUG,this);
     
     private static final int intAddress(String addr) { 
-       try {
-           return intAddress(InetAddress.getByName(addr));
-       } catch (java.net.UnknownHostException e) {
-           return 0;
+               try {
+               return intAddress(InetAddress.getByName(addr));
+               } catch (java.net.UnknownHostException e) {
+               return 0;
+               }
+    }
+
+       private static final int intAddress(InetAddress addr) {
+               boolean logDEBUG =
+                       Core.logger.shouldLog(Logger.DEBUG, 
BaseLocalNIOInterface.class);
+               if (logDEBUG)
+                       Core.logger.log(
+                               LocalNIOInterface.class,
+                               "intAddress(" + addr.toString() + ")",
+                               Logger.DEBUG);
+               byte[] b = addr.getAddress();
+               if (logDEBUG)
+                       Core.logger.log(
+                               LocalNIOInterface.class,
+                               "Address: "
+                                       + (((int) b[0]) & 0xff)
+                                       + "."
+                                       + (((int) b[1]) & 0xff)
+                                       + "."
+                                       + (((int) b[2]) & 0xff)
+                                       + "."
+                                       + (((int) b[3]) & 0xff)
+                                       + " ("
+                                       + b.length
+                                       + ")",
+                               Logger.DEBUG);
+               long x =
+                       ((((long) b[0]) & 0xff) << 24)
+                               + ((((int) b[1]) & 0xff) << 16)
+                               + ((((int) b[2]) & 0xff) << 8)
+                               + (((int) b[3]) & 0xff);
+               if (logDEBUG)
+                       Core.logger.log(
+                               LocalNIOInterface.class,
+                               "Returning " + Fields.longToHex(x),
+                               Logger.DEBUG);
+               return (int) x;
        }
-    }
-
-    private static final int intAddress(InetAddress addr) { 
-       boolean logDEBUG = 
Core.logger.shouldLog(Logger.DEBUG,BaseLocalNIOInterface.class);
-       if(logDEBUG) Core.logger.log(LocalNIOInterface.class, "intAddress("+
-                                    addr.toString()+")", Logger.DEBUG);
-       byte[] b = addr.getAddress();
-       if(logDEBUG) Core.logger.log(LocalNIOInterface.class, "Address: "+
-                                    (((int)b[0]) & 0xff) +"."+(((int)b[1]) & 0xff)+
-                                    "."+(((int)b[2]) & 0xff)+"."+
-                                    (((int)b[3]) & 0xff)+" ("+
-                                    b.length+")", Logger.DEBUG);
-       long x = ((((long)b[0]) & 0xff) << 24) + ((((int)b[1]) & 0xff) << 16) + 
-           ((((int)b[2]) & 0xff) << 8) + (((int)b[3]) & 0xff);
-       if(logDEBUG) Core.logger.log(LocalNIOInterface.class, "Returning "+
-                                    Fields.longToHex(x), Logger.DEBUG);
-       return (int) x;
-    }
     
     private static final int mask(int addr, int maskbits) {
-       int mbits = 32 - maskbits;
-       int power;
-       if(mbits < 32)
-           power = 1 << mbits;
-       else
-           power = 0; // 1 << 32 = 1 !! - check before you "fix" this
-       int ones = power - 1;
-       int mask = ~ones;
-       //int mask = ~((1 << (32-maskbits)) -1);
-       int out = addr & mask;
-       return out;
+               int mbits = 32 - maskbits;
+               int power;
+               if(mbits < 32)
+               power = 1 << mbits;
+               else
+               power = 0; // 1 << 32 = 1 !! - check before you "fix" this
+               int ones = power - 1;
+               int mask = ~ones;
+               //int mask = ~((1 << (32-maskbits)) -1);
+               int out = addr & mask;
+               return out;
     }
     
     int lowRunningConnections; // reenable interface when go below this
@@ -73,23 +93,23 @@
         throws ListenException {
         super(listenAddr);
         this.allowedHosts = allowedHosts;
-       this.lowRunningConnections = lowRunningConnections;
-       this.highRunningConnections = highRunningConnections;
-       this.listener = getListener(listenAddr);
+               this.lowRunningConnections = lowRunningConnections;
+               this.highRunningConnections = highRunningConnections;
+               this.listener = getListener(listenAddr);
     }
     
     private NIOListener getListener(ListeningAddress listenAddr) 
-       throws ListenException {
-       return ((tcpListeningAddress)listenAddr).getNIOListener();
+               throws ListenException {
+               return ((tcpListeningAddress)listenAddr).getNIOListener();
     }
     
     protected synchronized void reopenListener() throws ListenException {
-       if(listening) return;
-       Core.logger.log(this, "Restarting listening on "+this,
+               if(listening) return;
+               Core.logger.log(this, "Restarting listening on "+this,
                        Logger.NORMAL);
-       this.listener = getListener(listenAddr);
-       register(loop);
-       listening = true;
+               this.listener = getListener(listenAddr);
+               register(loop);
+               listening = true;
     }
     
     /**
@@ -103,87 +123,117 @@
                                 int lowRunningConnections, 
                                 int highRunningConnections)
         throws ListenException {
-       super(listenAddr);
-        int[][] allowedHostsAddr = null;
-
-       if (allowedHosts == null || allowedHosts.trim().equals("")) {
-           allowedHosts = "127.0.0.0/8";
-       }
-
-       if (allowedHosts.trim().equals("*")) {
-           allowedHosts = "0.0.0.0/0";
-       }
+               super(listenAddr);
+               setAllowedHosts(allowedHosts);
+               this.lowRunningConnections = lowRunningConnections;
+               this.highRunningConnections = highRunningConnections;
+               this.listener = getListener(listenAddr);
+    }
+    
+       /**
+       * @param  allowedHosts set of Addresses to do an equalsHost() check with;
+       *                      null means to allow all hosts
+       */
+    public void setAllowedHosts(String allowedHosts){
+               int[][] allowedHostsAddr = null;
+
+               if (allowedHosts == null || allowedHosts.trim().equals("")) {
+                       allowedHosts = "127.0.0.0/8";
+               }
+
+               if (allowedHosts.trim().equals("*")) {
+                       allowedHosts = "0.0.0.0/0";
+               }
        
-       if(logDEBUG)
-           Core.logger.log(this, "New BaseLocalNIOInterface: "+listenAddr+
-                           ","+allowedHosts, Logger.DEBUG);
+               if(logDEBUG)
+                       Core.logger.log(this, "New BaseLocalNIOInterface: "+listenAddr+
+                                       ","+allowedHosts, Logger.DEBUG);
        
-       String[] hosts = Fields.commaList(allowedHosts);
+               String[] hosts = Fields.commaList(allowedHosts);
 
-       allowedHostsAddr = new int [hosts.length][2];
+               allowedHostsAddr = new int [hosts.length][2];
        
-       for (int i = 0; i < hosts.length; ++i) {
-           int host, subnet, div = hosts[i].indexOf('/');
-           if (div == -1) {
-               subnet = 32;
-               host = intAddress(hosts[i]);
-           } else {
-               subnet = Integer.parseInt(hosts[i].substring(div+1));
-               host = intAddress(hosts[i].substring(0,div));
-           }
-           allowedHostsAddr[i][0] = mask(host, subnet);
-           allowedHostsAddr[i][1] = subnet;
+               for (int i = 0; i < hosts.length; ++i) {
+                       hostAndSubnetPair h = parseHostOrNetString(hosts[i]);
+                       allowedHostsAddr[i][0] = mask(h.host, h.subnet);
+                       allowedHostsAddr[i][1] = h.subnet;
+               }
+               this.allowedHosts = allowedHostsAddr;
+    }
+    private class hostAndSubnetPair{
+       public int host,subnet;
+               hostAndSubnetPair(int host, int subnet){
+                       this.host = host;
+                       this.subnet = subnet;
+               }
+    }
+    private hostAndSubnetPair parseHostOrNetString(String hostOrNetString){
+               int host, subnet, div = hostOrNetString.indexOf('/');
+               if (div == -1) { //Consider the absence of a subnetmask as 
255.255.255.255 (=only the exact host specified)
+                       subnet = 32;
+                       host = intAddress(hostOrNetString);
+               } else {
+                       subnet = Integer.parseInt(hostOrNetString.substring(div+1));
+                       host = intAddress(hostOrNetString.substring(0,div));
+               }
+               return new hostAndSubnetPair(host,subnet);
+    }
+    
+       private boolean hostAllowed(Address addr)
+               throws RejectedConnectionException {
+               boolean allow = false;
+               //insert some code to make sure ha is a tcpAddress and
+               //handle things correctly when it's not. --thelema
+               int inta;
+               try {
+                       inta = intAddress(((tcpAddress) addr).getHost());
+               } catch (java.net.UnknownHostException e) {
+                       Core.logger.log(
+                               this,
+                               "Unknown Host on incoming connection!!",
+                               Core.logger.ERROR);
+                       throw new RejectedConnectionException("unknown host on 
incoming connection!");
+               }
+
+               for (int i = 0; !allow && i < allowedHosts.length; i++) {
+                       int subnet = allowedHosts[i][0];
+                       int maskbits = allowedHosts[i][1];
+                       allow |= (mask(inta, maskbits) == subnet);
+                       if (logDEBUG)
+                               Core.logger.log(
+                                       this,
+                                       "Trying "
+                                               + Fields.intToHex(subnet)
+                                               + ":"
+                                               + Fields.intToHex(maskbits)
+                                               + " for "
+                                               + Fields.intToHex(inta),
+                                       Core.logger.DEBUG);
+               }
+               return allow;
        }
-       this.allowedHosts = allowedHostsAddr;
-       this.lowRunningConnections = lowRunningConnections;
-       this.highRunningConnections = highRunningConnections;
-       this.listener = getListener(listenAddr);
-    }
 
 
     /**
      *  Description of the Method
      *
      * @param  conn                             Description of the Parameter
-     * @exception  RejectedConnectionException  Description of the Exception
+     * @exception  RejectedConnectionException  Thrown when connections from the 
connecting host isn't allowed
      */
     protected void dispatch(Connection conn) throws RejectedConnectionException {
-       logDEBUG = Core.logger.shouldLog(Logger.DEBUG,this);
-       boolean allow = false;
-       Address ha = conn.getPeerAddress();
-       if(logDEBUG)
-           Core.logger.log(this, 
-                           "Dispatching connection on a BaseLocalNIOInterface from " +
-                           ha.toString(), Core.logger.DEBUG);
-       //insert some code to make sure ha is a tcpAddress and
-       //handle things correctly when it's not. --thelema
-       int inta;
-       try {
-           inta = intAddress(((tcpAddress) ha).getHost());
-       } catch (java.net.UnknownHostException e) {
-           Core.logger.log(this, "Unknown Host on incoming connection!!",
-                           Core.logger.ERROR);
-           throw new RejectedConnectionException("unknown host on incoming 
connection!");
-       }
-       
-       for (int i = 0; !allow && i < allowedHosts.length; i++) {
-           int subnet = allowedHosts[i][0];
-           int maskbits = allowedHosts[i][1];
-           allow |= (mask(inta, maskbits) == subnet);
-           if(logDEBUG)
-               Core.logger.log(this, "Trying "+Fields.intToHex(subnet)+
-                               ":"+Fields.intToHex(maskbits)+" for "+
-                               Fields.intToHex(inta), Core.logger.DEBUG);
-       }
-       
-       if (allow) {
-           handleConnection(conn);
-       } else {
-           if(logDEBUG)
-               Core.logger.log(this, "Rejecting local connection",
-                               Core.logger.DEBUG);
-           throw new RejectedConnectionException("host not allowed: " + ha);
-       }
+               logDEBUG = Core.logger.shouldLog(Logger.DEBUG,this);
+               Address ha = conn.getPeerAddress();
+               if(logDEBUG)
+               Core.logger.log(this, 
+                                   "Dispatching connection on a BaseLocalNIOInterface 
from " +
+                               ha.toString(), Core.logger.DEBUG);
+               if ( hostAllowed(ha)) {
+               handleConnection(conn);
+               } else {
+               if(logDEBUG)
+                               Core.logger.log(this, "Rejecting local connection",    
 Core.logger.DEBUG);
+               throw new RejectedConnectionException("host not allowed: " + ha);
+               }
     }
     
     protected abstract void handleConnection(Connection conn);

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to