apr-bugs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 46389] New: Datagram (UDP) broadcasts fail with "permission denied" on platforms that require SO_BROADCAST to be set first
Date Sat, 13 Dec 2008 00:25:40 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=46389

           Summary: Datagram (UDP) broadcasts fail with "permission denied"
                    on platforms that require SO_BROADCAST to be set first
           Product: APR
           Version: HEAD
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: APR
        AssignedTo: bugs@apr.apache.org
        ReportedBy: madalexonline@yahoo.co.uk


Created an attachment (id=23011)
 --> (https://issues.apache.org/bugzilla/attachment.cgi?id=23011)
Patch to allow UDP broadcasts on platforms which require SO_BROADCAST to be set

Consider the following (simplified) code snippet for broadcasting a message on
a particular network interface:


bool
broadcast_message( apr_pool_t* pool, /* memory pool to allocate stuff in */
                   const char* data, /* message to send */
                   apr_size_t  len ) /* length of message to send */
{
    apr_socket_t*   sock;
    apr_sockaddr_t* dest;

    /* Create a socket for sending UDP messages */
    if ( APR_SUCCESS != apr_socket_create( &sock, APR_INET, SOCK_DGRAM,
                                           APR_PROTO_UDP, pool ) )
        return false;
    /* Get the broadcast address for the network (suppose we're sending on an
     * interface with address 192.168.0.1/24) */
    if ( APR_SUCCESS != apr_sockaddr_info_get( &dest, "192.168.0.255",
                                               APR_INET, 1234, 0, pool ) )
        return false;
    /* Send (broadcast) the message */
    if ( APR_SUCCESS != apr_socket_sendto( sock, dest, 0, data, &len ) )
        return false;
    return true;
}


This succeeds on Windows (only tested on XP SP2) but on Linux (only tested with
2.6.26 kernel, glibc 2.6.1) the apr_socket_sendto call fails with status code
APR_EACCES. This failure occurs because the SO_BROADCAST option is not set on
the socket.

Since apr_socket_opt_set does not support setting the SO_BROADCAST option the
only workaround is to obtain the underlying socket descriptor and call
setsockopt directly, handling all portability issues oneself.


It seems to me that there are two possible solutions:

1. Always allow broadcasts. Call setsockopt to set SO_BROADCAST on platforms
supporting it when apr_socket_sendto is used on a SOCK_DGRAM socket.

2. Implement support for SO_BROADCAST in apr_socket_opt_[gs]et, testing
ourselves for a broadcast address in apr_socket_sendto on platforms which do
not support SO_BROADCAST or ignore its setting.


I attach a patch (against current subversion trunk) which implements option
(1), since that was easiest and I'm lazy. The patch also extends testsockets.c
to cover the case of UDP broadcasts.

Disclaimer: The patch has only been tested on Linux.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


Mime
View raw message