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

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 12 Aug 2010 15:50:32 +0200 (CEST)

Author: axeld
Date: 2010-08-12 15:50:32 +0200 (Thu, 12 Aug 2010)
New Revision: 38060
Changeset: http://dev.haiku-os.org/changeset/38060

Modified:
   haiku/trunk/headers/os/net/NetworkInterface.h
   haiku/trunk/src/kits/network/libnetapi/NetworkInterface.cpp
Log:
* Added convenience methods FindAddress(), and FindFirstAddress().
* Added BNetworkInterfaceAddress::Destination() as synonym for Broadcast()
  (depending on the configuration, it could be either one).
* Media() will now return -1 in case of an error.
* Added "const" where it makes sense.
* Added convenience functions for AddAddress(), and RemoveAddress() that only
  take the actual address.


Modified: haiku/trunk/headers/os/net/NetworkInterface.h
===================================================================
--- haiku/trunk/headers/os/net/NetworkInterface.h       2010-08-12 13:45:18 UTC 
(rev 38059)
+++ haiku/trunk/headers/os/net/NetworkInterface.h       2010-08-12 13:50:32 UTC 
(rev 38060)
@@ -21,20 +21,24 @@
                                                                
BNetworkInterfaceAddress();
                                                                
~BNetworkInterfaceAddress();
 
