Space indent -> tab indent.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33351 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2009-09-29 14:08:39 +00:00
parent 73ce611089
commit 49714746f2
+144 -143
View File
@@ -34,7 +34,7 @@ BNetAddress::BNetAddress(const char* hostname, unsigned short port)
: :
fInit(B_NO_INIT) fInit(B_NO_INIT)
{ {
SetTo(hostname, port); SetTo(hostname, port);
} }
@@ -58,7 +58,7 @@ BNetAddress::BNetAddress(uint32 addr, int port)
: :
fInit(B_NO_INIT) fInit(B_NO_INIT)
{ {
SetTo(addr, port); SetTo(addr, port);
} }
@@ -79,21 +79,21 @@ BNetAddress::BNetAddress(const BNetAddress& other)
BNetAddress::BNetAddress(BMessage* archive) BNetAddress::BNetAddress(BMessage* archive)
{ {
int16 int16value; int16 int16value;
if (archive->FindInt16("bnaddr_family", &int16value) != B_OK) if (archive->FindInt16("bnaddr_family", &int16value) != B_OK)
return; return;
fFamily = int16value; fFamily = int16value;
if (archive->FindInt16("bnaddr_port", &int16value) != B_OK) if (archive->FindInt16("bnaddr_port", &int16value) != B_OK)
return; return;
fPort = int16value; fPort = int16value;
if (archive->FindInt32("bnaddr_addr", &fAddress) != B_OK) if (archive->FindInt32("bnaddr_addr", &fAddress) != B_OK)
return; return;
fInit = B_OK; fInit = B_OK;
} }
@@ -105,145 +105,146 @@ BNetAddress::~BNetAddress()
BNetAddress& BNetAddress&
BNetAddress::operator=( const BNetAddress& other) BNetAddress::operator=( const BNetAddress& other)
{ {
fInit = other.fInit; fInit = other.fInit;
fFamily = other.fFamily; fFamily = other.fFamily;
fPort = other.fPort; fPort = other.fPort;
fAddress = other.fAddress; fAddress = other.fAddress;
return *this; return *this;
} }
/* GetAddr /* GetAddr
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Class accessor. * Class accessor.
* *
* Output parameters: * Output parameters:
* hostname : Host name associated with this instance (default: NULL). * hostname: Host name associated with this instance (default: NULL).
* In this particular implementation, hostname will be an * In this particular implementation, hostname will be an
* ASCII-fied representation of an IP address. * ASCII-fied representation of an IP address.
* port : Port number associated with this instance *
* (default: NULL). Will be converted to host byte order * port: Port number associated with this instance
* here, so it is not necessary to call ntohs() after * (default: NULL). Will be converted to host byte order
* calling this method. * here, so it is not necessary to call ntohs() after
* calling this method.
* *
* Returns: * Returns:
* B_OK for success, B_NO_INIT if instance was not properly constructed. * B_OK for success, B_NO_INIT if instance was not properly constructed.
* *
* Remarks: * Remarks:
* Hostname and/or port can be NULL; although having them both NULL would * Hostname and/or port can be NULL; although having them both NULL would
* be a pointless waste of CPU cycles. ;-) * be a pointless waste of CPU cycles. ;-)
* *
* The hostname output parameter can be a variety of things, but in this * The hostname output parameter can be a variety of things, but in this
* method we convert the IP address to a string. See the relevant * method we convert the IP address to a string. See the relevant
* documentation about inet_ntoa() for details. * documentation about inet_ntoa() for details.
* *
* Make sure hostname is large enough or you will step on someone * Make sure hostname is large enough or you will step on someone
* else's toes. (Can you say "buffer overflow exploit" boys and girls?) * else's toes. (Can you say "buffer overflow exploit" boys and girls?)
* You can generally be safe using the MAXHOSTNAMELEN define, which * You can generally be safe using the MAXHOSTNAMELEN define, which
* defaults to 64 bytes--don't forget to leave room for the NULL! * defaults to 64 bytes--don't forget to leave room for the NULL!
*/ */
status_t status_t
BNetAddress::GetAddr(char* hostname, unsigned short* port) const BNetAddress::GetAddr(char* hostname, unsigned short* port) const
{ {
if (fInit != B_OK) if (fInit != B_OK)
return B_NO_INIT; return B_NO_INIT;
if (port != NULL) if (port != NULL)
*port = ntohs(fPort); *port = ntohs(fPort);
if (hostname != NULL) { if (hostname != NULL) {
struct in_addr addr; struct in_addr addr;
addr.s_addr = fAddress; addr.s_addr = fAddress;
char* text = inet_ntoa(addr); char* text = inet_ntoa(addr);
if (text != NULL) if (text != NULL)
strcpy(hostname, text); strcpy(hostname, text);
} }
return B_OK; return B_OK;
} }
/* GetAddr /* GetAddr
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Class accessor. * Class accessor.
* *
* Output parameter: * Output parameter:
* sa : sockaddr_in struct to be filled. * sa: sockaddr_in struct to be filled.
* *
* Returns: * Returns:
* B_OK for success, B_NO_INIT if instance was not properly constructed. * B_OK for success, B_NO_INIT if instance was not properly constructed.
* *
* Remarks: * Remarks:
* This method fills in the sin_addr, sin_family, and sin_port fields of * This method fills in the sin_addr, sin_family, and sin_port fields of
* the output parameter, all other fields are untouched so we can work * the output parameter, all other fields are untouched so we can work
* with both POSIX and non-POSIX versions of said struct. The port and * with both POSIX and non-POSIX versions of said struct. The port and
* address values added to the output parameter are in network byte order. * address values added to the output parameter are in network byte order.
*/ */
status_t BNetAddress::GetAddr( struct sockaddr_in& sa ) const status_t BNetAddress::GetAddr( struct sockaddr_in& sa ) const
{ {
if ( fInit != B_OK ) if ( fInit != B_OK )
return B_NO_INIT; return B_NO_INIT;
sa.sin_port = fPort; sa.sin_port = fPort;
sa.sin_addr.s_addr = fAddress; sa.sin_addr.s_addr = fAddress;
if (check_r5_compatibility()) { if (check_r5_compatibility()) {
r5_sockaddr_in* r5Addr = (r5_sockaddr_in *)&sa; r5_sockaddr_in* r5Addr = (r5_sockaddr_in *)&sa;
if (fFamily == AF_INET) if (fFamily == AF_INET)
r5Addr->sin_family = R5_AF_INET; r5Addr->sin_family = R5_AF_INET;
else else
r5Addr->sin_family = fFamily; r5Addr->sin_family = fFamily;
} else } else
sa.sin_family = fFamily; sa.sin_family = fFamily;
return B_OK; return B_OK;
} }
/* GetAddr /* GetAddr
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Class accessor. * Class accessor.
* *
* Output parameters: * Output parameters:
* addr : in_addr struct to fill. * addr: in_addr struct to fill.
* port : optional port number to fill. * port: optional port number to fill.
* *
* Returns: * Returns:
* B_OK for success, B_NO_INIT if instance was not properly constructed. * B_OK for success, B_NO_INIT if instance was not properly constructed.
* *
* Remarks: * Remarks:
* Output port will be in host byte order, but addr will be in the usual * Output port will be in host byte order, but addr will be in the usual
* network byte order (ready to be used by other network functions). * network byte order (ready to be used by other network functions).
*/ */
status_t BNetAddress::GetAddr( in_addr& addr, unsigned short* port ) const status_t BNetAddress::GetAddr( in_addr& addr, unsigned short* port ) const
{ {
if ( fInit != B_OK ) if ( fInit != B_OK )
return B_NO_INIT; return B_NO_INIT;
addr.s_addr = fAddress; addr.s_addr = fAddress;
if ( port != NULL ) if ( port != NULL )
*port = ntohs(fPort); *port = ntohs(fPort);
return B_OK; return B_OK;
} }
/* InitCheck /* InitCheck
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Determine whether or not this instance is properly initialized. * Determine whether or not this instance is properly initialized.
* *
* Returns: * Returns:
* B_OK if this instance is initialized, B_ERROR if not. * B_OK if this instance is initialized, B_ERROR if not.
*/ */
status_t BNetAddress::InitCheck( void ) const status_t BNetAddress::InitCheck( void ) const
{ {
return ( fInit == B_OK ) ? B_OK : B_ERROR; return ( fInit == B_OK ) ? B_OK : B_ERROR;
} }
@@ -256,82 +257,82 @@ status_t BNetAddress::InitCheck()
/* Archive /* Archive
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Serialize this instance into the passed BMessage parameter. * Serialize this instance into the passed BMessage parameter.
* *
* Input parameter: * Input parameter:
* deep : [ignored] default==true. * deep: [ignored] default==true.
* *
* Output parameter: * Output parameter:
* into : BMessage object to serialize into. * into: BMessage object to serialize into.
* *
* Returns: * Returns:
* B_OK/BERROR on success/failure. Returns B_NO_INIT if instance not * B_OK/BERROR on success/failure. Returns B_NO_INIT if instance not
* properly initialized. * properly initialized.
*/ */
status_t BNetAddress::Archive( BMessage* into, bool deep ) const status_t BNetAddress::Archive( BMessage* into, bool deep ) const
{ {
if ( fInit != B_OK ) if ( fInit != B_OK )
return B_NO_INIT; return B_NO_INIT;
if ( into->AddInt16( "bnaddr_family", fFamily ) != B_OK ) if ( into->AddInt16( "bnaddr_family", fFamily ) != B_OK )
return B_ERROR; return B_ERROR;
if ( into->AddInt16( "bnaddr_port", fPort ) != B_OK ) if ( into->AddInt16( "bnaddr_port", fPort ) != B_OK )
return B_ERROR; return B_ERROR;
if ( into->AddInt32( "bnaddr_addr", fAddress ) != B_OK ) if ( into->AddInt32( "bnaddr_addr", fAddress ) != B_OK )
return B_ERROR; return B_ERROR;
return B_OK; return B_OK;
} }
/* Instantiate /* Instantiate
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Un-serialize and instantiate from the passed BMessage parameter. * Un-serialize and instantiate from the passed BMessage parameter.
* *
* Input parameter: * Input parameter:
* archive : Archived BMessage object for (de)serialization. * archive: Archived BMessage object for (de)serialization.
* *
* Returns: * Returns:
* NULL if a BNetAddress instance can not be initialized, otherwise * NULL if a BNetAddress instance can not be initialized, otherwise
* a new BNetAddress object instantiated from the BMessage parameter. * a new BNetAddress object instantiated from the BMessage parameter.
*/ */
BArchivable* BArchivable*
BNetAddress::Instantiate(BMessage* archive) BNetAddress::Instantiate(BMessage* archive)
{ {
if (!validate_instantiation(archive, "BNetAddress")) if (!validate_instantiation(archive, "BNetAddress"))
return NULL; return NULL;
BNetAddress* address = new (std::nothrow) BNetAddress(archive); BNetAddress* address = new (std::nothrow) BNetAddress(archive);
if (address == NULL) if (address == NULL)
return NULL; return NULL;
if (address->InitCheck() != B_OK) { if (address->InitCheck() != B_OK) {
delete address; delete address;
return NULL; return NULL;
} }
return address; return address;
} }
/* SetTo /* SetTo
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Set hostname and port network address data. * Set hostname and port network address data.
* *
* Input parameters: * Input parameters:
* hostname : Can be one of three things: * hostname: Can be one of three things:
* 1. An ASCII-string representation of an IP address. * 1. An ASCII-string representation of an IP address.
* 2. A canonical hostname. * 2. A canonical hostname.
* 3. NULL. If NULL, then by default the address will be * 3. NULL. If NULL, then by default the address will be
* set to INADDR_ANY (0.0.0.0). * set to INADDR_ANY (0.0.0.0).
* port : Duh. * port: Duh.
* *
* Returns: * Returns:
* B_OK/B_ERROR for success/failure. * B_OK/B_ERROR for success/failure.
*/ */
status_t status_t
BNetAddress::SetTo(const char* hostname, unsigned short port) BNetAddress::SetTo(const char* hostname, unsigned short port)
@@ -375,13 +376,13 @@ BNetAddress::SetTo(const struct sockaddr_in& addr)
fAddress = addr.sin_addr.s_addr; fAddress = addr.sin_addr.s_addr;
if (check_r5_compatibility()) { if (check_r5_compatibility()) {
const r5_sockaddr_in* r5Addr = (const r5_sockaddr_in *)&addr; const r5_sockaddr_in* r5Addr = (const r5_sockaddr_in *)&addr;
if (r5Addr->sin_family == R5_AF_INET) if (r5Addr->sin_family == R5_AF_INET)
fFamily = AF_INET; fFamily = AF_INET;
else else
fFamily = r5Addr->sin_family; fFamily = r5Addr->sin_family;
} else } else
fFamily = addr.sin_family; fFamily = addr.sin_family;
return fInit = B_OK; return fInit = B_OK;
} }
@@ -428,38 +429,38 @@ BNetAddress::SetTo(uint32 addr, int port)
/* SetTo /* SetTo
*=--------------------------------------------------------------------------=* *=--------------------------------------------------------------------------=*
* Purpose: * Purpose:
* Set from passed in hostname and protocol/service information. * Set from passed in hostname and protocol/service information.
* *
* Input parameters: * Input parameters:
* hostname : Can be one of three things: * hostname: Can be one of three things:
* 1. An ASCII-string representation of an IP address. * 1. An ASCII-string representation of an IP address.
* 2. A canonical hostname. * 2. A canonical hostname.
* 3. NULL. If NULL, then by default the address will be * 3. NULL. If NULL, then by default the address will be
* set to INADDR_ANY (0.0.0.0). * set to INADDR_ANY (0.0.0.0).
* protocol : Datagram type, typically "TCP" or "UDP" * protocol: Datagram type, typically "TCP" or "UDP"
* service : The name of the service, such as http, ftp, et al. This * service: The name of the service, such as http, ftp, et al. This
* must be one of the official service names listed in * must be one of the official service names listed in
* /etc/services -- but you already knew that because * /etc/services -- but you already knew that because
* you're doing network/sockets programming, RIIIGHT???. * you're doing network/sockets programming, RIIIGHT???.
* *
* Returns: * Returns:
* B_OK/B_ERROR on success/failure. * B_OK/B_ERROR on success/failure.
* *
* Remarks: * Remarks:
* The protocol and service input parameters must be one of the official * The protocol and service input parameters must be one of the official
* types listed in /etc/services. We use these two parameters to * types listed in /etc/services. We use these two parameters to
* determine the port number (see getservbyname(3)). This method will * determine the port number (see getservbyname(3)). This method will
* fail if the aforementioned precondition is not met. * fail if the aforementioned precondition is not met.
*/ */
status_t status_t
BNetAddress::SetTo(const char* hostname, const char* protocol, BNetAddress::SetTo(const char* hostname, const char* protocol,
const char* service) const char* service)
{ {
struct servent* serviceEntry = getservbyname(service, protocol); struct servent* serviceEntry = getservbyname(service, protocol);
if (serviceEntry == NULL) if (serviceEntry == NULL)
return B_ERROR; return B_ERROR;
return SetTo(hostname, serviceEntry->s_port); return SetTo(hostname, serviceEntry->s_port);
} }