/src/open5gs/lib/core/ogs-udp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2019 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-core.h" |
21 | | |
22 | | #undef OGS_LOG_DOMAIN |
23 | 0 | #define OGS_LOG_DOMAIN __ogs_sock_domain |
24 | | |
25 | | ogs_sock_t *ogs_udp_server( |
26 | | ogs_sockaddr_t *sa_list, ogs_sockopt_t *socket_option) |
27 | 0 | { |
28 | 0 | char buf[OGS_ADDRSTRLEN]; |
29 | |
|
30 | 0 | ogs_sock_t *new = NULL; |
31 | 0 | ogs_sockaddr_t *addr; |
32 | 0 | ogs_sockopt_t option; |
33 | |
|
34 | 0 | ogs_assert(sa_list); |
35 | | |
36 | 0 | ogs_sockopt_init(&option); |
37 | 0 | if (socket_option) |
38 | 0 | memcpy(&option, socket_option, sizeof option); |
39 | |
|
40 | 0 | addr = sa_list; |
41 | 0 | while (addr) { |
42 | 0 | new = ogs_sock_socket(addr->ogs_sa_family, SOCK_DGRAM, IPPROTO_UDP); |
43 | 0 | if (!new) { |
44 | 0 | addr = addr->next; |
45 | 0 | continue; |
46 | 0 | } |
47 | 0 | if (ogs_sock_bind(new, addr) != OGS_OK) { |
48 | 0 | ogs_sock_destroy(new); |
49 | 0 | addr = addr->next; |
50 | 0 | continue; |
51 | 0 | } |
52 | 0 | ogs_debug("udp_server() [%s]:%d", OGS_ADDR(addr, buf), OGS_PORT(addr)); |
53 | 0 | if (option.so_bindtodevice) { |
54 | 0 | if (ogs_bind_to_device(new->fd, option.so_bindtodevice) != OGS_OK) { |
55 | 0 | ogs_sock_destroy(new); |
56 | 0 | addr = addr->next; |
57 | 0 | continue; |
58 | 0 | } |
59 | 0 | ogs_info("udp_server() [%s]:%d bound to device `%s`", |
60 | 0 | OGS_ADDR(addr, buf), OGS_PORT(addr), |
61 | 0 | option.so_bindtodevice); |
62 | 0 | } |
63 | 0 | break; |
64 | 0 | } |
65 | |
|
66 | 0 | if (addr == NULL) { |
67 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, |
68 | 0 | "udp_server() [%s]:%d failed", |
69 | 0 | OGS_ADDR(sa_list, buf), OGS_PORT(sa_list)); |
70 | 0 | return NULL; |
71 | 0 | } |
72 | | |
73 | 0 | return new; |
74 | 0 | } |
75 | | |
76 | | ogs_sock_t *ogs_udp_client( |
77 | | ogs_sockaddr_t *sa_list, ogs_sockopt_t *socket_option) |
78 | 0 | { |
79 | 0 | char buf[OGS_ADDRSTRLEN]; |
80 | |
|
81 | 0 | ogs_sock_t *new = NULL; |
82 | 0 | ogs_sockaddr_t *addr; |
83 | |
|
84 | 0 | ogs_sockopt_t option; |
85 | |
|
86 | 0 | ogs_assert(sa_list); |
87 | | |
88 | 0 | ogs_sockopt_init(&option); |
89 | 0 | if (socket_option) |
90 | 0 | memcpy(&option, socket_option, sizeof option); |
91 | |
|
92 | 0 | addr = sa_list; |
93 | 0 | while (addr) { |
94 | 0 | new = ogs_sock_socket(addr->ogs_sa_family, SOCK_DGRAM, IPPROTO_UDP); |
95 | 0 | if (new) { |
96 | 0 | if (ogs_sock_connect(new, addr) == OGS_OK) { |
97 | 0 | ogs_debug("udp_client() [%s]:%d", |
98 | 0 | OGS_ADDR(addr, buf), OGS_PORT(addr)); |
99 | 0 | break; |
100 | 0 | } |
101 | | |
102 | 0 | ogs_sock_destroy(new); |
103 | 0 | } |
104 | | |
105 | 0 | addr = addr->next; |
106 | 0 | } |
107 | |
|
108 | 0 | if (addr == NULL) { |
109 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, |
110 | 0 | "udp_client() [%s]:%d failed", |
111 | 0 | OGS_ADDR(sa_list, buf), OGS_PORT(sa_list)); |
112 | 0 | return NULL;; |
113 | 0 | } |
114 | | |
115 | 0 | return new; |
116 | 0 | } |
117 | | |
118 | | int ogs_udp_connect(ogs_sock_t *sock, ogs_sockaddr_t *sa_list) |
119 | 0 | { |
120 | 0 | ogs_sockaddr_t *addr; |
121 | 0 | char buf[OGS_ADDRSTRLEN]; |
122 | |
|
123 | 0 | ogs_assert(sock); |
124 | 0 | ogs_assert(sa_list); |
125 | | |
126 | 0 | addr = sa_list; |
127 | 0 | while (addr) { |
128 | 0 | if (ogs_sock_connect(sock, addr) == OGS_OK) { |
129 | 0 | ogs_debug("udp_connect() [%s]:%d", |
130 | 0 | OGS_ADDR(addr, buf), OGS_PORT(addr)); |
131 | 0 | break; |
132 | 0 | } |
133 | | |
134 | 0 | addr = addr->next; |
135 | 0 | } |
136 | |
|
137 | 0 | if (addr == NULL) { |
138 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, |
139 | 0 | "udp_connect() [%s]:%d failed", |
140 | 0 | OGS_ADDR(sa_list, buf), OGS_PORT(sa_list)); |
141 | 0 | return OGS_ERROR; |
142 | 0 | } |
143 | | |
144 | 0 | return OGS_OK; |
145 | 0 | } |