3 * Copyright 2007,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
26 #include <gr_udp_sink.h>
27 #include <gr_io_signature.h>
29 #if defined(HAVE_SOCKET)
32 typedef void* optval_t;
35 #define inet_aton(N,A) ( (A)->s_addr = inet_addr(N), ( (A)->s_addr != INADDR_NONE ) )
36 typedef char* optval_t;
41 gr_udp_sink::gr_udp_sink (size_t itemsize,
42 const char *src, unsigned short port_src,
43 const char *dst, unsigned short port_dst,
45 : gr_sync_block ("udp_sink",
46 gr_make_io_signature (1, 1, itemsize),
47 gr_make_io_signature (0, 0, 0)),
48 d_itemsize (itemsize), d_updated(false), d_payload_size(payload_size)
52 // Set up the address stucture for the source address and port numbers
53 // Get the source IP address from the host name
54 struct hostent *hsrc = gethostbyname(src);
55 if(hsrc) { // if the source was provided as a host namex
56 d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];
58 else { // assume it was specified as an IP address
59 if((ret=inet_aton(src, &d_ip_src)) == 0) { // format IP address
60 perror("Not a valid source IP address or host name");
61 throw std::runtime_error("can't initialize source socket");
65 // Get the destination IP address from the host name
66 struct hostent *hdst = gethostbyname(dst);
67 if(hdst) { // if the source was provided as a host namex
68 d_ip_dst = *(struct in_addr*)hdst->h_addr_list[0];
70 else { // assume it was specified as an IP address
71 if((ret=inet_aton(dst, &d_ip_dst)) == 0) { // format IP address
72 perror("Not a valid destination IP address or host name");
73 throw std::runtime_error("can't initialize destination socket");
77 d_port_src = htons(port_src); // format port number
78 d_port_dst = htons(port_dst); // format port number
80 d_sockaddr_src.sin_family = AF_INET;
81 d_sockaddr_src.sin_addr = d_ip_src;
82 d_sockaddr_src.sin_port = d_port_src;
84 d_sockaddr_dst.sin_family = AF_INET;
85 d_sockaddr_dst.sin_addr = d_ip_dst;
86 d_sockaddr_dst.sin_port = d_port_dst;
91 // public constructor that returns a shared_ptr
94 gr_make_udp_sink (size_t itemsize,
95 const char *src, unsigned short port_src,
96 const char *dst, unsigned short port_dst,
99 return gr_udp_sink_sptr (new gr_udp_sink (itemsize,
105 gr_udp_sink::~gr_udp_sink ()
113 omni_mutex_lock l(d_mutex); // hold mutex for duration of this function
116 if((d_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
117 perror("socket open");
118 throw std::runtime_error("can't open socket");
121 // Turn on reuse address
123 if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
124 perror("SO_REUSEADDR");
125 throw std::runtime_error("can't set socket option SO_REUSEADDR");
128 // Don't wait when shutting down
132 if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
134 throw std::runtime_error("can't set socket option SO_LINGER");
137 // bind socket to an address and port number to listen on
138 if(bind (d_socket, (sockaddr*)&d_sockaddr_src, sizeof(struct sockaddr)) == -1) {
139 perror("socket bind");
140 throw std::runtime_error("can't bind socket");
143 // Not sure if we should throw here or allow retries
144 if(connect(d_socket, (sockaddr*)&d_sockaddr_dst, sizeof(struct sockaddr)) == -1) {
145 perror("socket connect");
146 throw std::runtime_error("can't connect to socket");
150 return d_socket != 0;
156 omni_mutex_lock l(d_mutex); // hold mutex for duration of this function
159 shutdown(d_socket, SHUT_RDWR);
166 gr_udp_sink::work (int noutput_items,
167 gr_vector_const_void_star &input_items,
168 gr_vector_void_star &output_items)
170 const char *in = (const char *) input_items[0];
171 ssize_t r=0, bytes_sent=0, bytes_to_send=0;
172 ssize_t total_size = noutput_items*d_itemsize;
175 printf("Entered upd_sink\n");
178 while(bytes_sent < total_size) {
179 bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
181 r = send(d_socket, (in+bytes_sent), bytes_to_send, 0);
182 if(r == -1) { // error on send command
183 perror("udp_sink"); // there should be no error case where this function
184 return -1; // should not exit immediately
189 printf("\tbyte sent: %d bytes\n", bytes);
194 printf("Sent: %d bytes (noutput_items: %d)\n", bytes_sent, noutput_items);
197 return noutput_items;