Cosmetic changes.
[radsecproxy.git] / lib / packet.c
index 4927725..9874dee 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
+#include <sys/time.h>
 #include <assert.h>
 #include <freeradius/libradius.h>
 #include <event2/event.h>
 #endif
 
 static int
+_loopbreak (struct rs_connection *conn)
+{
+  int err = event_base_loopbreak (conn->evb);
+  if (err < 0)
+    rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
+                        "event_base_loopbreak: %s",
+                        evutil_gai_strerror (err));
+  return err;
+}
+
+static int
 _do_send (struct rs_packet *pkt)
 {
   int err;
@@ -33,9 +46,11 @@ _do_send (struct rs_packet *pkt)
   assert (pkt->rpkt);
   assert (!pkt->original);
 
+  /* Add Message-Authenticator, RFC 2869.  */
+  /* FIXME: Make Message-Authenticator optional?  */
   vp = paircreate (PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
   if (!vp)
-    return rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
+    return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
                                "paircreate: %s", fr_strerror ());
   pairadd (&pkt->rpkt->vps, vp);
 
@@ -63,15 +78,16 @@ _do_send (struct rs_packet *pkt)
   if (err < 0)
     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
                                "bufferevent_write: %s",
-                               evutil_gai_strerror(err));
+                               evutil_gai_strerror (err));
   return RSE_OK;
 }
 
 static void
 _on_connect (struct rs_connection *conn)
 {
-  conn->active_peer->is_connected = 1;
+  conn->is_connected = 1;
   rs_debug (("%s: %p connected\n", __func__, conn->active_peer));
+  evtimer_del (conn->tev);
   if (conn->callbacks.connected_cb)
     conn->callbacks.connected_cb (conn->user_data);
 }
