Пример #1
0
int main(int argc, char *argv[])
{
	int opt;
    bool dump_flag = false;
	
    buffer = (char *)malloc(BUFFER_SIZE);
    
	while ((opt = getopt(argc, argv, "d")) != -1) {
		switch (opt) {
			case 'd':
				dump_flag = true;
				break;
			default:
				print_usage(argv[0]);
				exit(EXIT_FAILURE);
		}
	}
	
	if (argc - optind < 2 || argc - optind > 4) {
		fprintf(stderr, "wrong argument count\n");
		print_usage(argv[0]);
		exit(EXIT_FAILURE);
	}
	
    if (strcmp(argv[optind], "listen") == 0) {
        optind++;
        _server_socket = open_server_socket(atoi(argv[optind]));
        server_with_select(NULL, dump_flag);
    } else if (strcmp(argv[optind], "send") == 0) {
        UDPClient *client;
        struct sockaddr_in from_address, to_address;
        size_t len;
        ssize_t read;
        
        optind++;
        
        to_address = socket_address(argv[optind], atoi(argv[optind + 1]));
        from_address = socket_address(NULL, atoi(argv[optind + 1]));
        
        read = getline(&buffer, &len, stdin);
        if (read > 0) {
            client = new UDPClient(from_address, to_address, -1, dump_flag);
            client->send_to_server(buffer, read);
        }
    } else if (strcmp(argv[optind], "relay") == 0) {
        struct sockaddr_in to_address;
        
        optind++;
        _server_socket = open_server_socket(atoi(argv[optind]));
        to_address = socket_address(argv[optind + 1], atoi(argv[optind + 2]));
        server_with_select(&to_address, dump_flag);
    }
    
	return 0;
}
Пример #2
0
YETI_Result HttpTcpConnector::connect(const HttpUrl & url,
                                      HttpClient & client,
                                      const HttpProxyAddress * proxy,
                                      bool reuse, HttpClient::Connection *& connection)
{
    connection = NULL;
    const char * server_hostname;
    YETI_UInt16 server_port;
    if (proxy) {
        server_hostname = (const char *)proxy->get_hostname();
        server_port = proxy->get_port();
    } else {
        server_hostname = (const char *)url.get_host();
        server_port = url.get_port();
    }

    IpAddress address;
    YETI_CHECK_FINE(address.resolve_name(server_hostname, client.get_config().m_name_resolver_timeout_));
    YETI_LOG_FINE_2("TCP connector will connect to %s : %d", server_hostname, server_port);
    TcpClientSocket * tcp_socket = new TcpClientSocket();
    SocketReference socket(tcp_socket, true);
    tcp_socket->set_read_timeout(client.get_config().m_io_timeout_);
    tcp_socket->set_write_timeout(client.get_config().m_io_timeout_);
    SocketAddress socket_address(address, server_port);
    YETI_CHECK_FINE(tcp_socket->connect(socket_address, client.get_config().m_connection_timeout_));

    HttpSimpleConnection * _connection = new HttpSimpleConnection();
    _connection->m_socket_ = socket;
    connection = _connection;
    tcp_socket->get_input_stream(_connection->m_inputstream_);
    tcp_socket->get_output_stream(_connection->m_outputstream_);

    return YETI_SUCCESS;
}
Пример #3
0
socket_address udt_socket::endpoint() const
{
	struct sockaddr_in endpoint;
	int size = sizeof(endpoint);
	UDT::getpeername(_sock, (sockaddr*)&endpoint, &size);
	return socket_address(inet_ntoa(endpoint.sin_addr), ntohs(endpoint.sin_port));
}
Пример #4
0
//
// CS_OpenConnection
//
// Opens a new connection to the remote host given by socket address string.
// This should only be used by the client application.
// Note that this requires CS_OpenNetInterface to be called prior.
//
void CS_OpenConnection(const std::string& socket_address_string)
{
	if (interface)
	{
		SocketAddress socket_address(socket_address_string);
		// TODO: test for valid addresses
		client_connection_id = interface->requestConnection(socket_address);
	}
}
Пример #5
0
int stat_me()
{
    switch ( style )
    {
    case SKT_STYLE_LISTEN:
	printf("%O: listening at %O\n", this_object(), addr);
	printf("    read_func=%O  close_func=%O\n", read_func, close_func);
	break;

    case SKT_STYLE_CONNECT:
	printf("%O: connected to %O\n", this_object(), socket_address(fdOwned));
	printf("    read_func=%O  close_func=%O\n", read_func, close_func);
	break;

    case SKT_STYLE_UDP:
	printf("%O: UDP at %O\n", this_object(), socket_address(fdOwned));
	printf("    read_func=%O\n", read_func);
	break;

    case SKT_STYLE_LISTEN_M:
	printf("%O: (mud) listening at %O\n", this_object(), addr);
	printf("    read_func=%O  close_func=%O\n", read_func, close_func);
	break;

    case SKT_STYLE_CONNECT_M:
	printf("%O: (mud) connected to %O\n", this_object(), socket_address(fdOwned));
	printf("    read_func=%O  close_func=%O\n", read_func, close_func);
	break;

    case SKT_STYLE_INT_ACQUIRE:
	printf("%O: accepted connection from %s\n", this_object(),
	       socket_address(fdOwned));
	printf("    read_func=%O  close_func=%O\n", read_func, close_func);
	break;
    }

    if ( sizeof(write_queue) )
	printf("queue: %O\n", write_queue);

    return 1;
}
Пример #6
0
int open_server_socket(int port)
{
    sockaddr_in server_address;
    struct timeval tv;
    int socket_result;
    
    server_address = socket_address(NULL, port);
    socket_result = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    tv.tv_sec = TIME_OUT_ACTIVITY;
    tv.tv_usec = 0;
    setsockopt(socket_result, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,  sizeof(tv));
    if (bind(socket_result, (struct sockaddr *)&server_address, sizeof(server_address)) == -1) {
        fprintf(stderr, "can't listen to %s:%d\n", inet_ntoa(server_address.sin_addr), ntohs(server_address.sin_port));
        exit(EXIT_FAILURE);
    }
    printf("listening from %s:%d\n", inet_ntoa(server_address.sin_addr), ntohs(server_address.sin_port));
    return socket_result;
}
Пример #7
0
int my_send()
{
    printf("==== Send\n");

    char * msg = "hello";
    UdpSocket sender;
    DataBuffer buffer(1024);
    buffer.set_data_size(1024);
    buffer.set_data((YETI_Byte *)msg, strlen(msg));
    IpAddress address;
    address.resolve_name("localhost");
    SocketAddress socket_address(address, 9123);
    YETI_Result result = sender.send(buffer, &socket_address);
    if (YETI_FAILED(result)) {
        fprintf(stderr, "send() failed (%d)\n", result);
        return result;
    }
    return 0;
}
Пример #8
0
socket_address udp_socket::endpoint() const
{
	return socket_address(inet_ntoa(_endpoint.sin_addr), ntohs(_endpoint.sin_port));
}
Пример #9
0
#include "mock/MockKnownPeers.h"
#include "mock/MockPeerCommandCenter.h"
#include "mock/MockLogger.h"
#include "socket/MockSocketWriter.h"

TEST_CASE( "PeerPacketHandlerTest/testOnPacket", "[unit]" )
{
	MockKnownPeers membership;
	membership.update("fooid", {"1.2.3.4:5"});
	membership._history.clear();
	MockPeerCommandCenter center;
	MockLogger logger;
	PeerPacketHandler handler(membership, center, logger);

	MockSocketWriter writer;
	writer._endpoint = socket_address("1.2.3.4", 5);
	handler.onPacket(writer, "foobar", 6);

	assertEquals( "lookupAddr(1.2.3.4:5)", membership._history.calls() );
	assertEquals( "run(fooid,foobar)", center._history.calls() );
	assertEquals( "", logger._history.calls() );
}

TEST_CASE( "PeerPacketHandlerTest/testOnPacket.BadPeer", "[unit]" )
{
	MockKnownPeers membership;
	MockPeerCommandCenter center;
	MockLogger logger;
	PeerPacketHandler handler(membership, center, logger);

	MockSocketWriter writer;