~ubuntu-branches/ubuntu/quantal/poco/quantal

« back to all changes in this revision

Viewing changes to Net/src/NetworkInterface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2008-11-15 11:39:15 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081115113915-7kauhm2c3m2i7oid
Tags: 1.3.3p1-2
* Fixed FTBFS with GCC 4.4 due to missing #include (Closes: #505619)
* Renamed 20_gcc43-missing-include.dpatch to 20_gcc44-missing-include.dpatch
* Downgraded dependencies on -dbg packages (Closes: #504342)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//
2
2
// NetworkInterface.cpp
3
3
//
4
 
// $Id: //poco/1.3/Net/src/NetworkInterface.cpp#4 $
 
4
// $Id: //poco/1.3/Net/src/NetworkInterface.cpp#8 $
5
5
//
6
6
// Library: Net
7
7
// Package: Sockets
39
39
#include "Poco/Net/NetException.h"
40
40
#include "Poco/NumberFormatter.h"
41
41
#include "Poco/RefCountedObject.h"
 
42
#if defined(_WIN32) && defined(POCO_WIN32_UTF8)
 
43
#include "Poco/UnicodeConverter.h"
 
44
#endif
42
45
#include <cstring>
43
46
 
44
47
 
59
62
{
60
63
public:
61
64
        NetworkInterfaceImpl();
62
 
        NetworkInterfaceImpl(const std::string& name, const IPAddress& address, int index = -1);
63
 
        NetworkInterfaceImpl(const std::string& name, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1);
 
65
        NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, int index = -1);
 
66
        NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1);
64
67
 
65
68
        int index() const;              
66
69
        const std::string& name() const;
 
70
        const std::string& displayName() const;
67
71
        const IPAddress& address() const;
68
72
        const IPAddress& subnetMask() const;
69
73
        const IPAddress& broadcastAddress() const;
73
77
 
74
78
private:        
75
79
        std::string _name;
 
80
        std::string _displayName;
76
81
        IPAddress   _address;
77
82
        IPAddress   _subnetMask;
78
83
        IPAddress   _broadcastAddress;
86
91
}
87
92
 
88
93
 
89
 
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const IPAddress& address, int index):
 
94
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, int index):
90
95
        _name(name),
 
96
        _displayName(displayName),
91
97
        _address(address),
92
98
        _index(index)
93
99
{
118
124
}
119
125
 
120
126
 
121
 
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
 
127
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
122
128
        _name(name),
 
129
        _displayName(displayName),
123
130
        _address(address),
124
131
        _subnetMask(subnetMask),
125
132
        _broadcastAddress(broadcastAddress),
145
152
}
146
153
 
147
154
 
 
155
inline const std::string& NetworkInterfaceImpl::displayName() const
 
156
{
 
157
        return _displayName;
 
158
}
 
159
 
 
160
 
148
161
inline const IPAddress& NetworkInterfaceImpl::address() const
149
162
{
150
163
        return _address;
184
197
}
185
198
 
186
199
 
 
200
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, int index):
 
201
        _pImpl(new NetworkInterfaceImpl(name, displayName, address, index))
 
202
{
 
203
}
 
204
 
 
205
 
 
206
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
 
207
        _pImpl(new NetworkInterfaceImpl(name, displayName, address, subnetMask, broadcastAddress, index))
 
208
{
 
209
}
 
210
 
 
211
 
187
212
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, int index):
188
 
        _pImpl(new NetworkInterfaceImpl(name, address, index))
 
213
        _pImpl(new NetworkInterfaceImpl(name, name, address, index))
189
214
{
190
215
}
191
216
 
192
217
 
193
218
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
194
 
        _pImpl(new NetworkInterfaceImpl(name, address, subnetMask, broadcastAddress, index))
 
219
        _pImpl(new NetworkInterfaceImpl(name, name, address, subnetMask, broadcastAddress, index))
195
220
{
196
221
}
197
222
 
229
254
}
230
255
 
231
256
 
 
257
const std::string& NetworkInterface::displayName() const
 
258
{
 
259
        return _pImpl->displayName();
 
260
}
 
261
 
 
262
 
232
263
const IPAddress& NetworkInterface::address() const
233
264
{
234
265
        return _pImpl->address();
292
323
                throw InterfaceNotFoundException(addr.toString(), "interface has no IP address");
293
324
        int index = 0;
294
325
#endif
295
 
        return NetworkInterface(name, addr, index);
 
326
        return NetworkInterface(name, name, addr, index);
296
327
#endif
297
328
}
298
329
 
378
409
                                if (pAddress->FirstUnicastAddress)
