Ignore ENOPROTOOPT return from setsockopt(SO_LINGER)
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_source.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_source.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_SOCKET)
33 #include <netdb.h>
34 typedef void* optval_t;
35 #else
36 #define SHUT_RDWR 2
37 #define inet_aton(N,A) ( (A)->s_addr = inet_addr(N), ( (A)->s_addr != INADDR_NONE ) )
38 typedef char* optval_t;
39 #endif
40
41 #define SRC_VERBOSE 0
42
43 gr_udp_source::gr_udp_source(size_t itemsize, const char *src, 
44                              unsigned short port_src, int payload_size)
45   : gr_sync_block ("udp_source",
46                    gr_make_io_signature(0, 0, 0),
47                    gr_make_io_signature(1, 1, itemsize)),
48     d_itemsize(itemsize), d_updated(false), d_payload_size(payload_size), d_residual(0), d_temp_offset(0)
49 {
50   int ret = 0;
51   
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];    
57   }
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");
62     }
63   }
64
65   d_port_src = htons(port_src);     // format port number
66   
67   d_sockaddr_src.sin_family = AF_INET;
68   d_sockaddr_src.sin_addr   = d_ip_src;
69   d_sockaddr_src.sin_port   = d_port_src;
70
71   d_temp_buff = new char[d_payload_size];   // allow it to hold up to payload_size bytes
72   
73   open();
74 }
75
76 gr_udp_source_sptr
77 gr_make_udp_source (size_t itemsize, const char *ipaddr, 
78                     unsigned short port, int payload_size)
79 {
80   return gr_udp_source_sptr (new gr_udp_source (itemsize, ipaddr, 
81                                                 port, payload_size));
82 }
83
84 gr_udp_source::~gr_udp_source ()
85 {
86   delete [] d_temp_buff;
87   close();
88 }
89
90 bool
91 gr_udp_source::open()
92 {
93   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
94   // create socket
95   d_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
96   if(d_socket == -1) {
97     perror("socket open");
98     throw std::runtime_error("can't open socket");
99   }
100
101   // Turn on reuse address
102   int opt_val = 1;
103   if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
104     perror("SO_REUSEADDR");
105     throw std::runtime_error("can't set socket option SO_REUSEADDR");
106   }
107
108   // Don't wait when shutting down
109   linger lngr;
110   lngr.l_onoff  = 1;
111   lngr.l_linger = 0;
112   if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
113     if(errno != ENOPROTOOPT) {  // no SO_LINGER for SOCK_DGRAM on Windows
114       perror("SO_LINGER");
115       throw std::runtime_error("can't set socket option SO_LINGER");
116     }
117   }
118
119   // Set a timeout on the receive function to not block indefinitely
120   // This value can (and probably should) be changed
121   timeval timeout;
122   timeout.tv_sec = 1;
123   timeout.tv_usec = 0;
124   if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (optval_t)&timeout, sizeof(timeout)) == -1) {
125     perror("SO_RCVTIMEO");
126     throw std::runtime_error("can't set socket option SO_RCVTIMEO");
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   d_updated = true;
136   return d_socket != 0;
137 }
138
139 void
140 gr_udp_source::close()
141 {
142   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
143
144   if (d_socket){
145     shutdown(d_socket, SHUT_RDWR);
146     d_socket = 0;
147   }
148   d_updated = true;
149 }
150
151 int 
152 gr_udp_source::work (int noutput_items,
153                      gr_vector_const_void_star &input_items,
154                      gr_vector_void_star &output_items)
155 {
156   char *out = (char *) output_items[0];
157   ssize_t r=0, nbytes=0, bytes_received=0;
158   ssize_t total_bytes = (ssize_t)(d_itemsize*noutput_items);
159
160   #if SRC_VERBOSE
161   printf("\nEntered udp_source\n");
162   #endif
163
164   // Remove items from temp buffer if they are in there
165   if(d_residual) {
166     nbytes = std::min(d_residual, total_bytes);
167     memcpy(out, d_temp_buff+d_temp_offset, nbytes);
168     bytes_received = nbytes;
169
170     #if SRC_VERBOSE
171     printf("\tTemp buff size: %d  offset: %d (bytes_received: %d) (noutput_items: %d)\n", 
172            d_residual, d_temp_offset, bytes_received, noutput_items);
173     #endif
174
175     // Increment pointer
176     out += bytes_received;
177     
178     // Update indexing of amount of bytes left in the buffer
179     d_residual -= nbytes;
180     d_temp_offset = d_temp_offset+d_residual;
181   }
182
183   while(1) {
184     // get the data into our output buffer and record the number of bytes
185     // This is a non-blocking call with a timeout set in the constructor
186     r = recv(d_socket, d_temp_buff, d_payload_size, 0);  // get the entire payload or the what's available
187
188     // Check if there was a problem; forget it if the operation just timed out
189     if(r == -1) {
190       if(errno == EAGAIN) {  // handle non-blocking call timeout
191         #if SRC_VERBOSE
192         printf("UDP receive timed out\n"); 
193         #endif
194
195         // Break here to allow the rest of the flow graph time to run and so ctrl-C breaks
196         break;
197       }
198       else {
199         perror("udp_source");
200         return -1;
201       }
202     }
203     else {
204       // Calculate the number of bytes we can take from the buffer in this call
205       nbytes = std::min(r, total_bytes-bytes_received);
206       
207       // adjust the total number of bytes we have to round down to nearest integer of an itemsize
208       nbytes -= ((bytes_received+nbytes) % d_itemsize);   
209
210       // copy the number of bytes we want to look at here
211       memcpy(out, d_temp_buff, nbytes);    
212
213       d_residual = r - nbytes;                      // save the number of bytes stored
214       d_temp_offset=nbytes;                         // reset buffer index
215
216       // keep track of the total number of bytes received
217       bytes_received += nbytes;
218
219       // increment the pointer
220       out += nbytes;
221
222       // Immediately return when data comes in
223       break;
224     }
225
226     #if SNK_VERBOSE
227     printf("\tbytes received: %d bytes (nbytes: %d)\n", bytes, nbytes);
228     #endif
229   }
230
231   #if SRC_VERBOSE
232   printf("Total Bytes Received: %d (bytes_received / noutput_items = %d / %d)\n", 
233          bytes_received, bytes_received, noutput_items);
234   #endif
235
236   // bytes_received is already set to some integer multiple of itemsize
237   return bytes_received/d_itemsize;
238 }
239