@@ -79,7 +95,8 @@ _on_connect (struct rs_connection *conn)
 static void
 _on_disconnect (struct rs_connection *conn)
 {
-  conn->active_peer->is_connected = 0;
+  conn->is_connecting = 0;
+  conn->is_connected = 0;
   rs_debug (("%s: %p disconnected\n", __func__, conn->active_peer));
   if (conn->callbacks.disconnected_cb)
     conn->callbacks.disconnected_cb (conn->user_data);
@@ -89,11 +106,11 @@ static void
 _event_cb (struct bufferevent *bev, short events, void *ctx)
 {
   struct rs_packet *pkt = (struct rs_packet *)ctx;
-  struct rs_connection *conn;
-  struct rs_peer *p;
-  int sockerr;
+  struct rs_connection *conn = NULL;
+  struct rs_peer *p = NULL;
+  int sockerr = 0;
 #if defined (RS_ENABLE_TLS)
-  unsigned long tlserr;
+  unsigned long tlserr = 0;
 #endif
 
   assert (pkt);
@@ -102,7 +119,7 @@ _event_cb (struct bufferevent *bev, short events, void *ctx)
   conn = pkt->conn;
   p = conn->active_peer;
 
-  p->is_connecting = 0;
+  conn->is_connecting = 0;
   if (events & BEV_EVENT_CONNECTED)
     {
       _on_connect (conn);
@@ -113,6 +130,12 @@ _event_cb (struct bufferevent *bev, short events, void *ctx)
     {
       _on_disconnect (conn);
     }
+  else if (events & BEV_EVENT_TIMEOUT)
+    {
+      rs_debug (("%s: %p times out on %s\n", __func__, p,
+                (events & BEV_EVENT_READING) ? "read" : "write"));
+      rs_err_conn_push_fl (pkt->conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
+    }
   else if (events & BEV_EVENT_ERROR)
     {
       sockerr = evutil_socket_geterror (conn->active_peer->fd);
@@ -124,11 +147,12 @@ _event_cb (struct bufferevent *bev, short events, void *ctx)
        {
          rs_err_conn_push_fl (pkt->conn, RSE_SOCKERR, __FILE__, __LINE__,
                               "%d: socket error %d (%s)",
-                              conn->active_peer->fd,
+                              conn->fd,
                               sockerr,
                               evutil_socket_error_to_string (sockerr));
-         rs_debug (("%s: socket error on fd %d: %d\n", __func__,
-                    conn->active_peer->fd,
+         rs_debug (("%s: socket error on fd %d: %s (%d)\n", __func__,
+                    conn->fd,
+                    evutil_socket_error_to_string (sockerr),
                     sockerr));
        }
 #if defined (RS_ENABLE_TLS)
@@ -141,11 +165,17 @@ _event_cb (struct bufferevent *bev, short events, void *ctx)
              rs_debug (("%s: openssl error: %s\n", __func__,
                         ERR_error_string (tlserr, NULL)));
              rs_err_conn_push_fl (pkt->conn, RSE_SSLERR, __FILE__, __LINE__,
-                                  "%d", tlserr);
+                                  ERR_error_string (tlserr, NULL));
            }
        }
 #endif /* RS_ENABLE_TLS */
+      _loopbreak (conn);
     }
+
+#if defined (DEBUG)
+  if (events & BEV_EVENT_ERROR && events != BEV_EVENT_ERROR)
+    rs_debug (("%s: BEV_EVENT_ERROR and more: 0x%x\n", __func__, events));
+#endif
 }
 
 static void
@@ -300,15 +330,15 @@ _read_packet (struct rs_packet *pkt)
    Inform upper layer about successful reception of valid RADIUS
    message by invoking conn->callbacks.recevied_cb(), if !NULL.  */
 static void
-_read_cb (struct bufferevent *bev, void *ctx)
+_read_cb (struct bufferevent *bev, void *user_data)
 {
-  struct rs_packet *pkt = (struct rs_packet *) ctx;
+  struct rs_packet *pkt = (struct rs_packet *) user_data;
 
   assert (pkt);
   assert (pkt->conn);
   assert (pkt->rpkt);
 
-  pkt->rpkt->sockfd = pkt->conn->active_peer->fd;
+  pkt->rpkt->sockfd = pkt->conn->fd;
   pkt->rpkt->vps = NULL;
 
   if (!pkt->hdr_read_flag)
@@ -348,37 +378,41 @@ _evlog_cb (int severity, const char *msg)
 static int
 _init_evb (struct rs_connection *conn)
 {
-  if (!conn->evb)
-    {
+  if (conn->evb)
+    return RSE_OK;
+
 #if defined (DEBUG)
-      event_enable_debug_mode ();
+  event_enable_debug_mode ();
 #endif
-      event_set_log_callback (_evlog_cb);
-      conn->evb = event_base_new ();
-      if (!conn->evb)
-       return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
-                                   "event_base_new");
-    }
+  event_set_log_callback (_evlog_cb);
+  conn->evb = event_base_new ();
+  if (!conn->evb)
+    return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
+                               "event_base_new");
+
   return RSE_OK;
 }
 
 static int
 _init_socket (struct rs_connection *conn, struct rs_peer *p)
 {
-  if (p->fd != -1)
+  if (conn->fd != -1)
     return RSE_OK;
 
   assert (p->addr);
-  p->fd = socket (p->addr->ai_family, p->addr->ai_socktype,
-                 p->addr->ai_protocol);
-  if (p->fd < 0)
-    return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
-                               strerror (errno));
-  if (evutil_make_socket_nonblocking (p->fd) < 0)
+  conn->fd = socket (p->addr->ai_family, p->addr->ai_socktype,
+                    p->addr->ai_protocol);
+  if (conn->fd < 0)
+    return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
+                               "socket: %d (%s)",
+                               errno, strerror (errno));
+  if (evutil_make_socket_nonblocking (conn->fd) < 0)
     {
-      evutil_closesocket (p->fd);
-      return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
-                                 strerror (errno));
+      evutil_closesocket (conn->fd);
+      conn->fd = -1;
+      return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
+                                 "evutil_make_socket_nonblocking: %d (%s)",
+                                 errno, strerror (errno));
     }
   return RSE_OK;
 }
