1 /* 2 * Copyright (C) 2020 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.libraries.rcs.simpleclient.protocol.sdp; 18 19 import static com.android.libraries.rcs.simpleclient.protocol.sip.SipUtils.isIPv6Address; 20 21 import com.android.libraries.rcs.simpleclient.protocol.msrp.MsrpUtils; 22 23 import com.google.common.base.Joiner; 24 import com.google.common.collect.ImmutableSet; 25 26 /** Collections of utility functions for SDP */ 27 public final class SdpUtils { 28 public static final String SDP_CONTENT_TYPE = "application"; 29 public static final String SDP_CONTENT_SUB_TYPE = "sdp"; 30 31 private static final ImmutableSet<String> DEFAULT_ACCEPT_TYPES = 32 ImmutableSet.of("message/cpim", "application/im-iscomposing+xml"); 33 private static final ImmutableSet<String> DEFAULT_ACCEPT_WRAPPED_TYPES = 34 ImmutableSet.of( 35 "text/plain", 36 "message/imdn+xml", 37 "application/vnd.gsma.rcs-ft-http+xml", 38 "application/vnd.gsma.rcspushlocation+xml"); 39 40 private static final String DEFAULT_NAME = "message"; 41 private static final String DEFAULT_SETUP = "active"; 42 private static final String DEFAULT_DIRECTION = "sendrecv"; 43 private static final int DEFAULT_MSRP_PORT = 9; 44 private static final String PROTOCOL_TCP_MSRP = "TCP/MSRP"; 45 private static final String PROTOCOL_TLS_MSRP = "TCP/TLS/MSRP"; 46 private static final String DEFAULT_FORMAT = "*"; 47 48 private static final String ATTRIBUTE_PATH = "path"; 49 private static final String ATTRIBUTE_SETUP = "setup"; 50 private static final String ATTRIBUTE_ACCEPT_TYPES = "accept-types"; 51 private static final String ATTRIBUTE_ACCEPT_WRAPPED_TYPES = "accept-wrapped-types"; 52 SdpUtils()53 private SdpUtils() { 54 } 55 56 /** 57 * Create a simple SDP message for MSRP. Most attributes except address and transport type 58 * will be 59 * generated automatically. 60 * 61 * @param address The local IP address of the MSRP connection. 62 * @param isTls True if the MSRP connection uses TLS. 63 */ createSdpForMsrp(String address, boolean isTls)64 public static SimpleSdpMessage createSdpForMsrp(String address, boolean isTls) { 65 return SimpleSdpMessage.newBuilder() 66 .setVersion("0") 67 .setOrigin(generateOrigin(address)) 68 .setSession("-") 69 .setConnection(generateConnection(address)) 70 .setTime("0 0") 71 .addMedia(createSdpMediaForMsrp(address, isTls)) 72 .build(); 73 } 74 generateOrigin(String address)75 private static String generateOrigin(String address) { 76 StringBuilder builder = new StringBuilder(); 77 builder 78 .append("TestRcsClient ") 79 .append(System.currentTimeMillis()) 80 .append(" ") 81 .append(System.currentTimeMillis()) 82 .append(" IN ") 83 .append(isIPv6Address(address) ? "IP6 " : "IP4 ") 84 .append(address); 85 86 return builder.toString(); 87 } 88 generateConnection(String address)89 private static String generateConnection(String address) { 90 return "IN " + (isIPv6Address(address) ? "IP6 " : "IP4 ") + address; 91 } 92 93 /** 94 * Create a media part of the SDP message for MSRP. Most attributes except address and transport 95 * type will be generated automatically. 96 * 97 * @param address The local IP address of the MSRP connection. 98 * @param isTls True if the MSRP connection uses TLS. 99 */ createSdpMediaForMsrp(String address, boolean isTls)100 public static SdpMedia createSdpMediaForMsrp(String address, boolean isTls) { 101 return SdpMedia.newBuilder() 102 .setName(DEFAULT_NAME) 103 .setPort(DEFAULT_MSRP_PORT) 104 .setProtocol(isTls ? PROTOCOL_TLS_MSRP : PROTOCOL_TCP_MSRP) 105 .setFormat(DEFAULT_FORMAT) 106 .addAttribute(ATTRIBUTE_PATH, 107 MsrpUtils.generatePath(address, DEFAULT_MSRP_PORT, isTls)) 108 .addAttribute(ATTRIBUTE_SETUP, DEFAULT_SETUP) 109 .addAttribute(ATTRIBUTE_ACCEPT_TYPES, Joiner.on(" ").join(DEFAULT_ACCEPT_TYPES)) 110 .addAttribute( 111 ATTRIBUTE_ACCEPT_WRAPPED_TYPES, 112 Joiner.on(" ").join(DEFAULT_ACCEPT_WRAPPED_TYPES)) 113 .addAttribute(DEFAULT_DIRECTION) 114 .build(); 115 } 116 } 117