Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / SCTP / e2sim_sctp.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>             //for close()
23 #include <stdlib.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <netinet/sctp.h>
27 #include <arpa/inet.h>  //for inet_ntop()
28 #include <assert.h>
29
30 #include "e2sim_sctp.h"
31
32 const int CLIENT_SEND_COUNT         = 1;
33 const int SERVER_LISTEN_QUEUE_SIZE  = 10;
34
35 int sctp_start_server(const char *server_ip_str, const int server_port)
36 {
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);
39       return -1;
40   }
41
42   int server_fd;
43   if((server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1) {
44     perror("socket");
45     return -1;
46   }
47
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);
53
54   if(bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
55     perror("bind");
56     return -1;
57   }
58
59   if(listen(server_fd, SERVER_LISTEN_QUEUE_SIZE) != 0) {
60     perror("listen");
61     return -1;
62   }
63
64   assert(server_fd != 0);
65
66   fprintf(stderr, "SCTP server started on %s:%d\n", server_ip_str, server_port);
67
68   return server_fd;
69 }
70
71 int sctp_start_client(const char *server_ip_str, const int server_port)
72 {
73   int client_fd;
74
75   if((client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1)
76   {
77      perror("socket");
78      return -1;
79   }
80
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);
88   //     return 0;
89   // }
90
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) {
93     perror("connect");
94     return -1;
95   }
96
97   assert(client_fd != 0);
98
99   fprintf(stderr, "OK\n");
100
101   return client_fd;
102
103 }
104
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)
107 {
108   int sent_len = 0;
109
110   sent_len = send(sockfd, buf, len, 0);
111
112   if(sent_len == -1)
113   {
114     perror("sctp_send_to_socket");
115     return -1;
116   }
117
118   return sent_len;
119 }
120
121 // int sctp_recv_from_socket(int sockfd, void *buf, size_t buf_size)
122 // {
123 //   //int len = 0;
124 //
125 //   memset(buf, 0, buf_size);
126 //
127 //   int len = recv(sockfd, &buf, buf_size, 0);
128 //   if(len == -1)
129 //   {
130 //     perror("sctp_recv_from_socket");
131 //     return -1;
132 //   }
133 //
134 //   return len;
135 // }
136
137
138 //test only
139 void client_send_multiple_test_msg(int client_fd)
140 {
141   char buf[1024];
142   int SEND_COUNT = 4;
143
144   for(int i = 0; i < SEND_COUNT; i++)
145   {
146     fprintf(stderr, "Sending message %d of %d \n", i+1, SEND_COUNT);
147
148     memset(buf, 0, sizeof(buf));
149     snprintf(buf, sizeof(buf)-1, "DATA %d", i+1);
150
151     if(send(client_fd, &buf, strlen(buf), 0) == -1) {
152       perror("send");
153       return;
154     }
155
156     memset(buf, 0, sizeof(buf));
157
158     if(recv(client_fd, &buf, sizeof(buf), 0) == -1) {
159       perror("recv");
160       return;
161     }
162
163     fprintf(stderr, "Server reply: %s\n", buf);
164
165     sleep(1);
166   }
167
168   fprintf(stderr, "Closing...\n");
169   if(close(client_fd) == -1) {
170       perror("close");
171       return;
172   }
173 }