void receive_probe_requests(NetworkInterface iface) { SnifferConfiguration config; config.set_rfmon(true); config.set_filter("type mgt subtype probe-req || type data subtype null"); Sniffer sniffer(iface.name(), config); while(true) process(sniffer.next_packet()); }
void PacketSender::open_l2_socket(const NetworkInterface& iface) { #if defined(BSD) || defined(__FreeBSD_kernel__) int sock = -1; // At some point, there should be an available device for (int i = 0; sock == -1;i++) { std::ostringstream oss; oss << "/dev/bpf" << i; sock = open(oss.str().c_str(), O_RDWR); } if(sock == -1) throw socket_open_error(make_error_string()); struct ifreq ifr; strncpy(ifr.ifr_name, iface.name().c_str(), sizeof(ifr.ifr_name) - 1); if(ioctl(sock, BIOCSETIF, (caddr_t)&ifr) < 0) { ::close(sock); throw socket_open_error(make_error_string()); } _ether_socket[iface.id()] = sock; #else if (_ether_socket == INVALID_RAW_SOCKET) { _ether_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (_ether_socket == -1) throw socket_open_error(make_error_string()); } #endif }
void NetworkInterfaceTest::testForName() { NetworkInterface::Map map = NetworkInterface::map(); for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it) { NetworkInterface ifc = NetworkInterface::forName(it->second.name()); assert (ifc.name() == it->second.name()); } }
NetworkInterface* NetworkInterface::GetByName(const String& name, bool do_update) { if ((__interfaces != NULL)&&(!do_update)) { for (uint32 i = 0; i < __interfaces->size(); ++i) { NetworkInterface* iface = (*__interfaces)[i]; if (iface->name() == name) return iface; } } if (do_update) { __update(); // and repeat same code from above (blah copy paste) for (uint32 i = 0; i < __interfaces->size(); ++i) { NetworkInterface* iface = (*__interfaces)[i]; if (iface->name() == name) return iface; } } return NULL; }
NetworkState::NetworkState(String interfaceName): interfaceName_(interfaceName), networkMask_(0) { Ref<RouteInfoList> routingTable = RouteInfo::queryTable(); for (int i = 0; i < routingTable->count(); ++i) { RouteInfo *entry = routingTable->at(i); if (entry->source() || entry->destination()) continue; if (interfaceName_ != "" && entry->outputInterface() != interfaceName_) continue; interfaceName_ = entry->outputInterface(); gateway_ = entry->gateway(); break; } Ref<NetworkInterfaceList> interfaceList = NetworkInterface::queryAll(AF_UNSPEC); for (int i = 0; i < interfaceList->count(); ++i) { NetworkInterface *candidate = interfaceList->at(i); if (candidate->name() == interfaceName_) { interface_ = candidate; Ref<SocketAddressList> addressList = interface_->addressList(); if (!gateway_) { for (int j = 0; j < addressList->count(); ++j) { SocketAddress *address = addressList->at(j); if (address->family() != AF_INET) continue; address_ = address; break; } } if (!address_) { for (int j = 0; j < addressList->count(); ++j) { SocketAddress *address = addressList->at(j); if (gateway_ && address->family() != gateway_->family()) continue; address_ = address; break; } } } } Ref<SocketAddressEntry> entry = cast<SocketAddressEntry>(address_); if (entry) networkMask_ = entry->networkMask(); }