Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / lib / udpfromto.c
index 8dac01f..e4535fc 100644 (file)
@@ -11,7 +11,7 @@
  *
  *   You should have received a copy of the GNU Lesser General Public
  *   License along with this library; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
  *  Helper functions to get/set addresses of UDP packets
  *  based on recvfromto by Miquel van Smoorenburg
  * Version: $Id$
  */
 
-#include "autoconf.h"
+#include <freeradius-devel/ident.h>
+RCSID("$Id$")
 
-#ifdef WITH_UDPFROMTO
-static const char rcsid[] = "$Id$";
+#include <freeradius-devel/udpfromto.h>
 
-#include <sys/types.h>
+#ifdef WITH_UDPFROMTO
 
 #ifdef HAVE_SYS_UIO_H
 #include <sys/uio.h>
 #endif
 
-#include <netinet/in.h>
-#include <errno.h>
-#include <unistd.h>
 #include <fcntl.h>
-#include <string.h>
 
-#include "udpfromto.h"
 
 int udpfromto_init(int s)
 {
@@ -180,11 +175,22 @@ int sendfromto(int s, void *buf, size_t len, int flags,
        struct in_pktinfo pktinfo, *pktinfo_ptr;
        memset(&pktinfo, 0, sizeof(struct in_pktinfo));
 # endif
+       struct sockaddr_in      *s4;
 
 # ifdef HAVE_IP_SENDSRCADDR
        char cmsgbuf[CMSG_SPACE(sizeof(struct in_addr))];
 # endif
 
+       s4 = (struct sockaddr_in *) from;
+
+       /*
+        *      Source is unset, empty, or unspecified,
+        *      fall back to sendto().
+        */
+       if (!s4 || (fromlen == 0) || (s4->sin_family == AF_UNSPEC)) {
+               return sendto(s, buf, len, flags, to, tolen);
+       }
+
        /* Set up iov and msgh structures. */
        memset(&msgh, 0, sizeof(struct msghdr));
        iov.iov_base = buf;