123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #ifndef CPLNETWORKS_SOURCE
- #define CPLNETWORKS_SOURCE
- #include <iostream>
- #include <cstring>
- #if defined (__linux__)
- # include <sys/socket.h>
- # include <net/ethernet.h>
- # include <net/if.h>
- #elif defined (__APPLE__)
- # include <sys/socket.h>
- # include <net/ethernet.h>
- # include <net/if.h>
- #elif defined (__OpenBSD__)
- # include <sys/socket.h>
- # include <net/if.h>
- # include <netinet/in.h>
- # include <netinet/if_ether.h>
- #elif defined (WINPCAP)
- # include <pcap.h>
- #else
- #error "Unknown environment"
- #endif
- #include "../classes/CPLNetworks.hpp"
- #include "../classes/CPLChannel.hpp"
- bool CPLNetworks::Empty (void) const
- {
- return (!this->mcount);
- }
- bool CPLNetworks::End (void) const
- {
- return (this->mindex >= this->mcount);
- }
- unsigned CPLNetworks::Count (void) const
- {
- return (this->mcount);
- }
- unsigned CPLNetworks::Index (void) const
- {
- return (this->mindex);
- }
- CPLNetworks & CPLNetworks::SelectFirst (void)
- {
- this->mindex = 0;
- return (*this);
- }
- CPLNetworks & CPLNetworks::SelectFinal (void)
- {
- this->mindex = this->mcount - 1;
- return (*this);
- }
- CPLNetworks & CPLNetworks::SelectPrev (void)
- {
- if (this->mindex > 0)
- {
- this->mindex--;
- }
- return (*this);
- }
- CPLNetworks & CPLNetworks::SelectNext (void)
- {
- if (this->mindex < this->mcount)
- {
- this->mindex++;
- }
- return (*this);
- }
- CPLNetworks & CPLNetworks::Select (unsigned index)
- {
- this->mindex = index;
- if (this->mindex > this->mcount)
- {
- this->mindex = this->mcount;
- }
- return (*this);
- }
- CPLNetworks & CPLNetworks::operator = (unsigned index)
- {
- return (this->Select (index));
- }
- CPLNetwork & CPLNetworks::operator [] (unsigned index)
- {
- return (this->Select (index).Selected ());
- }
- CPLNetwork & CPLNetworks::Selected (void) const
- {
- return (* this->mtable [this->mindex]);
- }
- CPLNetwork & CPLNetworks::Network (void) const
- {
- return (* this->mtable [this->mindex]);
- }
- CPLNetworks & CPLNetworks::Enumerate (void)
- {
- for (unsigned index = 0; index < this->mcount; index++)
- {
- this->mtable [index]->Enumerate ();
- }
- return (*this);
- }
- CPLNetworks::CPLNetworks (char const * ifname, unsigned timeout)
- {
- CPLChannel channel (ifname, timeout);
- byte bridge [CPLCHANNEL_BRIDGES_MAX][ETHER_ADDR_LEN];
- this->mtable = new CPLNetwork * [CPLCHANNEL_BRIDGES_MAX];
- this->mcount = channel.Bridges (bridge, sizeof (bridge));
- this->mindex = 0;
- while (this->mindex < this->mcount)
- {
- channel.ImportPeerAddress (bridge [this->mindex]);
- this->mtable [this->mindex] = new CPLNetwork (&channel);
- this->mindex++;
- }
- this->mindex = 0;
- return;
- }
- CPLNetworks::~CPLNetworks ()
- {
- delete [] this->mtable;
- return;
- }
- #endif
|