00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "config.hh"
00021
00022 #ifndef QOLYESTER_ENABLE_VIRTUAL
00023
00024 # ifndef QOLYESTER_DAEMON_NET_REALINTERFACE_HXX
00025 # define QOLYESTER_DAEMON_NET_REALINTERFACE_HXX 1
00026
00027 # include "cst/constants.hh"
00028 # include "sys/realinterfaceinfo.hh"
00029 # include "sys/socket.hh"
00030
00031 # include "realinterface.hh"
00032
00033 namespace olsr {
00034
00035 extern sch::Scheduler scheduler;
00036
00037 namespace net {
00038
00039 void
00040 RealInterface::configure() {
00041 assert(config_ != 0);
00042 config_->setup();
00043 }
00044
00045 void
00046 RealInterface::unconfigure() {
00047 assert(config_ != 0);
00048 config_->revert();
00049 }
00050
00051 RealInterface::RealInterface()
00052 : info_(),
00053 addr_(),
00054 prefix_(0),
00055 # ifdef QOLYESTER_TWO_SOCKETS
00056 insock_(sys::Socket::dummy()),
00057 outsock_(sys::Socket::dummy()),
00058 # else // !QOLYESTER_TWO_SOCKETS
00059 sock_(sys::Socket::dummy()),
00060 # endif
00061 events_(),
00062 usecount_(new unsigned(1)),
00063 config_(0)
00064 {}
00065
00066 RealInterface::RealInterface(const sys::RealInterfaceInfo& info)
00067 : info_(info),
00068 addr_(info_.get_addr()),
00069 prefix_(info_.get_prefix(addr_)),
00070 # ifdef QOLYESTER_TWO_SOCKETS
00071 insock_(),
00072 outsock_(),
00073 events_(),
00074 usecount_(new unsigned(1)),
00075 config_(new sys::RealInterfaceInfo::config_t(info_.name())) {
00076 insock_.set_mtu(info_.mtu() - address_t::header_length);
00077 outsock_.set_mtu(info_.mtu() - address_t::header_length);
00078
00079 address_t bcast = info_.get_bcast(addr_);
00080
00081 # if QOLYESTER_FAMILY_INET == 6
00082 insock_.bind_multicast(info_, bcast, OLSR_PORT_NUMBER);
00083 outsock_.set_multicast(info_);
00084 if (addr_.is_linklocal())
00085 outsock_.bind(addr_, OLSR_PORT_NUMBER, info_.index());
00086 else
00087 outsock_.bind(addr_, OLSR_PORT_NUMBER);
00088 # else // QOLYESTER_FAMILY_INET != 6
00089
00090 insock_.bind(bcast, OLSR_PORT_NUMBER);
00091 outsock_.bind_multicast(info_, addr_, OLSR_PORT_NUMBER);
00092 # endif
00093
00094 outsock_.set_baddr(bcast);
00095 # else // !QOLYESTER_TWO_SOCKETS
00096 sock_(),
00097 events_(),
00098 usecount_(new unsigned(1)),
00099 config_(new sys::RealInterfaceInfo::config_t(info_.name())) {
00100 address_t bcast = info_.get_bcast(addr_);
00101 sock_.set_mtu(info_.mtu() - address_t::header_length);
00102 sock_.bind_multicast(info_, bcast, OLSR_PORT_NUMBER);
00103 sock_.set_baddr(bcast);
00104 # endif
00105 }
00106
00107 RealInterface::RealInterface(const sys::RealInterfaceInfo& info,
00108 const address_t& addr)
00109 : info_(info),
00110 addr_(info_.get_addr(addr)),
00111 prefix_(info_.get_prefix(addr)),
00112 # ifdef QOLYESTER_TWO_SOCKETS
00113 insock_(),
00114 outsock_(),
00115 events_(),
00116 usecount_(new unsigned(1)),
00117 config_(new sys::RealInterfaceInfo::config_t(info_.name())) {
00118 insock_.set_mtu(info_.mtu() - address_t::header_length);
00119 outsock_.set_mtu(info_.mtu() - address_t::header_length);
00120
00121 address_t bcast = info_.get_bcast(addr_);
00122
00123 # if QOLYESTER_FAMILY_INET == 6
00124 insock_.bind_multicast(info_, bcast, OLSR_PORT_NUMBER);
00125 outsock_.set_multicast(info_);
00126 if (addr_.is_linklocal())
00127 outsock_.bind(addr_, OLSR_PORT_NUMBER, info_.index());
00128 else
00129 outsock_.bind(addr_, OLSR_PORT_NUMBER);
00130 # else // QOLYESTER_FAMILY_INET != 6
00131 insock_.bind(bcast, OLSR_PORT_NUMBER);
00132 outsock_.bind_multicast(info_, addr_, OLSR_PORT_NUMBER);
00133 # endif
00134
00135 outsock_.set_baddr(bcast);
00136 # else // !QOLYSTER_TWO_SOCKETS
00137 sock_(),
00138 events_(),
00139 usecount_(new unsigned(1)),
00140 config_(new sys::RealInterfaceInfo::config_t(info_.name())) {
00141 address_t bcast = info_.get_bcast(addr_);
00142 sock_.set_mtu(info_.mtu() - address_t::header_length);
00143 sock_.bind_multicast(info_, bcast, OLSR_PORT_NUMBER);
00144 sock_.set_baddr(bcast);
00145 # endif
00146 }
00147
00148 RealInterface::RealInterface(const This& other)
00149 : Super(other),
00150 info_(other.info_),
00151 addr_(other.addr_),
00152 prefix_(other.prefix_),
00153 # ifdef QOLYESTER_TWO_SOCKETS
00154 insock_(other.insock_),
00155 outsock_(other.outsock_),
00156 # else // !QOLYESTER_TWO_SOCKETS
00157 sock_(other.sock_),
00158 # endif
00159 events_(),
00160 usecount_(other.usecount_),
00161 config_(other.config_) {
00162 ++(*usecount_);
00163 }
00164
00165 RealInterface::~RealInterface() {
00166 debug << "Destroying interface instance {\n";
00167 for (events_t::iterator i = events_.begin(); i != events_.end();
00168 ) {
00169 debug << " Destroying " << (*i)->name() << "\n";
00170 events_t::iterator tmp = i++;
00171 scheduler.destroy(*tmp);
00172 }
00173 debug << "}" << std::endl;
00174 --(*usecount_);
00175 if (*usecount_ == 0) {
00176 delete usecount_;
00177 # ifdef QOLYESTER_TWO_SOCKETS
00178 insock_.close();
00179 outsock_.close();
00180 # else // !QOLYESTER_TWO_SOCKETS
00181 sock_.close();
00182 # endif
00183 if (config_ != 0) {
00184 unconfigure();
00185 delete config_;
00186 }
00187 }
00188 }
00189
00190 unsigned
00191 RealInterface::mtu() const {
00192 return info_.mtu() - address_t::header_length;
00193 }
00194
00195 sch::IOEvent::p_t
00196 RealInterface::recv_p() const {
00197 # ifdef QOLYESTER_TWO_SOCKETS
00198 return insock_.read_p();
00199 # else
00200 return sock_.read_p();
00201 # endif
00202 }
00203
00204 sch::IOEvent::p_t
00205 RealInterface::send_p() const {
00206 # ifdef QOLYESTER_TWO_SOCKETS
00207 return outsock_.write_p();
00208 # else
00209 return sock_.write_p();
00210 # endif
00211 }
00212
00213 void
00214 RealInterface::insert_event(sch::IOEvent* e) {
00215 debug << "Inserting " << e->name() << " into interface" << std::endl;
00216 events_.insert(e);
00217 }
00218
00219 void
00220 RealInterface::erase_event(sch::IOEvent* e) {
00221 debug << "Erasing " << e->name() << " from interface" << std::endl;
00222 events_.erase(e);
00223 }
00224
00225 void
00226 RealInterface::destroy_all_events() {
00227 while (!events_.empty()) {
00228 sch::IOEvent* e = *events_.begin();
00229 events_.erase(events_.begin());
00230 scheduler.destroy(e);
00231 }
00232 }
00233
00234
00235
00236
00237
00238
00239 pkt::Packet
00240 RealInterface::receive() const {
00241 address_t from;
00242 # ifdef QOLYESTER_TWO_SOCKETS
00243 utl::Data data = insock_.receive(from);
00244 # else // !QOLYESTER_TWO_SOCKETS
00245 utl::Data data = sock_.receive(from);
00246 # endif
00247 return pkt::Packet(from, data);
00248 }
00249
00250 void
00251 RealInterface::send(const pkt::Packet& p) const {
00252 # ifdef QOLYESTER_TWO_SOCKETS
00253 outsock_.sendto_bcast(p.data());
00254 # else // !QOLYESTER_TWO_SOCKETS
00255 sock_.sendto_bcast(p.data());
00256 # endif
00257 }
00258
00259 bool
00260 RealInterface::operator<(const This& rhs) const {
00261 return addr_ < rhs.addr_;
00262 }
00263
00264 RealInterface&
00265 RealInterface::make_key(const address_t& a) {
00266 const_cast<address_t&>(dummy_for_find_.addr_) = a;
00267 return dummy_for_find_;
00268 }
00269
00270 }
00271
00272 }
00273
00274 # endif // ! QOLYESTER_DAEMON_NET_REALINTERFACE_HXX
00275
00276 #endif // ! QOLYESTER_ENABLE_VIRTUAL