TEST(DownloadManager, NormalTest) { ProtocolFactory factory; factory.addProtocol(std::auto_ptr<ProtocolBase>(new HttpProtocol)); DownloadManager manager(&factory); const char *uri = "http://curl.haxx.se/libcurl/c/curl_easy_setopt.html"; std::string taskOptions = manager.getTaskOptions(uri); std::cout << "task options:\n" << taskOptions << std::endl; Task task = manager.addTask(uri, "./", NULL, taskOptions.c_str(), NULL); manager.startTask(task); size_t upload = 0, download = 0; while (manager.perform(&download, &upload) > 0) { if (task.totalSize() > 0) printf("progress: %lu%%, %d/%d\r, download: %lu bytes, upload: %lu bytes", task.downloadSize() * 100 / task.totalSize(), task.validSource(), task.totalSource(), download, upload ); } manager.removeTask(task); }
void ConfigWizardSetUpAccountPage::initializePage() { ProtocolFactory *pf = field("choose-network.protocol-factory").value<ProtocolFactory *>(); if (!pf) return; if (field("choose-network.new").toBool()) AccountWidget = pf->newCreateAccountWidget(false, this); else if (field("choose-network.existing").toBool()) AccountWidget = pf->newAddAccountWidget(false, this); if (AccountWidget) { formLayout()->addRow(QString(), AccountWidget.data()); if (AccountWidget.data()->stateNotifier()) connect( AccountWidget.data()->stateNotifier(), SIGNAL(stateChanged(ConfigurationValueState)), this, SIGNAL(completeChanged())); // NOTE: This signal is declared by AccountCreateWidget and AccountCreateWidget // but not by ModalConfigurationWidget. It will work correctly with Qt meta-object system, though. connect(AccountWidget.data(), SIGNAL(accountCreated(Account)), this, SLOT(accountCreated(Account))); // Same as above, window() is QWizard. connect(AccountWidget.data(), SIGNAL(destroyed()), window(), SLOT(back())); } }
bool ConfigWizardWindow::goToAccountSetUp() const { if (field("choose-network.ignore").toBool()) return false; ProtocolFactory *pf = field("choose-network.protocol-factory").value<ProtocolFactory *>(); if (!pf) return false; if (field("choose-network.new").toBool() && !pf->canRegister()) return false; return true; }
bool ProtocolsModelProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const { if (!sourceModel()) return QSortFilterProxyModel::lessThan(left, right); QVariant lVariant = sourceModel()->data(left, ProtocolRole); QVariant rVariant = sourceModel()->data(right, ProtocolRole); if (!lVariant.canConvert<ProtocolFactory *>() || !rVariant.canConvert<ProtocolFactory *>()) return QSortFilterProxyModel::lessThan(left, right); ProtocolFactory *leftProtocol = lVariant.value<ProtocolFactory *>(); ProtocolFactory *rightProtocol = rVariant.value<ProtocolFactory *>(); int displayCompare = compareNames(leftProtocol->name(), rightProtocol->name()); return displayCompare < 0; }
/** \brief check that channels in a factory equal given channel URIs */ inline void checkChannelListEqual(const ProtocolFactory& factory, const std::set<std::string>& channelUris) { std::set<std::string> expected(channelUris); // make a copy so we can erase as we go for (const auto& channel : factory.getChannels()) { std::string uri = channel->getUri().toString(); if (expected.erase(uri) == 0) { BOOST_ERROR("Unexpected channel " << uri); } } for (const auto& uri : expected) { BOOST_ERROR("Missing channel " << uri); } }
inline void createFace(ProtocolFactory& factory, const FaceUri& uri, ndn::nfd::FacePersistency persistency, bool wantLocalFieldsEnabled, const CreateFaceExpectedResult& expected) { factory.createFace(uri, persistency, wantLocalFieldsEnabled, [expected] (const shared_ptr<Face>&) { BOOST_CHECK_EQUAL(CreateFaceExpectedResult::SUCCESS, expected.result); }, [expected] (uint32_t actualStatus, const std::string& actualReason) { BOOST_CHECK_EQUAL(CreateFaceExpectedResult::FAILURE, expected.result); BOOST_CHECK_EQUAL(actualStatus, expected.status); BOOST_CHECK_EQUAL(actualReason, expected.reason); }); }
inline void createFace(ProtocolFactory& factory, const FaceUri& remoteUri, const optional<FaceUri>& localUri, const FaceParams& params, const CreateFaceExpectedResult& expected, const std::function<void(const Face&)>& extraChecks = nullptr) { factory.createFace({remoteUri, localUri, params}, [expected, extraChecks] (const shared_ptr<Face>& face) { BOOST_CHECK_EQUAL(CreateFaceExpectedResult::SUCCESS, expected.result); if (extraChecks) { extraChecks(*face); } }, [expected] (uint32_t actualStatus, const std::string& actualReason) { BOOST_CHECK_EQUAL(CreateFaceExpectedResult::FAILURE, expected.result); BOOST_CHECK_EQUAL(actualStatus, expected.status); BOOST_CHECK_EQUAL(actualReason, expected.reason); }); }
bool Client::startClients(unsigned clientId, unsigned num_clients, ProtocolFactory & _protocolFactory, ContentManagerFactory & _contentManagerFactory, Host _server) { std::unique_lock<std::mutex> lck(lock); if (workers.find(clientId) != workers.end()) { return false; } std::vector<std::unique_ptr<ContentManager>> clients; for (unsigned cnt = 0; cnt < num_clients; cnt++) { std::unique_ptr<Protocol> protocol(_protocolFactory.createProtocol()); if (protocol->connect(_server)) { clients.push_back(_contentManagerFactory.createContentManager(std::move(protocol), false)); } } for (unsigned cnt = 0; cnt < clients.size(); cnt++) { if (!clients[cnt]->Start()) { return false; } } workers[clientId] = std::move(clients); return workers[clientId].size() == num_clients; }
int ConfigParser::parse(Poco::XML::Document* xmlDocument, NetworkLayout* networkLayout) { Node* configNode = xmlDocument->firstChild(); if(!configNode || (configNode->nodeName()!=XMLConstants::CONFIG_TAG)){ std::cout << "config.xml: No element with name " << XMLConstants::CONFIG_TAG << " at the appropiate place." << std::endl; exit(1); } Node* trafficNode = configNode->firstChild(); if(!trafficNode || (trafficNode->nodeName()!=XMLConstants::TRAFFIC_TAG)){ std::cout << "config.xml: No element with name " << XMLConstants::TRAFFIC_TAG << " at the appropiate place." << std::endl; exit(1); } int numTrafficNodes = 0; //For each traffic block while (trafficNode != 0) { ConfigContent* content = new ConfigContent(); numTrafficNodes++; //initialize non-mandatory content content->tcp_flags = std::vector<Flags>(); content->tcp_flags.push_back(SYN); content->icmp_type = 0; content->icmp_code = -1; content->dumpPath = std::pair<std::string, bool>("", false); content->destIP = IPAddress(); content->sourceIP = IPAddress(); content->inInterface = ""; content->outInterface = ""; content->numPackets = 1; Node* elementNode = trafficNode->firstChild(); while (elementNode != 0){ //elementNode must be an element node with name XMLConstants::SOURCEIPTAG or XMLConstants::SOURCEPORTTAG or ... if( (elementNode->nodeName()!=XMLConstants::SOURCE_IP_TAG) && (elementNode->nodeName()!=XMLConstants::SOURCE_PORT_TAG) && (elementNode->nodeName()!=XMLConstants::DESTINATION_IP_TAG) && (elementNode->nodeName()!=XMLConstants::DESTINATION_PORT_TAG) && (elementNode->nodeName()!=XMLConstants::PROTOCOL_TAG) && (elementNode->nodeName()!=XMLConstants::TCP_FLAGS_TAG) && (elementNode->nodeName()!=XMLConstants::ICMP_TYPE) && (elementNode->nodeName()!=XMLConstants::ICMP_CODE) && (elementNode->nodeName()!=XMLConstants::POLICY_TAG) && (elementNode->nodeName()!=XMLConstants::IN_INTERFACE_TAG) && (elementNode->nodeName()!=XMLConstants::OUT_INTERFACE_TAG) && (elementNode->nodeName()!=XMLConstants::DUMP_TAG) && (elementNode->nodeName()!=XMLConstants::ASCII_DUMP_TAG) && (elementNode->nodeName()!=XMLConstants::NUM_PACKETS)){ std::cout << "config.xml: XML element " << elementNode->nodeName() << " is not supported." << std::endl; exit(1); } Node* textNode = elementNode->firstChild(); if(!textNode){ std::cout << "config.xml: XML elements must contain something else than whitespace."<<std::endl; exit(1); } if (elementNode->nodeName() == XMLConstants::SOURCE_IP_TAG){ std::string ip = textNode->nodeValue(); removeWhitespace(ip); try { content->sourceIP = IPAddress(ip); } catch (Poco::Exception e) { std::cout << "config.xml: Parsed faulty source IP address" << ip << ": '" << e.message() << "'. Terminating." << std::endl; exit(1); } } else if (elementNode->nodeName() == XMLConstants::SOURCE_PORT_TAG) { if ((content->protocol != UDP) && (content->protocol != TCP)){ std::cout << "config.xml: You can only specify a source port if you have already specified the protocol as tcp or udp." << std::endl; exit(1); } content->sourcePort = atoi(textNode->nodeValue().c_str()); } else if (elementNode->nodeName() == XMLConstants::DESTINATION_IP_TAG) { std::string ip = textNode->nodeValue(); removeWhitespace(ip); try { content->destIP = IPAddress(ip); } catch (Poco::Exception e) { std::cout << "config.xml: Parsed faulty destination IP address " << ip << ": '" << e.message() << "'. Terminating." << std::endl; exit(1); } } else if (elementNode->nodeName() == XMLConstants::DESTINATION_PORT_TAG) { if ((content->protocol != UDP) && (content->protocol != TCP)){ std::cout << "config.xml: You can only specify a destination port if you have already specified the protocol as tcp or udp." << std::endl; exit(1); } content->destPort = atoi(textNode->nodeValue().c_str()); } else if (elementNode->nodeName() == XMLConstants::PROTOCOL_TAG) { ProtocolFactory* factory = ProtocolFactory::getInstance(); content->protocol = factory->parse(textNode->nodeValue()); } else if (elementNode->nodeName() == XMLConstants::TCP_FLAGS_TAG) { if (content->protocol != TCP){ std::cout << "config.xml: You can only specify tcp flags if you have already specified the protocol as tcp." << std::endl; exit(1); } Node* flagNode = elementNode->firstChild(); while (flagNode != 0) { Node* textNode = flagNode->firstChild(); if(!textNode){ std::cout << "config.xml: XML elements must contain something else than whitespace."<<std::endl; exit(1); } std::string value = textNode->getNodeValue(); removeWhitespace(value); if(value == "1") { if (flagNode->nodeName()==XMLConstants::URG_FLAG_TAG) { content->tcp_flags.push_back(URG); } else if (flagNode->nodeName()==XMLConstants::ACK_FLAG_TAG) { content->tcp_flags.push_back(ACK); } else if (flagNode->nodeName()==XMLConstants::PSH_FLAG_TAG) { content->tcp_flags.push_back(PSH); } else if (flagNode->nodeName()==XMLConstants::RST_FLAG_TAG) { content->tcp_flags.push_back(RST); } else if (flagNode->nodeName()==XMLConstants::SYN_FLAG_TAG) { content->tcp_flags.push_back(SYN); } else if (flagNode->nodeName()==XMLConstants::FIN_FLAG_TAG) { content->tcp_flags.push_back(FIN); } else { std::cout << "config.xml: XML element " << flagNode->nodeName() << " is not supported." << std::endl; exit(1); } } else if (value == "0") { if (flagNode->nodeName()==XMLConstants::SYN_FLAG_TAG) { for (std::vector<Flags>::iterator it = content->tcp_flags.begin(); it != content->tcp_flags.end(); it++) { if (*it == SYN) { content->tcp_flags.erase(it); } } } } else if ((flagNode->nodeName()!=XMLConstants::URG_FLAG_TAG) && (flagNode->nodeName()!=XMLConstants::ACK_FLAG_TAG) && (flagNode->nodeName()!=XMLConstants::PSH_FLAG_TAG) && (flagNode->nodeName()!=XMLConstants::RST_FLAG_TAG) && (flagNode->nodeName()!=XMLConstants::SYN_FLAG_TAG) && (flagNode->nodeName()!=XMLConstants::FIN_FLAG_TAG)){ std::cout << "config.xml: XML element " << flagNode->nodeName() << " is not supported as a tcp flag." << std::endl; exit(1); } else { std::cout << "config.xml: Content of XML element " << flagNode->nodeName() << " should be 0 or 1." << std::endl; exit(1); } flagNode = flagNode->nextSibling(); } } else if (elementNode->nodeName() == XMLConstants::ICMP_TYPE) { if (content->protocol != ICMP){ std::cout << "config.xml: You can only specify an icmp type if you have already specified the protocol as icmp." << std::endl; exit(1); } content->icmp_type = atoi(textNode->nodeValue().c_str()); } else if (elementNode->nodeName() == XMLConstants::ICMP_CODE) { if (content->protocol != ICMP){ std::cout << "config.xml: You can only specify an icmp code if you have already specified the protocol as icmp." << std::endl; exit(1); } content->icmp_code = atoi(textNode->nodeValue().c_str()); } else if (elementNode->nodeName() == XMLConstants::POLICY_TAG) { PolicyFactory* factory = PolicyFactory::getInstance(); content->policy = factory->parse(textNode->nodeValue()); } else if (elementNode->nodeName() == XMLConstants::IN_INTERFACE_TAG) { std::string inIf = textNode->nodeValue(); removeWhitespace(inIf); if(!networkLayout->hasInterface(inIf)){ std::cout << "config.xml: You can only specify an in-interface that's specified in network_layout.xml" << std::endl; exit(1); } content->inInterface = inIf; } else if (elementNode->nodeName() == XMLConstants::OUT_INTERFACE_TAG) { std::string outIf = textNode->nodeValue(); removeWhitespace(outIf); if(!networkLayout->hasInterface(outIf)){ std::cout << "config.xml: You can only specify an out-interface that's specified in network_layout.xml" << std::endl; exit(1); } content->outInterface = outIf; } else if (elementNode->nodeName() == XMLConstants::DUMP_TAG) { std::string dumpFile = textNode->nodeValue(); removeWhitespace(dumpFile); std::string path = _path + dumpFile; if(!fileExist(path)){ std::cout << "config.xml: The specified dump file " << textNode->nodeValue() << " is not found in the config folder." << std::endl; exit(1); } content->dumpPath = std::pair<std::string, bool>(path, false); } else if (elementNode->nodeName() == XMLConstants::ASCII_DUMP_TAG) { std::string dumpFile = textNode->nodeValue(); removeWhitespace(dumpFile); std::string path = _path + dumpFile; if(!fileExist(path)){ std::cout << "config.xml: The specified ascii dump file " << textNode->nodeValue() << " is not found in the config folder." << std::endl; exit(1); } content->dumpPath = std::pair<std::string, bool>(path, true); } else if (elementNode->nodeName() == XMLConstants::NUM_PACKETS) { content->numPackets = atoi(textNode->nodeValue().c_str()); } elementNode = elementNode->nextSibling(); } if (content->dumpPath.first!="") { //traffic from dump file detected. if(content->inInterface == "" || content->outInterface == ""){ std::cout << "config.xml: If you specify a " << (content->dumpPath.second?XMLConstants::ASCII_DUMP_TAG:XMLConstants::DUMP_TAG) << ", then you must also specify " << XMLConstants::IN_INTERFACE_TAG << "and " << XMLConstants::OUT_INTERFACE_TAG << "." << std::endl; exit(1); } //check existence of in- and out-interfaces. if (!networkLayout->hasInterface(content->inInterface) || !networkLayout->hasInterface(content->outInterface)){ std::cout << "config.xml: The specified default in- and out-interfaces " << content->inInterface << " and " << content->outInterface << " do not exist within network_layout.xml." << std::endl; exit(1); } checkDump(content->dumpPath, content->inInterface, content->outInterface, networkLayout); } else { //src ip and dst ip must be set if ((content->sourceIP.toString() == "0.0.0.0") || (content->destIP.toString()=="0.0.0.0")){ std::cout << "config.xml: No source or destination IP address specified. Otherwise you have to specify a dump file." << std::endl; exit(1); } //No dump file specified if (networkLayout->isInternal(content->sourceIP.toString(), content->destIP.toString())){ //src and dst if must be set if((content->inInterface=="") || (content->outInterface=="")){ std::cout << "config.xml: INTERN: Based on the specified ip addresses " << content->sourceIP.toString() << " and "<< content->destIP.toString() << ", the packet is classified as internal firewall traffic. You must specify the in- and out-interfaces properly." << std::endl; exit(1); } //check existence of in- and out-interfaces. if (!networkLayout->hasInterface(content->inInterface) || !networkLayout->hasInterface(content->outInterface)){ std::cout << "config.xml: INTERN: The specified in- and out-interfaces " << content->inInterface << " and " << content->outInterface << " do not exist within network_layout.xml." << std::endl; exit(1); } //check source and destination if(content->sourceIP.toString() != networkLayout->getIpAddress(content->outInterface)) { std::cout << "config.xml: INTERN: Source ip address " << content->sourceIP.toString() << " does not correspond to the specified out-interface " << content->outInterface << " (" << networkLayout->getIpAddress(content->outInterface)<< ")" << std::endl; exit(1); } if (content->destIP.toString() != networkLayout->getIpAddress(content->inInterface)){ std::cout << "config.xml: INTERN: Destination ip address " << content->destIP.toString() << " does not correspond to the specified in-interface " << content->inInterface << " (" << networkLayout->getIpAddress(content->inInterface)<< ")" << std::endl; exit(1); } } else if (networkLayout->isOutput(content->sourceIP.toString())){ //src if must be set if(content->outInterface == ""){ std::cout << "config.xml: OUTPUT: Based on the specified ip addresses " << content->sourceIP.toString() << " and "<< content->destIP.toString() << ", the packet is classified as firewall output traffic. You must specify the out-interface properly." << std::endl; exit(1); } //check existence of the out-interface. if (!networkLayout->hasInterface(content->outInterface)){ std::cout << "config.xml: OUTPUT: The specified out-interface " << content->outInterface << " does not exist within network_layout.xml." << std::endl; exit(1); } //check source if(content->sourceIP.toString() != networkLayout->getIpAddress(content->outInterface)){ std::cout << "config.xml: OUTPUT: Source ip address " << content->sourceIP.toString() << " does not correspond to the specified out-interface " << content->outInterface << " (" << networkLayout->getIpAddress(content->outInterface)<< ")" << std::endl; exit(1); } } else if (networkLayout->isInput(content->destIP.toString())) { //dst if must be set if(content->inInterface == ""){ std::cout << "config.xml: INPUT: Based on the specified ip addresses " << content->sourceIP.toString() << " and "<< content->destIP.toString() << ", the packet is classified as firewall input traffic. You must specify the in-interface properly." << std::endl; exit(1); } //check existence of the in-interface. if (!networkLayout->hasInterface(content->inInterface)){ std::cout << "config.xml: INPUT: The specified in-interface " << content->inInterface << " does not exist within network_layout.xml." << std::endl; exit(1); } //check destination if(content->destIP.toString() != networkLayout->getIpAddress(content->inInterface)){ std::cout << "config.xml: INPUT: Destination ip address " << content->destIP.toString() << " does not correspond to the specified in-interface " << content->inInterface << " (" << networkLayout->getIpAddress(content->inInterface)<< ")" << std::endl; exit(1); } } else { //forward //src and dst if must be set if((content->inInterface == "") || (content->outInterface == "")){ std::cout << "config.xml: FORWARD: Based on the specified ip addresses " << content->sourceIP.toString() << " and "<< content->destIP.toString() << ", the packet is classified as forward firewall traffic. You must specify the in- and out-interfaces properly." << std::endl; exit(1); } //check existence of in- and out-interfaces. if (!networkLayout->hasInterface(content->inInterface) || !networkLayout->hasInterface(content->outInterface)){ std::cout << "config.xml: FORWARD: The specified in- and out-interfaces " << content->inInterface << " and " << content->outInterface << " do not exist within network_layout.xml." << std::endl; exit(1); } } } _content.push_back(content); trafficNode = trafficNode->nextSibling(); } return numTrafficNodes; }