Ejemplo n.º 1
0
/// Initializes the connection, create request, sets up the SSL...
static onion_request *onion_connection_start(onion *o, int clientfd, struct sockaddr_storage *cli_addr, socklen_t cli_len){
  signal(SIGPIPE, SIG_IGN); // FIXME. remove the thread better. Now it will try to write and fail on it.
  
	// sorry all the ifdefs, but here is the worst case where i would need it.. and both are almost the same.
#ifdef HAVE_GNUTLS
	gnutls_session_t session=NULL;
	if (o->flags&O_SSL_ENABLED){
		session=onion_prepare_gnutls_session(o, clientfd);
		if (session==NULL){
			ONION_ERROR("Could not create session");
			return NULL;
		}
	}
#endif

	onion_request *req;
#ifdef HAVE_GNUTLS
	if (o->flags&O_SSL_ENABLED){
		onion_server_set_write(o->server, (onion_write)gnutls_record_send); // Im lucky, has the same signature.
		onion_server_set_read(o->server, (onion_read)gnutls_record_recv); // Im lucky, has the same signature.
		onion_server_set_close(o->server, (onion_close)onion_ssl_close);
		req=onion_request_new_from_socket(o->server, session, cli_addr, cli_len);
	}
	else
#endif
	{
		onion_server_set_write(o->server, (onion_write)onion_write_to_socket);
		onion_server_set_read(o->server, (onion_read)onion_read_from_socket);
		onion_server_set_close(o->server, (onion_close)onion_close_socket);
		req=onion_request_new_from_socket(o->server, (void*)(long int)clientfd, cli_addr, cli_len);
	}
	req->fd=clientfd;
	if (!( (o->flags&O_THREADED) || (o->flags&O_POLL) ))
		onion_request_set_no_keep_alive(req);
	
	return req;
}
Ejemplo n.º 2
0
void t08_sockaddr_storage(){
	INIT_LOCAL();
	onion_request *req;
	
	
	{
		struct sockaddr_storage client_addr;
		socklen_t client_len=0;
		
		req=onion_request_new(custom_io);
		FAIL_IF_EQUAL(onion_request_get_sockadd_storage(req, NULL), &client_addr);
		FAIL_IF_EQUAL(onion_request_get_sockadd_storage(req, &client_len), &client_addr);
		FAIL_IF_NOT_EQUAL_INT(client_len, 0);
		onion_request_free(req);
	}
	
	{
		struct sockaddr_storage *client_addr;
		socklen_t client_len=0;
		struct addrinfo hints;
		struct addrinfo *result, *rp;
		char hostA[128], portA[16];
		char hostB[128], portB[16];
		
		memset(&hints,0, sizeof(struct addrinfo));
		hints.ai_canonname=NULL;
		hints.ai_addr=NULL;
		hints.ai_next=NULL;
		hints.ai_socktype=SOCK_STREAM;
		hints.ai_family=AF_UNSPEC;
		hints.ai_flags=AI_PASSIVE|AI_NUMERICSERV;

		int err=getaddrinfo("localhost","8080", &hints, &result);
		FAIL_IF_NOT_EQUAL_INT(err,0);
		if (err!=0)
			goto exit;
		for(rp=result;rp!=NULL;rp=rp->ai_next){
			memset(hostA,0,sizeof(hostA));
			memset(hostB,0,sizeof(hostB));
			memset(portA,0,sizeof(portA));
			memset(portB,0,sizeof(portB));
			
			getnameinfo(rp->ai_addr, rp->ai_addrlen, hostA, sizeof(hostA), portA, sizeof(portA), NI_NUMERICHOST | NI_NUMERICSERV);
			req=onion_request_new_from_socket(NULL, 0,(struct sockaddr_storage *)rp->ai_addr, rp->ai_addrlen);
			client_addr=onion_request_get_sockadd_storage(req, &client_len);
			FAIL_IF_EQUAL(client_addr, (struct sockaddr_storage *)rp->ai_addr);
			FAIL_IF_EQUAL(client_addr, NULL);
			
			getnameinfo((struct sockaddr *)client_addr, client_len, hostB, sizeof(hostB), portB, sizeof(portB), NI_NUMERICHOST | NI_NUMERICSERV);
			
			FAIL_IF_NOT_EQUAL_STR(hostA, hostB);
			FAIL_IF_NOT_EQUAL_STR(portA, portB);
			FAIL_IF_NOT_EQUAL_STR(hostA, onion_request_get_client_description(req));
			onion_request_free(req);
		}
		freeaddrinfo(result);
		
	}
	
	{
		req=onion_request_new(custom_io); //NULL, NULL, NULL);
		struct sockaddr_storage *client_addr;
		socklen_t client_len;
		client_addr=onion_request_get_sockadd_storage(req, &client_len);
		FAIL_IF_NOT_EQUAL(client_addr, NULL);
		onion_request_free(req);
	}
	
exit:
	END_LOCAL();
}