/src/open5gs/lib/core/ogs-tcp.c
Line | Count | Source |
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_tcp_server( |
26 | | ogs_sockaddr_t *sa_list, ogs_sockopt_t *socket_option) |
27 | 0 | { |
28 | 0 | int rv; |
29 | 0 | char buf[OGS_ADDRSTRLEN]; |
30 | |
|
31 | 0 | ogs_sock_t *new = NULL; |
32 | 0 | ogs_sockaddr_t *addr; |
33 | 0 | ogs_sockopt_t option; |
34 | |
|
35 | 0 | ogs_assert(sa_list); |
36 | | |
37 | 0 | ogs_sockopt_init(&option); |
38 | 0 | if (socket_option) |
39 | 0 | memcpy(&option, socket_option, sizeof option); |
40 | |
|
41 | 0 | addr = sa_list; |
42 | 0 | while(addr) { |
43 | 0 | new = ogs_sock_socket(addr->ogs_sa_family, SOCK_STREAM, IPPROTO_TCP); |
44 | 0 | if (new) { |
45 | 0 | if (option.tcp_nodelay == true) { |
46 | 0 | rv = ogs_tcp_nodelay(new->fd, true); |
47 | 0 | ogs_assert(rv == OGS_OK); |
48 | 0 | } else |
49 | 0 | ogs_warn("TCP NO_DELAY Disabled"); |
50 | | |
51 | 0 | if (option.so_linger.l_onoff == true) { |
52 | 0 | rv = ogs_so_linger(new->fd, option.so_linger.l_linger); |
53 | 0 | ogs_assert(rv == OGS_OK); |
54 | 0 | } |
55 | | |
56 | 0 | rv = ogs_listen_reusable(new->fd, true); |
57 | 0 | ogs_assert(rv == OGS_OK); |
58 | | |
59 | 0 | if (ogs_sock_bind(new, addr) == OGS_OK) { |
60 | 0 | ogs_debug("tcp_server() [%s]:%d", |
61 | 0 | OGS_ADDR(addr, buf), OGS_PORT(addr)); |
62 | 0 | break; |
63 | 0 | } |
64 | | |
65 | 0 | ogs_sock_destroy(new); |
66 | 0 | } |
67 | | |
68 | 0 | addr = addr->next; |
69 | 0 | } |
70 | | |
71 | 0 | if (addr == NULL) { |
72 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, |
73 | 0 | "tcp_server() [%s]:%d failed", |
74 | 0 | OGS_ADDR(sa_list, buf), OGS_PORT(sa_list)); |
75 | 0 | return NULL; |
76 | 0 | } |
77 | | |
78 | 0 | rv = ogs_sock_listen(new); |
79 | 0 | ogs_assert(rv == OGS_OK); |
80 | | |
81 | 0 | return new; |
82 | 0 | } |
83 | | |
84 | | ogs_sock_t *ogs_tcp_client( |
85 | | ogs_sockaddr_t *sa_list, ogs_sockopt_t *socket_option) |
86 | 0 | { |
87 | 0 | int rv; |
88 | 0 | char buf[OGS_ADDRSTRLEN]; |
89 | |
|
90 | 0 | ogs_sock_t *new = NULL; |
91 | 0 | ogs_sockaddr_t *addr; |
92 | 0 | ogs_sockopt_t option; |
93 | |
|
94 | 0 | ogs_assert(sa_list); |
95 | | |
96 | 0 | ogs_sockopt_init(&option); |
97 | 0 | if (socket_option) |
98 | 0 | memcpy(&option, socket_option, sizeof option); |
99 | |
|
100 | 0 | addr = sa_list; |
101 | 0 | while (addr) { |
102 | 0 | new = ogs_sock_socket(addr->ogs_sa_family, SOCK_STREAM, IPPROTO_TCP); |
103 | 0 | if (new) { |
104 | 0 | if (option.sctp_nodelay == true) { |
105 | 0 | rv = ogs_tcp_nodelay(new->fd, true); |
106 | 0 | ogs_assert(rv == OGS_OK); |
107 | 0 | } else |
108 | 0 | ogs_warn("TCP NO_DELAY Disabled"); |
109 | | |
110 | 0 | if (option.so_linger.l_onoff == true) { |
111 | 0 | rv = ogs_so_linger(new->fd, option.so_linger.l_linger); |
112 | 0 | ogs_assert(rv == OGS_OK); |
113 | 0 | } |
114 | | |
115 | 0 | if (ogs_sock_connect(new, addr) == OGS_OK) { |
116 | 0 | ogs_debug("tcp_client() [%s]:%d", |
117 | 0 | OGS_ADDR(addr, buf), OGS_PORT(addr)); |
118 | 0 | break; |
119 | 0 | } |
120 | | |
121 | 0 | ogs_sock_destroy(new); |
122 | 0 | } |
123 | | |
124 | 0 | addr = addr->next; |
125 | 0 | } |
126 | | |
127 | 0 | if (addr == NULL) { |
128 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, |
129 | 0 | "tcp_client() [%s]:%d failed", |
130 | 0 | OGS_ADDR(sa_list, buf), OGS_PORT(sa_list)); |
131 | 0 | return NULL; |
132 | 0 | } |
133 | | |
134 | 0 | return new; |
135 | 0 | } |