summaryrefslogtreecommitdiff
path: root/lib/send.c
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2011-03-06 15:53:58 +0100
committerLinus Nordberg <linus@nordu.net>2011-03-06 15:53:58 +0100
commit5c60297a1eaab7b10d6f584ba329493a41b812d0 (patch)
treeec1eef1d6adb859ef94e5aeeff8f190d35e63215 /lib/send.c
parent7636505962a348d9564e53922834dc6df1274653 (diff)
Restructure code, moving most code out of packet.c
Also, move copyright notice out of COPYING and into every file.
Diffstat (limited to 'lib/send.c')
-rw-r--r--lib/send.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/lib/send.c b/lib/send.c
new file mode 100644
index 0000000..c49eaa9
--- /dev/null
+++ b/lib/send.c
@@ -0,0 +1,128 @@
+/* Copyright 2011 NORDUnet A/S. All rights reserved.
+ See the file COPYING for licensing information. */
+
+#if defined HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <event2/event.h>
+#include <event2/bufferevent.h>
+#include <radsec/radsec.h>
+#include <radsec/radsec-impl.h>
+#include "debug.h"
+#include "packet.h"
+#include "event.h"
+#include "peer.h"
+#include "tcp.h"
+#include "udp.h"
+
+static int
+_conn_open (struct rs_connection *conn, struct rs_packet *pkt)
+{
+ if (event_init_eventbase (conn))
+ return -1;
+
+ if (!conn->active_peer)
+ peer_pick_peer (conn);
+ if (!conn->active_peer)
+ return rs_err_conn_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
+
+ if (event_init_socket (conn, conn->active_peer))
+ return -1;
+
+ if (conn->realm->type == RS_CONN_TYPE_TCP
+ || conn->realm->type == RS_CONN_TYPE_TLS)
+ {
+ if (event_init_bufferevent (conn, conn->active_peer))
+ return -1;
+ }
+ else
+ {
+ if (udp_init (conn, pkt))
+ return -1;
+ }
+
+ if (!conn->is_connected)
+ if (!conn->is_connecting)
+ event_do_connect (conn);
+
+ return RSE_OK;
+}
+
+static int
+_conn_is_open_p (struct rs_connection *conn)
+{
+ return conn->active_peer && conn->is_connected;
+}
+
+/* User callback used when we're dispatching for user. */
+static void
+_wcb (void *user_data)
+{
+ struct rs_packet *pkt = (struct rs_packet *) user_data;
+ assert (pkt);
+ pkt->written_flag = 1;
+ if (pkt->conn->bev)
+ bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
+ else
+ event_del (pkt->conn->wev);
+}
+
+int
+rs_packet_send (struct rs_packet *pkt, void *user_data)
+{
+ struct rs_connection *conn = NULL;
+ int err = 0;
+
+ assert (pkt);
+ assert (pkt->conn);
+ conn = pkt->conn;
+
+ if (_conn_is_open_p (conn))
+ packet_do_send (pkt);
+ else
+ if (_conn_open (conn, pkt))
+ return -1;
+
+ assert (conn->evb);
+ assert (conn->active_peer);
+ assert (conn->fd >= 0);
+
+ conn->user_data = user_data;
+
+ if (conn->bev) /* TCP */
+ {
+ bufferevent_setcb (conn->bev, NULL, tcp_write_cb, tcp_event_cb, pkt);
+ bufferevent_enable (conn->bev, EV_WRITE);
+ }
+ else /* UDP */
+ {
+ err = event_add (conn->wev, NULL);
+ if (err < 0)
+ return rs_err_conn_push_fl (pkt->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)
+ {
+ 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__,
+ "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;
+ }
+
+ return RSE_OK;
+}