b447dd3b3a89abb9cadb0a64b49f411df8b58477
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_sink.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009 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 <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #if defined(HAVE_NETDB_H)
33 typedef void* optval_t;
34 #else
35 // if not posix, assume winsock
36 #define USING_WINSOCK
37 #define SHUT_RDWR 2
38 typedef char* optval_t;
39 #endif
40
41 #include <gruel/thread.h>
42
43 #define SNK_VERBOSE 0
44
45 static int is_error( int perr )
46 {
47   // Compare error to posix error code; return nonzero if match.
48 #if defined(USING_WINSOCK)
49 #define ENOPROTOOPT 109
50 #define ECONNREFUSED 111
51   // All codes to be checked for must be defined below
52   int werr = WSAGetLastError();
53   switch( werr ) {
54   case WSAETIMEDOUT:
55     return( perr == EAGAIN );
56   case WSAENOPROTOOPT:
57     return( perr == ENOPROTOOPT );
58   case WSAECONNREFUSED:
59     return( perr == ECONNREFUSED );
60   default:
61     fprintf(stderr,"gr_udp_source/is_error: unknown error %d\n", perr );
62     throw std::runtime_error("internal error");
63   }
64   return 0;
65 #else
66   return( perr == errno );
67 #endif
68 }
69
70 static void report_error( const char *msg1, const char *msg2 )
71 {
72   // Deal with errors, both posix and winsock
73 #if defined(USING_WINSOCK)
74   int werr = WSAGetLastError();
75   fprintf(stderr, "%s: winsock error %d\n", msg1, werr );
76 #else
77   perror(msg1);
78 #endif
79   if( msg2 != NULL )
80     throw std::runtime_error(msg2);
81   return;
82 }
83
84 gr_udp_sink::gr_udp_sink (size_t itemsize, 
85                           const char *src, unsigned short port_src,
86                           const char *dst, unsigned short port_dst,
87                           int payload_size)
88   : gr_sync_block ("udp_sink",
89                    gr_make_io_signature (1, 1, itemsize),
90                    gr_make_io_signature (0, 0, 0)),
91     d_itemsize (itemsize), d_updated(false), d_payload_size(payload_size)
92 {
93   int ret = 0;
94
95 #if !defined(HAVE_SOCKET) // for Windows (with MinGW)
96   // initialize winsock DLL
97   WSADATA wsaData;
98   int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
99   if( iResult != NO_ERROR ) {
100     report_error( "gr_udp_source WSAStartup", "can't open socket" );
101   }
102 #endif
103   
104   // Set up the address stucture for the source address and port numbers
105   // Get the source IP address from the host name
106   struct addrinfo hints;
107   memset( (void*)&hints, 0, sizeof(hints) );
108   hints.ai_family = AF_INET;
109   hints.ai_socktype = SOCK_DGRAM;
110   hints.ai_protocol = IPPROTO_UDP;
111   char port_str[7];
112   sprintf( port_str, "%d", port_src );
113   ret = getaddrinfo( src, port_str, &hints, &d_ip_src );
114   if( ret != 0 )
115     report_error("gr_udp_source/getaddrinfo",
116                  "can't initialize source socket" );
117
118   // Get the destination IP address from the host name
119   sprintf( port_str, "%d", port_dst );
120   ret = getaddrinfo( dst, port_str, &hints, &d_ip_dst );
121   if( ret != 0 )
122     report_error("gr_udp_source/getaddrinfo",
123                  "can't initialize destination socket" );
124   
125   open();
126 }
127
128 // public constructor that returns a shared_ptr
129
130 gr_udp_sink_sptr
131 gr_make_udp_sink (size_t itemsize, 
132                   const char *src, unsigned short port_src,
133                   const char *dst, unsigned short port_dst,
134                   int payload_size)
135 {
136   return gr_udp_sink_sptr (new gr_udp_sink (itemsize, 
137                                             src, port_src,
138                                             dst, port_dst,
139                                             payload_size));
140 }
141
142 gr_udp_sink::~gr_udp_sink ()
143 {
144   freeaddrinfo(d_ip_src);
145   freeaddrinfo(d_ip_dst);
146   close();
147
148 #if !defined(HAVE_SOCKET) // for Windows (with MinGW)
149   // free winsock resources
150   WSACleanup();
151 #endif
152 }
153
154 bool
155 gr_udp_sink::open()
156 {
157   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
158
159   // create socket
160   d_socket = socket(d_ip_src->ai_family, d_ip_src->ai_socktype,
161                     d_ip_src->ai_protocol);
162   if(d_socket == -1) {
163     report_error("socket open","can't open socket");
164   }
165
166   // Turn on reuse address
167   int opt_val = true;
168   if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
169     report_error("SO_REUSEADDR","can't set socket option SO_REUSEADDR");
170   }
171
172   // Don't wait when shutting down
173   linger lngr;
174   lngr.l_onoff  = 1;
175   lngr.l_linger = 0;
176   if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
177     if( !is_error(ENOPROTOOPT) ) {  // no SO_LINGER for SOCK_DGRAM on Windows
178       report_error("SO_LINGER","can't set socket option SO_LINGER");
179     }
180   }
181
182   // bind socket to an address and port number to listen on
183   if(bind (d_socket, d_ip_src->ai_addr, d_ip_src->ai_addrlen) == -1) {
184     report_error("socket bind","can't bind socket");
185   }
186
187   // Not sure if we should throw here or allow retries
188   if(connect(d_socket, d_ip_dst->ai_addr, d_ip_dst->ai_addrlen) == -1) {
189     report_error("socket connect","can't connect to socket");
190   }
191
192   d_updated = true;
193   return d_socket != 0;
194 }
195
196 void
197 gr_udp_sink::close()
198 {
199   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
200
201   if (d_socket){
202     shutdown(d_socket, SHUT_RDWR);
203 #if defined(USING_WINSOCK)
204     closesocket(d_socket);
205 #else
206     ::close(d_socket);
207 #endif
208     d_socket = 0;
209   }
210   d_updated = true;
211 }
212
213 int 
214 gr_udp_sink::work (int noutput_items,
215                    gr_vector_const_void_star &input_items,
216                    gr_vector_void_star &output_items)
217 {
218   const char *in = (const char *) input_items[0];
219   ssize_t r=0, bytes_sent=0, bytes_to_send=0;
220   ssize_t total_size = noutput_items*d_itemsize;
221
222   #if SNK_VERBOSE
223   printf("Entered udp_sink\n");
224   #endif
225
226   while(bytes_sent <  total_size) {
227     bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
228   
229     r = send(d_socket, (in+bytes_sent), bytes_to_send, 0);
230     if(r == -1) {         // error on send command
231       if( is_error(ECONNREFUSED) )
232         r = bytes_to_send;  // discard data until receiver is started
233       else {
234         report_error("udp_sink",NULL); // there should be no error case where
235         return -1;                  // this function should not exit immediately
236       }
237     }
238     bytes_sent += r;
239     
240     #if SNK_VERBOSE
241     printf("\tbyte sent: %d bytes\n", bytes);
242     #endif
243   }
244
245   #if SNK_VERBOSE
246   printf("Sent: %d bytes (noutput_items: %d)\n", bytes_sent, noutput_items);
247   #endif
248
249   return noutput_items;
250 }