Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
#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;
}