Exemple #1
0
t_socket_server *sockets_createServer(char *ip, int port){
	t_socket_server *sckt = malloc( sizeof(t_socket_server) );
	if( (sckt->socket = sockets_create(ip, port)) == NULL ){
		free(sckt);
		return NULL;
	}
	sckt->maxconexions = DEFAULT_MAX_CONEXIONS;
	return sckt;
}
Exemple #2
0
t_socket_client *sockets_createClient(char *ip, int port){
	t_socket_client* client = malloc( sizeof(t_socket_client) );
	if( (client->socket = sockets_create(ip, port)) == NULL ){
		free(client);
		return NULL;
	}
	if (client->socket == NULL){
		return NULL;
	}
	sockets_setState(client, SOCKETSTATE_DISCONNECTED);
	return client;
}
Exemple #3
0
bool bbruntime_create(){
	if( blitz_create() ){
				if( hook_create() ){
						if( event_create() ){
								if( math_create() ){
										if( string_create() ){
												if( stdio_create() ){
														if( stream_create() ){
																if( sockets_create() ){
																		if( enet_create() ){
																				if( runtime_create() ){
																						if( system_create() ){
																								if( bank_create() ){
																										if( system_windows_create() ){
																												if( filesystem_create() ){
																														if( filesystem_windows_create() ){
																																if( timer_windows_create() ){
																																		if( input_create() ){
																																				if( input_directinput8_create() ){
																																						if( audio_create() ){
																																								if( audio_fmod_create() ){
																																										if( userlibs_create() ){
																																												if( pixmap_create() ){
																																														if( blitz2d_create() ){
																																																if( graphics_create() ){
																																																		if( runtime_glfw3_create() ){
																																																				return true;
																									}else sue( "runtime_glfw3_create failed" );
																									graphics_destroy();
																								}else sue( "graphics_create failed" );
																								blitz2d_destroy();
																							}else sue( "blitz2d_create failed" );
																							pixmap_destroy();
																						}else sue( "pixmap_create failed" );
																						userlibs_destroy();
																					}else sue( "userlibs_create failed" );
																					audio_fmod_destroy();
																				}else sue( "audio_fmod_create failed" );
																				audio_destroy();
																			}else sue( "audio_create failed" );
																			input_directinput8_destroy();
																		}else sue( "input_directinput8_create failed" );
																		input_destroy();
																	}else sue( "input_create failed" );
																	timer_windows_destroy();
																}else sue( "timer_windows_create failed" );
																filesystem_windows_destroy();
															}else sue( "filesystem_windows_create failed" );
															filesystem_destroy();
														}else sue( "filesystem_create failed" );
														system_windows_destroy();
													}else sue( "system_windows_create failed" );
													bank_destroy();
												}else sue( "bank_create failed" );
												system_destroy();
											}else sue( "system_create failed" );
											runtime_destroy();
										}else sue( "runtime_create failed" );
										enet_destroy();
									}else sue( "enet_create failed" );
									sockets_destroy();
								}else sue( "sockets_create failed" );
								stream_destroy();
							}else sue( "stream_create failed" );
							stdio_destroy();
						}else sue( "stdio_create failed" );
						string_destroy();
					}else sue( "string_create failed" );
					math_destroy();
				}else sue( "math_create failed" );
				event_destroy();
			}else sue( "event_create failed" );
			hook_destroy();
		}else sue( "hook_create failed" );
		blitz_destroy();
	}else sue( "blitz_create failed" );
	return false;
}
int main(int argc, char *argv[])
{
	struct sockaddr *address;
	struct sockaddr_in address_in;
	struct sockaddr_in6 address_in6;
	socklen_t addrlen;
	uint8_t *address_start;

	int *socket_ids;
	char *data;
	int index;
	struct timeval time_before;
	struct timeval time_after;

	int rc;

	size = 28;
	verbose = false;
	type = SOCK_DGRAM;
	sockets = 10;
	messages = 10;
	family = PF_INET;
	port = 7;

	/*
	 * Parse the command line arguments.
	 *
	 * Stop before the last argument if it does not start with dash ('-')
	 */
	for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
	    (argv[index][0] == '-')); ++index) {

		/* Options should start with dash ('-') */
		if (argv[index][0] == '-') {
			rc = nettest2_parse_opt(argc, argv, &index);
			if (rc != EOK)
				return rc;
		} else {
			nettest2_print_help();
			return EINVAL;
		}
	}

	/* If not before the last argument containing the address */
	if (index >= argc) {
		printf("Command line error: missing address\n");
		nettest2_print_help();
		return EINVAL;
	}

	/* Prepare the address buffer */

	switch (family) {
	case PF_INET:
		address_in.sin_family = AF_INET;
		address_in.sin_port = htons(port);
		address = (struct sockaddr *) &address_in;
		addrlen = sizeof(address_in);
		address_start = (uint8_t *) &address_in.sin_addr.s_addr;
		break;
	case PF_INET6:
		address_in6.sin6_family = AF_INET6;
		address_in6.sin6_port = htons(port);
		address = (struct sockaddr *) &address_in6;
		addrlen = sizeof(address_in6);
		address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
		break;
	default:
		fprintf(stderr, "Address family is not supported\n");
		return EAFNOSUPPORT;
	}

	/* Parse the last argument which should contain the address. */
	rc = inet_pton(family, argv[argc - 1], address_start);
	if (rc != EOK) {
		fprintf(stderr, "Address parse error %d\n", rc);
		return rc;
	}

	/* Check data buffer size. */
	if (size <= 0) {
		fprintf(stderr, "Data buffer size too small (%zu). Using 1024 "
		    "bytes instead.\n", size);
		size = 1024;
	}

	/*
	 * Prepare the buffer. Allocate size bytes plus one for terminating
	 * null character.
	 */
	data = (char *) malloc(size + 1);
	if (!data) {
		fprintf(stderr, "Failed to allocate data buffer.\n");
		return ENOMEM;
	}

	/* Fill buffer with a pattern. */
	nettest2_fill_buffer(data, size);

	/* Check socket count. */
	if (sockets <= 0) {
		fprintf(stderr, "Socket count too small (%d). Using "
		    "2 instead.\n", sockets);
		sockets = 2;
	}

	/*
	 * Prepare the socket buffer.
	 * Allocate count entries plus the terminating null (\0)
	 */
	socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
	if (!socket_ids) {
		fprintf(stderr, "Failed to allocate receive buffer.\n");
		return ENOMEM;
	}
	socket_ids[sockets] = 0;

	if (verbose)
		printf("Starting tests\n");

	rc = sockets_create(verbose, socket_ids, sockets, family, type);
	if (rc != EOK)
		return rc;

	if (type == SOCK_STREAM) {
		rc = sockets_connect(verbose, socket_ids, sockets,
		    address, addrlen);
		if (rc != EOK)
			return rc;
	}

	if (verbose)
		printf("\n");

	rc = gettimeofday(&time_before, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
	    &addrlen, data, size, messages);
	if (rc != EOK)
		return rc;

	rc = gettimeofday(&time_after, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	if (verbose)
		printf("\tOK\n");

	printf("sendto + recvfrom tested in %ld microseconds\n",
	    tv_sub(&time_after, &time_before));

	rc = gettimeofday(&time_before, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
	    data, size, messages);
	if (rc != EOK)
		return rc;

	rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
	    data, size, messages);
	if (rc != EOK)
		return rc;

	rc = gettimeofday(&time_after, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	if (verbose)
		printf("\tOK\n");

	printf("sendto, recvfrom tested in %ld microseconds\n",
	    tv_sub(&time_after, &time_before));

	rc = sockets_close(verbose, socket_ids, sockets);
	if (rc != EOK)
		return rc;

	if (verbose)
		printf("\nExiting\n");

	return EOK;
}
static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
{
	int rc;

	if (verbose)
		printf("%d sockets, %d messages\n", nsockets, nmessages);

	rc = sockets_create(verbose, socket_ids, nsockets, family, type);
	if (rc != EOK)
		return rc;

	if (type == SOCK_STREAM) {
		rc = sockets_connect(verbose, socket_ids, nsockets, address,
		    addrlen);
		if (rc != EOK)
			return rc;
	}

	rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
	    &addrlen, data, size, nmessages);
	if (rc != EOK)
		return rc;

	rc = sockets_close(verbose, socket_ids, nsockets);
	if (rc != EOK)
		return rc;

	if (verbose)
		printf("\tOK\n");

	/****/

	rc = sockets_create(verbose, socket_ids, nsockets, family, type);
	if (rc != EOK)
		return rc;

	if (type == SOCK_STREAM) {
		rc = sockets_connect(verbose, socket_ids, nsockets, address,
		    addrlen);
		if (rc != EOK)
			return rc;
	}

	rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
	    data, size, nmessages);
	if (rc != EOK)
		return rc;

	rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
	    data, size, nmessages);
	if (rc != EOK)
		return rc;

	rc = sockets_close(verbose, socket_ids, nsockets);
	if (rc != EOK)
		return rc;

	if (verbose)
		printf("\tOK\n");

	return EOK;
}