/* -*- c++ -*- */
/*
- * Copyright 2006 Free Software Foundation, Inc.
+ * Copyright 2007,2008,2009,2010 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
+ * the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
-
#include <gr_udp_sink.h>
#include <gr_io_signature.h>
-#include <cstdio>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
#include <stdexcept>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#if defined(HAVE_NETDB_H)
+typedef void* optval_t;
+#elif defined(HAVE_WINDOWS_H)
+// if not posix, assume winsock
+#define USING_WINSOCK
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#define SHUT_RDWR 2
+typedef char* optval_t;
+#endif
-#define SNK_VERBOSE 0
-
-gr_udp_sink::gr_udp_sink (size_t itemsize,
- const char *ipaddrl, unsigned short portl,
- const char *ipaddrr, unsigned short portr,
- unsigned int mtu)
- : gr_sync_block ("udp_sink",
- gr_make_io_signature (1, 1, itemsize),
- gr_make_io_signature (0, 0, 0)),
- d_itemsize (itemsize), d_updated(false), d_mtu(mtu)
-{
- // Set up the address stucture for the local address and port numbers
- inet_aton(ipaddrl, &d_ipaddr_local); // format IP address
- inet_aton(ipaddrr, &d_ipaddr_remote); // format IP address
- d_port_local = htons(portl); // format port number
- d_port_remote = htons(portr); // format port number
-
- d_sockaddr_local.sin_family = AF_INET;
- d_sockaddr_local.sin_addr = d_ipaddr_local;
- d_sockaddr_local.sin_port = d_port_local;
-
- d_sockaddr_remote.sin_family = AF_INET;
- d_sockaddr_remote.sin_addr = d_ipaddr_remote;
- d_sockaddr_remote.sin_port = d_port_remote;
-
- open();
-}
+#include <gruel/thread.h>
-// public constructor that returns a shared_ptr
+#define SNK_VERBOSE 0
-gr_udp_sink_sptr
-gr_make_udp_sink (size_t itemsize,
- const char *ipaddrl, unsigned short portl,
- const char *ipaddrr, unsigned short portr,
- unsigned int mtu)
+static int is_error( int perr )
{
- return gr_udp_sink_sptr (new gr_udp_sink (itemsize,
- ipaddrl, portl,
- ipaddrr, portr,
- mtu));
+ // Compare error to posix error code; return nonzero if match.
+#if defined(USING_WINSOCK)
+#define ENOPROTOOPT 109
+#define ECONNREFUSED 111
+ // All codes to be checked for must be defined below
+ int werr = WSAGetLastError();
+ switch( werr ) {
+ case WSAETIMEDOUT:
+ return( perr == EAGAIN );
+ case WSAENOPROTOOPT:
+ return( perr == ENOPROTOOPT );
+ case WSAECONNREFUSED:
+ return( perr == ECONNREFUSED );
+ default:
+ fprintf(stderr,"gr_udp_source/is_error: unknown error %d\n", perr );
+ throw std::runtime_error("internal error");
+ }
+ return 0;
+#else
+ return( perr == errno );
+#endif
}
-gr_udp_sink::~gr_udp_sink ()
+static void report_error( const char *msg1, const char *msg2 )
{
- close();
+ // Deal with errors, both posix and winsock
+#if defined(USING_WINSOCK)
+ int werr = WSAGetLastError();
+ fprintf(stderr, "%s: winsock error %d\n", msg1, werr );
+#else
+ perror(msg1);
+#endif
+ if( msg2 != NULL )
+ throw std::runtime_error(msg2);
+ return;
}
-bool
-gr_udp_sink::open()
+gr_udp_sink::gr_udp_sink (size_t itemsize,
+ const char *src, unsigned short port_src,
+ const char *dst, unsigned short port_dst,
+ int payload_size)
+ : gr_sync_block ("udp_sink",
+ gr_make_io_signature (1, 1, itemsize),
+ gr_make_io_signature (0, 0, 0)),
+ d_itemsize (itemsize), d_payload_size(payload_size)
{
- omni_mutex_lock l(d_mutex); // hold mutex for duration of this function
+ int ret = 0;
+ struct addrinfo *ip_src; // store the source ip info
+ struct addrinfo *ip_dst; // store the destination ip info
+
+#if defined(USING_WINSOCK) // for Windows (with MinGW)
+ // initialize winsock DLL
+ WSADATA wsaData;
+ int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
+ if( iResult != NO_ERROR ) {
+ report_error( "gr_udp_source WSAStartup", "can't open socket" );
+ }
+#endif
+
+ // Set up the address stucture for the source address and port numbers
+ // Get the source IP address from the host name
+ struct addrinfo hints;
+ memset( (void*)&hints, 0, sizeof(hints) );
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_protocol = IPPROTO_UDP;
+ char port_str[7];
+ sprintf( port_str, "%d", port_src );
+ ret = getaddrinfo( src, port_str, &hints, &ip_src );
+ if( ret != 0 )
+ report_error("gr_udp_source/getaddrinfo",
+ "can't initialize source socket" );
+
+ // Get the destination IP address from the host name
+ sprintf( port_str, "%d", port_dst );
+ ret = getaddrinfo( dst, port_str, &hints, &ip_dst );
+ if( ret != 0 )
+ report_error("gr_udp_source/getaddrinfo",
+ "can't initialize destination socket" );
// create socket
- if((d_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == NULL) {
- perror("socket open");
- throw std::runtime_error("can't open socket");
+ d_socket = socket(ip_src->ai_family, ip_src->ai_socktype,
+ ip_src->ai_protocol);
+ if(d_socket == -1) {
+ report_error("socket open","can't open socket");
}
// Turn on reuse address
- bool opt_val = true;
- if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&opt_val, sizeof(int))) {
- perror("SO_REUSEADDR");
- throw std::runtime_error("can't set socket option SO_REUSEADDR");
+ int opt_val = true;
+ if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
+ report_error("SO_REUSEADDR","can't set socket option SO_REUSEADDR");
}
// Don't wait when shutting down
linger lngr;
lngr.l_onoff = 1;
lngr.l_linger = 0;
- if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (void*)&lngr, sizeof(linger))) {
- perror("SO_LINGER");
- throw std::runtime_error("can't set socket option SO_LINGER");
+ if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
+ if( !is_error(ENOPROTOOPT) ) { // no SO_LINGER for SOCK_DGRAM on Windows
+ report_error("SO_LINGER","can't set socket option SO_LINGER");
+ }
}
// bind socket to an address and port number to listen on
- if(bind (d_socket, (sockaddr*)&d_sockaddr_local, sizeof(struct sockaddr))) {
- perror("socket bind");
- throw std::runtime_error("can't bind socket");
+ if(bind (d_socket, ip_src->ai_addr, ip_src->ai_addrlen) == -1) {
+ report_error("socket bind","can't bind socket");
}
// Not sure if we should throw here or allow retries
- if(connect(d_socket, (sockaddr*)&d_sockaddr_remote, sizeof(struct sockaddr))) {
- perror("socket connect");
- throw std::runtime_error("can't connect to socket");
+ if(connect(d_socket, ip_dst->ai_addr, ip_dst->ai_addrlen) == -1) {
+ report_error("socket connect","can't connect to socket");
}
- d_updated = true;
- return d_socket != 0;
+ freeaddrinfo(ip_src);
+ freeaddrinfo(ip_dst);
}
-void
-gr_udp_sink::close()
+// public constructor that returns a shared_ptr
+
+gr_udp_sink_sptr
+gr_make_udp_sink (size_t itemsize,
+ const char *src, unsigned short port_src,
+ const char *dst, unsigned short port_dst,
+ int payload_size)
{
- omni_mutex_lock l(d_mutex); // hold mutex for duration of this function
+ return gr_udp_sink_sptr (new gr_udp_sink (itemsize,
+ src, port_src,
+ dst, port_dst,
+ payload_size));
+}
+gr_udp_sink::~gr_udp_sink ()
+{
if (d_socket){
shutdown(d_socket, SHUT_RDWR);
+#if defined(USING_WINSOCK)
+ closesocket(d_socket);
+#else
+ ::close(d_socket);
+#endif
d_socket = 0;
}
- d_updated = true;
+
+#if defined(USING_WINSOCK) // for Windows (with MinGW)
+ // free winsock resources
+ WSACleanup();
+#endif
}
int
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
- char *in = (char *) input_items[0];
- socklen_t bytes=0, bytes_sent=0, bytes_to_send=0;
- unsigned int total_size = noutput_items*d_itemsize;
-
- while(bytes_sent < total_size) {
- bytes_to_send = (bytes_sent+d_mtu < total_size ? d_mtu : total_size-bytes_sent);
- bytes =send(d_socket, (in+bytes_sent), bytes_to_send, MSG_DONTWAIT);
- bytes_sent += bytes;
+ const char *in = (const char *) input_items[0];
+ ssize_t r=0, bytes_sent=0, bytes_to_send=0;
+ ssize_t total_size = noutput_items*d_itemsize;
+
+ #if SNK_VERBOSE
+ printf("Entered udp_sink\n");
+ #endif
+
+ while(bytes_sent < total_size) {
+ bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
+
+ r = send(d_socket, (in+bytes_sent), bytes_to_send, 0);
+ if(r == -1) { // error on send command
+ if( is_error(ECONNREFUSED) )
+ r = bytes_to_send; // discard data until receiver is started
+ else {
+ report_error("udp_sink",NULL); // there should be no error case where
+ return -1; // this function should not exit immediately
+ }
+ }
+ bytes_sent += r;
+
+ #if SNK_VERBOSE
+ printf("\tbyte sent: %d bytes\n", bytes);
+ #endif
}
#if SNK_VERBOSE