|
Java example source code file (SdpProvider.java)
The SdpProvider.java Java example source code/* * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.net.sdp; import sun.net.NetHooks; import java.net.InetAddress; import java.net.Inet4Address; import java.net.UnknownHostException; import java.util.*; import java.io.File; import java.io.FileDescriptor; import java.io.IOException; import java.io.PrintStream; import java.security.AccessController; import sun.net.sdp.SdpSupport; import sun.security.action.GetPropertyAction; /** * A NetHooks provider that converts sockets from the TCP to SDP protocol prior * to binding or connecting. */ public class SdpProvider extends NetHooks.Provider { // maximum port private static final int MAX_PORT = 65535; // indicates if SDP is enabled and the rules for when the protocol is used private final boolean enabled; private final List<Rule> rules; // logging for debug purposes private PrintStream log; public SdpProvider() { // if this property is not defined then there is nothing to do. String file = AccessController.doPrivileged( new GetPropertyAction("com.sun.sdp.conf")); if (file == null) { this.enabled = false; this.rules = null; return; } // load configuration file List<Rule> list = null; if (file != null) { try { list = loadRulesFromFile(file); } catch (IOException e) { fail("Error reading %s: %s", file, e.getMessage()); } } // check if debugging is enabled PrintStream out = null; String logfile = AccessController.doPrivileged( new GetPropertyAction("com.sun.sdp.debug")); if (logfile != null) { out = System.out; if (logfile.length() > 0) { try { out = new PrintStream(logfile); } catch (IOException ignore) { } } } this.enabled = !list.isEmpty(); this.rules = list; this.log = out; } // supported actions private static enum Action { BIND, CONNECT; } // a rule for matching a bind or connect request private static interface Rule { boolean match(Action action, InetAddress address, int port); } // rule to match port[-end] private static class PortRangeRule implements Rule { private final Action action; private final int portStart; private final int portEnd; PortRangeRule(Action action, int portStart, int portEnd) { this.action = action; this.portStart = portStart; this.portEnd = portEnd; } Action action() { return action; } @Override public boolean match(Action action, InetAddress address, int port) { return (action == this.action && port >= this.portStart && port <= this.portEnd); } } // rule to match address[/prefix] port[-end] private static class AddressPortRangeRule extends PortRangeRule { private final byte[] addressAsBytes; private final int prefixByteCount; private final byte mask; AddressPortRangeRule(Action action, InetAddress address, int prefix, int port, int end) { super(action, port, end); this.addressAsBytes = address.getAddress(); this.prefixByteCount = prefix >> 3; this.mask = (byte)(0xff << (8 - (prefix % 8))); } @Override public boolean match(Action action, InetAddress address, int port) { if (action != action()) return false; byte[] candidate = address.getAddress(); // same address type? if (candidate.length != addressAsBytes.length) return false; // check bytes for (int i=0; i<prefixByteCount; i++) { if (candidate[i] != addressAsBytes[i]) return false; } // check remaining bits if ((prefixByteCount < addressAsBytes.length) && ((candidate[prefixByteCount] & mask) != (addressAsBytes[prefixByteCount] & mask))) return false; return super.match(action, address, port); } } // parses port:[-end] private static int[] parsePortRange(String s) { int pos = s.indexOf('-'); try { int[] result = new int[2]; if (pos < 0) { boolean all = s.equals("*"); result[0] = all ? 0 : Integer.parseInt(s); result[1] = all ? MAX_PORT : result[0]; } else { String low = s.substring(0, pos); if (low.length() == 0) low = "*"; String high = s.substring(pos+1); if (high.length() == 0) high = "*"; result[0] = low.equals("*") ? 0 : Integer.parseInt(low); result[1] = high.equals("*") ? MAX_PORT : Integer.parseInt(high); } return result; } catch (NumberFormatException e) { return new int[0]; } } private static void fail(String msg, Object... args) { Formatter f = new Formatter(); f.format(msg, args); throw new RuntimeException(f.out().toString()); } // loads rules from the given file // Each non-blank/non-comment line must have the format: // ("bind" | "connect") 1*LWSP-char (hostname | ipaddress["/" prefix]) // 1*LWSP-char ("*" | port) [ "-" ("*" | port) ] private static List<Rule> loadRulesFromFile(String file) throws IOException { Scanner scanner = new Scanner(new File(file)); try { List<Rule> result = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java SdpProvider.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.