/src/open5gs/lib/proto/conv.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2019-2022 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "ogs-proto.h" |
21 | | |
22 | | void ogs_extract_digit_from_string(char *digit, char *string) |
23 | 0 | { |
24 | 0 | bool extracting = false; |
25 | 0 | int i = 0; |
26 | |
|
27 | 0 | ogs_assert(string); |
28 | 0 | ogs_assert(digit); |
29 | | |
30 | 0 | while (*string && i < OGS_MAX_IMSI_BCD_LEN) { |
31 | 0 | if (*string >= '0' && *string <= '9') { |
32 | 0 | *digit++ = *string; |
33 | 0 | extracting = true; |
34 | 0 | } else if (extracting == true) { |
35 | 0 | break; |
36 | 0 | } |
37 | 0 | string++; |
38 | 0 | i++; |
39 | 0 | } |
40 | |
|
41 | 0 | *digit = 0; |
42 | 0 | } |
43 | | |
44 | | int ogs_paa_to_ip(const ogs_paa_t *paa, ogs_ip_t *ip) |
45 | 0 | { |
46 | 0 | ogs_assert(paa); |
47 | 0 | ogs_assert(ip); |
48 | | |
49 | 0 | memset(ip, 0, sizeof *ip); |
50 | |
|
51 | 0 | if (paa->session_type == OGS_PDU_SESSION_TYPE_IPV4V6) { |
52 | 0 | ip->ipv4 = 1; |
53 | 0 | ip->addr = paa->both.addr; |
54 | 0 | ip->ipv6 = 1; |
55 | 0 | memcpy(ip->addr6, paa->both.addr6, OGS_IPV6_LEN); |
56 | 0 | } else if (paa->session_type == OGS_PDU_SESSION_TYPE_IPV4) { |
57 | 0 | ip->ipv4 = 1; |
58 | 0 | ip->ipv6 = 0; |
59 | 0 | ip->addr = paa->addr; |
60 | 0 | } else if (paa->session_type == OGS_PDU_SESSION_TYPE_IPV6) { |
61 | 0 | ip->ipv4 = 0; |
62 | 0 | ip->ipv6 = 1; |
63 | 0 | memcpy(ip->addr6, paa->addr6, OGS_IPV6_LEN); |
64 | 0 | } else { |
65 | 0 | ogs_error("No IPv4 or IPv6"); |
66 | 0 | return OGS_ERROR; |
67 | 0 | } |
68 | | |
69 | 0 | return OGS_OK; |
70 | 0 | } |
71 | | |
72 | | int ogs_ip_to_paa(const ogs_ip_t *ip, ogs_paa_t *paa) |
73 | 0 | { |
74 | 0 | if (ip->ipv4 && ip->ipv6) { |
75 | 0 | paa->session_type = OGS_PDU_SESSION_TYPE_IPV4V6; |
76 | 0 | paa->both.addr = ip->addr; |
77 | 0 | memcpy(paa->both.addr6, ip->addr6, OGS_IPV6_LEN); |
78 | 0 | } else if (ip->ipv6) { |
79 | 0 | paa->session_type = OGS_PDU_SESSION_TYPE_IPV6; |
80 | 0 | memcpy(paa->addr6, ip->addr6, OGS_IPV6_LEN); |
81 | 0 | } else if (ip->ipv4) { |
82 | 0 | paa->session_type = OGS_PDU_SESSION_TYPE_IPV4; |
83 | 0 | paa->addr = ip->addr; |
84 | 0 | } else { |
85 | 0 | return OGS_ERROR; |
86 | 0 | } |
87 | 0 | return OGS_OK; |
88 | 0 | } |