Increment version.
[cascardo/rnetproxy.git] / popproxy.c
index d2ccbf7..47c2e94 100644 (file)
@@ -42,6 +42,7 @@ struct pop_address
   char *server;
   char *port;
   int ssl;
+  char *priority;
 };
 
 static HCConn *
@@ -79,7 +80,7 @@ server_conn_new (char *server, char *port, int ssl)
 }
 
 static HCConn *
-client_conn_new (int fd)
+client_conn_new (int fd, struct pop_address *address)
 {
   HCConn *conn;
   HCConn *ssl_conn;
@@ -93,8 +94,12 @@ client_conn_new (int fd)
       close (fd);
       return NULL;
     }
+
   ssl_conn = hc_conn_new (NULL, NULL);
   hc_conn_set_driver_ssl_server (ssl_conn, conn);
+  if (address && address->priority)
+    hc_conn_ssl_server_set_priority (ssl_conn, address->priority);
+
   if (r != 0)
     {
       hc_conn_close (ssl_conn);
@@ -130,11 +135,34 @@ push_other (HCConn *conn, HCEvent event, gpointer data)
     }
 }
 
+static void
+ssl_connected (HCConn *client_conn, HCEvent event, gpointer data)
+{
+  struct pop_address *address = data;
+  HCConn *server_conn;
+  if (event != HC_EVENT_CONNECT)
+    {
+      g_debug ("Did not get connect event when trying to handshake:"
+               " got %d", event);
+      hc_conn_close (client_conn);
+      return;
+    }
+  server_conn = server_conn_new (address->server, address->port,
+                                 address->ssl);
+  if (server_conn == NULL)
+    {
+      g_debug ("Failure to create connection to server.");
+      hc_conn_close (client_conn);
+      return;
+    }
+  hc_conn_set_callback (client_conn, push_other, server_conn);
+  hc_conn_set_callback (server_conn, push_other, client_conn);
+}
+
 static void
 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
 {
   HCConn *client_conn;
-  HCConn *server_conn;
   struct pop_address *address = data;
   if (fd < 0)
     {
@@ -146,21 +174,13 @@ new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
   g_message ("Received connection from %s.",
              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
 
-  server_conn = server_conn_new (address->server, address->port,
-                                 address->ssl);
-  if (server_conn == NULL)
-    {
-      return;
-    }
-  client_conn = client_conn_new (fd);
+  client_conn = client_conn_new (fd, address);
   if (client_conn == NULL)
     {
-      hc_conn_close (server_conn);
       return;
     }
 
-  hc_conn_set_callback (client_conn, push_other, server_conn);
-  hc_conn_set_callback (server_conn, push_other, client_conn);
+  hc_conn_set_callback (client_conn, ssl_connected, address);
 
 }
 
@@ -188,6 +208,7 @@ int main (int argc, char **argv)
   gchar *server_address;
   gchar *server_port;
   int server_ssl;
+  gchar *server_priority;
   gchar *certfile;
   gchar *ssl_keyfile;
   gchar *policy;
@@ -282,6 +303,14 @@ int main (int argc, char **argv)
       server_ssl = 0;
       g_error_free (error);
     }
+  error = NULL;
+  server_priority = g_key_file_get_string (keyfile, "global", "priority",
+                                           &error);
+  if (server_priority == NULL && error != NULL)
+    {
+      server_priority = g_strdup ("NORMAL");
+      g_error_free (error);
+    }
 
   error = NULL;
   policy = g_key_file_get_string (keyfile, "global", "policy",
@@ -300,6 +329,7 @@ int main (int argc, char **argv)
   pop_address.server = server_address;
   pop_address.port = server_port;
   pop_address.ssl = server_ssl;
+  pop_address.priority = server_priority;
 
   server_fd = hc_tcp_server (port);
   if (server_fd < 0)