agent: implement connection loop with retry

This commit is contained in:
2019-01-21 23:26:39 +02:00
parent 598fd7aa74
commit 986af66adb
2 changed files with 186 additions and 155 deletions

View File

@@ -83,10 +83,11 @@ static void signal_handler(int sig)
static void agent_shutdown(void) static void agent_shutdown(void)
{ {
printf("Shutting down agent...\n"); printf("Shutting down agent...\n");
SSL_shutdown(ssl); if (ssl) SSL_shutdown(ssl);
SSL_free(ssl); if (ssl) SSL_free(ssl);
SSL_CTX_free(ctx); /* release context */ if (ctx) SSL_CTX_free(ctx); /* release context */
close(server); /* close socket */ close(server); /* close socket */
_exit(EXIT_SUCCESS);
} }
static void set_env(void) static void set_env(void)
@@ -116,7 +117,7 @@ static void set_env(void)
int main(int count, char *strings[]) int main(int count, char *strings[])
{ {
int bytes, i; int bytes, i, err;
char *hostname, *portnum; char *hostname, *portnum;
if (count != 6) { if (count != 6) {
@@ -127,10 +128,14 @@ int main(int count, char *strings[])
portnum=strings[2]; portnum=strings[2];
if ((ctx = init_ctx()) == NULL) if ((ctx = init_ctx()) == NULL)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
while (1) {
server = connect_to_rmps(hostname, atoi(portnum)); server = connect_to_rmps(hostname, atoi(portnum));
if (!server) { if (!server) {
fprintf(stderr, "Failed to connect to RMPS: %s:%d\n", hostname, atoi(portnum)); fprintf(stderr, "Failed to connect to RMPS on %s:%d - %s\n", hostname, atoi(portnum), strerror(errno));
_exit(EXIT_FAILURE); printf("Retrying...\n");
sleep(5);
continue;
} }
load_certs(ctx, strings[3], strings[4], strings[5]); load_certs(ctx, strings[3], strings[4], strings[5]);
ssl = SSL_new(ctx); /* create new SSL connection state */ ssl = SSL_new(ctx); /* create new SSL connection state */
@@ -181,7 +186,8 @@ int main(int count, char *strings[])
if (bytes != sizeof(struct msg_t)) { if (bytes != sizeof(struct msg_t)) {
fprintf( stderr, fprintf( stderr,
"Received non-standard data from server!\n" ); "Received non-standard data from server!\n" );
continue; //conntinue;
return 1;
} }
if (buf.chunk.id == 0) { if (buf.chunk.id == 0) {
if ((index = get_job_slot()) == FAIL) { if ((index = get_job_slot()) == FAIL) {
@@ -281,4 +287,30 @@ int main(int count, char *strings[])
SSL_shutdown(ssl); SSL_shutdown(ssl);
SSL_free(ssl); /* release connection state */ SSL_free(ssl); /* release connection state */
} while (bytes); } while (bytes);
if (SSL_get_shutdown(ssl)) {
printf("RMPS server has shutdown, trying to reconnect...\n");
sleep(5);
} else {
err = SSL_get_error(ssl, bytes);
switch (err) {
case SSL_ERROR_WANT_WRITE:
printf("want_write\n");
return 0;
case SSL_ERROR_WANT_READ:
printf("want_read\n");
return 0;
case SSL_ERROR_ZERO_RETURN:
printf("zero_return\n");
return -1;
case SSL_ERROR_SYSCALL:
printf("syscall\n");
return -1;
case SSL_ERROR_SSL:
printf("ssl\n");
return -1;
default:
return -1;
}
}
}
} }

View File

@@ -49,7 +49,6 @@ int connect_to_rmps(const char *hostname, int port)
addr.sin_addr.s_addr = *(long*)(host->h_addr); addr.sin_addr.s_addr = *(long*)(host->h_addr);
if (connect(sd, (struct sockaddr*)&addr, sizeof(addr)) == FAIL) { if (connect(sd, (struct sockaddr*)&addr, sizeof(addr)) == FAIL) {
close(sd); close(sd);
perror(hostname);
return 0; return 0;
} }
return sd; return sd;