rust_rcs_core/third_gen_pp/
addressing.rs

1// Copyright 2023 宋昊文
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::{CountryCode, NetworkCode, ThreeDigit};
16
17pub fn impi_from_imsi(imsi: &str, mcc: CountryCode, mnc: NetworkCode) -> String {
18    format!(
19        "{}@ims.mnc{}.mcc{}.3gppnetwork.org",
20        imsi,
21        mnc.string_repr(),
22        mcc.string_repr()
23    )
24}
25
26pub fn tmpu_from_impi(impi: &str) -> String {
27    format!("sip:{}", impi)
28}
29
30pub fn bsf_address(mcc: CountryCode, mnc: NetworkCode) -> String {
31    format!(
32        "bsf.mnc{}.mcc{}.pub.3gppnetwork.org",
33        mnc.string_repr(),
34        mcc.string_repr()
35    )
36}
37
38const BSF_PREFIX: &str = "bsf.";
39
40const SUFFIX: &str = "3gppnetwork.org";
41
42const DOT_SUFFIX: &str = ".3gppnetwork.org";
43
44const PUBLIC_SUFFIX: &str = "pub.3gppnetwork.org";
45
46const DOT_PUBLIC_SUFFIX: &str = ".pub.3gppnetwork.org";
47
48fn make_public_3gpp_domain(domain: &str) -> String {
49    if domain != PUBLIC_SUFFIX && !domain.ends_with(DOT_PUBLIC_SUFFIX) {
50        let length = domain.len() - SUFFIX.len();
51
52        return format!("{}{}", &domain[..length], PUBLIC_SUFFIX);
53    }
54
55    String::from(domain)
56}
57
58pub fn bsf_address_from_impi(impi: &str) -> Option<String> {
59    if let Some(idx) = impi.find('@') {
60        let domain = &impi[idx + 1..];
61
62        if domain == SUFFIX {
63            return Some(String::from("bsf.pub.3gppnetwork.org"));
64        } else if domain.ends_with(DOT_SUFFIX) {
65            let domain = make_public_3gpp_domain(domain);
66
67            if domain.starts_with(BSF_PREFIX) {
68                return Some(domain);
69            } else {
70                return Some(format!("{}{}", BSF_PREFIX, domain));
71            }
72        } else {
73            return Some(format!("{}{}", BSF_PREFIX, domain));
74        }
75    }
76
77    None
78}