TcpStream TcpStream::connect(const Ipv4Address& address) { Descriptor fd(socket(AF_INET, SOCK_STREAM, 0)); if (fd < 0) { throw SystemError("IPv4 socket error"); } if (::connect(fd, address.address(), address.length()) < 0) { throw SystemError("IPv4 connect error"); } return TcpStream(std::move(fd)); }
Ipv4Listener::Ipv4Listener(uint16_t port) : _fd(socket(AF_INET, SOCK_STREAM, 0)) { if(_fd < 0) { throw SystemError("IPv4 socket error"); } Ipv4Address address = Ipv4Address::any(port); if(bind(_fd, address.address(), address.length()) < 0) { throw SystemError("IPv4 bind error"); } if(listen(_fd, 20) < 0) { throw SystemError("IPv4 listen error"); } }
bool IpcsClassifierRecord::CheckMatchSrcAddr (Ipv4Address srcAddress) const { for (std::vector<struct ipv4Addr>::const_iterator iter = m_srcAddr.begin (); iter != m_srcAddr.end (); ++iter) { NS_LOG_INFO ("src addr check match: pkt=" << srcAddress << " cls=" << (*iter).Address << "/" << (*iter).Mask); if (srcAddress.CombineMask ((*iter).Mask) == (*iter).Address) { return true; } } NS_LOG_INFO ("NOT OK!"); return false; }
void Ipv4AddressHelper::SetBase ( const Ipv4Address network, const Ipv4Mask mask, const Ipv4Address address) { NS_LOG_FUNCTION_NOARGS (); m_network = network.Get (); m_mask = mask.Get (); m_base = m_address = address.Get (); // // Some quick reasonableness testing. // NS_ASSERT_MSG ((m_network & ~m_mask) == 0, "Ipv4AddressHelper::SetBase(): Inconsistent network and mask"); // // Figure out how much to shift network numbers to get them aligned, and what // the maximum allowed address is with respect to the current mask. // m_shift = NumAddressBits (m_mask); m_max = (1 << m_shift) - 2; NS_ASSERT_MSG (m_shift <= 32, "Ipv4AddressHelper::SetBase(): Unreasonable address length"); // // Shift the network down into the normalized position. // m_network >>= m_shift; NS_LOG_LOGIC ("m_network == " << m_network); NS_LOG_LOGIC ("m_mask == " << m_mask); NS_LOG_LOGIC ("m_address == " << m_address); }
TcpListener::TcpListener(Dispatcher& dispatcher, const Ipv4Address& addr, uint16_t port) : dispatcher(&dispatcher) { std::string message; listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listener == -1) { message = "socket failed, " + lastErrorMessage(); } else { int flags = fcntl(listener, F_GETFL, 0); if (flags == -1 || fcntl(listener, F_SETFL, flags | O_NONBLOCK) == -1) { message = "fcntl failed, " + lastErrorMessage(); } else { int on = 1; if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) == -1) { message = "setsockopt failed, " + lastErrorMessage(); } else { sockaddr_in address; address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr.s_addr = htonl( addr.getValue()); if (bind(listener, reinterpret_cast<sockaddr *>(&address), sizeof address) != 0) { message = "bind failed, " + lastErrorMessage(); } else if (listen(listener, SOMAXCONN) != 0) { message = "listen failed, " + lastErrorMessage(); } else { epoll_event listenEvent; listenEvent.events = 0; listenEvent.data.ptr = nullptr; if (epoll_ctl(dispatcher.getEpoll(), EPOLL_CTL_ADD, listener, &listenEvent) == -1) { message = "epoll_ctl failed, " + lastErrorMessage(); } else { context = nullptr; return; } } } } int result = close(listener); assert(result != -1); } throw std::runtime_error("TcpListener::TcpListener, " + message); }
TcpListener::TcpListener(Dispatcher& dispatcher, const Ipv4Address& addr, uint16_t port) : dispatcher(&dispatcher) { std::string message; listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listener == -1) { message = "socket() failed, errno=" + std::to_string(errno); } else { int flags = fcntl(listener, F_GETFL, 0); if (flags == -1 || (fcntl(listener, F_SETFL, flags | O_NONBLOCK) == -1)) { message = "fcntl() failed errno=" + std::to_string(errno); } else { int on = 1; if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) == -1) { message = "setsockopt failed, errno=" + std::to_string(errno); } else { sockaddr_in address; address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr.s_addr = htonl(addr.getValue()); if (bind(listener, reinterpret_cast<sockaddr*>(&address), sizeof address) != 0) { message = "bind failed, errno=" + std::to_string(errno); } else if (listen(listener, SOMAXCONN) != 0) { message = "listen failed, errno=" + std::to_string(errno); } else { struct kevent event; EV_SET(&event, listener, EVFILT_READ, EV_ADD | EV_DISABLE | EV_CLEAR, 0, SOMAXCONN, NULL); if (kevent(dispatcher.getKqueue(), &event, 1, NULL, 0, NULL) == -1) { message = "kevent() failed, errno=" + std::to_string(errno); } else { context = nullptr; return; } } } } if (close(listener) == -1) { message = "close failed, errno=" + std::to_string(errno); } } throw std::runtime_error("TcpListener::TcpListener, " + message); }
TcpListener::TcpListener(Dispatcher& dispatcher, const Ipv4Address& address, uint16_t port) : dispatcher(&dispatcher) { std::string message; listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listener == INVALID_SOCKET) { message = "socket failed, " + errorMessage(WSAGetLastError()); } else { sockaddr_in addressData; addressData.sin_family = AF_INET; addressData.sin_port = htons(port); addressData.sin_addr.S_un.S_addr = htonl(address.getValue()); if (bind(listener, reinterpret_cast<sockaddr*>(&addressData), sizeof(addressData)) != 0) { message = "bind failed, " + errorMessage(WSAGetLastError()); } else if (listen(listener, SOMAXCONN) != 0) { message = "listen failed, " + errorMessage(WSAGetLastError()); } else { GUID guidAcceptEx = WSAID_ACCEPTEX; DWORD read = sizeof acceptEx; if (acceptEx == nullptr && WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &guidAcceptEx, sizeof guidAcceptEx, &acceptEx, sizeof acceptEx, &read, NULL, NULL) != 0) { message = "WSAIoctl failed, " + errorMessage(WSAGetLastError()); } else { assert(read == sizeof acceptEx); if (CreateIoCompletionPort(reinterpret_cast<HANDLE>(listener), dispatcher.getCompletionPort(), 0, 0) != dispatcher.getCompletionPort()) { message = "CreateIoCompletionPort failed, " + lastErrorMessage(); } else { context = nullptr; return; } } } int result = closesocket(listener); assert(result == 0); } throw std::runtime_error("TcpListener::TcpListener, " + message); }
TcpConnection TcpConnector::connect(const Ipv4Address& address, uint16_t port) { assert(dispatcher != nullptr); assert(context == nullptr); if (dispatcher->interrupted()) { throw InterruptedException(); } std::string message; int connection = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (connection == -1) { message = "socket failed, " + lastErrorMessage(); } else { sockaddr_in bindAddress; bindAddress.sin_family = AF_INET; bindAddress.sin_port = 0; bindAddress.sin_addr.s_addr = INADDR_ANY; if (bind(connection, reinterpret_cast<sockaddr*>(&bindAddress), sizeof bindAddress) != 0) { message = "bind failed, " + lastErrorMessage(); } else { int flags = fcntl(connection, F_GETFL, 0); if (flags == -1 || fcntl(connection, F_SETFL, flags | O_NONBLOCK) == -1) { message = "fcntl failed, " + lastErrorMessage(); } else { sockaddr_in addressData; addressData.sin_family = AF_INET; addressData.sin_port = htons(port); addressData.sin_addr.s_addr = htonl(address.getValue()); int result = ::connect(connection, reinterpret_cast<sockaddr *>(&addressData), sizeof addressData); if (result == -1) { if (errno == EINPROGRESS) { ConnectorContext connectorContext; connectorContext.context = dispatcher->getCurrentContext(); connectorContext.interrupted = false; connectorContext.connection = connection; struct kevent event; EV_SET(&event, connection, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, &connectorContext); if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) { message = "kevent failed, " + lastErrorMessage(); } else { context = &connectorContext; dispatcher->getCurrentContext()->interruptProcedure = [&] { assert(dispatcher != nullptr); assert(context != nullptr); ConnectorContext* connectorContext = static_cast<ConnectorContext*>(context); if (!connectorContext->interrupted) { if (close(connectorContext->connection) == -1) { throw std::runtime_error("TcpListener::stop, close failed, " + lastErrorMessage()); } dispatcher->pushContext(connectorContext->context); connectorContext->interrupted = true; } }; dispatcher->dispatch(); dispatcher->getCurrentContext()->interruptProcedure = nullptr; assert(dispatcher != nullptr); assert(connectorContext.context == dispatcher->getCurrentContext()); assert(context == &connectorContext); context = nullptr; connectorContext.context = nullptr; if (connectorContext.interrupted) { throw InterruptedException(); } struct kevent event; EV_SET(&event, connection, EVFILT_WRITE, EV_ADD | EV_DISABLE, 0, 0, NULL); if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) { message = "kevent failed, " + lastErrorMessage(); } else { int retval = -1; socklen_t retValLen = sizeof(retval); int s = getsockopt(connection, SOL_SOCKET, SO_ERROR, &retval, &retValLen); if (s == -1) { message = "getsockopt failed, " + lastErrorMessage(); } else { if (retval != 0) { message = "getsockopt failed, " + lastErrorMessage(); } else { return TcpConnection(*dispatcher, connection); } } } } } } else { return TcpConnection(*dispatcher, connection); } } } int result = close(connection); if (result) {} assert(result != -1);; } throw std::runtime_error("TcpConnector::connect, " + message); }
TcpConnection TcpConnector::connect(const Ipv4Address& address, uint16_t port) { assert(dispatcher != nullptr); assert(context == nullptr); if (dispatcher->interrupted()) { throw InterruptedException(); } std::string message; SOCKET connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (connection == INVALID_SOCKET) { message = "socket failed, result=" + std::to_string(WSAGetLastError()); } else { sockaddr_in bindAddress; bindAddress.sin_family = AF_INET; bindAddress.sin_port = 0; bindAddress.sin_addr.s_addr = INADDR_ANY; if (bind(connection, reinterpret_cast<sockaddr*>(&bindAddress), sizeof bindAddress) != 0) { message = "bind failed, result=" + std::to_string(WSAGetLastError()); } else { GUID guidConnectEx = WSAID_CONNECTEX; DWORD read = sizeof connectEx; if (connectEx == nullptr && WSAIoctl(connection, SIO_GET_EXTENSION_FUNCTION_POINTER, &guidConnectEx, sizeof guidConnectEx, &connectEx, sizeof connectEx, &read, NULL, NULL) != 0) { message = "WSAIoctl failed, result=" + std::to_string(WSAGetLastError()); } else { assert(read == sizeof connectEx); if (CreateIoCompletionPort(reinterpret_cast<HANDLE>(connection), dispatcher->getCompletionPort(), 0, 0) != dispatcher->getCompletionPort()) { message = "CreateIoCompletionPort failed, result=" + std::to_string(GetLastError()); } else { sockaddr_in addressData; addressData.sin_family = AF_INET; addressData.sin_port = htons(port); addressData.sin_addr.S_un.S_addr = htonl(address.getValue()); TcpConnectorContext context2; context2.hEvent = NULL; if (connectEx(connection, reinterpret_cast<sockaddr*>(&addressData), sizeof addressData, NULL, 0, NULL, &context2) == TRUE) { message = "ConnectEx returned immediately, which is not supported."; } else { int lastError = WSAGetLastError(); if (lastError != WSA_IO_PENDING) { message = "ConnectEx failed, result=" + std::to_string(lastError); } else { context2.context = dispatcher->getCurrentContext(); context2.connection = connection; context2.interrupted = false; context = &context2; dispatcher->getCurrentContext()->interruptProcedure = [&]() { assert(dispatcher != nullptr); assert(context != nullptr); TcpConnectorContext* context2 = static_cast<TcpConnectorContext*>(context); if (!context2->interrupted) { if (CancelIoEx(reinterpret_cast<HANDLE>(context2->connection), context2) != TRUE) { DWORD lastError = GetLastError(); if (lastError != ERROR_NOT_FOUND) { throw std::runtime_error("TcpConnector::stop, CancelIoEx failed, result=" + std::to_string(GetLastError())); } context2->context->interrupted = true; } context2->interrupted = true; } }; dispatcher->dispatch(); dispatcher->getCurrentContext()->interruptProcedure = nullptr; assert(context2.context == dispatcher->getCurrentContext()); assert(context2.connection == connection); assert(dispatcher != nullptr); assert(context == &context2); context = nullptr; DWORD transferred; DWORD flags; if (WSAGetOverlappedResult(connection, &context2, &transferred, FALSE, &flags) != TRUE) { lastError = WSAGetLastError(); if (lastError != ERROR_OPERATION_ABORTED) { message = "ConnectEx failed, result=" + std::to_string(lastError); } else { assert(context2.interrupted); if (closesocket(connection) != 0) { throw std::runtime_error("TcpConnector::connect, closesocket failed, result=" + std::to_string(WSAGetLastError())); } else { throw InterruptedException(); } } } else { assert(transferred == 0); assert(flags == 0); DWORD value = 1; if (setsockopt(connection, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, reinterpret_cast<char*>(&value), sizeof(value)) != 0) { message = "setsockopt failed, result=" + std::to_string(WSAGetLastError()); } else { return TcpConnection(*dispatcher, connection); } } } } } } } int result = closesocket(connection); assert(result == 0); } throw std::runtime_error("TcpConnector::connect, " + message); }
/* * If we have an exact match, we return it. * Otherwise, if we find a generic match, we return it. * Otherwise, we return 0. */ Ipv4EndPointDemux::EndPoints Ipv4EndPointDemux::Lookup (Ipv4Address daddr, uint16_t dport, Ipv4Address saddr, uint16_t sport, Ptr<Ipv4Interface> incomingInterface) { NS_LOG_FUNCTION (this << daddr << dport << saddr << sport << incomingInterface); EndPoints retval1; // Matches exact on local port, wildcards on others EndPoints retval2; // Matches exact on local port/adder, wildcards on others EndPoints retval3; // Matches all but local address EndPoints retval4; // Exact match on all 4 NS_LOG_DEBUG ("Looking up endpoint for destination address " << daddr); for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++) { Ipv4EndPoint* endP = *i; NS_LOG_DEBUG ("Looking at endpoint dport=" << endP->GetLocalPort () << " daddr=" << endP->GetLocalAddress () << " sport=" << endP->GetPeerPort () << " saddr=" << endP->GetPeerAddress ()); if (endP->GetLocalPort () != dport) { NS_LOG_LOGIC ("Skipping endpoint " << &endP << " because endpoint dport " << endP->GetLocalPort () << " does not match packet dport " << dport); continue; } if (endP->GetBoundNetDevice ()) { if (endP->GetBoundNetDevice () != incomingInterface->GetDevice ()) { NS_LOG_LOGIC ("Skipping endpoint " << &endP << " because endpoint is bound to specific device and" << endP->GetBoundNetDevice () << " does not match packet device " << incomingInterface->GetDevice ()); continue; } } bool subnetDirected = false; Ipv4Address incomingInterfaceAddr = daddr; // may be a broadcast for (uint32_t i = 0; i < incomingInterface->GetNAddresses (); i++) { Ipv4InterfaceAddress addr = incomingInterface->GetAddress (i); if (addr.GetLocal ().CombineMask (addr.GetMask ()) == daddr.CombineMask (addr.GetMask ()) && daddr.IsSubnetDirectedBroadcast (addr.GetMask ())) { subnetDirected = true; incomingInterfaceAddr = addr.GetLocal (); } } bool isBroadcast = (daddr.IsBroadcast () || subnetDirected == true); NS_LOG_DEBUG ("dest addr " << daddr << " broadcast? " << isBroadcast); bool localAddressMatchesWildCard = endP->GetLocalAddress () == Ipv4Address::GetAny (); bool localAddressMatchesExact = endP->GetLocalAddress () == daddr; if (isBroadcast) { NS_LOG_DEBUG ("Found bcast, localaddr " << endP->GetLocalAddress ()); } if (isBroadcast && (endP->GetLocalAddress () != Ipv4Address::GetAny ())) { localAddressMatchesExact = (endP->GetLocalAddress () == incomingInterfaceAddr); } // if no match here, keep looking if (!(localAddressMatchesExact || localAddressMatchesWildCard)) continue; bool remotePeerMatchesExact = endP->GetPeerPort () == sport; bool remotePeerMatchesWildCard = endP->GetPeerPort () == 0; bool remoteAddressMatchesExact = endP->GetPeerAddress () == saddr; bool remoteAddressMatchesWildCard = endP->GetPeerAddress () == Ipv4Address::GetAny (); // If remote does not match either with exact or wildcard, // skip this one if (!(remotePeerMatchesExact || remotePeerMatchesWildCard)) continue; if (!(remoteAddressMatchesExact || remoteAddressMatchesWildCard)) continue; // Now figure out which return list to add this one to if (localAddressMatchesWildCard && remotePeerMatchesWildCard && remoteAddressMatchesWildCard) { // Only local port matches exactly retval1.push_back (endP); } if ((localAddressMatchesExact || (isBroadcast && localAddressMatchesWildCard))&& remotePeerMatchesWildCard && remoteAddressMatchesWildCard) { // Only local port and local address matches exactly retval2.push_back (endP); } if (localAddressMatchesWildCard && remotePeerMatchesExact && remoteAddressMatchesExact) { // All but local address retval3.push_back (endP); } if (localAddressMatchesExact && remotePeerMatchesExact && remoteAddressMatchesExact) { // All 4 match retval4.push_back (endP); } } // Here we find the most exact match if (!retval4.empty ()) return retval4; if (!retval3.empty ()) return retval3; if (!retval2.empty ()) return retval2; return retval1; // might be empty if no matches }