~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/include/Poco/Net/NetworkInterface.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// NetworkInterface.h
 
3
//
 
4
// $Id: //poco/1.2/Net/include/Poco/Net/NetworkInterface.h#1 $
 
5
//
 
6
// Library: Net
 
7
// Package: Sockets
 
8
// Module:  NetworkInterface
 
9
//
 
10
// Definition of the NetworkInterface class.
 
11
//
 
12
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
13
// and Contributors.
 
14
//
 
15
// Permission is hereby granted, free of charge, to any person or organization
 
16
// obtaining a copy of the software and accompanying documentation covered by
 
17
// this license (the "Software") to use, reproduce, display, distribute,
 
18
// execute, and transmit the Software, and to prepare derivative works of the
 
19
// Software, and to permit third-parties to whom the Software is furnished to
 
20
// do so, all subject to the following:
 
21
// 
 
22
// The copyright notices in the Software and this entire statement, including
 
23
// the above license grant, this restriction and the following disclaimer,
 
24
// must be included in all copies of the Software, in whole or in part, and
 
25
// all derivative works of the Software, unless such copies or derivative
 
26
// works are solely in the form of machine-executable object code generated by
 
27
// a source language processor.
 
28
// 
 
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
35
// DEALINGS IN THE SOFTWARE.
 
36
//
 
37
 
 
38
 
 
39
#ifndef Net_NetworkInterface_INCLUDED
 
40
#define Net_NetworkInterface_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Net/Net.h"
 
44
#include "Poco/Net/IPAddress.h"
 
45
#include "Poco/Mutex.h"
 
46
#include <vector>
 
47
 
 
48
 
 
49
namespace Poco {
 
50
namespace Net {
 
51
 
 
52
 
 
53
class Net_API NetworkInterface
 
54
        /// This class represents a network interface.
 
55
        /// 
 
56
        /// NetworkInterface is used with MulticastSocket to specify
 
57
        /// multicast interfaces for sending and receiving multicast
 
58
        /// messages.
 
59
{
 
60
public:
 
61
        typedef std::vector<NetworkInterface> NetworkInterfaceList;
 
62
 
 
63
        NetworkInterface();
 
64
                /// Creates a NetworkInterface representing the
 
65
                /// default interface.
 
66
                ///
 
67
                /// The name is empty, the IP address is the wildcard
 
68
                /// address and the index is zero.
 
69
        
 
70
        NetworkInterface(const NetworkInterface& interface);
 
71
                /// Creates the NetworkInterface by copying another one.
 
72
 
 
73
        ~NetworkInterface();
 
74
                /// Destroys the NetworkInterface.
 
75
 
 
76
        NetworkInterface& operator = (const NetworkInterface& interface);
 
77
                /// Assigns another NetworkInterface.
 
78
                
 
79
        int index() const;
 
80
                /// Returns the interface index.
 
81
                ///
 
82
                /// Only supported if IPv6 is available.
 
83
                /// Returns -1 if IPv6 is not available.
 
84
                
 
85
        const std::string& name() const;
 
86
                /// Returns the interface name.
 
87
                
 
88
        const IPAddress& address() const;
 
89
                /// Returns the IP address bound to the interface.
 
90
                
 
91
        bool supportsIPv4() const;
 
92
                /// Returns true if the interface supports IPv4.
 
93
                
 
94
        bool supportsIPv6() const;
 
95
                /// Returns true if the interface supports IPv6.        
 
96
                
 
97
        static NetworkInterface forName(const std::string& name, bool requireIPv6 = false);
 
98
                /// Returns the NetworkInterface for the given name.
 
99
                /// 
 
100
                /// If requireIPv6 is false, an IPv4 interface is returned.
 
101
                /// Otherwise, an IPv6 interface is returned.
 
102
                ///
 
103
                /// Throws an InterfaceNotFoundException if an interface
 
104
                /// with the give name does not exist.
 
105
                
 
106
        static NetworkInterface forAddress(const IPAddress& address);
 
107
                /// Returns the NetworkInterface for the given IP address.
 
108
                ///
 
109
                /// Throws an InterfaceNotFoundException if an interface
 
110
                /// with the give address does not exist.
 
111
 
 
112
        static NetworkInterface forIndex(int index);
 
113
                /// Returns the NetworkInterface for the given interface index.
 
114
                /// If an index of 0 is specified, a NetworkInterface instance
 
115
                /// representing the default interface (empty name and
 
116
                /// wildcard address) is returned.
 
117
                ///
 
118
                /// Throws an InterfaceNotFoundException if an interface
 
119
                /// with the given index does not exist (or IPv6 is not
 
120
                /// available).
 
121
                
 
122
        static NetworkInterfaceList list();
 
123
                /// Returns a list with all network interfaces
 
124
                /// on the system.
 
125
                ///
 
126
                /// If there are multiple addresses bound to one interface,
 
127
                /// multiple NetworkInterface instances are created for
 
128
                /// the same interface.
 
129
 
 
130
protected:
 
131
        NetworkInterface(const std::string& name, const IPAddress& address, int index = -1);
 
132
                /// Creates the NetworkInterface.
 
133
                
 
134
        IPAddress interfaceNameToAddress(const std::string& interfaceName) const;
 
135
                /// Determines the IPAddress bound to the interface with the given name.
 
136
                
 
137
        int interfaceNameToIndex(const std::string& interfaceName) const;
 
138
                /// Determines the interface index of the interface with the given name.
 
139
 
 
140
private:
 
141
        std::string _name;
 
142
        IPAddress   _address;
 
143
        int         _index;
 
144
        
 
145
        static Poco::FastMutex _mutex;
 
146
};
 
147
 
 
148
 
 
149
//
 
150
// inlines
 
151
//
 
152
inline int NetworkInterface::index() const
 
153
{
 
154
        return _index;
 
155
}
 
156
 
 
157
 
 
158
inline const std::string& NetworkInterface::name() const
 
159
{
 
160
        return _name;
 
161
}
 
162
 
 
163
 
 
164
inline const IPAddress& NetworkInterface::address() const
 
165
{
 
166
        return _address;
 
167
}
 
168
 
 
169
 
 
170
inline bool NetworkInterface::supportsIPv4() const
 
171
{
 
172
        return _index == -1;
 
173
}
 
174
 
 
175
        
 
176
inline bool NetworkInterface::supportsIPv6() const
 
177
{
 
178
        return _index != -1;
 
179
}
 
180
 
 
181
 
 
182
} } // namespace Poco::Net
 
183
 
 
184
 
 
185
#endif // Net_NetworkInterface_INCLUDED