00001 /* -*- c++ -*- */ 00002 /* 00003 * Copyright 2007,2008 Free Software Foundation, Inc. 00004 * 00005 * This file is part of GNU Radio 00006 * 00007 * GNU Radio is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 3, or (at your option) 00010 * any later version. 00011 * 00012 * GNU Radio is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with GNU Radio; see the file COPYING. If not, write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, 00020 * Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #ifndef INCLUDED_GR_UDP_SINK_H 00024 #define INCLUDED_GR_UDP_SINK_H 00025 00026 #include <gr_sync_block.h> 00027 #include <gnuradio/omnithread.h> 00028 #if defined(HAVE_SOCKET) 00029 #include <sys/socket.h> 00030 #include <arpa/inet.h> 00031 #elif defined(HAVE_WINDOWS_H) 00032 #include <winsock2.h> 00033 #include <windows.h> 00034 #endif 00035 #if defined(HAVE_NETINET_IN_H) 00036 #include <netinet/in.h> 00037 #endif 00038 00039 class gr_udp_sink; 00040 typedef boost::shared_ptr<gr_udp_sink> gr_udp_sink_sptr; 00041 00042 gr_udp_sink_sptr 00043 gr_make_udp_sink (size_t itemsize, 00044 const char *src, unsigned short port_src, 00045 const char *dst, unsigned short port_dst, 00046 int payload_size=1472); 00047 00048 /*! 00049 * \brief Write stream to an UDP socket. 00050 * \ingroup sink_blk 00051 * 00052 * \param itemsize The size (in bytes) of the item datatype 00053 * \param src The source address as either the host name or the 'numbers-and-dots' 00054 * IP address 00055 * \param port_src Destination port to bind to (0 allows socket to choose an appropriate port) 00056 * \param dst The destination address as either the host name or the 'numbers-and-dots' 00057 * IP address 00058 * \param port_dst Destination port to connect to 00059 * \param payload_size UDP payload size by default set to 00060 * 1472 = (1500 MTU - (8 byte UDP header) - (20 byte IP header)) 00061 */ 00062 00063 class gr_udp_sink : public gr_sync_block 00064 { 00065 friend gr_udp_sink_sptr gr_make_udp_sink (size_t itemsize, 00066 const char *src, unsigned short port_src, 00067 const char *dst, unsigned short port_dst, 00068 int payload_size); 00069 private: 00070 size_t d_itemsize; 00071 bool d_updated; 00072 omni_mutex d_mutex; 00073 00074 int d_payload_size; // maximum transmission unit (packet length) 00075 int d_socket; // handle to socket 00076 int d_socket_rcv; // handle to socket retuned in the accept call 00077 struct in_addr d_ip_src; // store the source ip info 00078 struct in_addr d_ip_dst; // store the destination ip info 00079 unsigned short d_port_src; // the port number to open for connections to this service 00080 unsigned short d_port_dst; // port number of the remove system 00081 struct sockaddr_in d_sockaddr_src; // store the source sockaddr data (formatted IP address and port number) 00082 struct sockaddr_in d_sockaddr_dst; // store the destination sockaddr data (formatted IP address and port number) 00083 00084 protected: 00085 /*! 00086 * \brief UDP Sink Constructor 00087 * 00088 * \param itemsize The size (in bytes) of the item datatype 00089 * \param src The source address as either the host name or the 'numbers-and-dots' 00090 * IP address 00091 * \param port_src Destination port to bind to (0 allows socket to choose an appropriate port) 00092 * \param dst The destination address as either the host name or the 'numbers-and-dots' 00093 * IP address 00094 * \param port_dst Destination port to connect to 00095 * \param payload_size UDP payload size by default set to 00096 * 1472 = (1500 MTU - (8 byte UDP header) - (20 byte IP header)) 00097 */ 00098 gr_udp_sink (size_t itemsize, 00099 const char *src, unsigned short port_src, 00100 const char *dst, unsigned short port_dst, 00101 int payload_size); 00102 00103 public: 00104 ~gr_udp_sink (); 00105 00106 /*! 00107 * \brief open a socket specified by the port and ip address info 00108 * 00109 * Opens a socket, binds to the address, and makes connectionless association 00110 * over UDP. If any of these fail, the fuction retuns the error and exits. 00111 */ 00112 bool open(); 00113 00114 /*! 00115 * \brief Close current socket. 00116 * 00117 * Shuts down read/write on the socket 00118 */ 00119 void close(); 00120 00121 /*! \brief return the PAYLOAD_SIZE of the socket */ 00122 int payload_size() { return d_payload_size; } 00123 00124 // should we export anything else? 00125 00126 int work (int noutput_items, 00127 gr_vector_const_void_star &input_items, 00128 gr_vector_void_star &output_items); 00129 }; 00130 00131 #endif /* INCLUDED_GR_UDP_SINK_H */