otg_rx_socket.c 9.25 KB
Newer Older
1
/*******************************************************************************
nikaeinn's avatar
nikaeinn committed
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

nikaeinn's avatar
nikaeinn committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


nikaeinn's avatar
nikaeinn committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

nikaeinn's avatar
nikaeinn committed
16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
nikaeinn's avatar
nikaeinn committed
22 23 24 25
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

*******************************************************************************/

/*! \file otg_rx_socket.c
* \brief function containing the OTG RX traffic generation functions with sockets
* \author A. Hafsaoui
* \date 2012
* \version 0.1
* \company Eurecom
* \email: openair_tech@eurecom.fr
* \note
* \warning
*/








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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>



#include <pthread.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1


#include "otg_config.h"
#include "otg_rx_socket.h"
#include "otg_vars.h"



typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
82 83


84 85 86
void *recv_ip4_tcp(void* csock)
{

87 88 89
  int sock_rcv;
  int socket=(int)csock;
  char buffer[PAYLOAD_MAX];
90

91
  int i=0;
92

93
  do {
94 95


96
    sock_rcv=recv(socket, buffer, PAYLOAD_MAX, 0);
97 98


99 100 101 102 103 104 105
    if  ((buffer!=NULL)&& (strlen(buffer)>0)) {
      //    payload_t* payload;
      //    payload->control_hdr = (control_hdr_t*) malloc (sizeof(control_hdr_t));
      //    payload->payload_rest = (char *) malloc (sock_rcv - sizeof(control_hdr_t));
      //    memcpy (payload->control_hdr, buffer, sizeof(control_hdr_t));
      //    memcpy (payload->payload_rest , buffer+sizeof(control_hdr_t), (sock_rcv - sizeof(control_hdr_t)));
      //    LOG_I(OTG,"SOCKET:: UDP-IP4 :: SRC=%d, DST=%d, PROTO=%d, IP VERSION=%d\n", payload->control_hdr->src,payload->control_hdr->dst, payload->control_hdr->trans_proto, payload->control_hdr->ip_v);
106

107 108 109 110 111

      LOG_I(OTG,"SOCKET:: TCP-IP4 :: size=%d  received=%d, Received buffer: %s   \n\n\n", strlen(buffer),  sock_rcv, buffer);
      buffer[PAYLOAD_MAX] != '\0';

    }
112 113


114
  } while(sock_rcv !=-1);;
115

116
  LOG_I(OTG,"SOCKET:: TCP-IP4 :: size %d \n ", i) ;
117 118 119



120 121
  close(socket);
  pthread_exit(NULL);
122

123 124 125


  return NULL;
126 127 128 129 130 131 132 133
}




