Exemple #1
0
int main(void) {
	struct UDPsopa sopa = {
		.sop = NULL,
		.pap = NULL,
		.port        = 0, // 0 = pick unused port
		.packet_size = 512,
		.packet_num  = 0,
		.host_port   = 4444
	};
	strcpy(sopa.host_name, "127.0.0.1");
	if (udp_setup(&sopa) != 0) {
		udp_cleanup(&sopa);
		exit(EXIT_FAILURE);
	}

	// additional client setup
	if (SDLNet_ResolveHost(&(sopa.pap->address), sopa.host_name, sopa.host_port) != 0) {
		printf("SDLNet_ResolveHost(&(sopa.pap->address), sopa.host_name, sopa.host_port) != 0\n");
		printf("SDLNet_GetError() = '%s'\n", SDLNet_GetError());
		udp_cleanup(&sopa);
		exit(EXIT_FAILURE);
	}

	srand(123);
	for (;;) {
		sleep(1); // 1 second
		sprintf((char*)sopa.pap->data, "rand_num = %d", rand());
		sopa.pap->len = strlen((char*)sopa.pap->data) + 1;

		const int channel = -1; // -1 = use packet's address as destination
		if (SDLNet_UDP_Send(sopa.sop, channel, sopa.pap) == 0) {
			printf("SDLNet_UDP_Send(sopa.sop, channel, sopa.pap) == 0\n");
			printf("SDLNet_GetError() = '%s'\n", SDLNet_GetError());
			udp_cleanup(&sopa);
			exit(EXIT_FAILURE);
		}

		switch (SDLNet_UDP_Recv(sopa.sop, sopa.pap)) {
		case -1:
			printf("SDLNet_UDP_Recv(sopa.sop, sopa.pap) == -1\n");
			printf("SDLNet_GetError() = '%s'\n", SDLNet_GetError());
			udp_cleanup(&sopa);
			exit(EXIT_FAILURE);
		case 0:
			printf("SDLNet_UDP_Recv(sopa.sop, sopa.pap) == 0 // no packets received\n");
			break;
		case 1:
			sopa.packet_num++;
			udp_print(&sopa);
			break;
		}
	} // for(;;)

	udp_cleanup(&sopa);
	exit(EXIT_SUCCESS);
}
Exemple #2
0
int clientBogusNameCheck(void)
{
    int passed = 1;
    char tempHostName[] = "badName";
    char oldHostName[1024] = "";

    reset_test_state();

    clientDataSent = 0;
    clientDataRcvd = 0;
    serverDataSent = 0;
    serverDataRcvd = 0;

    printf("Checking closing socket in client mode.\n");
    if (passed)
    {
        udp_cleanup(&clientUdpCommStruct);
        passed &= !udp_isOpen(&clientUdpCommStruct);
    }

    if (passed)
    {
        //Backup old host name
        strcpy(oldHostName, clientUdpCommStruct.hostname);

        //Swapping char * to place bogus name
        strcpy(clientUdpCommStruct.hostname, tempHostName);

        printf("Confirming bogus name does not work.\n");
        if (passed)
        {
            udp_init(&clientUdpCommStruct);
            passed &= !udp_isOpen(&clientUdpCommStruct);
        }

        if (passed)
            passed &= !biDirectionalCommCheck();

        //Restore old host name
        strcpy(clientUdpCommStruct.hostname, oldHostName);

        //Reconnect with old host name
        if (passed)
        {
            udp_init(&clientUdpCommStruct);
            passed &= udp_isOpen(&clientUdpCommStruct);
        }
    }

    return passed;
}
Exemple #3
0
int serverBogusPortCheck(void)
{
    int passed = 1;
    char tempPortName[] = "-53";
    char oldPortName[1024] = "";

    reset_test_state();

    clientDataSent = 0;
    clientDataRcvd = 0;
    serverDataSent = 0;
    serverDataRcvd = 0;

    printf("Checking closing socket in server mode.\n");
    if (passed)
    {
        udp_cleanup(&serverUdpCommStruct);
        passed &= !udp_isOpen(&serverUdpCommStruct);
    }

    if (passed)
    {
        //Backup old host name
        strcpy(oldPortName, serverUdpCommStruct.port);

        //Swapping char * to place bogus name
        strcpy(serverUdpCommStruct.port, tempPortName);

        printf("Confirming bogus port does not work.\n");
        if (passed)
        {
            udp_init(&serverUdpCommStruct);
            passed &= !udp_isOpen(&serverUdpCommStruct);
        }

        if (passed)
            passed &= !biDirectionalCommCheck();

        //Restore old host name
        strcpy(serverUdpCommStruct.port, oldPortName);

        //Reconnect with old host name
        if (passed)
        {
            udp_init(&serverUdpCommStruct);
            passed &= udp_isOpen(&serverUdpCommStruct);
        }
    }

    return passed;
}
Exemple #4
0
void ip_cleanup(Slirp *slirp)
{
    udp_cleanup(slirp);
    tcp_cleanup(slirp);
    icmp_cleanup(slirp);
}
Exemple #5
0
int reOpenTest(void *args)
{
    int passed = 1;

    reset_test_state();

    printf("Confirming server and client sockets are open.\n");
    if (passed)
    {
        //Checking that both sockets are initially working and reporting correctly
        passed &= udp_isOpen(&clientUdpCommStruct);
        passed &= udp_isOpen(&serverUdpCommStruct);
    }

    //Confirming data comm works to start with
    if (passed)
        passed &= biDirectionalCommCheck();

    printf("Checking closing and reopening in client mode.\n");
    if (passed)
    {
        udp_cleanup(&clientUdpCommStruct);
        passed &= !udp_isOpen(&clientUdpCommStruct);
    }

    printf("Checking comm doesn't work on a closed client.\n");
    if (passed)
        passed &= !biDirectionalCommCheck();

    if (passed)
    {
        udp_init(&clientUdpCommStruct);
        passed &= udp_isOpen(&clientUdpCommStruct);
    }

    if (passed)
        passed &= biDirectionalCommCheck();

    printf("Checking closing and reopening in server mode.\n");
    if (passed)
    {
        udp_cleanup(&clientUdpCommStruct);
        passed &= !udp_isOpen(&clientUdpCommStruct);
    }

    printf("Checking comm doesn't work on a closed server.\n");
    if (passed)
        passed &= !biDirectionalCommCheck();

    if (passed)
    {
        udp_init(&clientUdpCommStruct);
        passed &= udp_isOpen(&clientUdpCommStruct);
    }

    if (passed)
        passed &= biDirectionalCommCheck();

    printf("Closing and Reopening Socket Test: %s\n", passed ? "PASSED" : "FAILED");

    return passed;
}