1 /* 2 * Copyright (C) 2021 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 17 package com.android.services.telephony.rcs.validator; 18 19 import android.telephony.ims.SipDelegateManager; 20 import android.telephony.ims.SipMessage; 21 import android.util.ArrayMap; 22 23 /** 24 * Tracks the incoming SIP message transport state from the ImsService to the remote IMS 25 * application. Validates incoming SIP messages based on this state. 26 */ 27 public class IncomingTransportStateValidator implements SipMessageValidator { 28 29 /** 30 * The message transport is closed, meaning there can be no more incoming messages 31 */ 32 private static final int STATE_CLOSED = 0; 33 34 /** 35 * The message transport is open and incoming traffic is not restricted. 36 */ 37 private static final int STATE_OPEN = 1; 38 39 private static final ArrayMap<Integer, String> ENUM_TO_STRING_MAP = new ArrayMap<>(2); 40 static { ENUM_TO_STRING_MAP.append(STATE_CLOSED, "CLOSED")41 ENUM_TO_STRING_MAP.append(STATE_CLOSED, "CLOSED"); ENUM_TO_STRING_MAP.append(STATE_OPEN, "OPEN")42 ENUM_TO_STRING_MAP.append(STATE_OPEN, "OPEN"); 43 } 44 45 private int mState = STATE_CLOSED; 46 private int mReason = SipDelegateManager.MESSAGE_FAILURE_REASON_DELEGATE_CLOSED; 47 48 /** 49 * The SIP message transport is open and will successfully validate SIP messages. 50 */ open()51 public void open() { 52 mState = STATE_OPEN; 53 mReason = -1; 54 } 55 56 /** 57 * The SIP message transport is closed for incoming SIP messages. 58 * @param reason The error reason sent in response to any incoming SIP messages requests. 59 */ close(int reason)60 public void close(int reason) { 61 mState = STATE_CLOSED; 62 mReason = reason; 63 } 64 65 @Override validate(SipMessage message)66 public ValidationResult validate(SipMessage message) { 67 if (mState != STATE_OPEN) { 68 return new ValidationResult(mReason, 69 "incoming transport closed"); 70 } 71 return ValidationResult.SUCCESS; 72 } 73 74 @Override toString()75 public String toString() { 76 return "Incoming Transport State: " + ENUM_TO_STRING_MAP.getOrDefault(mState, 77 String.valueOf(mState)) + ", reason: " 78 + SipDelegateManager.MESSAGE_FAILURE_REASON_STRING_MAP.getOrDefault(mReason, 79 String.valueOf(mReason)); 80 } 81 } 82