mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.1 KiB
C
126 lines
3.1 KiB
C
11 years ago
|
/**
|
||
|
*
|
||
|
* \section COPYRIGHT
|
||
|
*
|
||
9 years ago
|
* Copyright 2013-2015 Software Radio Systems Limited
|
||
11 years ago
|
*
|
||
|
* \section LICENSE
|
||
|
*
|
||
10 years ago
|
* This file is part of the srsLTE library.
|
||
11 years ago
|
*
|
||
10 years ago
|
* srsLTE is free software: you can redistribute it and/or modify
|
||
10 years ago
|
* it under the terms of the GNU Affero General Public License as
|
||
11 years ago
|
* published by the Free Software Foundation, either version 3 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
10 years ago
|
* srsLTE is distributed in the hope that it will be useful,
|
||
11 years ago
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
* GNU Affero General Public License for more details.
|
||
11 years ago
|
*
|
||
10 years ago
|
* A copy of the GNU Affero General Public License can be found in
|
||
11 years ago
|
* the LICENSE file in the top-level directory of this distribution
|
||
|
* and at http://www.gnu.org/licenses/.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <sys/socket.h>
|
||
|
#include <netinet/in.h>
|
||
|
#include <arpa/inet.h>
|
||
|
#include <unistd.h>
|
||
10 years ago
|
#include <fcntl.h>
|
||
11 years ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <strings.h>
|
||
10 years ago
|
#include <errno.h>
|
||
11 years ago
|
|
||
10 years ago
|
#include "srslte/io/netsource.h"
|
||
11 years ago
|
|
||
10 years ago
|
int srslte_netsource_init(srslte_netsource_t *q, char *address, int port, srslte_netsource_type_t type) {
|
||
|
bzero(q, sizeof(srslte_netsource_t));
|
||
11 years ago
|
|
||
10 years ago
|
q->sockfd=socket(AF_INET,type==SRSLTE_NETSOURCE_TCP?SOCK_STREAM:SOCK_DGRAM,0);
|
||
11 years ago
|
|
||
10 years ago
|
if (q->sockfd < 0) {
|
||
|
perror("socket");
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
q->type = type;
|
||
10 years ago
|
|
||
11 years ago
|
q->servaddr.sin_family = AF_INET;
|
||
|
q->servaddr.sin_addr.s_addr=inet_addr(address);
|
||
|
q->servaddr.sin_port=htons(port);
|
||
10 years ago
|
|
||
|
if (bind(q->sockfd,(struct sockaddr *)&q->servaddr,sizeof(struct sockaddr_in))) {
|
||
|
perror("bind");
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
q->connfd = 0;
|
||
11 years ago
|
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void srslte_netsource_free(srslte_netsource_t *q) {
|
||
11 years ago
|
if (q->sockfd) {
|
||
|
close(q->sockfd);
|
||
|
}
|
||
10 years ago
|
bzero(q, sizeof(srslte_netsource_t));
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int srslte_netsource_read(srslte_netsource_t *q, void *buffer, int nbytes) {
|
||
|
if (q->type == SRSLTE_NETSOURCE_UDP) {
|
||
10 years ago
|
int n = recv(q->sockfd, buffer, nbytes, 0);
|
||
|
|
||
|
if (n == -1) {
|
||
|
if (errno == EAGAIN) {
|
||
|
return 0;
|
||
|
} else {
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
} else {
|
||
10 years ago
|
return n;
|
||
|
}
|
||
10 years ago
|
} else {
|
||
10 years ago
|
if (q->connfd == 0) {
|
||
|
printf("Waiting for TCP connection\n");
|
||
|
listen(q->sockfd, 1);
|
||
|
socklen_t clilen = sizeof(q->cliaddr);
|
||
|
q->connfd = accept(q->sockfd, (struct sockaddr *)&q->cliaddr, &clilen);
|
||
|
if (q->connfd < 0) {
|
||
|
perror("accept");
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
int n = read(q->connfd, buffer, nbytes);
|
||
10 years ago
|
if (n == 0) {
|
||
|
printf("Connection closed\n");
|
||
|
close(q->connfd);
|
||
|
q->connfd = 0;
|
||
|
return 0;
|
||
|
}
|
||
10 years ago
|
if (n == -1) {
|
||
10 years ago
|
perror("read");
|
||
10 years ago
|
}
|
||
10 years ago
|
return n;
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int srslte_netsource_set_nonblocking(srslte_netsource_t *q) {
|
||
10 years ago
|
if (fcntl(q->sockfd, F_SETFL, O_NONBLOCK)) {
|
||
|
perror("fcntl");
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
10 years ago
|
int srslte_netsource_set_timeout(srslte_netsource_t *q, uint32_t microseconds) {
|
||
10 years ago
|
struct timeval t;
|
||
|
t.tv_sec = 0;
|
||
|
t.tv_usec = microseconds;
|
||
|
if (setsockopt(q->sockfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(struct timeval))) {
|
||
|
perror("setsockopt");
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|