Merged -r7436:7453 eb/freebsd into trunk. This is a part of a set of
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_sink.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 #include <gr_udp_sink.h>
27 #include <gr_io_signature.h>
28 #include <stdexcept>
29 #include <netdb.h>
30
31 #define SNK_VERBOSE 0
32
33 gr_udp_sink::gr_udp_sink (size_t itemsize, 
34                           const char *src, unsigned short port_src,
35                           const char *dst, unsigned short port_dst,
36                           int payload_size)
37   : gr_sync_block ("udp_sink",
38                    gr_make_io_signature (1, 1, itemsize),
39                    gr_make_io_signature (0, 0, 0)),
40     d_itemsize (itemsize), d_updated(false), d_payload_size(payload_size)
41 {
42   int ret = 0;
43   
44   // Set up the address stucture for the source address and port numbers
45   // Get the source IP address from the host name
46   struct hostent *hsrc = gethostbyname(src);
47   if(hsrc) {   // if the source was provided as a host namex
48     d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];    
49   }
50   else { // assume it was specified as an IP address
51     if((ret=inet_aton(src, &d_ip_src)) == 0) {            // format IP address
52       perror("Not a valid source IP address or host name");
53       throw std::runtime_error("can't initialize source socket");
54     }
55   }
56
57   // Get the destination IP address from the host name
58   struct hostent *hdst = gethostbyname(dst);
59   if(hdst) {   // if the source was provided as a host namex
60     d_ip_dst = *(struct in_addr*)hdst->h_addr_list[0];    
61   }
62   else { // assume it was specified as an IP address
63     if((ret=inet_aton(dst, &d_ip_dst)) == 0) {            // format IP address
64       perror("Not a valid destination IP address or host name");
65       throw std::runtime_error("can't initialize destination socket");
66     }
67   }
68
69   d_port_src = htons(port_src);           // format port number
70   d_port_dst = htons(port_dst);           // format port number
71
72   d_sockaddr_src.sin_family = AF_INET;
73   d_sockaddr_src.sin_addr   = d_ip_src;
74   d_sockaddr_src.sin_port   = d_port_src;
75
76   d_sockaddr_dst.sin_family = AF_INET;
77   d_sockaddr_dst.sin_addr   = d_ip_dst;
78   d_sockaddr_dst.sin_port   = d_port_dst;
79   
80   open();
81 }
82
83 // public constructor that returns a shared_ptr
84
85 gr_udp_sink_sptr
86 gr_make_udp_sink (size_t itemsize, 
87                   const char *src, unsigned short port_src,
88                   const char *dst, unsigned short port_dst,
89                   int payload_size)
90 {
91   return gr_udp_sink_sptr (new gr_udp_sink (itemsize, 
92                                             src, port_src,
93                                             dst, port_dst,
94                                             payload_size));
95 }
96
97 gr_udp_sink::~gr_udp_sink ()
98 {
99   close();
100 }
101
102 bool
103 gr_udp_sink::open()
104 {
105   omni_mutex_lock l(d_mutex);   // hold mutex for duration of this function
106
107   // create socket
108   if((d_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
109     perror("socket open");
110     throw std::runtime_error("can't open socket");
111   }
112
113   // Turn on reuse address
114   int opt_val = true;
115   if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&opt_val, sizeof(int)) == -1) {
116     perror("SO_REUSEADDR");
117     throw std::runtime_error("can't set socket option SO_REUSEADDR");
118   }
119
120   // Don't wait when shutting down
121   linger lngr;
122   lngr.l_onoff  = 1;
123   lngr.l_linger = 0;
124   if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (void*)&lngr, sizeof(linger)) == -1) {
125     perror("SO_LINGER");
126     throw std::runtime_error("can't set socket option SO_LINGER");
127   }
128
129   // bind socket to an address and port number to listen on
130   if(bind (d_socket, (sockaddr*)&d_sockaddr_src, sizeof(struct sockaddr)) == -1) {
131     perror("socket bind");
132     throw std::runtime_error("can't bind socket");
133   }
134
135   // Not sure if we should throw here or allow retries
136   if(connect(d_socket, (sockaddr*)&d_sockaddr_dst, sizeof(struct sockaddr)) == -1) {
137     perror("socket connect");
138     throw std::runtime_error("can't connect to socket");
139   }
140
141   d_updated = true;
142   return d_socket != 0;
143 }
144
145 void
146 gr_udp_sink::close()
147 {
148   omni_mutex_lock l(d_mutex);   // hold mutex for duration of this function
149
150   if (d_socket){
151     shutdown(d_socket, SHUT_RDWR);
152     d_socket = 0;
153   }
154   d_updated = true;
155 }
156
157 int 
158 gr_udp_sink::work (int noutput_items,
159                    gr_vector_const_void_star &input_items,
160                    gr_vector_void_star &output_items)
161 {
162   const char *in = (const char *) input_items[0];
163   ssize_t r=0, bytes_sent=0, bytes_to_send=0;
164   ssize_t total_size = noutput_items*d_itemsize;
165
166   #if SNK_VERBOSE
167   printf("Entered upd_sink\n");
168   #endif
169
170   while(bytes_sent <  total_size) {
171     bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
172   
173     r = send(d_socket, (in+bytes_sent), bytes_to_send, 0);
174     if(r == -1) {         // error on send command
175       perror("udp_sink"); // there should be no error case where this function 
176       return -1;          // should not exit immediately
177     }
178     bytes_sent += r;
179     
180     #if SNK_VERBOSE
181     printf("\tbyte sent: %d bytes\n", bytes);
182     #endif
183   }
184
185   #if SNK_VERBOSE
186   printf("Sent: %d bytes (noutput_items: %d)\n", bytes_sent, noutput_items);
187   #endif
188
189   return noutput_items;
190 }