Ticket #5716: bind_connect_enhancements.diff

File bind_connect_enhancements.diff, 7.6 KB (added by atis.elsts, 12 years ago)
  • src/add-ons/kernel/network/protocols/l2cap/l2cap_address.cpp

     
    100100        && (!checkPort || ((sockaddr_l2cap *)address)->l2cap_psm == 0) );
    101101}
    102102
     103/*! Checks if the given \a address is L2CAP address.
     104    \return false if \a address is NULL, or with family different from AF_BLUETOOTH
     105        true if it has AF_BLUETOOTH address family
     106*/
     107static bool
     108l2cap_is_same_family(const sockaddr *address)
     109{
     110    if (address == NULL)
     111        return false;
     112 
     113    return address->sa_family == AF_BLUETOOTH;
     114}
    103115
    104116/*!
    105117    Compares the IP-addresses of the two given address structures \a a and \a b.
     
    401413    l2cap_equal_addresses_and_ports,
    402414    l2cap_equal_masked_addresses,
    403415    l2cap_is_empty_address,
     416    l2cap_is_same_family,
    404417    l2cap_first_mask_bit,
    405418    l2cap_check_mask,
    406419    l2cap_print_address,
     
    413426    l2cap_update_to,
    414427    l2cap_hash_address_pair,
    415428    l2cap_checksum_address,
    416     NULL // l2cap_matches_broadcast_address,
     429    NULL,   // l2cap_matches_broadcast_address,
     430    NULL    // get_loopback_address
    417431};
  • src/add-ons/kernel/network/protocols/ipv4/ipv4_address.cpp

     
    107107        && (!checkPort || ((sockaddr_in *)address)->sin_port == 0);
    108108}
    109109
     110/*! Checks if the given \a address is an IPv4 address.
     111    \return false if \a address is NULL, or with family different from AF_INET
     112        true if it has AF_INET address family
     113*/
     114static bool
     115ipv4_is_same_family(const sockaddr *address)
     116{
     117    if (address == NULL)
     118        return false;
    110119
     120    return address->sa_family == AF_INET;
     121}
     122
    111123/*! Compares the IP-addresses of the two given address structures \a a and \a b.
    112124    \return true if IP-addresses of \a a and \a b are equal, false if not
    113125*/
     
    477489    return B_OK;
    478490}
    479491
     492static void
     493ipv4_get_loopback_address(sockaddr *result)
     494{
     495    sockaddr_in *resultIn = (sockaddr_in *)result;
     496    memset(resultIn, 0, sizeof(resultIn));
     497    resultIn->sin_len = sizeof(sockaddr_in);
     498    resultIn->sin_family = AF_INET;
     499    resultIn->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
     500}
    480501
     502
    481503net_address_module_info gIPv4AddressModule = {
    482504    {
    483505        NULL,
     
    491513    ipv4_equal_addresses_and_ports,
    492514    ipv4_equal_masked_addresses,
    493515    ipv4_is_empty_address,
     516    ipv4_is_same_family,
    494517    ipv4_first_mask_bit,
    495518    ipv4_check_mask,
    496519    ipv4_print_address,
     
    503526    ipv4_update_to,
    504527    ipv4_hash_address_pair,
    505528    ipv4_checksum_address,
    506     NULL // ipv4_matches_broadcast_address,
     529    NULL, // ipv4_matches_broadcast_address,
     530    ipv4_get_loopback_address
    507531};
  • src/add-ons/kernel/network/protocols/unix/UnixAddress.cpp

     
    112112        && memcmp(address, &kEmptyAddress, kEmptyAddress.sun_len) == 0;
    113113}
    114114
     115static bool
     116unix_is_same_family(const sockaddr *address)
     117{
     118    if (address == NULL)
     119        return false;
     120 
     121    return address->sa_family == AF_UNIX;
     122}
    115123
    116124static int32
    117125unix_first_mask_bit(const sockaddr *mask)
     
    290298    unix_equal_addresses_and_ports,
    291299    unix_equal_masked_addresses,
    292300    unix_is_empty_address,
     301    unix_is_same_family,
    293302    unix_first_mask_bit,
    294303    unix_check_mask,
    295304    unix_print_address,
     
    302311    unix_update_to,
    303312    unix_hash_address_pair,
    304313    unix_checksum_address,
    305     NULL    // matches_broadcast_address
     314    NULL,   // matches_broadcast_address
     315    NULL    // get_loopback_address
    306316};
  • src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp

     
    584584{
    585585    TRACE("Connect() on address %s", PrintAddress(address));
    586586
     587    if (!AddressModule()->is_same_family(address))
     588        return EAFNOSUPPORT;
     589
    587590    MutexLocker locker(fLock);
    588591
    589592    if (gStackModule->is_restarted_syscall()) {
     
    604607    } else if (fState != CLOSED)
    605608        return EINPROGRESS;
    606609
    607     // TODO: this is IPv4 specific, and doesn't belong here!
    608610    // consider destination address INADDR_ANY as INADDR_LOOPBACK
    609     sockaddr_in _address;
    610     if (((sockaddr_in*)address)->sin_addr.s_addr == INADDR_ANY) {
    611         memcpy(&_address, address, sizeof(sockaddr_in));
    612         _address.sin_len = sizeof(sockaddr_in);
    613         _address.sin_addr.s_addr = INADDR_LOOPBACK;
    614         address = (sockaddr*)&_address;
     611    sockaddr_storage _address;
     612    if (AddressModule()->is_empty_address(address, false)) {
     613        AddressModule()->get_loopback_address((sockaddr *)&_address);
     614        // for IPv4 and IPv6 the port is at the same offset
     615        ((sockaddr_in &)_address).sin_port = ((sockaddr_in *)address)->sin_port;
     616        address = (sockaddr *)&_address;
    615617    }
    616618
    617619    status_t status = _PrepareSendPath(address);
  • src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp

     
    362362status_t
    363363EndpointManager::Bind(TCPEndpoint* endpoint, const sockaddr* address)
    364364{
    365     // TODO: check the family:
    366     // if (!AddressModule()->is_understandable(address))
    367     //  return EAFNOSUPPORT;
     365    // check the family
     366    if (!AddressModule()->is_same_family(address))
     367        return EAFNOSUPPORT;
    368368
    369369    WriteLocker locker(fLock);
    370370
  • src/add-ons/kernel/network/protocols/udp/udp.cpp

     
    287287UdpDomainSupport::BindEndpoint(UdpEndpoint *endpoint,
    288288    const sockaddr *address)
    289289{
     290    if (!AddressModule()->is_same_family(address))
     291        return EAFNOSUPPORT;
     292
    290293    MutexLocker _(fLock);
    291294
    292295    if (endpoint->IsActive())
     
    312315        // so we reset the peer address:
    313316        endpoint->PeerAddress().SetToEmpty();
    314317    } else {
     318        if (!AddressModule()->is_same_family(address))
     319            return EAFNOSUPPORT;
     320
     321        // consider destination address INADDR_ANY as INADDR_LOOPBACK
     322        sockaddr_storage _address;
     323        if (AddressModule()->is_empty_address(address, false)) {
     324            AddressModule()->get_loopback_address((sockaddr *)&_address);
     325            // for IPv4 and IPv6 the port is at the same offset
     326            ((sockaddr_in&)_address).sin_port = ((sockaddr_in *)address)->sin_port;
     327            address = (sockaddr *)&_address;
     328        }
     329
    315330        status_t status = endpoint->PeerAddress().SetTo(address);
    316331        if (status < B_OK)
    317332            return status;
  • headers/private/net/net_datalink.h

     
    113113    bool (*equal_masked_addresses)(const sockaddr *a, const sockaddr *b,
    114114                    const sockaddr *mask);
    115115    bool (*is_empty_address)(const sockaddr *address, bool checkPort);
     116    bool (*is_same_family)(const sockaddr *address);
    116117
    117118    int32 (*first_mask_bit)(const sockaddr *mask);
    118119
     
    142143
    143144    bool (*matches_broadcast_address)(const sockaddr *address,
    144145                    const sockaddr *mask, const sockaddr *broadcastAddr);
     146
     147    void (*get_loopback_address)(sockaddr *result);
    145148};
    146149
    147150#endif  // NET_DATALINK_H