void server_socket_tcp_ip4()
{
#if defined (WIN32)
134 135
#include <winsock2.h>
  typedef int socklen_t;
136 137 138 139 140 141 142
#elif defined (linux)

#endif
#define PORT 7777


#if defined (WIN32)
143 144 145 146 147
  WSADATA WSAData;
  int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
  int erreur = 0;
#endif
148

149 150 151
  SOCKADDR_IN sin;
  int sock;
  int recsize = sizeof sin;
152

153
  int sock_err, sock_rcv;
154 155 156



157 158
  if(!erreur) {
    sock = socket(AF_INET, SOCK_STREAM, 0);
159

160 161
    if(sock != INVALID_SOCKET) {
      LOG_I(OTG,"SOCKET:: TCP-IP4 :: Socket number= %d  is opend using TCP and IPv4\n", sock);
162

163 164 165 166
      sin.sin_addr.s_addr = htonl(INADDR_ANY);
      sin.sin_family = AF_INET;
      sin.sin_port = htons(PORT);
      sock_err = bind(sock, (SOCKADDR*) &sin, recsize);
167

168 169 170
      if(sock_err != SOCKET_ERROR) {
        sock_err = listen(sock, 5);
        LOG_I(OTG,"SOCKET:: TCP-IP4 :: Port Open %d...\n", PORT);
171

172 173
        if(sock_err != SOCKET_ERROR) {
          int cmpt_cl=1;
174

175 176
          /* Creation of the set of reading */
          fd_set readfs;
177

178
          while(1) {
179

180 181
            int csock;    /* conncted socket  */
            pthread_t id;  /* thread that manage the opened connection */
182 183


184 185 186
            /* Empty the set of reading and add the server to the socket */
            FD_ZERO(&readfs);
            FD_SET(sock, &readfs);
187

188 189 190 191 192
            /* If an error occurred at the select */
            if(select(sock + 1, &readfs, NULL, NULL, NULL) < 0) {
              perror("select()");
              exit(errno);
            }
193

194 195 196
            /* check if the socket server provides information to read */
            if(FD_ISSET(sock, &readfs)) {
              /* the server socket necessarily means that a client wants to connect to the server*/
197

198 199
              SOCKADDR_IN csin;
              int crecsize = sizeof csin;
200 201


202
              csock = accept(sock, (SOCKADDR *) &csin, &crecsize);
203 204


205 206 207
              /* create  new thread for the new connection */
              if (pthread_create(&id, NULL, (void *)recv_ip4_tcp, (void*)csock))
                LOG_W(OTG,"SOCKET:: TCP-IP4 ::pthread_create OK!\n");
208

209 210
              else
                LOG_W(OTG,"SOCKET:: TCP-IP4 ::Error in pthread_create \n");
211

212 213 214 215 216 217 218
              if (pthread_detach(id))
                LOG_W(OTG,"SOCKET:: TCP-IP4 ::pthread_detach OK!\n");
              else
                LOG_W(OTG,"SOCKET:: TCP-IP4 ::Error in pthread_detach\n");

              LOG_I(OTG,"SOCKET:: TCP-IP4 :: Client n=%d finish transmission\n", cmpt_cl);
              cmpt_cl+=1;
219
            }
220
          }
221
        }
222
      }
223
    }
224
  }
225

226 227 228
#if defined (WIN32)
  WSACleanup();
#endif
229 230 231 232 233 234 235 236

}