379
410
                                {
380
411
                                        IPAddress addr(pAddress->FirstUnicastAddress->Address.lpSockaddr, pAddress->FirstUnicastAddress->Address.iSockaddrLength);
381
 
                                        result.push_back(NetworkInterface(std::string(pAddress->AdapterName), addr, pAddress->Ipv6IfIndex));
 
412
                                        std::string name(pAddress->AdapterName);
 
413
                                        std::string displayName;
 
414
#ifdef POCO_WIN32_UTF8
 
415
                                        Poco::UnicodeConverter::toUTF8(pAddress->Description, displayName);
 
416
#else
 
417
                                        char displayNameBuffer[1024];
 
418
                                        int rc = WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR, pAddress->Description, -1, displayNameBuffer, sizeof(displayNameBuffer), NULL, NULL);
 
419
                                        if (rc) displayName = displayNameBuffer;
 
420
#endif
 
421
                                        result.push_back(NetworkInterface(name, displayName, addr, pAddress->Ipv6IfIndex));
382
422
                                        pAddress = pAddress->Next;
383
423
                                }
384
424
                        }
394
434
#endif
395
435
 
396
436
        // Add IPv4 loopback interface (not returned by GetAdaptersInfo)
397
 
        result.push_back(NetworkInterface("Loopback", IPAddress("127.0.0.1"), IPAddress("255.0.0.0"), IPAddress(), -1));
 
437
        result.push_back(NetworkInterface("Loopback", "Loopback Interface", IPAddress("127.0.0.1"), IPAddress("255.0.0.0"), IPAddress(), -1));
398
438
        // On Windows 2000 we use GetAdaptersInfo.
399
439
        PIP_ADAPTER_INFO pAdapterInfo;
400
440
        PIP_ADAPTER_INFO pInfo = 0;
425
465
                                        IPAddress subnetMask(std::string(pInfo->IpAddressList.IpMask.String));
426
466
                                        IPAddress broadcastAddress(address);
427
467
                                        broadcastAddress.mask(subnetMask, IPAddress("255.255.255.255"));
428
 
                                        result.push_back(NetworkInterface(std::string(pInfo->AdapterName), address, subnetMask, broadcastAddress));
 
468
                                        std::string name(pInfo->AdapterName);
 
469
                                        std::string displayName(pInfo->Description);
 
470
                                        result.push_back(NetworkInterface(name, displayName, address, subnetMask, broadcastAddress));
429
471
                                }
430
472
                                pInfo = pInfo->Next;
431
473
                        }
475
517
                {
476
518
                        if (ifap->ifa_addr->sa_family == AF_INET)
477
519
                        {
 
520
                                std::string name(ifap->ifa_name);
478
521
                                IPAddress addr(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_addr)->sin_addr, sizeof(struct in_addr));
479
 
                                result.push_back(NetworkInterface(std::string(ifap->ifa_name), addr));
 
522
                                IPAddress subnetMask(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_netmask)->sin_addr, sizeof(struct in_addr));
 
523
                                IPAddress broadcastAddr;
 
524
                                if (ifap->ifa_flags & IFF_BROADCAST)
 
525
                                        broadcastAddr = IPAddress(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_dstaddr)->sin_addr, sizeof(struct in_addr));
 
526
                                result.push_back(NetworkInterface(name, name, addr, subnetMask, broadcastAddr));
480
527
                        }
481
528
#if defined(POCO_HAVE_IPv6)
482
529
                        else if (ifap->ifa_addr->sa_family == AF_INET6)
483
530
                        {
484
531
                                IPAddress addr(&reinterpret_cast<struct sockaddr_in6*>(ifap->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
485
 
                                result.push_back(NetworkInterface(std::string(ifap->ifa_name), addr, if_nametoindex(ifap->ifa_name)));
 
532
                                std::string name(ifap->ifa_name);
 
533
                                result.push_back(NetworkInterface(name, name, addr, if_nametoindex(ifap->ifa_name)));
486
534
                        }
487
535
#endif
488
536
                }
564
612
#else
565
613
                                int index = -1;
566
614
#endif
567
 
                                result.push_back(NetworkInterface(std::string(ifr->ifr_name), addr, index));
 
615
                                std::string name(ifr->ifr_name);
 
616
                                result.push_back(NetworkInterface(name, name, addr, index));
568
617
                        }
569
618
                        ptr += sizeof(struct ifreq);
570
619
                }
659
708
#else
660
709
                                int index = -1;
661
710
#endif
662
 
                                result.push_back(NetworkInterface(std::string(ifr->ifr_name), addr, index));
 
711
                                std::string name(ifr->ifr_name);
 
712
                                result.push_back(NetworkInterface(name, name, addr, index));
663
713
                        }
664
714
                        len += sizeof(ifr->ifr_name);
665
715
                        ptr += len;