Enable tls psk
[radsecproxy.git] / send.c
1 /* Copyright 2011,2013 NORDUnet A/S. All rights reserved.
2    See LICENSE 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;
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       event_assign (conn->wev, conn->evb, event_get_fd (conn->wev),
107                     EV_WRITE, event_get_callback (conn->wev), pkt);
108       err = event_add (conn->wev, NULL);
109       if (err < 0)
110         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
111                                     "event_add: %s",
112                                     evutil_gai_strerror (err));
113     }
114
115   /* Do dispatch, unless the user wants to do it herself.  */
116   if (!conn_user_dispatch_p (conn))
117     {
118       conn->callbacks.sent_cb = _wcb;
119       conn->user_data = pkt;
120       rs_debug (("%s: entering event loop\n", __func__));
121       err = event_base_dispatch (conn->evb);
122       if (err < 0)
123         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
124                                     "event_base_dispatch: %s",
125                                     evutil_gai_strerror (err));
126       rs_debug (("%s: event loop done\n", __func__));
127       conn->callbacks.sent_cb = NULL;
128       conn->user_data = NULL;
129
130       if ((pkt->flags & RS_PACKET_SENT) == 0)
131         {
132           assert (rs_err_conn_peek_code (conn));
133           return rs_err_conn_peek_code (conn);
134         }
135     }
136
137   return RSE_OK;
138 }