1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
4 # Copyright 2019 Nokia *
6 # Licensed under the Apache License, Version 2.0 (the "License"); *
7 # you may not use this file except in compliance with the License. *
8 # You may obtain a copy of the License at *
10 # http://www.apache.org/licenses/LICENSE-2.0 *
12 # Unless required by applicable law or agreed to in writing, software *
13 # distributed under the License is distributed on an "AS IS" BASIS, *
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 # See the License for the specific language governing permissions and *
16 # limitations under the License. *
18 ******************************************************************************/
21 #include <unistd.h> //for close()
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/sctp.h>
26 #include <arpa/inet.h> //for inet_ntop()
29 #include "e2sim_sctp.hpp"
30 // #include "e2sim_defs.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/sctp.h>
42 int sctp_start_server(const char *server_ip_str, const int server_port)
44 if(server_port < 1 || server_port > 65535) {
45 LOG_E("Invalid port number (%d). Valid values are between 1 and 65535.\n", server_port);
50 struct sockaddr* server_addr;
53 struct sockaddr_in server4_addr;
54 memset(&server4_addr, 0, sizeof(struct sockaddr_in));
56 struct sockaddr_in6 server6_addr;
57 memset(&server6_addr, 0, sizeof(struct sockaddr_in6));
59 if(inet_pton(AF_INET, server_ip_str, &server4_addr.sin_addr) == 1)
61 server4_addr.sin_family = AF_INET;
62 server4_addr.sin_port = htons(server_port);
64 server_addr = (struct sockaddr*)&server4_addr;
66 addr_len = sizeof(server4_addr);
68 else if(inet_pton(AF_INET6, server_ip_str, &server6_addr.sin6_addr) == 1)
70 server6_addr.sin6_family = AF_INET6;
71 server6_addr.sin6_port = htons(server_port);
73 server_addr = (struct sockaddr*)&server6_addr;
75 addr_len = sizeof(server6_addr);
78 perror("inet_pton()");
82 if((server_fd = socket(af, SOCK_STREAM, IPPROTO_SCTP)) == -1) {
88 // int sendbuff = 10000;
89 // socklen_t optlen = sizeof(sendbuff);
90 // if(getsockopt(server_fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen) == -1) {
91 // perror("getsockopt send");
95 // LOG_D("[SCTP] send buffer size = %d\n", sendbuff);
98 if(bind(server_fd, server_addr, addr_len) == -1) {
103 if(listen(server_fd, SERVER_LISTEN_QUEUE_SIZE) != 0) {
108 assert(server_fd != 0);
110 LOG_I("[SCTP] Server started on %s:%d", server_ip_str, server_port);
115 int sctp_start_client(const char *server_ip_str, const int server_port)
119 struct sockaddr* server_addr;
122 struct sockaddr_in server4_addr;
123 memset(&server4_addr, 0, sizeof(struct sockaddr_in));
125 struct sockaddr_in6 server6_addr;
126 memset(&server6_addr, 0, sizeof(struct sockaddr_in6));
128 if(inet_pton(AF_INET, server_ip_str, &server4_addr.sin_addr) == 1)
130 server4_addr.sin_family = AF_INET;
131 server4_addr.sin_port = htons(server_port);
132 server_addr = (struct sockaddr*)&server4_addr;
133 addr_len = sizeof(server4_addr);
135 else if(inet_pton(AF_INET6, server_ip_str, &server6_addr.sin6_addr) == 1)
137 server6_addr.sin6_family = AF_INET6;
138 server6_addr.sin6_port = htons(server_port);
139 server_addr = (struct sockaddr*)&server6_addr;
140 addr_len = sizeof(server6_addr);
143 perror("inet_pton()");
147 if((client_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP)) == -1)
153 // int sendbuff = 10000;
154 // socklen_t optlen = sizeof(sendbuff);
155 // if(getsockopt(client_fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen) == -1) {
156 // perror("getsockopt send");
160 // LOG_D("[SCTP] send buffer size = %d\n", sendbuff);
162 //--------------------------------
163 //Bind before connect
165 if( setsockopt(client_fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof optval) != 0 ){
166 perror("setsockopt port");
170 if( setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) != 0 ){
171 perror("setsockopt addr");
175 struct sockaddr_in6 client6_addr {};
176 client6_addr.sin6_family = AF_INET6;
177 client6_addr.sin6_port = htons(RIC_SCTP_SRC_PORT);
178 client6_addr.sin6_addr = in6addr_any;
180 LOG_I("[SCTP] Binding client socket to source port %d", RIC_SCTP_SRC_PORT);
181 if(bind(client_fd, (struct sockaddr*)&client6_addr, sizeof(client6_addr)) == -1) {
185 // end binding ---------------------
187 LOG_I("[SCTP] Connecting to server at %s:%d ...", server_ip_str, server_port);
188 if(connect(client_fd, server_addr, addr_len) == -1) {
192 assert(client_fd != 0);
194 LOG_I("[SCTP] Connection established");
199 int sctp_accept_connection(const char *server_ip_str, const int server_fd)
201 LOG_I("[SCTP] Waiting for new connection...");
203 struct sockaddr client_addr;
204 socklen_t client_addr_size;
208 client_fd = accept(server_fd, &client_addr, &client_addr_size);
215 //Retrieve client IP_ADDR
216 char client_ip6_addr[INET6_ADDRSTRLEN], client_ip4_addr[INET_ADDRSTRLEN];
217 if(strchr(server_ip_str, ':') != NULL) //IPv6
219 struct sockaddr_in6* client_ipv6 = (struct sockaddr_in6*)&client_addr;
220 inet_ntop(AF_INET6, &(client_ipv6->sin6_addr), client_ip6_addr, INET6_ADDRSTRLEN);
221 LOG_I("[SCTP] New client connected from %s", client_ip6_addr);
224 struct sockaddr_in* client_ipv4 = (struct sockaddr_in*)&client_addr;
225 inet_ntop(AF_INET, &(client_ipv4->sin_addr), client_ip4_addr, INET_ADDRSTRLEN);
226 LOG_I("[SCTP] New client connected from %s", client_ip4_addr);
232 int sctp_send_data(int &socket_fd, sctp_buffer_t &data)
234 int sent_len = send(socket_fd, (void*)(&(data.buffer[0])), data.len, 0);
237 perror("[SCTP] sctp_send_data");
244 int sctp_send_data_X2AP(int &socket_fd, sctp_buffer_t &data)
246 int sent_len = sctp_sendmsg(socket_fd, (void*)(&(data.buffer[0])), data.len,
247 NULL, 0, (uint32_t) X2AP_PPID, 0, 0, 0, 0);
250 perror("[SCTP] sctp_send_data");
257 Receive data from SCTP socket
260 0: close the connection
263 int sctp_receive_data(int &socket_fd, sctp_buffer_t &data)
265 //clear out the data before receiving
266 memset(data.buffer, 0, sizeof(data.buffer));
269 //receive data from the socket
270 int recv_len = recv(socket_fd, &(data.buffer), sizeof(data.buffer), 0);
273 perror("[SCTP] recv");
276 else if (recv_len == 0)
278 LOG_I("[SCTP] Connection closed by remote peer");
279 if(close(socket_fd) == -1)
281 perror("[SCTP] close");