Rename COPYING -> LICENSE.
[libradsec.git] / lib / send.c
index c49eaa9..6b887c5 100644 (file)
@@ -1,5 +1,5 @@
 /* Copyright 2011 NORDUnet A/S. All rights reserved.
-   See the file COPYING for licensing information.  */
+   See LICENSE for licensing information.  */
 
 #if defined HAVE_CONFIG_H
 #include <config.h>
@@ -14,6 +14,7 @@
 #include "packet.h"
 #include "event.h"
 #include "peer.h"
+#include "conn.h"
 #include "tcp.h"
 #include "udp.h"
 
@@ -34,6 +35,8 @@ _conn_open (struct rs_connection *conn, struct rs_packet *pkt)
   if (conn->realm->type == RS_CONN_TYPE_TCP
       || conn->realm->type == RS_CONN_TYPE_TLS)
     {
+      if (tcp_init_connect_timer (conn))
+       return -1;
       if (event_init_bufferevent (conn, conn->active_peer))
        return -1;
     }
@@ -41,6 +44,8 @@ _conn_open (struct rs_connection *conn, struct rs_packet *pkt)
     {
       if (udp_init (conn, pkt))
        return -1;
+      if (udp_init_retransmit_timer (conn))
+       return -1;
     }
 
   if (!conn->is_connected)
@@ -62,7 +67,7 @@ _wcb (void *user_data)
 {
   struct rs_packet *pkt = (struct rs_packet *) user_data;
   assert (pkt);
-  pkt->written_flag = 1;
+  pkt->flags |= rs_packet_sent_flag;
   if (pkt->conn->bev)
     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
   else
@@ -96,32 +101,37 @@ rs_packet_send (struct rs_packet *pkt, void *user_data)
       bufferevent_setcb (conn->bev, NULL, tcp_write_cb, tcp_event_cb, pkt);
       bufferevent_enable (conn->bev, EV_WRITE);
     }
-  else                 /* UDP */
+  else                         /* UDP */
     {
+      event_assign (conn->wev, conn->evb, event_get_fd (conn->wev),
+                   EV_WRITE, event_get_callback (conn->wev), pkt);
       err = event_add (conn->wev, NULL);
       if (err < 0)
-       return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
+       return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
                                    "event_add: %s",
                                    evutil_gai_strerror (err));
     }
 
   /* Do dispatch, unless the user wants to do it herself.  */
-  if (!conn->user_dispatch_flag)
+  if (!conn_user_dispatch_p (conn))
     {
       conn->callbacks.sent_cb = _wcb;
       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__,
+       return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
                                    "event_base_dispatch: %s",
                                    evutil_gai_strerror (err));
       rs_debug (("%s: event loop done\n", __func__));
       conn->callbacks.sent_cb = NULL;
       conn->user_data = NULL;
 
-      if (!pkt->written_flag)
-       return -1;
+      if ((pkt->flags & rs_packet_sent_flag) == 0)
+       {
+         assert (rs_err_conn_peek_code (conn));
+         return rs_err_conn_peek_code (conn);
+       }
     }
 
   return RSE_OK;