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