Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * Copyright 2013 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * This file is part of GNU Radio |
| 6 | * |
| 7 | * GNU Radio is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * GNU Radio is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Radio; see the file COPYING. If not, write to |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <gnuradio/thread/thread.h> |
| 28 | #include <gnuradio/io_signature.h> |
| 29 | #include <gnuradio/blocks/pdu.h> |
| 30 | #include <pmt/pmt.h> |
| 31 | |
| 32 | #include <boost/lexical_cast.hpp> |
Piotr Krysik | 9930553 | 2018-05-05 12:38:11 +0200 | [diff] [blame] | 33 | #include "grgsm/misc_utils/udp_socket.h" |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 34 | |
| 35 | using boost::asio::ip::udp; |
| 36 | |
| 37 | namespace gr { |
Piotr Krysik | 264fbf6 | 2017-11-05 12:25:51 +0100 | [diff] [blame] | 38 | namespace gsm { |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 39 | |
| 40 | udp_socket::udp_socket( |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 41 | const std::string &bind_addr, |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 42 | const std::string &src_port, |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 43 | const std::string &remote_addr, |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 44 | const std::string &dst_port, |
| 45 | size_t mtu) |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 46 | { |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 47 | // Resize receive buffer according to MTU value |
| 48 | d_rxbuf.resize(mtu); |
| 49 | |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 50 | // Resolve remote host address |
| 51 | udp::resolver resolver(d_io_service); |
| 52 | |
| 53 | udp::resolver::query rx_query( |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 54 | udp::v4(), bind_addr, src_port, |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 55 | boost::asio::ip::resolver_query_base::passive); |
| 56 | udp::resolver::query tx_query( |
| 57 | udp::v4(), remote_addr, dst_port, |
| 58 | boost::asio::ip::resolver_query_base::passive); |
| 59 | |
| 60 | d_udp_endpoint_rx = *resolver.resolve(rx_query); |
| 61 | d_udp_endpoint_tx = *resolver.resolve(tx_query); |
| 62 | |
| 63 | // Create a socket |
| 64 | d_udp_socket.reset(new udp::socket(d_io_service, d_udp_endpoint_rx)); |
| 65 | |
| 66 | // Setup read handler |
| 67 | d_udp_socket->async_receive_from( |
| 68 | boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, |
| 69 | boost::bind(&udp_socket::handle_udp_read, this, |
| 70 | boost::asio::placeholders::error, |
| 71 | boost::asio::placeholders::bytes_transferred)); |
| 72 | |
| 73 | // Start server |
| 74 | d_thread = gr::thread::thread( |
| 75 | boost::bind(&udp_socket::run_io_service, this)); |
| 76 | } |
| 77 | |
| 78 | udp_socket::~udp_socket() |
| 79 | { |
| 80 | // Stop server |
| 81 | d_io_service.stop(); |
| 82 | d_thread.interrupt(); |
| 83 | d_thread.join(); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | udp_socket::run_io_service(void) |
| 88 | { |
| 89 | d_io_service.run(); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | udp_socket::udp_send(uint8_t *data, size_t len) |
| 94 | { |
| 95 | d_udp_socket->send_to( |
| 96 | boost::asio::buffer(data, len), |
| 97 | d_udp_endpoint_tx); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | udp_socket::handle_udp_read( |
| 102 | const boost::system::error_code& error, |
| 103 | size_t bytes_transferred) |
| 104 | { |
| 105 | if (error) |
| 106 | return; |
| 107 | |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 108 | // Call incoming data handler |
| 109 | if (udp_rx_handler != NULL) |
| 110 | udp_rx_handler((uint8_t *) &d_rxbuf[0], bytes_transferred); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 111 | |
| 112 | d_udp_socket->async_receive_from( |
| 113 | boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, |
| 114 | boost::bind(&udp_socket::handle_udp_read, this, |
| 115 | boost::asio::placeholders::error, |
| 116 | boost::asio::placeholders::bytes_transferred)); |
| 117 | } |
| 118 | |
Piotr Krysik | 264fbf6 | 2017-11-05 12:25:51 +0100 | [diff] [blame] | 119 | } /* namespace gsm */ |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 120 | }/* namespace gr */ |