-                       status_t                        
SetTo(BNetworkInterface& interface,
+                       status_t                        SetTo(const 
BNetworkInterface& interface,
                                                                        int32 
index);
 
-                       void                            
SetAddress(BNetworkAddress& address);
-                       void                            
SetMask(BNetworkAddress& mask);
-                       void                            
SetBroadcast(BNetworkAddress& broadcast);
+                       void                            SetAddress(const 
BNetworkAddress& address);
+                       void                            SetMask(const 
BNetworkAddress& mask);
+                       void                            SetBroadcast(const 
BNetworkAddress& broadcast);
+                       void                            SetDestination(
+                                                                       const 
BNetworkAddress& destination);
 
                        BNetworkAddress&        Address() { return fAddress; }
                        BNetworkAddress&        Mask() { return fMask; }
                        BNetworkAddress&        Broadcast() { return 
fBroadcast; }
+                       BNetworkAddress&        Destination() { return 
fBroadcast; }
 
                        const BNetworkAddress& Address() const { return 
fAddress; }
                        const BNetworkAddress& Mask() const { return fMask; }
                        const BNetworkAddress& Broadcast() const { return 
fBroadcast; }
+                       const BNetworkAddress& Destination() const { return 
fBroadcast; }
 
                        void                            SetFlags(uint32 flags);
                        uint32                          Flags() const { return 
fFlags; }
@@ -67,7 +71,7 @@
                        uint32                          Index() const;
                        uint32                          Flags() const;
                        uint32                          MTU() const;
-                       uint32                          Media() const;
+                       int32                           Media() const;
                        uint32                          Metric() const;
                        uint32                          Type() const;
                        status_t                        GetStats(ifreq_stats& 
stats);
@@ -75,19 +79,23 @@
 
                        status_t                        SetFlags(uint32 flags);
                        status_t                        SetMTU(uint32 mtu);
-                       status_t                        SetMedia(uint32 media);
+                       status_t                        SetMedia(int32 media);
                        status_t                        SetMetric(uint32 
metric);
 
                        int32                           CountAddresses() const;
                        status_t                        GetAddressAt(int32 
index,
                                                                        
BNetworkInterfaceAddress& address);
+                       int32                           FindAddress(const 
BNetworkAddress& address);
+                       int32                           FindFirstAddress(int 
family);
 
                        status_t                        AddAddress(
                                                                        const 
BNetworkInterfaceAddress& address);
+                       status_t                        AddAddress(const 
BNetworkAddress& address);
                        status_t                        SetAddress(
                                                                        const 
BNetworkInterfaceAddress& address);
                        status_t                        RemoveAddress(
                                                                        const 
BNetworkInterfaceAddress& address);
+                       status_t                        RemoveAddress(const 
BNetworkAddress& address);
                        status_t                        RemoveAddressAt(int32 
index);
 
                        status_t                        
GetHardwareAddress(BNetworkAddress& address);

Modified: haiku/trunk/src/kits/network/libnetapi/NetworkInterface.cpp
===================================================================
--- haiku/trunk/src/kits/network/libnetapi/NetworkInterface.cpp 2010-08-12 
13:45:18 UTC (rev 38059)
+++ haiku/trunk/src/kits/network/libnetapi/NetworkInterface.cpp 2010-08-12 
13:50:32 UTC (rev 38060)
@@ -81,7 +81,7 @@
 
 BNetworkInterfaceAddress::BNetworkInterfaceAddress()
        :
-       fIndex(0),
+       fIndex(-1),
        fFlags(0)
 {
 }
@@ -93,7 +93,7 @@
 
 
 status_t
-BNetworkInterfaceAddress::SetTo(BNetworkInterface& interface, int32 index)
+BNetworkInterfaceAddress::SetTo(const BNetworkInterface& interface, int32 
index)
 {
        fIndex = index;
        return do_ifaliasreq(interface.Name(), B_SOCKET_GET_ALIAS, *this, true);
@@ -101,27 +101,34 @@
 
 
 void
-BNetworkInterfaceAddress::SetAddress(BNetworkAddress& address)
+BNetworkInterfaceAddress::SetAddress(const BNetworkAddress& address)
 {
        fAddress = address;
 }
 
 
 void
-BNetworkInterfaceAddress::SetMask(BNetworkAddress& mask)
+BNetworkInterfaceAddress::SetMask(const BNetworkAddress& mask)
 {
        fMask = mask;
 }
 
 
 void
-BNetworkInterfaceAddress::SetBroadcast(BNetworkAddress& broadcast)
+BNetworkInterfaceAddress::SetBroadcast(const BNetworkAddress& broadcast)
 {
        fBroadcast = broadcast;
 }
 
 
 void
+BNetworkInterfaceAddress::SetDestination(const BNetworkAddress& destination)
+{
+       fBroadcast = destination;
+}
+
+
+void
 BNetworkInterfaceAddress::SetFlags(uint32 flags)
 {
        fFlags = flags;
@@ -220,12 +227,12 @@
 }
 
 
-uint32
+int32
 BNetworkInterface::Media() const
 {
        ifreq request;
        if (do_request(request, Name(), SIOCGIFMEDIA) != B_OK)
-               return 0;
+               return -1;
 
        return request.ifr_media;
 }
@@ -292,7 +299,7 @@
 
 
 status_t
-BNetworkInterface::SetMedia(uint32 media)
+BNetworkInterface::SetMedia(int32 media)
 {
        ifreq request;
        request.ifr_media = media;
@@ -327,6 +334,54 @@
 }
 
 
+int32
+BNetworkInterface::FindAddress(const BNetworkAddress& address)
+{
+       int socket = ::socket(address.Family(), SOCK_DGRAM, 0);
+       if (socket < 0)
+               return errno;
+
+       FileDescriptorCloser closer(socket);
+
+       ifaliasreq request;
+       memset(&request, 0, sizeof(ifaliasreq));
+
+       strlcpy(request.ifra_name, Name(), IF_NAMESIZE);
+       request.ifra_index = -1;
+       memcpy(&request.ifra_addr, &address.SockAddr(), address.Length());
+       
+       if (ioctl(socket, B_SOCKET_GET_ALIAS, &request, sizeof(struct 
ifaliasreq))
+                       < 0)
+               return errno;
+
+       return request.ifra_index;
+}
+
+
+int32
+BNetworkInterface::FindFirstAddress(int family)
+{
+       int socket = ::socket(family, SOCK_DGRAM, 0);
+       if (socket < 0)
+               return errno;
+
+       FileDescriptorCloser closer(socket);
+
+       ifaliasreq request;
+       memset(&request, 0, sizeof(ifaliasreq));
+
+       strlcpy(request.ifra_name, Name(), IF_NAMESIZE);
+       request.ifra_index = -1;
+       request.ifra_addr.ss_family = AF_UNSPEC;
+       
+       if (ioctl(socket, B_SOCKET_GET_ALIAS, &request, sizeof(struct 
ifaliasreq))
+                       < 0)
+               return errno;
+
+       return request.ifra_index;
+}
+
+
 status_t
 BNetworkInterface::AddAddress(const BNetworkInterfaceAddress& address)
 {
@@ -335,6 +390,16 @@
 
 
 status_t
+BNetworkInterface::AddAddress(const BNetworkAddress& local)
+{
+       BNetworkInterfaceAddress address;
+       address.SetAddress(local);
+
+       return do_ifaliasreq(Name(), B_SOCKET_ADD_ALIAS, address);
+}
+
+
+status_t
 BNetworkInterface::SetAddress(const BNetworkInterfaceAddress& address)
 {
        return do_ifaliasreq(Name(), B_SOCKET_SET_ALIAS, address);
@@ -353,6 +418,16 @@
 
 
 status_t
+BNetworkInterface::RemoveAddress(const BNetworkAddress& address)
+{
+       ifreq request;
+       memcpy(&request.ifr_addr, &address.SockAddr(), address.Length());
+
+       return do_request(request, Name(), B_SOCKET_REMOVE_ALIAS);
+}
+
+
+status_t
 BNetworkInterface::RemoveAddressAt(int32 index)
 {
        BNetworkInterfaceAddress address;


Other related posts:

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