[haiku-commits] r39590 - in haiku/trunk: headers/os/net src/kits/network/libnetapi

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 23 Nov 2010 22:00:05 +0100 (CET)

Author: axeld
Date: 2010-11-23 22:00:05 +0100 (Tue, 23 Nov 2010)
New Revision: 39590
Changeset: http://dev.haiku-os.org/changeset/39590

Modified:
   haiku/trunk/headers/os/net/NetworkAddress.h
   haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp
Log:
* Added SetAddress() methods for IPv4, and IPv6 addresses.
* Improved API.


Modified: haiku/trunk/headers/os/net/NetworkAddress.h
===================================================================
--- haiku/trunk/headers/os/net/NetworkAddress.h 2010-11-23 17:49:40 UTC (rev 
39589)
+++ haiku/trunk/headers/os/net/NetworkAddress.h 2010-11-23 21:00:05 UTC (rev 
39590)
@@ -33,8 +33,10 @@
                                                                
BNetworkAddress(const sockaddr_in& address);
                                                                
BNetworkAddress(const sockaddr_in6& address);
                                                                
BNetworkAddress(const sockaddr_dl& address);
-                                                               
BNetworkAddress(const in_addr_t address);
-                                                               
BNetworkAddress(const in6_addr* address);
+                                                               
BNetworkAddress(in_addr_t address,
+                                                                       uint16 
port = 0);
+                                                               
BNetworkAddress(const in6_addr& address,
+                                                                       uint16 
port = 0);
                                                                
BNetworkAddress(const BNetworkAddress& other);
                                                                
BNetworkAddress(BMessage* archive);
        virtual                                         ~BNetworkAddress();
@@ -57,15 +59,18 @@
                        void                            SetTo(const 
sockaddr_in& address);
                        void                            SetTo(const 
sockaddr_in6& address);
                        void                            SetTo(const 
sockaddr_dl& address);
-                       void                            SetTo(const in_addr_t 
address);
-                       void                            SetTo(const in6_addr* 
address);
+                       void                            SetTo(in_addr_t 
address, uint16 port = 0);
+                       void                            SetTo(const in6_addr& 
address, uint16 port = 0);
                        void                            SetTo(const 
BNetworkAddress& other);
 
                        status_t                        SetToBroadcast(int 
family, uint16 port = 0);
                        status_t                        SetToLocal();
                        status_t                        SetToLoopback();
                        status_t                        SetToMask(int family, 
uint32 prefixLength);
-                       status_t                        SetToWildcard(int 
family);
+                       status_t                        SetToWildcard(int 
family, uint16 port = 0);
+
+                       status_t                        SetAddress(in_addr_t 
address);
+                       status_t                        SetAddress(const 
in6_addr& address);
                        void                            SetPort(uint16 port);
                        status_t                        SetPort(const char* 
service);
 
@@ -126,7 +131,9 @@
 
                                                                operator const 
sockaddr*() const;
                                                                operator const 
sockaddr&() const;
+                                                               operator const 
sockaddr*();
                                                                operator 
sockaddr*();
+                                                               operator const 
sockaddr&();
                                                                operator 
sockaddr&();
 
 private:

Modified: haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp
===================================================================
--- haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp   2010-11-23 
17:49:40 UTC (rev 39589)
+++ haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp   2010-11-23 
21:00:05 UTC (rev 39590)
@@ -73,15 +73,15 @@
 }
 
 
-BNetworkAddress::BNetworkAddress(const in_addr_t address)
+BNetworkAddress::BNetworkAddress(in_addr_t address, uint16 port)
 {
-       SetTo(address);
+       SetTo(address, port);
 }
 
 
-BNetworkAddress::BNetworkAddress(const in6_addr* address)
+BNetworkAddress::BNetworkAddress(const in6_addr& address, uint16 port)
 {
-       SetTo(address);
+       SetTo(address, port);
 }
 
 
@@ -266,28 +266,28 @@
 
 
 void
-BNetworkAddress::SetTo(const in_addr_t inetAddress)
+BNetworkAddress::SetTo(in_addr_t inetAddress, uint16 port)
 {
-       sockaddr_in& address = (sockaddr_in&)fAddress;
        memset(&fAddress, 0, sizeof(sockaddr_storage));
 
-       address.sin_family = AF_INET;
-       address.sin_len = sizeof(sockaddr_in);
-       address.sin_addr.s_addr = inetAddress;
+       fAddress.ss_family = AF_INET;
+       fAddress.ss_len = sizeof(sockaddr_in);
+       SetAddress(inetAddress);
+       SetPort(port);
        fStatus = B_OK;
 }
 
 
 void
-BNetworkAddress::SetTo(const in6_addr* inet6Address)
+BNetworkAddress::SetTo(const in6_addr& inet6Address, uint16 port)
 {
-       sockaddr_in6& address = (sockaddr_in6&)fAddress;
        memset(&fAddress, 0, sizeof(sockaddr_storage));
 
-       address.sin6_family = AF_INET6;
-       address.sin6_len = sizeof(sockaddr_in6);
-       memcpy(address.sin6_addr.s6_addr, inet6Address,
-               sizeof(address.sin6_addr.s6_addr));
+       fAddress.ss_family = AF_INET6;
+       fAddress.ss_len = sizeof(sockaddr_in6);
+       SetAddress(inet6Address);
+       SetPort(port);
+
        fStatus = B_OK;
 }
 
@@ -383,12 +383,37 @@
 
 
 status_t
-BNetworkAddress::SetToWildcard(int family)
+BNetworkAddress::SetToWildcard(int family, uint16 port)
 {
-       return SetTo(family, NULL);
+       return SetTo(family, NULL, port);
 }
 
 
+status_t
+BNetworkAddress::SetAddress(in_addr_t inetAddress)
+{
+       if (Family() != AF_INET)
+               return B_BAD_VALUE;
+
+       sockaddr_in& address = (sockaddr_in&)fAddress;
+       address.sin_addr.s_addr = inetAddress;
+       return B_OK;
+}
+
+
+status_t
+BNetworkAddress::SetAddress(const in6_addr& inet6Address)
+{
+       if (Family() != AF_INET6)
+               return B_BAD_VALUE;
+
+       sockaddr_in6& address = (sockaddr_in6&)fAddress;
+       memcpy(address.sin6_addr.s6_addr, &inet6Address,
+               sizeof(address.sin6_addr.s6_addr));
+       return B_OK;
+}
+
+
 void
 BNetworkAddress::SetPort(uint16 port)
 {
@@ -1022,7 +1047,19 @@
 }
 
 
+BNetworkAddress::operator const sockaddr*()
+{
+       return (sockaddr*)&fAddress;
+}
+
+
 BNetworkAddress::operator sockaddr&()
 {
        return (sockaddr&)fAddress;
 }
+
+
+BNetworkAddress::operator const sockaddr&()
+{
+       return (sockaddr&)fAddress;
+}


Other related posts:

  • » [haiku-commits] r39590 - in haiku/trunk: headers/os/net src/kits/network/libnetapi - axeld