0872471e78b1316add002aa6fd106d65c5509c54
[libradsec.git] / lib / send.c
1 /* Copyright 2011 NORDUnet A/S. All rights reserved.
2    See the file COPYING for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <assert.h>
9 #include <event2/event.h>
10 #include <event2/bufferevent.h>
11 #include <radsec/radsec.h>
12 #include <radsec/radsec-impl.h>
13 #include "debug.h"
14 #include "packet.h"
15 #include "event.h"
16 #include "peer.h"
17 #include "conn.h"
18 #include "tcp.h"
19 #include "udp.h"
20
21 static int
22 _conn_open (struct rs_connection *conn, struct rs_packet *pkt)
23 {
24   if (event_init_eventbase (conn))
25     return -1;
26
27   if (!conn->active_peer)
28     peer_pick_peer (conn);
29   if (!conn->active_peer)
30     return rs_err_conn_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
31
32   if (event_init_socket (conn, conn->active_peer))
33     return -1;
34
35   if (conn->realm->type == RS_CONN_TYPE_TCP
36       || conn->realm->type == RS_CONN_TYPE_TLS)
37     {
38       if (tcp_init_connect_timer (conn))
39         return -1;
40       if (event_init_bufferevent (conn, conn->active_peer))
41         return -1;
42     }
43   else
44     {
45       if (udp_init (conn, pkt))
46         return -1;
47       if (udp_init_retransmit_timer (conn))
48         return -1;
49     }
50
51   if (!conn->is_connected)
52     if (!conn->is_connecting)
53       event_do_connect (conn);
54
55   return RSE_OK;
56 }
57
58 static int
59 _conn_is_open_p (struct rs_connection *conn)
60 {
61   return conn->active_peer && conn->is_connected;
62 }
63
64 /* User callback used when we're dispatching for user.  */
65 static void
66 _wcb (void *user_data)
67 {
68   struct rs_packet *pkt = (struct rs_packet *) user_data;
69   assert (pkt);
70   pkt->flags |= rs_packet_sent_flag;
71   if (pkt->conn->bev)
72     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
73   else
74     event_del (pkt->conn->wev);
75 }
76
77 int
78 rs_packet_send (struct rs_packet *pkt, void *user_data)
79 {
80   struct rs_connection *conn = NULL;
81   int err = 0;
82
83   assert (pkt);
84   assert (pkt->conn);
85   conn = pkt->conn;
86
87   if (_conn_is_open_p (conn))
88     packet_do_send (pkt);
89   else
90     if (_conn_open (conn, pkt))
91       return -1;
92
93   assert (conn->evb);
94   assert (conn->active_peer);
95   assert (conn->fd >= 0);
96
97   conn->user_data = user_data;
98
99   if (conn->bev)                /* TCP */
100     {
101       bufferevent_setcb (conn->bev, NULL, tcp_write_cb, tcp_event_cb, pkt);
102       bufferevent_enable (conn->bev, EV_WRITE);
103     }
104   else                          /* UDP */
105     {
106       err = event_add (conn->wev, NULL);
107       if (err < 0)
108         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
109                                     "event_add: %s",
110                                     evutil_gai_strerror (err));
111     }
112
113   /* Do dispatch, unless the user wants to do it herself.  */
114   if (!conn_user_dispatch_p (conn))
115     {
116       conn->callbacks.sent_cb = _wcb;
117       conn->user_data = pkt;
118       rs_debug (("%s: entering event loop\n", __func__));
119       err = event_base_dispatch (conn->evb);
120       if (err < 0)
121         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
122                                     "event_base_dispatch: %s",
123                                     evutil_gai_strerror (err));
124       rs_debug (("%s: event loop done\n", __func__));
125       conn->callbacks.sent_cb = NULL;
126       conn->user_data = NULL;
127
128       if ((pkt->flags & rs_packet_sent_flag) == 0)
129         {
130           assert (rs_err_conn_peek_code (conn));
131           return rs_err_conn_peek_code (conn);
132         }
133     }
134
135   return RSE_OK;
136 }