sctp_cu.c 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file sctp_server.c
 * \brief sctp server procedures for f1ap
 * \author EURECOM/NTUST
 * \date 2018
 * \version 0.1
 * \company Eurecom
 * \email: navid.nikaein@eurecom.fr, bing-kai.hong@eurecom.fr
 * \note
 * \warning
 */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h> // for close
#include <stdlib.h>
Bing-Kai Hong's avatar
F1:  
Bing-Kai Hong committed
43
#include "f1ap_handlers.h"
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

#define MAX_BUFFER  1024

/* server */

int sfd, cfd, len, flags;
struct sctp_sndrcvinfo sndrcvinfo;
struct sockaddr_in saddr, caddr;
struct sctp_initmsg initmsg;
uint8_t buff[INET_ADDRSTRLEN]; 

void send_func(int cfd) {
    uint8_t buffer_send[MAX_BUFFER+1];
    /* Changing 9th character the character after # in the message buffer */
    buffer_send[0] = rand();
    sctp_sendmsg( cfd, (void *)buffer_send, (size_t)strlen(buffer_send),
                NULL, 0, htons(62), 0, 1 /* stream */, 0, 0 );
    printf("S - Sent: %s\n", buffer_send);
}

void *recv_func(void *cfd) {
    int ret;
    uint8_t buffer_recv[MAX_BUFFER+1];

    //recv message
    bzero( (void *)&buffer_recv, sizeof(buffer_recv) );
    while(ret = sctp_recvmsg( *(int*)cfd, (void *)buffer_recv, sizeof(buffer_recv),
                        (struct sockaddr *)NULL, 0, &sndrcvinfo, &flags )) {
         //send_func(*(int*)cfd);
         //printf("S - cfd = %d\n", *(int*)cfd);
        printf("S - Received following data on ppid %d, data is: %x\n",
                sndrcvinfo.sinfo_ppid, buffer_recv);
        printf("S - Received following data on stream %d, data is: \n", sndrcvinfo.sinfo_stream);

        int i_ret;
        for (i_ret = 0; i_ret < sizeof(buffer_recv); i_ret++) {
          printf("%x ", *(buffer_recv+i_ret));
        }

        printf("\n");

Bing-Kai Hong's avatar
F1:  
Bing-Kai Hong committed
85 86 87 88
        f1ap_handle_message(1/*sctp_data_ind->assoc_id*/, 1/*sctp_data_ind->stream*/,
                            buffer_recv, sizeof(buffer_recv));

        //f1ap_decode_pdu(NULL , buffer_recv, sizeof(buffer_recv));
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    }
    printf("ret = %d\n", ret);
    close( *(int*)cfd );
}

int sctp_cu_init() {
    pthread_t threads;
    printf("S - Waiting for socket_accept\n"); 

    sfd = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
    bzero( (void *)&saddr, sizeof(saddr) );
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl( INADDR_ANY );
    saddr.sin_port = htons(29008);
 
    bind( sfd, (struct sockaddr *)&saddr, sizeof(saddr) );
 
    /* Maximum of 3 streams will be available per socket */
    memset( &initmsg, 0, sizeof(initmsg) );
    initmsg.sinit_num_ostreams = 3;
    initmsg.sinit_max_instreams = 3;
    initmsg.sinit_max_attempts = 2;
    setsockopt( sfd, IPPROTO_SCTP, SCTP_INITMSG,
            &initmsg, sizeof(initmsg) );
 
    listen( sfd, 5 );

    while(cfd=accept(sfd, (struct sockaddr *)&caddr, (socklen_t*)&caddr)) {
        printf("-------- S - Connected to %s\n",
               inet_ntop(AF_INET, &caddr.sin_addr, buff,
               sizeof(buff)));
        int *thread_args = malloc(sizeof(int));
        *thread_args = cfd;
        pthread_create(&threads, NULL, recv_func, thread_args);
    }

    printf("S - close\n");
    return 0;
}