s_time.c 26.6 KB
Newer Older
	#ifdef DEBUG
	printf("[DEBUG] Host %s resolved\n", host); 
	#endif

	memset(&addr, 0, sizeof(addr));
	addr.sin_addr = *(struct in_addr*)
	hp->h_addr_list[0];
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);




	if((sock=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP))<0){
		err_exit("Couldn't create socket");
	}

	
	#ifdef DEBUG
	printf("[DEBUG] Socket correctly created\n"); 
	#endif

	if(connect(sock,(struct sockaddr *)&addr, sizeof(addr))<0){
		err_exit("Couldn't connect socket");
	}
	#ifdef DEBUG
	printf("[DEBUG] Socket connected\n"); 
	#endif
	return sock;
}






/***********************************************************************
 * doConnection - make a connection
 * Args:
 *		scon	= earlier ssl connection for session id, or NULL
 * Returns:
 *		SSL *	= the connection pointer.
 */
static SSL *doConnection(SSL *scon, char *proto){
	BIO *conn;
	SSL *serverCon;
	int width, i=0;
	fd_set readfds;
	SPP_PROXY *proxies[N_proxies];
	SPP_SLICE *slice_set[slices_len];


	/* WARNING ONLY FOR TESTING, THIS OVERRIDES RETURN TYPE FOR PLN PROTOCOL */

	if ((strcmp(proto, "pln")) == 0){
		char * host_ip = strtok(strdup(host), ":");
		int host_port = atoi(strtok(NULL, ":")); 
		return tcp_connect(host_ip, host_port); // WARNING, THIS OVERRIDES RETURN TYPE. ONLY FOR TESTING
	}


	// what is this?
	if ((conn = BIO_new(BIO_s_connect())) == NULL){
		return(NULL);
	}

	BIO_set_conn_hostname(conn, host);
	#ifdef DEBUG
	printf("[DEBUG] Connecting to: %s  \n", host);
	#endif 


	// Create a new SSL* 
	if (scon == NULL){
		serverCon = SSL_new(tm_ctx);
		if ((strcmp(proto, "spp")) == 0){
			// Assign proxies
			int j; 
			for (j = 0; j < N_proxies; j++){
				proxies[j] = SPP_generate_proxy(serverCon, proxies_address[j]);
				#ifdef DEBUG
				printf("[DEBUG] Generating proxy: %s\n", proxies[j]->address);
				#endif 
			}
			// Generate and assign slices
			slices_management(serverCon, slice_set, proxies); 		
		}
	} 
	// Re-use SSL* passed as argument
	else {
		serverCon = scon;		
		// Get proxies and slices
		int temp_N; 
		int temp_S; 
		SPP_get_proxies(serverCon, proxies, &temp_N); 
		SPP_get_slices(serverCon, slice_set, &temp_S);		
		SSL_set_connect_state(serverCon);
	}

	SSL_set_bio(serverCon, conn, conn);

	// ok, lets connect -- weird 
	for(;;) {
		// Check here 
		if ((strcmp(proto, "spp")) == 0){
			i = SPP_connect(serverCon, slice_set, slices_len, proxies, N_proxies); 
		}
		if ((strcmp(proto, "ssl")) == 0){
			i = SSL_connect(serverCon);
		}		
		if (BIO_sock_should_retry(i)){
			BIO_printf(bio_err,"DELAY\n");

			i = SSL_get_fd(serverCon);
			width = i+1;
			FD_ZERO(&readfds);
			openssl_fdset(i, &readfds);
			/* Note: under VMS with SOCKETSHR the 2nd parameter
			 * is currently of type (int *) whereas under other
			 * systems it is (void *) if you don't have a cast it
			 * will choke the compiler: if you do have a cast then
			 * you can either go for (int *) or (void *).
			 */
			select(width, (void *)&readfds, NULL, NULL, NULL);
			continue;
		}
		break;
	}

	// Negative socket descriptor = error 
	if(i <= 0) {
		BIO_printf(bio_err,"ERROR\n");
		if (verify_error != X509_V_OK){
			BIO_printf(bio_err,"verify error:%s\n", X509_verify_cert_error_string(verify_error));
		} else {
			ERR_print_errors(bio_err);
		}
		if (scon == NULL){
			SSL_free(serverCon);
		}
		return NULL;
	}

	return serverCon;
}