3 * Copyright 2019 AT&T Intellectual Property
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.
22 #include <unistd.h> //for close()
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <netinet/sctp.h>
27 #include <arpa/inet.h> //for inet_ntop()
30 #include "e2sim_sctp.h"
32 const int CLIENT_SEND_COUNT = 1;
33 const int SERVER_LISTEN_QUEUE_SIZE = 10;
35 int sctp_start_server(const char *server_ip_str, const int server_port)
37 if(server_port < 1 || server_port > 65535) {
38 fprintf(stderr, "Invalid port number (%d). Valid values are between 1 and 65535.\n", server_port);
43 if((server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1) {
48 struct sockaddr_in server_addr;
49 memset(&server_addr, 0, sizeof(struct sockaddr_in));
50 server_addr.sin_family = AF_INET;
51 server_addr.sin_port = htons(server_port);
52 server_addr.sin_addr.s_addr = inet_addr(server_ip_str);
54 if(bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
59 if(listen(server_fd, SERVER_LISTEN_QUEUE_SIZE) != 0) {
64 assert(server_fd != 0);
66 fprintf(stderr, "SCTP server started on %s:%d\n", server_ip_str, server_port);
71 int sctp_start_client(const char *server_ip_str, const int server_port)
75 if((client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1)
81 struct sockaddr_in peer_addr;
82 memset(&peer_addr, 0, sizeof(struct sockaddr_in));
83 peer_addr.sin_family = AF_INET;
84 peer_addr.sin_port = htons(server_port);
85 peer_addr.sin_addr.s_addr = inet_addr(server_ip_str);
86 // if(inet_pton(AF_INET, server_ip, &(peer_addr.sin_addr)) != 1) {
87 // printf("Error converting IP address (%s) to sockaddr_in structure\n", server_ip);
91 fprintf(stderr, "Connecting to server at %s:%d ...", server_ip_str, server_port);
92 if(connect(client_fd, (struct sockaddr*)&peer_addr, sizeof(peer_addr)) == -1) {
97 assert(client_fd != 0);
99 fprintf(stderr, "OK\n");
105 //ssize_t sctp_send_to_socket(int sockfd, const void* buf, size_t len)
106 int sctp_send_to_socket(int sockfd, const void* buf, size_t len)
110 sent_len = send(sockfd, buf, len, 0);
114 perror("sctp_send_to_socket");
121 // int sctp_recv_from_socket(int sockfd, void *buf, size_t buf_size)
125 // memset(buf, 0, buf_size);
127 // int len = recv(sockfd, &buf, buf_size, 0);
130 // perror("sctp_recv_from_socket");
139 void client_send_multiple_test_msg(int client_fd)
144 for(int i = 0; i < SEND_COUNT; i++)
146 fprintf(stderr, "Sending message %d of %d \n", i+1, SEND_COUNT);
148 memset(buf, 0, sizeof(buf));
149 snprintf(buf, sizeof(buf)-1, "DATA %d", i+1);
151 if(send(client_fd, &buf, strlen(buf), 0) == -1) {
156 memset(buf, 0, sizeof(buf));
158 if(recv(client_fd, &buf, sizeof(buf), 0) == -1) {
163 fprintf(stderr, "Server reply: %s\n", buf);
168 fprintf(stderr, "Closing...\n");
169 if(close(client_fd) == -1) {