Enable tls psk
[radsecproxy.git] / conn.c
1 /* Copyright 2010-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 <string.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <assert.h>
12 #include <event2/event.h>
13 #include <event2/bufferevent.h>
14 #include <radsec/radsec.h>
15 #include <radsec/radsec-impl.h>
16 #include "debug.h"
17 #include "conn.h"
18 #include "event.h"
19 #include "packet.h"
20 #include "tcp.h"
21
22 int
23 conn_user_dispatch_p (const struct rs_connection *conn)
24 {
25   assert (conn);
26
27   return (conn->callbacks.connected_cb ||
28           conn->callbacks.disconnected_cb ||
29           conn->callbacks.received_cb ||
30           conn->callbacks.sent_cb);
31 }
32
33
34 int
35 conn_activate_timeout (struct rs_connection *conn)
36 {
37   assert (conn);
38   assert (conn->tev);
39   assert (conn->evb);
40   if (conn->timeout.tv_sec || conn->timeout.tv_usec)
41     {
42       rs_debug (("%s: activating timer: %d.%d\n", __func__,
43                  conn->timeout.tv_sec, conn->timeout.tv_usec));
44       if (evtimer_add (conn->tev, &conn->timeout))
45         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
46                                     "evtimer_add: %d", errno);
47     }
48   return RSE_OK;
49 }
50
51 int
52 conn_type_tls (const struct rs_connection *conn)
53 {
54   return conn->realm->type == RS_CONN_TYPE_TLS
55     || conn->realm->type == RS_CONN_TYPE_DTLS;
56 }
57
58 int
59 conn_cred_psk (const struct rs_connection *conn)
60 {
61   return conn->realm->transport_cred &&
62     conn->realm->transport_cred->type == RS_CRED_TLS_PSK;
63 }
64
65
66 /* Public functions. */
67 int
68 rs_conn_create (struct rs_context *ctx,
69                 struct rs_connection **conn,
70                 const char *config)
71 {
72   struct rs_connection *c;
73
74   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
75   if (!c)
76     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
77
78   memset (c, 0, sizeof(struct rs_connection));
79   c->ctx = ctx;
80   c->fd = -1;
81   if (config)
82     {
83       struct rs_realm *r = rs_conf_find_realm (ctx, config);
84       if (r)
85         {
86           struct rs_peer *p;
87
88           c->realm = r;
89           c->peers = r->peers;  /* FIXME: Copy instead?  */
90           for (p = c->peers; p; p = p->next)
91             p->conn = c;
92           c->timeout.tv_sec = r->timeout;
93           c->tryagain = r->retries;
94         }
95       else
96         {
97           c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
98           if (!c->realm)
99             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
100                                        NULL);
101           memset (c->realm, 0, sizeof (struct rs_realm));
102         }
103     }
104
105   if (conn)
106     *conn = c;
107   return RSE_OK;
108 }
109
110 void
111 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
112 {
113   assert (conn);
114   assert (conn->realm);
115   conn->realm->type = type;
116 }
117
118 int
119 rs_conn_add_listener (struct rs_connection *conn,
120                       rs_conn_type_t type,
121                       const char *hostname,
122                       int port)
123 {
124   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
125 }
126
127
128 int
129 rs_conn_disconnect (struct rs_connection *conn)
130 {
131   int err = 0;
132
133   assert (conn);
134
135   if (conn->is_connected)
136     event_on_disconnect (conn);
137
138   if (conn->bev)
139     {
140       bufferevent_free (conn->bev);
141       conn->bev = NULL;
142     }
143   if (conn->rev)
144     {
145       event_free (conn->rev);
146       conn->rev = NULL;
147     }
148   if (conn->wev)
149     {
150       event_free (conn->wev);
151       conn->wev = NULL;
152     }
153
154   err = evutil_closesocket (conn->fd);
155   conn->fd = -1;
156   return err;
157 }
158
159 int
160 rs_conn_destroy (struct rs_connection *conn)
161 {
162   int err = 0;
163
164   assert (conn);
165
166   /* NOTE: conn->realm is owned by context.  */
167   /* NOTE: conn->peers is owned by context.  */
168
169   if (conn->is_connected)
170     err = rs_conn_disconnect (conn);
171
172 #if defined (RS_ENABLE_TLS)
173   if (conn->tls_ssl) /* FIXME: Free SSL strucxt in rs_conn_disconnect?  */
174     SSL_free (conn->tls_ssl);
175   if (conn->tls_ctx)
176     SSL_CTX_free (conn->tls_ctx);
177 #endif
178
179   if (conn->tev)
180     event_free (conn->tev);
181   if (conn->bev)
182     bufferevent_free (conn->bev);
183   if (conn->rev)
184     event_free (conn->rev);
185   if (conn->wev)
186     event_free (conn->wev);
187   if (conn->evb)
188     event_base_free (conn->evb);
189
190   rs_free (conn->ctx, conn);
191
192   return err;
193 }
194
195 int
196 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
197 {
198   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
199 }
200
201 void
202 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
203 {
204   assert (conn);
205   memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
206 }
207
208 void
209 rs_conn_del_callbacks (struct rs_connection *conn)
210 {
211   assert (conn);
212   memset (&conn->callbacks, 0, sizeof (conn->callbacks));
213 }
214
215 struct rs_conn_callbacks *
216 rs_conn_get_callbacks(struct rs_connection *conn)
217 {
218   assert (conn);
219   return &conn->callbacks;
220 }
221
222 int
223 rs_conn_select_peer (struct rs_connection *conn, const char *name)
224 {
225   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
226 }
227
228 int
229 rs_conn_get_current_peer (struct rs_connection *conn,
230                           const char *name,
231                           size_t buflen)
232 {
233   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
234 }
235
236 int rs_conn_fd (struct rs_connection *conn)
237 {
238   assert (conn);
239   assert (conn->active_peer);
240   return conn->fd;
241 }
242
243 static void
244 _rcb (struct rs_packet *packet, void *user_data)
245 {
246   struct rs_packet *pkt = (struct rs_packet *) user_data;
247   assert (pkt);
248   assert (pkt->conn);
249
250   pkt->flags |= RS_PACKET_RECEIVED;
251   if (pkt->conn->bev)
252     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
253   else
254     event_del (pkt->conn->rev);
255 }
256
257 int
258 rs_conn_receive_packet (struct rs_connection *conn,
259                         struct rs_packet *req_msg,
260                         struct rs_packet **pkt_out)
261 {
262   int err = 0;
263   struct rs_packet *pkt = NULL;
264
265   assert (conn);
266   assert (conn->realm);
267   assert (!conn_user_dispatch_p (conn)); /* Blocking mode only.  */
268
269   if (rs_packet_create (conn, &pkt))
270     return -1;
271
272   assert (conn->evb);
273   assert (conn->fd >= 0);
274
275   conn->callbacks.received_cb = _rcb;
276   conn->user_data = pkt;
277   pkt->flags &= ~RS_PACKET_RECEIVED;
278
279   if (conn->bev)                /* TCP.  */
280     {
281       bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
282       bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
283       bufferevent_enable (conn->bev, EV_READ);
284     }
285   else                          /* UDP.  */
286     {
287       /* Put fresh packet in user_data for the callback and enable the
288          read event.  */
289       event_assign (conn->rev, conn->evb, event_get_fd (conn->rev),
290                     EV_READ, event_get_callback (conn->rev), pkt);
291       err = event_add (conn->rev, NULL);
292       if (err < 0)
293         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
294                                     "event_add: %s",
295                                     evutil_gai_strerror (err));
296
297       /* Activate retransmission timer.  */
298       conn_activate_timeout (pkt->conn);
299     }
300
301   rs_debug (("%s: entering event loop\n", __func__));
302   err = event_base_dispatch (conn->evb);
303   conn->callbacks.received_cb = NULL;
304   if (err < 0)
305     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
306                                 "event_base_dispatch: %s",
307                                 evutil_gai_strerror (err));
308   rs_debug (("%s: event loop done\n", __func__));
309
310   if ((pkt->flags & RS_PACKET_RECEIVED) != 0)
311     {
312       /* If the caller passed a request, check the response. */
313       if (req_msg)
314         err = packet_verify_response (pkt->conn, pkt, req_msg);
315
316       /* If the response was OK and the caller wants it, hand it
317          over, else free it. */
318       if (err == RSE_OK && pkt_out)
319         *pkt_out = pkt;
320       else
321         rs_packet_destroy (pkt);
322     }
323   else
324     err = rs_err_conn_peek_code (pkt->conn);
325
326   return err;
327 }
328
329 void
330 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)
331 {
332   assert (conn);
333   assert (tv);
334   conn->timeout = *tv;
335 }