otg_rx_socket.c 9.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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.0  (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
 */

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*! \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
*/








40

41
42
43
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
#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;
74
75


76
77
78
void *recv_ip4_tcp(void* csock)
{

79
80
81
  int sock_rcv;
  int socket=(int)csock;
  char buffer[PAYLOAD_MAX];
82

83
  int i=0;
84

85
  do {
86
87


88
    sock_rcv=recv(socket, buffer, PAYLOAD_MAX, 0);
89
90


91
92
93
94
95
96
97
    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);
98

99
100

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

    }
104
105


106
  } while(sock_rcv !=-1);;
107

108
  LOG_I(OTG,"SOCKET:: TCP-IP4 :: size %d \n ", i) ;
109
110
111



112
113
  close(socket);
  pthread_exit(NULL);
114

115
116
117


  return NULL;
118
119
120
121
122
123
124
125
}




void server_socket_tcp_ip4()
{
#if defined (WIN32)
126
127
#include <winsock2.h>
  typedef int socklen_t;
128
129
130
131
132
133
134
#elif defined (linux)

#endif
#define PORT 7777


#if defined (WIN32)
135
136
137
138
139
  WSADATA WSAData;
  int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
  int erreur = 0;
#endif
140

141
142
143
  SOCKADDR_IN sin;
  int sock;
  int recsize = sizeof sin;
144

145
  int sock_err, sock_rcv;
146
147
148



149
150
  if(!erreur) {
    sock = socket(AF_INET, SOCK_STREAM, 0);
151

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

155
156
157
158
      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);
159

160
161
162
      if(sock_err != SOCKET_ERROR) {
        sock_err = listen(sock, 5);
        LOG_I(OTG,"SOCKET:: TCP-IP4 :: Port Open %d...\n", PORT);
163

164
165
        if(sock_err != SOCKET_ERROR) {
          int cmpt_cl=1;
166

167
168
          /* Creation of the set of reading */
          fd_set readfs;
169

170
          while(1) {
171

172
173
            int csock;    /* conncted socket  */
            pthread_t id;  /* thread that manage the opened connection */
174
175


176
177
178
            /* Empty the set of reading and add the server to the socket */
            FD_ZERO(&readfs);
            FD_SET(sock, &readfs);
179

180
181
182
183
184
            /* If an error occurred at the select */
            if(select(sock + 1, &readfs, NULL, NULL, NULL) < 0) {
              perror("select()");
              exit(errno);
            }
185

186
187
188
            /* 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*/
189

190
191
              SOCKADDR_IN csin;
              int crecsize = sizeof csin;
192
193


194
              csock = accept(sock, (SOCKADDR *) &csin, &crecsize);
195
196


197
198
199
              /* 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");
200

201
202
              else
                LOG_W(OTG,"SOCKET:: TCP-IP4 ::Error in pthread_create \n");
203

204
205
206
207
208
209
210
              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;
211
            }
212
          }
213
        }
214
      }
215
    }
216
  }
217

218
219
220
#if defined (WIN32)
  WSACleanup();
#endif
221
222
223
224
225
226
227
228

}


void server_socket_udp_ip4()
{


229
  int sockfd, bytes_recv, addr_in_size, cmpt_cl=1;
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  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;

246
  if((sockfd = socket (PF_INET, SOCK_DGRAM, 0)) < 0) {
247
248
249
250
    LOG_W(OTG,"SOCKET:: UDP-IP4 :: Error %d in socket: %s\n",errno,sys_errlist[errno]);
    exit(errno);
  };

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

254
255
256
257
258
    if(errno != EADDRINUSE) exit(errno);
  };

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

259
  do {
260
261
262
263
    bytes_recv = recvfrom (sockfd,msg,PAYLOAD_MAX,0,(struct sockaddr *)from, &addr_in_size);



264
265
266
267
268
269
270
    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)));

271
272


273
274
      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;
275

276
277
278
279
      // 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);
      //
280
281


282
283
      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);
    }
284

285
  } while(bytes_recv !=-1);
286

287
  close(sockfd);
288

289
290
  LOG_I(OTG,"SOCKET:: TCP-IP4 :: Client n=%d finish transmission\n", cmpt_cl);
  cmpt_cl+=1;
291
292
293
294
295
296
297

}





298
299
int main (int argc, char **argv)
{
300

301
302
303
  int i;
  char *protocol=NULL;
  char *ip_version=NULL;
304

305
306
  for (i = 1; i <argc ; i ++) {
    if ('-' == argv[i][0]) {
307

308
309
310
      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);
311

312
      }
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
      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);
        }

      }
    }
  }
332
333


334
  //Select the server to use
335
336


337
338
339
340
341
342
  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();
  }
343

344
345
346
347

  return 0;

}
348
349