diff common/ayiya.c common/ayiya.c --- common/ayiya.c +++ common/ayiya.c @@ -77,6 +77,27 @@ void ayiya_log(int level, const char *wh dolog(level, buf); } +static bool ayiya_reconnect(void); +static bool ayiya_reconnect(void) +{ + char *addr = inet_ntoa(ayiya_ipv4_pop); + TLSSOCKET new_socket = connect_client(addr, AYIYA_PORT, AF_INET, SOCK_DGRAM); + + if (!new_socket) + { + ayiya_log(LOG_WARNING, reader_name, NULL, 0, "Error while reconnecting to AYIYA server %s: %s (%d)\n", addr, strerror(errno), errno); + + return false; + } + + ayiya_log(LOG_DEBUG, reader_name, NULL, 0, "Reconnected to server %s...\n", addr); + + sock_free(ayiya_socket); + ayiya_socket = new_socket; + + return true; +} + /* Tun -> Socket */ void ayiya_reader(char *buf, unsigned int length); void ayiya_reader(char *buf, unsigned int length) @@ -137,6 +158,13 @@ void ayiya_reader(char *buf, unsigned in if (lenout < 0) { ayiya_log(LOG_ERR, reader_name, NULL, 0, "Error (%d) while sending %u bytes to network: %s (%d)\n", lenout, length, strerror(errno), errno); + +#ifdef EADDRNOTAVAIL + if (errno == EADDRNOTAVAIL) + { + ayiya_reconnect(); + } +#endif } else if (length != (unsigned int)lenout) { @@ -335,6 +363,13 @@ void ayiya_beat(void) if (lenout < 0) { ayiya_log(LOG_ERR, beat_name, NULL, 0, "Error (%d) while sending %u bytes sent to network: %s (%d)\n", lenout, n, strerror(errno), errno); + +#ifdef EADDRNOTAVAIL + if (errno == EADDRNOTAVAIL) + { + ayiya_reconnect(); + } +#endif } else if (n != lenout) {