Skip to content

Instantly share code, notes, and snippets.

@thotypous
Created May 30, 2010 04:24
Show Gist options
  • Select an option

  • Save thotypous/418779 to your computer and use it in GitHub Desktop.

Select an option

Save thotypous/418779 to your computer and use it in GitHub Desktop.
--- aiccu/common/ayiya.c.orig 2007-01-09 20:45:09.000000000 -0200
+++ aiccu/common/ayiya.c 2010-06-01 15:53:30.054314793 -0300
@@ -29,11 +29,19 @@
sha1_byte ayiya_hash[SHA1_DIGEST_LENGTH]; /* SHA1 Hash of the shared secret. */
TLSSOCKET ayiya_socket = NULL;
+static struct TIC_Tunnel *gTunnel = NULL;
+#ifndef _WIN32
+static pthread_t writer_thread = 0;
+#else
+static HANDLE writer_thread = 0;
+#endif
static const char reader_name[] = "tundev->tun";
static const char writer_name[] = "tun->tundev";
static const char beat_name[] = "beat";
+static void ayiya_retry_socket(void);
+
void ayiya_log(int level, const char *what, struct sockaddr_storage *clientaddr, socklen_t addrlen, const char *fmt, ...);
void ayiya_log(int level, const char *what, struct sockaddr_storage *clientaddr, socklen_t addrlen, const char *fmt, ...)
{
@@ -87,6 +95,12 @@
sha1_byte hash[SHA1_DIGEST_LENGTH];
struct sockaddr_in target;
+ if (!ayiya_socket)
+ {
+ ayiya_retry_socket();
+ return;
+ }
+
/* We tunnel over IPv4 */
memcpy(&target.sin_addr, &ayiya_ipv4_pop, sizeof(target.sin_addr));
target.sin_family = AF_INET;
@@ -137,6 +151,10 @@
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);
+ if (errno == EINVAL)
+ {
+ ayiya_retry_socket();
+ }
}
else if (length != (unsigned int)lenout)
{
@@ -172,6 +190,15 @@
while (true)
{
+ if (!ayiya_socket)
+ {
+#ifndef _WIN32
+ return NULL;
+#else
+ return 0;
+#endif
+ }
+
cl = sizeof(ci);
memset(buf, 0, sizeof(buf));
n = recvfrom(ayiya_socket->socket, (char *)buf, sizeof(buf), 0, (struct sockaddr *)&ci, &cl);
@@ -283,6 +310,12 @@
struct pseudo_ayh s;
int lenout, n;
+ if (!ayiya_socket)
+ {
+ ayiya_retry_socket();
+ return;
+ }
+
/* We tunnel over IPv4 */
memcpy(&target.sin_addr, &ayiya_ipv4_pop, sizeof(target.sin_addr));
target.sin_family = AF_INET;
@@ -335,6 +368,11 @@
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);
+ if (errno == EINVAL)
+ {
+ ayiya_retry_socket();
+ }
+
}
else if (n != lenout)
{
@@ -342,20 +380,55 @@
}
}
+static void ayiya_retry_socket(void)
+{
+#ifdef _WIN32
+ DWORD pID;
+#endif
+ TLSSOCKET tmp_socket = ayiya_socket;
+ ayiya_socket = NULL;
+ if (writer_thread)
+ {
+#ifndef _WIN32
+ pthread_cancel(writer_thread);
+#else
+ TerminateThread(writer_thread, 0);
+#endif
+ writer_thread = 0;
+ }
+ ayiya_log(LOG_ERR, "retry", NULL, 0, "Trying to reopen UDP socket to %s.\n", gTunnel->sIPv4_POP);
+ if (tmp_socket)
+ sock_free(tmp_socket);
+ ayiya_socket = connect_client(gTunnel->sIPv4_POP , AYIYA_PORT, AF_INET, SOCK_DGRAM);
+ if (ayiya_socket)
+ {
+ ayiya_log(LOG_ERR, "retry", NULL, 0, "Connection created. Restarting the writer thread.\n");
+#ifndef _WIN32
+ pthread_create(&writer_thread, NULL, ayiya_writer, (void *)gTunnel);
+#else
+ writer_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ayiya_writer, gTunnel, 0, &pID);
+#endif
+ }
+ else
+ {
+ ayiya_log(LOG_ERR, "retry", NULL, 0, "Connection error:: could not create connection to AYIYA server\n");
+ }
+}
+
+
bool ayiya(struct TIC_Tunnel *hTunnel)
{
SHA_CTX sha1;
struct addrinfo hints, *res, *ressave;
-#ifndef _WIN32
- pthread_t thread;
-#else
+#ifdef _WIN32
DWORD pID;
- HANDLE h;
#endif
/* Setup the tunnel */
if (!tun_start(&ayiya_tun)) return false;
+ gTunnel = hTunnel;
+
/* Resolve hTunnel entries */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
@@ -464,9 +537,9 @@
/* Launch a thread for reader */
#ifndef _WIN32
- pthread_create(&thread, NULL, ayiya_writer, (void *)hTunnel);
+ pthread_create(&writer_thread, NULL, ayiya_writer, (void *)hTunnel);
#else
- h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ayiya_writer, hTunnel, 0, &pID);
+ writer_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ayiya_writer, hTunnel, 0, &pID);
#endif
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment