60 String file = props.getProperty("com.sun.sdp.conf");
61 if (file == null) {
62 this.enabled = false;
63 this.rules = null;
64 return;
65 }
66
67 // load configuration file
68 List<Rule> list = null;
69 try {
70 list = loadRulesFromFile(file);
71 } catch (IOException e) {
72 fail("Error reading %s: %s", file, e.getMessage());
73 }
74
75 // check if debugging is enabled
76 PrintStream out = null;
77 String logfile = props.getProperty("com.sun.sdp.debug");
78 if (logfile != null) {
79 out = System.out;
80 if (logfile.length() > 0) {
81 try {
82 out = new PrintStream(logfile);
83 } catch (IOException ignore) { }
84 }
85 }
86
87 this.enabled = !list.isEmpty();
88 this.rules = list;
89 this.log = out;
90 }
91
92 // supported actions
93 private static enum Action {
94 BIND,
95 CONNECT;
96 }
97
98 // a rule for matching a bind or connect request
99 private static interface Rule {
100 boolean match(Action action, InetAddress address, int port);
150 // check remaining bits
151 if ((prefixByteCount < addressAsBytes.length) &&
152 ((candidate[prefixByteCount] & mask) !=
153 (addressAsBytes[prefixByteCount] & mask)))
154 return false;
155 return super.match(action, address, port);
156 }
157 }
158
159 // parses port:[-end]
160 private static int[] parsePortRange(String s) {
161 int pos = s.indexOf('-');
162 try {
163 int[] result = new int[2];
164 if (pos < 0) {
165 boolean all = s.equals("*");
166 result[0] = all ? 0 : Integer.parseInt(s);
167 result[1] = all ? MAX_PORT : result[0];
168 } else {
169 String low = s.substring(0, pos);
170 if (low.length() == 0) low = "*";
171 String high = s.substring(pos+1);
172 if (high.length() == 0) high = "*";
173 result[0] = low.equals("*") ? 0 : Integer.parseInt(low);
174 result[1] = high.equals("*") ? MAX_PORT : Integer.parseInt(high);
175 }
176 return result;
177 } catch (NumberFormatException e) {
178 return new int[0];
179 }
180 }
181
182 private static void fail(String msg, Object... args) {
183 Formatter f = new Formatter();
184 f.format(msg, args);
185 throw new RuntimeException(f.out().toString());
186 }
187
188 // loads rules from the given file
189 // Each non-blank/non-comment line must have the format:
190 // ("bind" | "connect") 1*LWSP-char (hostname | ipaddress["/" prefix])
191 // 1*LWSP-char ("*" | port) [ "-" ("*" | port) ]
192 private static List<Rule> loadRulesFromFile(String file)
193 throws IOException
194 {
195 Scanner scanner = new Scanner(new File(file));
196 try {
197 List<Rule> result = new ArrayList<>();
198 while (scanner.hasNextLine()) {
199 String line = scanner.nextLine().trim();
200
201 // skip blank lines and comments
202 if (line.length() == 0 || line.charAt(0) == '#')
203 continue;
204
205 // must have 3 fields
206 String[] s = line.split("\\s+");
207 if (s.length != 3) {
208 fail("Malformed line '%s'", line);
209 continue;
210 }
211
212 // first field is the action ("bind" or "connect")
213 Action action = null;
214 for (Action a: Action.values()) {
215 if (s[0].equalsIgnoreCase(a.name())) {
216 action = a;
217 break;
218 }
219 }
220 if (action == null) {
221 fail("Action '%s' not recognized", s[0]);
222 continue;
|
60 String file = props.getProperty("com.sun.sdp.conf");
61 if (file == null) {
62 this.enabled = false;
63 this.rules = null;
64 return;
65 }
66
67 // load configuration file
68 List<Rule> list = null;
69 try {
70 list = loadRulesFromFile(file);
71 } catch (IOException e) {
72 fail("Error reading %s: %s", file, e.getMessage());
73 }
74
75 // check if debugging is enabled
76 PrintStream out = null;
77 String logfile = props.getProperty("com.sun.sdp.debug");
78 if (logfile != null) {
79 out = System.out;
80 if (!logfile.isEmpty()) {
81 try {
82 out = new PrintStream(logfile);
83 } catch (IOException ignore) { }
84 }
85 }
86
87 this.enabled = !list.isEmpty();
88 this.rules = list;
89 this.log = out;
90 }
91
92 // supported actions
93 private static enum Action {
94 BIND,
95 CONNECT;
96 }
97
98 // a rule for matching a bind or connect request
99 private static interface Rule {
100 boolean match(Action action, InetAddress address, int port);
150 // check remaining bits
151 if ((prefixByteCount < addressAsBytes.length) &&
152 ((candidate[prefixByteCount] & mask) !=
153 (addressAsBytes[prefixByteCount] & mask)))
154 return false;
155 return super.match(action, address, port);
156 }
157 }
158
159 // parses port:[-end]
160 private static int[] parsePortRange(String s) {
161 int pos = s.indexOf('-');
162 try {
163 int[] result = new int[2];
164 if (pos < 0) {
165 boolean all = s.equals("*");
166 result[0] = all ? 0 : Integer.parseInt(s);
167 result[1] = all ? MAX_PORT : result[0];
168 } else {
169 String low = s.substring(0, pos);
170 if (low.isEmpty()) low = "*";
171 String high = s.substring(pos+1);
172 if (high.isEmpty()) high = "*";
173 result[0] = low.equals("*") ? 0 : Integer.parseInt(low);
174 result[1] = high.equals("*") ? MAX_PORT : Integer.parseInt(high);
175 }
176 return result;
177 } catch (NumberFormatException e) {
178 return new int[0];
179 }
180 }
181
182 private static void fail(String msg, Object... args) {
183 Formatter f = new Formatter();
184 f.format(msg, args);
185 throw new RuntimeException(f.out().toString());
186 }
187
188 // loads rules from the given file
189 // Each non-blank/non-comment line must have the format:
190 // ("bind" | "connect") 1*LWSP-char (hostname | ipaddress["/" prefix])
191 // 1*LWSP-char ("*" | port) [ "-" ("*" | port) ]
192 private static List<Rule> loadRulesFromFile(String file)
193 throws IOException
194 {
195 Scanner scanner = new Scanner(new File(file));
196 try {
197 List<Rule> result = new ArrayList<>();
198 while (scanner.hasNextLine()) {
199 String line = scanner.nextLine().trim();
200
201 // skip blank lines and comments
202 if (line.isEmpty() || line.charAt(0) == '#')
203 continue;
204
205 // must have 3 fields
206 String[] s = line.split("\\s+");
207 if (s.length != 3) {
208 fail("Malformed line '%s'", line);
209 continue;
210 }
211
212 // first field is the action ("bind" or "connect")
213 Action action = null;
214 for (Action a: Action.values()) {
215 if (s[0].equalsIgnoreCase(a.name())) {
216 action = a;
217 break;
218 }
219 }
220 if (action == null) {
221 fail("Action '%s' not recognized", s[0]);
222 continue;
|