Skip to content

Commit d630926

Browse files
committed
Fixes for handshake
1 parent c460188 commit d630926

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/cobo/handshake.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ static int handshake_main(int sockfd, handshake_protocol_t *hdata, uint64_t sess
380380
debug_printf("Checking random number packets\n");
381381

382382
debug_printf("Checking initial packet\n");
383-
fprintf(stderr,"Checking initial packet\n");
384383
result = compare_packets( &expected_packet, &recvd_packet);
385384
if (result < 0) {
386385
debug_printf("Error checking initial packet\n");
@@ -430,8 +429,7 @@ static int handshake_main(int sockfd, handshake_protocol_t *hdata, uint64_t sess
430429

431430
expected_random_number_packet.random_number = packet.random_number;
432431
expected_random_number_packet.signature = is_server ? CLIENT_TO_SERVER_SIG : SERVER_TO_CLIENT_SIG;
433-
434-
432+
435433
debug_printf("Checking random number packets\n");
436434
result = compare_random_number_packets(&recvd_random_number_packet, &expected_random_number_packet);
437435
if (result < 0) {
@@ -491,34 +489,49 @@ static int encode_addr(struct sockaddr *addr, unsigned char *target_addr, uint16
491489
}
492490

493491
int random_number(random_number_t *rand_num, uint64_t session_id) {
494-
int local_errno, result;
492+
int local_errno;
493+
int timeout = 100 * 30; //30 seconds
494+
useconds_t ten_milliseconds = 10000;
495+
ssize_t result, bytes_to_read, bytes_read;
495496
char hostname_buf[HOSTNAME_MAX_LEN];
496497

497498
// Get hostname
498499
if (gethostname(hostname_buf, HOSTNAME_MAX_LEN) != 0) {
499500
local_errno = errno;
500-
debug_printf("Trouble getting hostname. Error: %s\n",strerror(local_errno));
501+
error_printf("Trouble getting hostname. Error: %s\n",strerror(local_errno));
501502
return -1;
502503
}
503504
hostname_buf[HOSTNAME_MAX_LEN-1] = '\0';
504505

505506
//random number file
506-
again: result = getrandom((void*) &rand_num->random_number,sizeof(rand_num->random_number),GRND_RANDOM|GRND_NONBLOCK);
507-
if (result<0) {
508-
local_errno = errno;
509-
if (local_errno == EAGAIN || local_errno == EINTR){
510-
goto again;
511-
}
512-
else{
513-
debug_printf("Random number collection failed. Error: %s\n",strerror(local_errno));
507+
bytes_to_read = sizeof(rand_num->random_number);
508+
bytes_read = 0;
509+
do {
510+
result = getrandom(((unsigned char*) &rand_num->random_number) + bytes_read,
511+
bytes_to_read - bytes_read,
512+
GRND_NONBLOCK);
513+
if (result<0) {
514+
local_errno = errno;
515+
if (local_errno == EAGAIN || local_errno == EINTR){
516+
if (--timeout <= 0) {
517+
error_printf("Random number collection failed with repeated EAGAIN or EINTR\n");
518+
return -1;
519+
}
520+
usleep(ten_milliseconds);
521+
continue;
522+
}
523+
else{
524+
error_printf("Random number collection failed. Error: %s\n", strerror(local_errno));
525+
return -1;
526+
}
514527
}
515-
return -1;
516-
}
528+
bytes_read += result;
529+
} while (bytes_read < bytes_to_read);
517530

518531
rand_num->session_id = session_id;
519532
rand_num->counter = unique_number_counter;
520533
strcpy(rand_num->hostname, hostname_buf);
521-
unique_number_counter +=1;
534+
unique_number_counter++;
522535

523536
return 0;
524537
}
@@ -968,7 +981,7 @@ static int munge_decrypt_packet(void *recvd_packet, size_t recvd_packet_size,
968981
munge_err_t result;
969982
munge_ctx_t ctx = NULL;
970983
void *payload = NULL;
971-
int payload_size, return_result, iresult;
984+
int payload_size, return_result = 0, iresult;
972985
uid_t uid;
973986
gid_t gid;
974987

@@ -1097,7 +1110,7 @@ static int compare_packets(handshake_packet_t *expected_packet, handshake_packet
10971110
//If sessions don't match, expect that we've just recv a packet
10981111
//from another instance of handshake running on the same node.
10991112
//Drop connection
1100-
error_printf("Received mismatching session IDs in initial packed. Expected %lu, got %lu\n",
1113+
debug_printf("Received mismatching session IDs in initial packed. Expected %lu, got %lu. Two different spindle sessions probably tried to connect.\n",
11011114
expected_packet->random_number.session_id, recvd_packet->random_number.session_id);
11021115
return HSHAKE_DROP_CONNECTION;
11031116
}

0 commit comments

Comments
 (0)