Changeset 26269

Show
Ignore:
Timestamp:
07/05/08 18:37:06 (5 months ago)
Author:
zooey
Message:

Resolved a couple of problems in BNetAddress implementation:
* fixed several byte order inconsistencies, it does not make sense to always

convert the byte order input/output values - no we convert where it can
be expected and leave it where it is confusing

* fixed size inconsistencies with respect to family and port, both of which

were sometimes handled as int8, as int16 and as int32 in different places
(now they are always int16)

These fixes make Beam connect to the correct address and port, but it still doesn't work, as it seems to be using UDP instead of TCP (doh!). Will look into that tomorrow.

Location:
haiku/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • haiku/trunk/headers/os/net/NetAddress.h

    r18680 r26269  
    5353 
    5454                status_t fInit; 
    55                 int32   fFamily; 
    56                 int32   fPort; 
     55                int16   fFamily; 
     56                int16   fPort; 
    5757                int32   fAddress; 
    58                 int32   fPrivateData[6]; 
     58                int32   fPrivateData[7]; 
    5959}; 
    6060 
  • haiku/trunk/src/kits/network/libnetapi/NetAddress.cpp

    r18680 r26269  
    11/* 
    2  * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. 
     2 * Copyright 2002-2006,2008, Haiku, Inc. All Rights Reserved. 
    33 * Distributed under the terms of the MIT License. 
    44 * 
    55 * Authors: 
    66 *              Scott T. Mansfield, thephantom@mac.com 
     7 *              Oliver Tappe, zooey@hirschkaefer.de 
    78 */ 
    89 
     
    1011        NetAddress.cpp -- Implementation of the BNetAddress class. 
    1112        Remarks: 
    12          * In all accessors, address and port are converted from network to 
     13         * In all accessors, non-struct output values are converted from network to 
    1314           host byte order. 
    14          * In all mutators, address and port are converted from host to 
     15         * In all mutators, non-struct input values are converted from host to 
    1516           network byte order. 
    1617         * No trouts were harmed during the development of this class. 
     
    7778BNetAddress::BNetAddress(BMessage* archive) 
    7879{ 
    79     int8 int8value; 
    80     if (archive->FindInt8("bnaddr_family", &int8value) != B_OK) 
     80    int16 int16value; 
     81    if (archive->FindInt16("bnaddr_family", &int16value) != B_OK) 
    8182        return; 
    8283 
    83     fFamily = int8value; 
    84  
    85     if (archive->FindInt8("bnaddr_port", &int8value) != B_OK) 
     84    fFamily = int16value; 
     85 
     86    if (archive->FindInt16("bnaddr_port", &int16value) != B_OK) 
    8687        return; 
    8788 
    88     fPort = int8value; 
     89    fPort = int16value; 
    8990 
    9091    if (archive->FindInt32("bnaddr_addr", &fAddress) != B_OK) 
     
    188189    } 
    189190 
    190     sa.sin_family = ( uint8 )fFamily; 
    191     sa.sin_port = ( uint8 )fPort; 
    192     sa.sin_addr.s_addr = ( in_addr_t )fAddress; 
     191    sa.sin_family = fFamily; 
     192    sa.sin_port = fPort; 
     193    sa.sin_addr.s_addr = fAddress; 
    193194 
    194195    return B_OK; 
     
    209210 * 
    210211 * Remarks: 
    211  *     Output parameters will be in network byte order, so it is not 
    212  *     necessary to call htons after calling this method. 
     212 *     Output port will be in host byte order, but addr will be in the usual 
     213 *     network byte order (ready to be used by other network functions). 
    213214 */ 
    214215status_t BNetAddress::GetAddr( in_addr& addr, unsigned short* port ) const 
    215216{ 
    216217    if ( fInit != B_OK ) 
    217     { 
    218218        return B_NO_INIT; 
    219     } 
    220219 
    221220    addr.s_addr = fAddress; 
    222221 
    223222    if ( port != NULL ) 
    224     { 
    225         *port = fPort; 
    226     } 
     223        *port = ntohs(fPort); 
    227224 
    228225    return B_OK; 
     
    262259{ 
    263260    if ( fInit != B_OK ) 
    264     { 
    265261        return B_NO_INIT; 
    266     } 
    267  
    268     if ( into->AddInt8( "bnaddr_family", fFamily ) != B_OK ) 
    269     { 
     262 
     263    if ( into->AddInt16( "bnaddr_family", fFamily ) != B_OK ) 
    270264        return B_ERROR; 
    271     } 
    272  
    273     if ( into->AddInt8( "bnaddr_port", fPort ) != B_OK ) 
    274     { 
     265 
     266    if ( into->AddInt16( "bnaddr_port", fPort ) != B_OK ) 
    275267        return B_ERROR; 
    276     } 
    277268 
    278269    if ( into->AddInt32( "bnaddr_addr", fAddress ) != B_OK ) 
    279     { 
    280270        return B_ERROR; 
    281     } 
    282271 
    283272    return B_OK; 
     
    355344        fFamily = AF_INET; 
    356345        fPort = htons(port); 
    357         fAddress = htonl(addr); 
     346        fAddress = addr; 
    358347 
    359348        return fInit = B_OK; 
     
    371360{ 
    372361        fFamily = addr.sin_family; 
    373         fPort = htons(addr.sin_port); 
    374         fAddress = htonl(addr.sin_addr.s_addr); 
     362        fPort = addr.sin_port; 
     363        fAddress = addr.sin_addr.s_addr; 
    375364 
    376365        return fInit = B_OK; 
     
    390379{ 
    391380        fFamily = AF_INET; 
    392         fPort = htons(port); 
    393         fAddress = htonl(addr.s_addr); 
     381        fPort = htons((short)port); 
     382        fAddress = addr.s_addr; 
    394383 
    395384        return fInit = B_OK; 
     
    409398{ 
    410399        fFamily = AF_INET; 
    411         fPort = htons(port); 
    412         fAddress = htonl(addr); 
     400        fPort = htons((short)port); 
     401        fAddress = addr; 
    413402 
    414403        return fInit = B_OK;