1/* 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#import "RTCIceServer+Private.h" 12 13#import "helpers/NSString+StdString.h" 14 15@implementation RTC_OBJC_TYPE (RTCIceServer) 16 17@synthesize urlStrings = _urlStrings; 18@synthesize username = _username; 19@synthesize credential = _credential; 20@synthesize tlsCertPolicy = _tlsCertPolicy; 21@synthesize hostname = _hostname; 22@synthesize tlsAlpnProtocols = _tlsAlpnProtocols; 23@synthesize tlsEllipticCurves = _tlsEllipticCurves; 24 25- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings { 26 return [self initWithURLStrings:urlStrings 27 username:nil 28 credential:nil]; 29} 30 31- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings 32 username:(NSString *)username 33 credential:(NSString *)credential { 34 return [self initWithURLStrings:urlStrings 35 username:username 36 credential:credential 37 tlsCertPolicy:RTCTlsCertPolicySecure]; 38} 39 40- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings 41 username:(NSString *)username 42 credential:(NSString *)credential 43 tlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy { 44 return [self initWithURLStrings:urlStrings 45 username:username 46 credential:credential 47 tlsCertPolicy:tlsCertPolicy 48 hostname:nil]; 49} 50 51- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings 52 username:(NSString *)username 53 credential:(NSString *)credential 54 tlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy 55 hostname:(NSString *)hostname { 56 return [self initWithURLStrings:urlStrings 57 username:username 58 credential:credential 59 tlsCertPolicy:tlsCertPolicy 60 hostname:hostname 61 tlsAlpnProtocols:[NSArray array]]; 62} 63 64- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings 65 username:(NSString *)username 66 credential:(NSString *)credential 67 tlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy 68 hostname:(NSString *)hostname 69 tlsAlpnProtocols:(NSArray<NSString *> *)tlsAlpnProtocols { 70 return [self initWithURLStrings:urlStrings 71 username:username 72 credential:credential 73 tlsCertPolicy:tlsCertPolicy 74 hostname:hostname 75 tlsAlpnProtocols:tlsAlpnProtocols 76 tlsEllipticCurves:[NSArray array]]; 77} 78 79- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings 80 username:(NSString *)username 81 credential:(NSString *)credential 82 tlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy 83 hostname:(NSString *)hostname 84 tlsAlpnProtocols:(NSArray<NSString *> *)tlsAlpnProtocols 85 tlsEllipticCurves:(NSArray<NSString *> *)tlsEllipticCurves { 86 NSParameterAssert(urlStrings.count); 87 if (self = [super init]) { 88 _urlStrings = [[NSArray alloc] initWithArray:urlStrings copyItems:YES]; 89 _username = [username copy]; 90 _credential = [credential copy]; 91 _tlsCertPolicy = tlsCertPolicy; 92 _hostname = [hostname copy]; 93 _tlsAlpnProtocols = [[NSArray alloc] initWithArray:tlsAlpnProtocols copyItems:YES]; 94 _tlsEllipticCurves = [[NSArray alloc] initWithArray:tlsEllipticCurves copyItems:YES]; 95 } 96 return self; 97} 98 99- (NSString *)description { 100 return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCIceServer):\n%@\n%@\n%@\n%@\n%@\n%@\n%@", 101 _urlStrings, 102 _username, 103 _credential, 104 [self stringForTlsCertPolicy:_tlsCertPolicy], 105 _hostname, 106 _tlsAlpnProtocols, 107 _tlsEllipticCurves]; 108} 109 110#pragma mark - Private 111 112- (NSString *)stringForTlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy { 113 switch (tlsCertPolicy) { 114 case RTCTlsCertPolicySecure: 115 return @"RTCTlsCertPolicySecure"; 116 case RTCTlsCertPolicyInsecureNoCheck: 117 return @"RTCTlsCertPolicyInsecureNoCheck"; 118 } 119} 120 121- (webrtc::PeerConnectionInterface::IceServer)nativeServer { 122 __block webrtc::PeerConnectionInterface::IceServer iceServer; 123 124 iceServer.username = [NSString stdStringForString:_username]; 125 iceServer.password = [NSString stdStringForString:_credential]; 126 iceServer.hostname = [NSString stdStringForString:_hostname]; 127 128 [_tlsAlpnProtocols enumerateObjectsUsingBlock:^(NSString *proto, NSUInteger idx, BOOL *stop) { 129 iceServer.tls_alpn_protocols.push_back(proto.stdString); 130 }]; 131 132 [_tlsEllipticCurves enumerateObjectsUsingBlock:^(NSString *curve, NSUInteger idx, BOOL *stop) { 133 iceServer.tls_elliptic_curves.push_back(curve.stdString); 134 }]; 135 136 [_urlStrings enumerateObjectsUsingBlock:^(NSString *url, 137 NSUInteger idx, 138 BOOL *stop) { 139 iceServer.urls.push_back(url.stdString); 140 }]; 141 142 switch (_tlsCertPolicy) { 143 case RTCTlsCertPolicySecure: 144 iceServer.tls_cert_policy = 145 webrtc::PeerConnectionInterface::kTlsCertPolicySecure; 146 break; 147 case RTCTlsCertPolicyInsecureNoCheck: 148 iceServer.tls_cert_policy = 149 webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck; 150 break; 151 } 152 return iceServer; 153} 154 155- (instancetype)initWithNativeServer: 156 (webrtc::PeerConnectionInterface::IceServer)nativeServer { 157 NSMutableArray *urls = 158 [NSMutableArray arrayWithCapacity:nativeServer.urls.size()]; 159 for (auto const &url : nativeServer.urls) { 160 [urls addObject:[NSString stringForStdString:url]]; 161 } 162 NSString *username = [NSString stringForStdString:nativeServer.username]; 163 NSString *credential = [NSString stringForStdString:nativeServer.password]; 164 NSString *hostname = [NSString stringForStdString:nativeServer.hostname]; 165 NSMutableArray *tlsAlpnProtocols = 166 [NSMutableArray arrayWithCapacity:nativeServer.tls_alpn_protocols.size()]; 167 for (auto const &proto : nativeServer.tls_alpn_protocols) { 168 [tlsAlpnProtocols addObject:[NSString stringForStdString:proto]]; 169 } 170 NSMutableArray *tlsEllipticCurves = 171 [NSMutableArray arrayWithCapacity:nativeServer.tls_elliptic_curves.size()]; 172 for (auto const &curve : nativeServer.tls_elliptic_curves) { 173 [tlsEllipticCurves addObject:[NSString stringForStdString:curve]]; 174 } 175 RTCTlsCertPolicy tlsCertPolicy; 176 177 switch (nativeServer.tls_cert_policy) { 178 case webrtc::PeerConnectionInterface::kTlsCertPolicySecure: 179 tlsCertPolicy = RTCTlsCertPolicySecure; 180 break; 181 case webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck: 182 tlsCertPolicy = RTCTlsCertPolicyInsecureNoCheck; 183 break; 184 } 185 186 self = [self initWithURLStrings:urls 187 username:username 188 credential:credential 189 tlsCertPolicy:tlsCertPolicy 190 hostname:hostname 191 tlsAlpnProtocols:tlsAlpnProtocols 192 tlsEllipticCurves:tlsEllipticCurves]; 193 return self; 194} 195 196@end 197