@@ -386,26 +420,70 @@ _init_socket (struct rs_connection *conn, struct rs_peer *p)
 static struct rs_peer *
 _pick_peer (struct rs_connection *conn)
 {
+  assert (conn);
+
+  if (conn->active_peer)
+    conn->active_peer = conn->active_peer->next; /* Next.  */
   if (!conn->active_peer)
-    conn->active_peer = conn->peers;
+    conn->active_peer = conn->peers; /* From the top.  */
+
   return conn->active_peer;
 }
 
+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);
+      _loopbreak (conn);
+    }
+}
+static int
+_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;
+}
+
 static int
 _init_bev (struct rs_connection *conn, struct rs_peer *peer)
 {
   if (conn->bev)
     return RSE_OK;
 
-  switch (conn->type)
+  switch (conn->realm->type)
     {
     case RS_CONN_TYPE_UDP:
+      /* Fall through.  */
+      /* NOTE: We know this is wrong for several reasons, most notably
+        because libevent doesn't work as expected with UDP.  The
+        timeout handling is wrong too.  */
     case RS_CONN_TYPE_TCP:
-      conn->bev = bufferevent_socket_new (conn->evb, peer->fd, 0);
+      conn->bev = bufferevent_socket_new (conn->evb, conn->fd, 0);
       if (!conn->bev)
        return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
                                    "bufferevent_socket_new");
       break;
+
 #if defined (RS_ENABLE_TLS)
     case RS_CONN_TYPE_TLS:
       if (rs_tls_init (conn))
@@ -414,62 +492,79 @@ _init_bev (struct rs_connection *conn, struct rs_peer *peer)
         seem to break when be_openssl_ctrl() (in libevent) calls
         SSL_set_bio() after BIO_new_socket() with flag=1.  */
       conn->bev =
-       bufferevent_openssl_socket_new (conn->evb, peer->fd, conn->tls_ssl,
+       bufferevent_openssl_socket_new (conn->evb, conn->fd, conn->tls_ssl,
                                        BUFFEREVENT_SSL_CONNECTING, 0);
       if (!conn->bev)
        return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
                                    "bufferevent_openssl_socket_new");
-
       break;
+
     case RS_CONN_TYPE_DTLS:
-      return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
-                                 "%s: NYI", __func__);
+      return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
 #endif /* RS_ENABLE_TLS */
+
     default:
       return rs_err_conn_push_fl (conn, RSE_INTERNAL, __FILE__, __LINE__,
                                  "%s: unknown connection type: %d", __func__,
-                                 conn->type);
+                                 conn->realm->type);
     }
 
   return RSE_OK;
 }
 
 static void
