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