void server_socket_udp_ip4()
{


237
  int sockfd, bytes_recv, addr_in_size, cmpt_cl=1;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  u_short portnum = 12345;
  struct sockaddr_in *my_addr, *from;
  char msg[PAYLOAD_MAX];
  u_long fromaddr;

  addr_in_size = sizeof(struct sockaddr_in);

  //msg = (char *)malloc(PAYLOAD_MAX);
  from = (struct sockaddr_in *)malloc(addr_in_size);
  my_addr = (struct sockaddr_in *)malloc(addr_in_size);

  memset((char *)my_addr,(char)0,addr_in_size);
  my_addr->sin_family = AF_INET;
  my_addr->sin_addr.s_addr = htonl(INADDR_ANY);
  my_addr->sin_port = portnum;

254
  if((sockfd = socket (PF_INET, SOCK_DGRAM, 0)) < 0) {
255 256 257 258
    LOG_W(OTG,"SOCKET:: UDP-IP4 :: Error %d in socket: %s\n",errno,sys_errlist[errno]);
    exit(errno);
  };

259
  if(bind(sockfd, (struct sockaddr *)my_addr, addr_in_size) < 0) {
260
    LOG_W(OTG,"SOCKET:: UDP-IP4 :: Error %d in bind: %s\n",errno,sys_errlist[errno]);
261

262 263 264 265 266
    if(errno != EADDRINUSE) exit(errno);
  };

  LOG_I(OTG,"SOCKET:: UDP-IP4 :: Ready to receive UDP traffic\n");

267
  do {
268 269 270 271
    bytes_recv = recvfrom (sockfd,msg,PAYLOAD_MAX,0,(struct sockaddr *)from, &addr_in_size);



272 273 274 275 276 277 278
    if  (bytes_recv>0) {
      payload_t* payload;
      payload->control_hdr = (control_hdr_t*) malloc (sizeof(control_hdr_t));
      payload->payload_rest = (char *) malloc (bytes_recv - sizeof(control_hdr_t));
      memcpy (payload->control_hdr, msg, sizeof(control_hdr_t));
      memcpy (payload->payload_rest , msg+sizeof(control_hdr_t), (bytes_recv - sizeof(control_hdr_t)));

279 280


281 282
      LOG_I(OTG,"SOCKET:: UDP-IP4 :: SRC=%d, DST=%d, PROTO=%d, IP VERSION=%d\n", payload->control_hdr->src,payload->control_hdr->dst, payload->control_hdr->trans_proto, payload->control_hdr->ip_v);
      fromaddr = from->sin_addr.s_addr;
283

284 285 286 287
      // Update RX OTG info
      //otg_info->rx_num_pkt[payload->control_hdr->src][payload->control_hdr->dst]+=1;
      //otg_info->rx_num_bytes[payload->control_hdr->src][payload->control_hdr->dst]+=  bytes_recv + (HDR_IP_v4 + HDR_UDP);
      //
288 289


290 291
      LOG_I(OTG,"SOCKET:: UDP-IP4 :: From=%s , port= %d, data= , bytes NB=%d\n", (gethostbyaddr((char *)&fromaddr, sizeof(fromaddr), AF_INET))->h_name, from->sin_port, bytes_recv);
    }
292

293
  } while(bytes_recv !=-1);
294

295
  close(sockfd);
296

297 298
  LOG_I(OTG,"SOCKET:: TCP-IP4 :: Client n=%d finish transmission\n", cmpt_cl);
  cmpt_cl+=1;
299 300 301 302 303 304 305

}





306 307
int main (int argc, char **argv)
{
308

309 310 311
  int i;
  char *protocol=NULL;
  char *ip_version=NULL;
312

313 314
  for (i = 1; i <argc ; i ++) {
    if ('-' == argv[i][0]) {
315

316 317 318
      if(('h' == argv[i][1]) || ('H' == argv[i][1])) {
        printf("Help OTG RX:  \n. ./server [-P (protocol: TCP or UDP)] [-I (ip version: IP4 or IP6)]\n");
        return(0);
319

320
      }
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
      else if ('P' == argv[i][1]) {
        protocol=argv[i+1];

        if ((strcmp(argv[i+1],"TCP")==0) || (strcmp(argv[i+1],"UDP")==0) || (strcmp(argv[i+1],"tcp")==0) || (strcmp(argv[i+1],"udp")==0)) {
          protocol=argv[i+1];
          printf("Protocol=%s\n", protocol);
        }
      }

      else if ('I' == argv[i][1]) {
        if ((strcmp(argv[i+1],"IP4")==0) || (strcmp(argv[i+1],"IP6")==0) || (strcmp(argv[i+1],"ip4")==0) || (strcmp(argv[i+1],"ip6")==0)) {
          ip_version=argv[i+1];
          printf("IP version=%s\n", ip_version);
        }

      }
    }
  }
340 341


342
  //Select the server to use
343 344


345 346 347 348 349 350
  if ((ip_version !=NULL) && (protocol!=NULL)) {
    if (((strcmp(ip_version,"IP4")==0) ||(strcmp(ip_version,"ip4")==0)) && ((strcmp(protocol,"TCP")==0) ||(strcmp(protocol,"tcp")==0)))
      server_socket_tcp_ip4();
    else if  (((strcmp(ip_version,"IP4")==0) ||(strcmp(ip_version,"ip4")==0)) && ((strcmp(protocol,"UDP")==0) ||(strcmp(protocol,"udp")==0)))
      server_socket_udp_ip4();
  }
351

352 353 354 355

  return 0;

}
356 357