-_do_connect (struct rs_peer *p)
+_do_connect (struct rs_connection *conn)
 {
+  struct rs_peer *p;
   int err;
 
+  assert (conn);
+  assert (conn->active_peer);
+  p = conn->active_peer;
+
+#if defined (DEBUG)
+  {
+    char host[80], serv[80];
+
+    getnameinfo (p->addr->ai_addr,
+                p->addr->ai_addrlen,
+                host, sizeof(host), serv, sizeof(serv),
+                0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
+    rs_debug (("%s: connecting to %s:%s\n", __func__, host, serv));
+  }
+#endif
+
+  _set_timeout (conn);
   err = bufferevent_socket_connect (p->conn->bev, p->addr->ai_addr,
                                    p->addr->ai_addrlen);
   if (err < 0)
     rs_err_conn_push_fl (p->conn, RSE_EVENT, __FILE__, __LINE__,
                         "bufferevent_socket_connect: %s",
-                        evutil_gai_strerror(err));
+                        evutil_gai_strerror (err));
   else
-    p->is_connecting = 1;
+    p->conn->is_connecting = 1;
 }
 
 static int
 _conn_open(struct rs_connection *conn, struct rs_packet *pkt)
 {
-  struct rs_peer *p;
-
   if (_init_evb (conn))
     return -1;
 
-  p = _pick_peer (conn);
-  if (!p)
+  if (!conn->active_peer)
+    _pick_peer (conn);
+  if (!conn->active_peer)
     return rs_err_conn_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
 
-  if (_init_socket (conn, p))
+  if (_init_socket (conn, conn->active_peer))
     return -1;
 
-  if (_init_bev (conn, p))
+  if (_init_bev (conn, conn->active_peer))
     return -1;
 
-  if (!p->is_connected)
-    if (!p->is_connecting)
-      _do_connect (p);
+  if (!conn->is_connected)
+    if (!conn->is_connecting)
+      _do_connect (conn);
 
   return RSE_OK;
 }
@@ -477,7 +572,7 @@ _conn_open(struct rs_connection *conn, struct rs_packet *pkt)
 static int
 _conn_is_open_p (struct rs_connection *conn)
 {
-  return conn->active_peer && conn->active_peer->is_connected;
+  return conn->active_peer && conn->is_connected;
 }
 
 /* Public functions.  */
@@ -509,9 +604,9 @@ rs_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
 }
 
 int
-rs_packet_create_auth_request (struct rs_connection *conn,
-                              struct rs_packet **pkt_out,
-                              const char *user_name, const char *user_pw)
+rs_packet_create_authn_request (struct rs_connection *conn,
+                               struct rs_packet **pkt_out,
+                               const char *user_name, const char *user_pw)
 {
   struct rs_packet *pkt;
   struct rs_attr *attr;
@@ -542,25 +637,17 @@ rs_packet_create_auth_request (struct rs_connection *conn,
 static void
 _wcb (void *user_data)
 {
-  struct rs_connection *conn = (struct rs_connection *) user_data;
-  int err;
-
-  assert (conn);
-
-  /* When we're running the event loop for the user, we must break
-     it in order to give the control back to the user.  */
-  err = event_base_loopbreak (conn->evb);
-  if (err < 0)
-    rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
-                        "event_base_loopbreak: %s",
-                        evutil_gai_strerror(err));
+  struct rs_packet *pkt = (struct rs_packet *) user_data;
+  assert (pkt);
+  pkt->written_flag = 1;
+  bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
 }
 
 int
 rs_packet_send (struct rs_packet *pkt, void *user_data)
 {
   struct rs_connection *conn = NULL;
-  int err = RSE_OK;
+  int err = 0;
 
   assert (pkt);
   assert (pkt->conn);
@@ -575,27 +662,28 @@ rs_packet_send (struct rs_packet *pkt, void *user_data)
   assert (conn->evb);
   assert (conn->bev);
   assert (conn->active_peer);
-  assert (conn->active_peer->fd >= 0);
+  assert (conn->fd >= 0);
 
   conn->user_data = user_data;
   bufferevent_setcb (conn->bev, NULL, _write_cb, _event_cb, pkt);
+  bufferevent_enable (conn->bev, EV_WRITE);
 
   /* Do dispatch, unless the user wants to do it herself.  */
   if (!conn->user_dispatch_flag)
     {
       conn->callbacks.sent_cb = _wcb;
-      conn->user_data = conn;
+      conn->user_data = pkt;
       rs_debug (("%s: entering event loop\n", __func__));
       err = event_base_dispatch (conn->evb);
       if (err < 0)
        return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
                                    "event_base_dispatch: %s",
-                                   evutil_gai_strerror(err));
+                                   evutil_gai_strerror (err));
       rs_debug (("%s: event loop done\n", __func__));
       conn->callbacks.sent_cb = NULL;
       conn->user_data = NULL;
 
-      if (!event_base_got_break (conn->evb))
+      if (!pkt->written_flag)
        return -1;
     }
 
