event_enable_debug_mode() must be called at most once.
[radsecproxy.git] / lib / event.c
index 97a08c8..ff05012 100644 (file)
 #endif
 #include <radsec/radsec.h>
 #include <radsec/radsec-impl.h>
+#include "tcp.h"
+#include "udp.h"
 #if defined (RS_ENABLE_TLS)
 #include "tls.h"
 #endif
-#include "udp.h"
 #include "event.h"
 #include "packet.h"
+#include "conn.h"
 #include "debug.h"
 
+#if defined (DEBUG)
+extern int _event_debug_mode_on;
+#endif
+
 static void
 _evlog_cb (int severity, const char *msg)
 {
@@ -50,6 +56,41 @@ _evlog_cb (int severity, const char *msg)
   fprintf (stderr, "libevent: [%s] %s\n", sevstr, msg); /* FIXME: stderr?  */
 }
 
+void
+event_conn_timeout_cb (int fd, short event, void *data)
+{
+  struct rs_connection *conn = NULL;
+
+  assert (data);
+  conn = (struct rs_connection *) data;
+
+  if (event & EV_TIMEOUT)
+    {
+      rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
+                __func__, conn, conn->fd, conn->active_peer));
+      conn->is_connecting = 0;
+      rs_err_conn_push_fl (conn, RSE_TIMEOUT_CONN, __FILE__, __LINE__, NULL);
+      event_loopbreak (conn);
+    }
+}
+
+void
+event_retransmit_timeout_cb (int fd, short event, void *data)
+{
+  struct rs_connection *conn = NULL;
+
+  assert (data);
+  conn = (struct rs_connection *) data;
+
+  if (event & EV_TIMEOUT)
+    {
+      rs_debug (("%s: retransmission timeout on %p (fd %d) sending to %p\n",
+                __func__, conn, conn->fd, conn->active_peer));
+      rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
+      event_loopbreak (conn);
+    }
+}
+
 int
 event_init_socket (struct rs_connection *conn, struct rs_peer *p)
 {
@@ -74,24 +115,6 @@ event_init_socket (struct rs_connection *conn, struct rs_peer *p)
   return RSE_OK;
 }
 
-static void
-_conn_timeout_cb (int fd, short event, void *data)
-{
-  struct rs_connection *conn;
-
-  assert (data);
-  conn = (struct rs_connection *) data;
-
-  if (event & EV_TIMEOUT)
-    {
-      rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
-                __func__, conn, conn->fd, conn->active_peer));
-      conn->is_connecting = 0;
-      rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
-      event_loopbreak (conn);
-    }
-}
-
 int
 event_init_bufferevent (struct rs_connection *conn, struct rs_peer *peer)
 {
@@ -155,7 +178,7 @@ event_do_connect (struct rs_connection *conn)
 
   if (p->conn->bev)            /* TCP */
     {
-      event_set_timeout (conn);
+      conn_activate_timeout (conn); /* Connect timeout.  */
       err = bufferevent_socket_connect (p->conn->bev, p->addr->ai_addr,
                                        p->addr->ai_addrlen);
       if (err < 0)
@@ -192,23 +215,6 @@ event_loopbreak (struct rs_connection *conn)
 }
 
 
-int
-event_set_timeout (struct rs_connection *conn)
-{
-  struct timeval tv;
-
-  if (!conn->tev)
-    conn->tev = evtimer_new (conn->evb, _conn_timeout_cb, conn);
-  if (!conn->tev)
-    return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
-                               "evtimer_new");
-  tv.tv_sec = conn->realm->timeout;
-  tv.tv_usec = 0;
-  evtimer_add (conn->tev, &tv);
-
-  return RSE_OK;
-}
-
 void
 event_on_disconnect (struct rs_connection *conn)
 {
@@ -225,10 +231,10 @@ event_on_connect (struct rs_connection *conn, struct rs_packet *pkt)
   assert (!conn->is_connecting);
   conn->is_connected = 1;
   rs_debug (("%s: %p connected\n", __func__, conn->active_peer));
-  if (conn->tev)
-    evtimer_del (conn->tev);
+
   if (conn->callbacks.connected_cb)
     conn->callbacks.connected_cb (conn->user_data);
+
   if (pkt)
     packet_do_send (pkt);
 }
@@ -236,11 +242,13 @@ event_on_connect (struct rs_connection *conn, struct rs_packet *pkt)
 int
 event_init_eventbase (struct rs_connection *conn)
 {
+  assert (conn);
   if (conn->evb)
     return RSE_OK;
 
 #if defined (DEBUG)
-  event_enable_debug_mode ();
+  if (!_event_debug_mode_on)
+    event_enable_debug_mode ();
 #endif
   event_set_log_callback (_evlog_cb);
   conn->evb = event_base_new ();