blob: d0cd17ff753b6425c04d428f0059c5645cb39e35 [file] [log] [blame]
Ben Gruver4efe9402013-04-02 21:18:41 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.firewall;
18
Ben Gruverf5323fe2013-07-31 15:09:51 -070019import android.content.ComponentName;
Ben Gruver4efe9402013-04-02 21:18:41 -070020import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.net.Uri;
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25
26import java.io.IOException;
27
28class PortFilter implements Filter {
29 private static final String ATTR_EQUALS = "equals";
30 private static final String ATTR_MIN = "min";
31 private static final String ATTR_MAX = "max";
32
33 private static final int NO_BOUND = -1;
34
35 // both bounds are inclusive
36 private final int mLowerBound;
37 private final int mUpperBound;
38
39 private PortFilter(int lowerBound, int upperBound) {
40 mLowerBound = lowerBound;
41 mUpperBound = upperBound;
42 }
43
44 @Override
Ben Gruverf5323fe2013-07-31 15:09:51 -070045 public boolean matches(IntentFirewall ifw, ComponentName resolvedComponent, Intent intent,
46 int callerUid, int callerPid, String resolvedType, ApplicationInfo resolvedApp) {
Ben Gruver4efe9402013-04-02 21:18:41 -070047 int port = -1;
48 Uri uri = intent.getData();
49 if (uri != null) {
50 port = uri.getPort();
51 }
52 return port != -1 &&
53 (mLowerBound == NO_BOUND || mLowerBound <= port) &&
54 (mUpperBound == NO_BOUND || mUpperBound >= port);
55 }
56
57 public static final FilterFactory FACTORY = new FilterFactory("port") {
58 @Override
59 public Filter newFilter(XmlPullParser parser)
60 throws IOException, XmlPullParserException {
61 int lowerBound = NO_BOUND;
62 int upperBound = NO_BOUND;
63
64 String equalsValue = parser.getAttributeValue(null, ATTR_EQUALS);
65 if (equalsValue != null) {
66 int value;
67 try {
68 value = Integer.parseInt(equalsValue);
69 } catch (NumberFormatException ex) {
70 throw new XmlPullParserException("Invalid port value: " + equalsValue,
71 parser, null);
72 }
73 lowerBound = value;
74 upperBound = value;
75 }
76
77 String lowerBoundString = parser.getAttributeValue(null, ATTR_MIN);
78 String upperBoundString = parser.getAttributeValue(null, ATTR_MAX);
79 if (lowerBoundString != null || upperBoundString != null) {
80 if (equalsValue != null) {
81 throw new XmlPullParserException(
82 "Port filter cannot use both equals and range filtering",
83 parser, null);
84 }
85
86 if (lowerBoundString != null) {
87 try {
88 lowerBound = Integer.parseInt(lowerBoundString);
89 } catch (NumberFormatException ex) {
90 throw new XmlPullParserException(
91 "Invalid minimum port value: " + lowerBoundString,
92 parser, null);
93 }
94 }
95
96 if (upperBoundString != null) {
97 try {
98 upperBound = Integer.parseInt(upperBoundString);
99 } catch (NumberFormatException ex) {
100 throw new XmlPullParserException(
101 "Invalid maximum port value: " + upperBoundString,
102 parser, null);
103 }
104 }
105 }
106
107 // an empty port filter is explicitly allowed, and checks for the existence of a port
108 return new PortFilter(lowerBound, upperBound);
109 }
110 };
111}