@@ -605,15 +693,10 @@ rs_packet_send (struct rs_packet *pkt, void *user_data)
 static void
 _rcb (struct rs_packet *packet, void *user_data)
 {
-  int err = 0;
-
-  /* When we're running the event loop for the user, we must break it
-     in order to give the control back to the user.  */
-  err = event_base_loopbreak (packet->conn->evb);
-  if (err < 0)
-    rs_err_conn_push_fl (packet->conn, RSE_EVENT, __FILE__, __LINE__,
-                        "event_base_loopbreak: %s",
-                        evutil_gai_strerror(err));
+  struct rs_packet *pkt = (struct rs_packet *) user_data;
+  assert (pkt);
+  pkt->valid_flag = 1;
+  bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
 }
 
 /* Special function used in libradsec blocking dispatching mode,
@@ -636,10 +719,11 @@ rs_conn_receive_packet (struct rs_connection *conn,
                        struct rs_packet *request,
                        struct rs_packet **pkt_out)
 {
-  int err = RSE_OK;
+  int err = 0;
   struct rs_packet *pkt = NULL;
 
   assert (conn);
+  assert (conn->realm);
   assert (!conn->user_dispatch_flag); /* Dispatching mode only.  */
 
   if (rs_packet_create (conn, pkt_out))
@@ -648,36 +732,32 @@ rs_conn_receive_packet (struct rs_connection *conn,
   pkt->conn = conn;
   pkt->original = request;
 
-  if (_conn_open (conn, pkt))
-    return -1;
   assert (conn->evb);
   assert (conn->bev);
   assert (conn->active_peer);
-  assert (conn->active_peer->fd >= 0);
+  assert (conn->fd >= 0);
 
-  /* Install read and event callbacks with libevent.  */
   bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
-  bufferevent_enable (conn->bev, EV_READ);
   bufferevent_setcb (conn->bev, _read_cb, NULL, _event_cb, pkt);
-
-  /* Install read callback with ourselves, for signaling successful
-     reception of message.  */
+  bufferevent_enable (conn->bev, EV_READ);
   conn->callbacks.received_cb = _rcb;
+  conn->user_data = pkt;
 
   /* Dispatch.  */
   rs_debug (("%s: entering event loop\n", __func__));
   err = event_base_dispatch (conn->evb);
+  conn->callbacks.received_cb = NULL;
   if (err < 0)
     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
                                "event_base_dispatch: %s",
-                               evutil_gai_strerror(err));
+                               evutil_gai_strerror (err));
   rs_debug (("%s: event loop done\n", __func__));
-  conn->callbacks.received_cb = NULL;
-  if (!event_base_got_break (conn->evb))
+
+  if (!pkt->valid_flag)
     return -1;
 
 #if defined (DEBUG)
-  rs_dump_packet (pkt);
+      rs_dump_packet (pkt);
 #endif
 
   pkt->original = NULL;                /* FIXME: Why?  */
@@ -685,21 +765,21 @@ rs_conn_receive_packet (struct rs_connection *conn,
 }
 
 void
-rs_packet_add_attr(struct rs_packet *pkt, struct rs_attr *attr)
+rs_packet_add_attr (struct rs_packet *pkt, struct rs_attr *attr)
 {
   pairadd (&pkt->rpkt->vps, attr->vp);
   attr->pkt = pkt;
 }
 
 struct radius_packet *
-rs_packet_frpkt(struct rs_packet *pkt)
+rs_packet_frpkt (struct rs_packet *pkt)
 {
   assert (pkt);
   return pkt->rpkt;
 }
 
 void
-rs_packet_destroy(struct rs_packet *pkt)
+rs_packet_destroy (struct rs_packet *pkt)
 {
   if (pkt)
     {