anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd. |
| 3 | * Copyright (c) 2017 Intel Corporation |
| 4 | * Copyright (c) 2019, Infosys Ltd. |
| 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 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <netinet/sctp.h> |
| 27 | |
| 28 | #include "log.h" |
| 29 | #include "sctp_conn.h" |
| 30 | #define ENB_PORT 62324 |
| 31 | #define S1AP_PPID 18 |
| 32 | int create_sctp_socket(unsigned int remote_ip, unsigned short port) |
| 33 | { |
| 34 | int connSock; |
| 35 | struct sockaddr_in servaddr; |
| 36 | int ret; |
| 37 | |
| 38 | connSock = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); |
| 39 | |
| 40 | if (connSock == -1) |
| 41 | { |
| 42 | log_msg(LOG_ERROR, "Socket creation failed\n"); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | //TODO: use remote_ip |
| 47 | bzero ((void *) &servaddr, sizeof (servaddr)); |
| 48 | servaddr.sin_family = AF_INET; |
| 49 | servaddr.sin_addr.s_addr = htonl(remote_ip); |
| 50 | //servaddr.sin_addr.s_addr = htonl (INADDR_ANY); |
| 51 | servaddr.sin_port = htons(port); |
| 52 | |
| 53 | ret = bind(connSock, (struct sockaddr *) &servaddr, sizeof (servaddr)); |
| 54 | |
| 55 | if(ret == -1 ) |
| 56 | { |
| 57 | perror("Bind:"); |
| 58 | log_msg(LOG_ERROR, "Bind failed \n"); |
| 59 | close(connSock); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | /* Specify maximum streams available per socket */ |
| 64 | struct sctp_initmsg initmsg; |
| 65 | memset (&initmsg, 0, sizeof (initmsg)); |
| 66 | initmsg.sinit_num_ostreams = MAX_STREAM; |
| 67 | initmsg.sinit_max_instreams = MAX_STREAM; |
| 68 | initmsg.sinit_max_attempts = 4; |
| 69 | ret = setsockopt(connSock, IPPROTO_SCTP, SCTP_INITMSG, |
| 70 | &initmsg, sizeof (initmsg)); |
| 71 | |
| 72 | if(ret == -1 ) |
| 73 | { |
| 74 | log_msg(LOG_ERROR, "setsockopt() failed \n"); |
| 75 | close(connSock); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | ret = listen(connSock, MAX_PENDING_CONN); |
| 80 | if(ret == -1 ) |
| 81 | { |
| 82 | log_msg(LOG_ERROR, "listen() failed \n"); |
| 83 | close(connSock); |
| 84 | return -1; |
| 85 | } |
| 86 | return connSock; |
| 87 | } |
| 88 | |
| 89 | int accept_sctp_socket(int connSock) |
| 90 | { |
| 91 | return accept(connSock, (struct sockaddr *) NULL, (socklen_t *) NULL); |
| 92 | } |
| 93 | |
| 94 | int recv_sctp_msg(int connSock, unsigned char *buffer, size_t len) |
| 95 | { |
| 96 | struct sctp_sndrcvinfo sndrcvinfo; |
| 97 | int flags; |
| 98 | return sctp_recvmsg(connSock, buffer, len, |
| 99 | (struct sockaddr *) NULL, 0, &sndrcvinfo, &flags); |
| 100 | } |
| 101 | |
| 102 | int send_sctp_msg(int connSock, unsigned char *buffer, size_t len, uint16_t stream_no) |
| 103 | { |
| 104 | uint32_t ppid = S1AP_PPID; |
| 105 | return sctp_sendmsg(connSock, (void *)buffer, len, |
| 106 | NULL, 0, htonl(ppid), 0, stream_no, 0, 0); |
| 107 | } |
| 108 | |
| 109 | int close_sctp_socket(int connSock) |
| 110 | { |
| 111 | return close(connSock); |
| 112 | } |