void RSASHA256Stream::reset()
	{
		EVP_MD_CTX_cleanup(&_ctx);

		EVP_MD_CTX_init(&_ctx);

		if (!_verify)
		{
			if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
			{
				IBRCOMMON_LOGGER(critical) << "failed to initialize the signature function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}
		else
		{
			if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
			{
				IBRCOMMON_LOGGER(critical) << "failed to initialize the verfication function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}

		_sign_valid = false;
	}
		void EMailConvergenceLayer::queue(const dtn::core::Node &node,
				const dtn::net::BundleTransfer &job)
		{
			// Check if node supports email convergence layer
			const std::list<dtn::core::Node::URI> uri_list = node.get(dtn::core::Node::CONN_EMAIL);
			if (uri_list.empty())
			{
				dtn::net::TransferAbortedEvent::raise(node.getEID(), job.getBundle(),
						dtn::net::TransferAbortedEvent::REASON_UNDEFINED);
				return;
			}

			// Get recipient
			std::string recipient = dtn::utils::Utils::tokenize("//", node.getEID().getString())[1];

			// Create new Task
			EMailSmtpService::Task *t = new EMailSmtpService::Task(node, job, recipient);

			// Submit Task
			if(_config.getSmtpSubmitInterval() <= 0)
			{
				_smtp.submitNow(t);
			}else{
				_smtp.queueTask(t);
			}

			IBRCOMMON_LOGGER(info) << "EMail Convergence Layer: Bundle " << t->getJob().getBundle().toString() << " stored in submit queue" << IBRCOMMON_LOGGER_ENDL;

		}
Example #3
0
		void Clock::setOffset(struct timeval &tv)
		{
			if (!modify_clock)
			{
				if (!Clock::_offset_init)
				{
					timerclear(&Clock::_offset);
					Clock::_offset_init = true;
				}
				timeradd(&Clock::_offset, &tv, &Clock::_offset);
				IBRCOMMON_LOGGER(info) << "[Clock] new local offset: " << _offset.tv_sec << " seconds and " << _offset.tv_usec << " microseconds" << IBRCOMMON_LOGGER_ENDL;
			}
			else
			{
				struct timezone tz;
				struct timeval now;
				::gettimeofday(&now, &tz);

				// adjust by the offset
				timersub(&now, &tv, &now);

				// set the local clock to the new timestamp
				::settimeofday(&now, &tz);
			}
		}
Example #4
0
		void IndependentComponent::startup()
		{
			try {
				this->start();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER(error) << "failed to start IndependentComponent\n" << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Example #5
0
		void EMailImapService::run() throw ()
		{
			while(true)
			{
				_threadMutex.enter();

				if(!_run)
					break;

				try {
					IBRCOMMON_LOGGER(info) << "EMail Convergence Layer: Looking for new bundles via IMAP" << IBRCOMMON_LOGGER_ENDL;
					queryServer();
				}catch(vmime::exception &e) {
					if(_run)
						IBRCOMMON_LOGGER(error) << "EMail Convergence Layer: Error during IMAP fetch operation: " << e.what() << IBRCOMMON_LOGGER_ENDL;
				}catch(IMAPException &e) {
					if(_run)
						IBRCOMMON_LOGGER(error) << "EMail Convergence Layer: IMAP error: " << e.what() << IBRCOMMON_LOGGER_ENDL;
				}
			}
		}
	int RSASHA256Stream:: overflow(int c)
	{
		char *ibegin = out_buf_;
		char *iend = pptr();

		// mark the buffer as free
		setp(out_buf_, out_buf_ + BUFF_SIZE - 1);

		if (!std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()))
		{
			//TODO writing one byte after the buffer?
			*iend++ = std::char_traits<char>::to_char_type(c);
		}

		// if there is nothing to send, just return
		if ((iend - ibegin) == 0)
		{
			return std::char_traits<char>::not_eof(c);
		}

		if (!_verify)
			// hashing
		{
			if (!EVP_SignUpdate(&_ctx, reinterpret_cast<unsigned char*>(out_buf_), iend - ibegin))
			{
				IBRCOMMON_LOGGER(critical) << "failed to feed data into the signature function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}
		else
		{
			if (!EVP_VerifyUpdate(&_ctx, reinterpret_cast<unsigned char*>(out_buf_), iend - ibegin))
			{
				IBRCOMMON_LOGGER(critical) << "failed to feed data into the verfication function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}

		return std::char_traits<char>::not_eof(c);
	}
	void NetLinkManager::run()
	{
		struct nl_handle *handle = nl_handle_alloc();
		nl_connect(handle, NETLINK_ROUTE);

		// init route messages
		nl_socket_add_membership(handle, RTNLGRP_IPV4_IFADDR);

//		IPv6 requires further support in the parsing procedures!
//		nl_socket_add_membership(handle, RTNLGRP_IPV6_IFADDR);
		nl_socket_add_membership(handle, RTNLGRP_LINK);

		// add netlink fd to vsocket
		_sock->add(nl_socket_get_fd(handle));

		try {
			while (_initialized)
			{
				std::list<int> fds;
				_sock->select(fds, NULL);
				int fd = fds.front();

				// create a new event object
				NetLinkManagerEvent lme(fd);

				// ignore if the event is unknown
				if (lme.getType() == LinkManagerEvent::EVENT_UNKOWN) continue;

				// ignore if this is an wireless extension event
				if (lme.isWirelessExtension()) continue;

				// need to refresh the cache
				{
					ibrcommon::MutexLock l(_call_mutex);
					_refresh_cache = true;
				}

				// print out some debugging
				IBRCOMMON_LOGGER_DEBUG(10) << lme.toString() << IBRCOMMON_LOGGER_ENDL;

				// notify all subscribers about this event
				raiseEvent(lme);
			}
		} catch (const vsocket_exception&) {
			// stopped / interrupted
			IBRCOMMON_LOGGER(error) << "NetLink connection stopped" << IBRCOMMON_LOGGER_ENDL;
		}

		nl_close(handle);
		nl_handle_destroy(handle);
	}
	RSASHA256Stream::RSASHA256Stream(EVP_PKEY * const pkey, bool verify)
	 : ostream(this), out_buf_(new char[BUFF_SIZE]), _pkey(pkey), _verify(verify), _sign_valid(false)
	{
		// Initialize get pointer.  This should be zero so that underflow is called upon first read.
		setp(out_buf_, out_buf_ + BUFF_SIZE - 1);
		EVP_MD_CTX_init(&_ctx);

		if (!_verify)
		{
			if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
			{
				IBRCOMMON_LOGGER(critical) << "failed to initialize the signature function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}
		else
		{
			if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
			{
				IBRCOMMON_LOGGER(critical) << "failed to initialize the verfication function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}
	}
	void lowpanstream::queue(char *buf, int len)
	{
		ibrcommon::MutexLock l(in_buf_cond);

		IBRCOMMON_LOGGER_DEBUG(10) << "lowpanstream::queue"<< IBRCOMMON_LOGGER_ENDL;

		// Retrieve sequence number of frame
		unsigned int seq_num = buf[0] & SEQ_NUM_MASK;

		IBRCOMMON_LOGGER_DEBUG(45) << "Received frame sequence number: " << seq_num << IBRCOMMON_LOGGER_ENDL;

		// Check if the sequence number is what we expect
		if (in_seq_num_ != seq_num)
		{
			IBRCOMMON_LOGGER(error) << "Received frame with out of bound sequence number (" << seq_num << " expected " << (int)in_seq_num_ << ")"<< IBRCOMMON_LOGGER_ENDL;
			_abort = true;
			in_buf_cond.signal();
			return;
		}

		if (buf[0] & SEGMENT_LAST)
		{
			// reset sequence number for first segment
			in_seq_num_ = 0;
		} else
		{
			// increment the sequence number
			in_seq_num_ = (in_seq_num_ + 1) % 8;
		}

		// check if this is the right segment
		if (_in_first_segment)
		{
			if (!(buf[0] & SEGMENT_FIRST)) return;
			if (!(buf[0] & SEGMENT_LAST)) _in_first_segment = false;
		}

		while (!in_buf_free)
		{
			in_buf_cond.wait();
		}

		memcpy(in_buf_, buf + 1, len - 1);
		in_buf_len = len - 1;
		in_buf_free = false;
		in_buf_cond.signal();
	}
Example #10
0
		void EMailImapService::queryServer()
		{
			// Check connection
			if(!isConnected())
				connect();

			// Get all unread messages
			std::vector<vmime::ref<vmime::net::message> > allMessages;
			try {
				allMessages = _folder->getMessages();
				_folder->fetchMessages(allMessages,
					vmime::net::folder::FETCH_FLAGS | vmime::net::folder::FETCH_FULL_HEADER | vmime::net::folder::FETCH_UID);
			}catch(vmime::exception&) {
				// No messages found (folder empty)
			}
			for(std::vector<vmime::ref<vmime::net::message> >::iterator it =
					allMessages.begin(); it != allMessages.end(); ++it)
			{
				const int flags = (*it).get()->getFlags();

				if(!flags & vmime::net::message::FLAG_SEEN) {
					// Looking for new bundles for me
					try {
						try {
							IBRCOMMON_LOGGER(info) << "EMail Convergence Layer: Generating bundle from mail (" << it->get()->getUniqueId().c_str() << ")" << IBRCOMMON_LOGGER_ENDL;
							generateBundle((*it));
						}catch(IMAPException &e){
							try {
								returningMailCheck((*it));
							}catch(BidNotFound&) {
								IBRCOMMON_LOGGER(warning) << "EMail Convergence Layer: " << e.what() << IBRCOMMON_LOGGER_ENDL;
							}
						}
					}catch(vmime::exceptions::no_such_field &e) {
						IBRCOMMON_LOGGER(warning) << "EMail Convergence Layer: Mail " << (*it)->getUniqueId() << " has no subject, skipping" << IBRCOMMON_LOGGER_ENDL;
					}

					// Set mail flag to 'seen'
					(*it)->setFlags(vmime::net::message::FLAG_SEEN, vmime::net::message::FLAG_MODE_SET);

					// Purge storage if enabled
					if(_config.imapPurgeMail())
					{
						(*it)->setFlags(vmime::net::message::FLAG_DELETED, vmime::net::message::FLAG_MODE_SET);
					}
				} else {
					continue;
				}
			}

			// Delete old tasks
			{
				ibrcommon::Mutex l(_processedTasksMutex);
				std::list<EMailSmtpService::Task*>::iterator it = _processedTasks.begin();
				while(it != _processedTasks.end())
				{
					if(!(*it)->checkForReturningMail()) {
						dtn::net::BundleTransfer job = (*it)->getJob();
						job.complete();
						delete *it;
						*it = NULL;
						_processedTasks.erase(it++);
					}
				}
			}

			disconnect();
		}