apache::thrift::async::TAsyncTransport::UniquePtr ThriftTransportBase::getConnectingSocket() { return folly::fibers::runInMainContext([this] { // TODO(@aap): Replace with createSocket() once Thrift works with // AsyncTransportWrapper. apache::thrift::async::TAsyncTransport::UniquePtr socket( new apache::thrift::async::TAsyncSocket(&eventBase_)); socket->setSendTimeout(connectionOptions_.writeTimeout.count()); auto sockAddressExpected = getSocketAddress(connectionOptions_); if (sockAddressExpected.hasError()) { const auto& ex = sockAddressExpected.error(); LOG_FAILURE( "ThriftTransport", failure::Category::kBadEnvironment, "{}", ex.what()); return apache::thrift::async::TAsyncTransport::UniquePtr{}; } folly::SocketAddress address = std::move(sockAddressExpected).value(); auto socketOptions = createSocketOptions(address, connectionOptions_); const auto mech = connectionOptions_.accessPoint->getSecurityMech(); connectionState_ = ConnectionState::Connecting; if (mech == SecurityMech::TLS13_FIZZ) { auto fizzClient = socket->getUnderlyingTransport<McFizzClient>(); fizzClient->connect( this, address, connectionOptions_.connectTimeout.count(), socketOptions); } else { auto asyncSock = socket->getUnderlyingTransport<folly::AsyncSocket>(); asyncSock->connect( this, address, connectionOptions_.connectTimeout.count(), socketOptions); } return socket; }); }
void getInetSocketAddress(char *hostName, int socketType, int port, struct sockaddr_in *inetSocketAddress) /* returns a socket address for an Internet node */ { getSocketAddress(hostName,SOCK_DGRAM, socketType, (struct sockaddr*) inetSocketAddress); inetSocketAddress->sin_port = port; /* * ASSIGNMENT * * Implement the following pseudocode: * * get a socket address (hint: getSocketAddress()) (You'll need to * cast inetSocketAddress to a "struct sockaddr" pointer.) * set `inetSocketAddress`'s port attribute (you'll have to find * the exact name) to `port` */ }
/* request, and deinitialize */ tRedirectStatus tspDoEchoRequest(char *address, tBrokerAddressType address_type, tConf *conf, uint32_t *distance) { rttengine_stat_t *engine = NULL; struct addrinfo *address_info = NULL; struct addrinfo *address_info_root = NULL; pal_socket_t sfd; tSocketAddressStatus socket_address_status = SOCKET_ADDRESS_OK; tRedirectStatus status = TSP_REDIRECT_OK; /* Initilalize some stuff */ *distance = 0; /* Get an address structure and see if we have the right address family */ socket_address_status = getSocketAddress(address, address_type, conf->tunnel_mode, &address_info_root, &address_info); /* Wrong family, log and modify the distance so it goes at the end of the sorted list */ if (socket_address_status == SOCKET_ADDRESS_WRONG_FAMILY) { *distance = ECHO_REQUEST_WRONG_FAMILY_ADJUST; Display(LOG_LEVEL_1, ELError, "tspDoEchoRequest", GOGO_STR_RDR_WRONG_ADDRESS_FAMILY, address); } /* There was some kind of error, adjust the distance to make it go after the brokers that */ /* could be timed properly. */ else if (socket_address_status == SOCKET_ADDRESS_ERROR) { *distance = ECHO_REQUEST_ERROR_ADJUST; if (address_info_root != NULL) { freeaddrinfo(address_info_root); } Display(LOG_LEVEL_1, ELError, "tspDoEchoRequest", GOGO_STR_RDR_ERROR_GET_SOCKADDRESS, address); return TSP_REDIRECT_ECHO_REQUEST_ERROR; } /* Could not resolve, this is treated the same as an error */ else if (socket_address_status == SOCKET_ADDRESS_PROBLEM_RESOLVING) { *distance = ECHO_REQUEST_ERROR_ADJUST; if (address_info_root != NULL) { freeaddrinfo(address_info_root); } Display(LOG_LEVEL_1, ELError, "tspDoEchoRequest", GOGO_STR_RDR_ERROR_RESOLVING_DN, address); return TSP_REDIRECT_ECHO_REQUEST_ERROR; } /* Connect to the address */ if ((sfd = createConnectedSocket(address_info)) == -1) { *distance += ECHO_REQUEST_ERROR_ADJUST; if (address_info_root != NULL) { freeaddrinfo(address_info_root); } destroySocket(sfd); Display(LOG_LEVEL_1, ELError, "tspDoEchoRequest", GOGO_STR_RDR_ERROR_CONNECT_SOCKET, address); return TSP_REDIRECT_ECHO_REQUEST_ERROR; } if (address_info_root != NULL) { freeaddrinfo(address_info_root); } /* Create a stat engine */ if ((engine = createStatEngine()) == NULL) { *distance += ECHO_REQUEST_ERROR_ADJUST; destroySocket(sfd); Display(LOG_LEVEL_1, ELError, "tspDoEchoRequest", GOGO_STR_RDR_ERROR_CREATE_STAT_ENGINE, address); return TSP_REDIRECT_ECHO_REQUEST_ERROR; } /* Initialize the stat engine */ if (rttengine_init(engine) != 1) { *distance += ECHO_REQUEST_ERROR_ADJUST; destroySocket(sfd); pal_free(engine); Display(LOG_LEVEL_1, ELError, "tspDoEchoRequest", GOGO_STR_RDR_ERROR_INIT_STAT_ENGINE, address); return TSP_REDIRECT_ECHO_REQUEST_ERROR; } /* Calculate the roundtrip time */ status = timeEchoRequestReply(sfd, address, engine, distance); /* Destroy the socket */ destroySocket(sfd); /* Uninitialize the stat engine */ rttengine_deinit(engine, NULL, NULL); /* Free the stat engine */ pal_free(engine); return status; }