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