Do free the connection object.
[radsecproxy.git] / lib / conn.c
1 /* Copyright 2010, 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 <string.h>
9 #include <assert.h>
10 #include <event2/event.h>
11 #include <event2/bufferevent.h>
12 #include <radsec/radsec.h>
13 #include <radsec/radsec-impl.h>
14 #include "debug.h"
15 #include "conn.h"
16 #include "event.h"
17 #include "packet.h"
18 #include "tcp.h"
19
20 int
21 conn_close (struct rs_connection **connp)
22 {
23   int r;
24   assert (connp);
25   assert (*connp);
26   r = rs_conn_destroy (*connp);
27   if (!r)
28     *connp = NULL;
29   return r;
30 }
31
32 int
33 conn_user_dispatch_p (const struct rs_connection *conn)
34 {
35   assert (conn);
36
37   return (conn->callbacks.connected_cb ||
38           conn->callbacks.disconnected_cb ||
39           conn->callbacks.received_cb ||
40           conn->callbacks.sent_cb);
41 }
42
43 int
44 rs_conn_create (struct rs_context *ctx, struct rs_connection **conn,
45                 const char *config)
46 {
47   struct rs_connection *c;
48
49   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
50   if (!c)
51     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
52
53   memset (c, 0, sizeof(struct rs_connection));
54   c->ctx = ctx;
55   c->fd = -1;
56   if (config)
57     {
58       struct rs_realm *r = rs_conf_find_realm (ctx, config);
59       if (r)
60         {
61           struct rs_peer *p;
62
63           c->realm = r;
64           c->peers = r->peers;  /* FIXME: Copy instead?  */
65           for (p = c->peers; p; p = p->next)
66             p->conn = c;
67           c->timeout.tv_sec = r->timeout;
68           c->tryagain = r->retries;
69         }
70       else
71         {
72           c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
73           if (!c->realm)
74             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
75                                        NULL);
76           memset (c->realm, 0, sizeof (struct rs_realm));
77         }
78     }
79
80   if (conn)
81     *conn = c;
82   return RSE_OK;
83 }
84
85 void
86 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
87 {
88   assert (conn);
89   assert (conn->realm);
90   conn->realm->type = type;
91 }
92
93 int
94 rs_conn_add_listener (struct rs_connection *conn, rs_conn_type_t type,
95                       const char *hostname, int port)
96 {
97   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
98 }
99
100
101 int
102 rs_conn_disconnect (struct rs_connection *conn)
103 {
104   int err = 0;
105
106   assert (conn);
107
108   err = evutil_closesocket (conn->fd);
109   conn->fd = -1;
110   return err;
111 }
112
113 int
114 rs_conn_destroy (struct rs_connection *conn)
115 {
116   int err = 0;
117
118   assert (conn);
119
120   /* NOTE: conn->realm is owned by context.  */
121   /* NOTE: conn->peers is owned by context.  */
122
123   if (conn->is_connected)
124     err = rs_conn_disconnect (conn);
125   if (conn->tev)
126     event_free (conn->tev);
127   if (conn->bev)
128     bufferevent_free (conn->bev);
129   if (conn->evb)
130     event_base_free (conn->evb);
131
132   /* TODO: free tls_ctx  */
133   /* TODO: free tls_ssl  */
134
135   rs_free (conn->ctx, conn);
136
137   return err;
138 }
139
140 int
141 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
142 {
143   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
144 }
145
146 void
147 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
148 {
149   assert (conn);
150   memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
151 }
152
153 void
154 rs_conn_del_callbacks (struct rs_connection *conn)
155 {
156   assert (conn);
157   memset (&conn->callbacks, 0, sizeof (conn->callbacks));
158 }
159
160 struct rs_conn_callbacks *
161 rs_conn_get_callbacks(struct rs_connection *conn)
162 {
163   assert (conn);
164   return &conn->callbacks;
165 }
166
167 int
168 rs_conn_select_peer (struct rs_connection *conn, const char *name)
169 {
170   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
171 }
172
173 int
174 rs_conn_get_current_peer (struct rs_connection *conn, const char *name,
175                           size_t buflen)
176 {
177   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
178 }
179
180 int rs_conn_fd (struct rs_connection *conn)
181 {
182   assert (conn);
183   assert (conn->active_peer);
184   return conn->fd;
185 }
186
187 static void
188 _rcb (struct rs_packet *packet, void *user_data)
189 {
190   struct rs_packet *pkt = (struct rs_packet *) user_data;
191   assert (pkt);
192   assert (pkt->conn);
193
194   pkt->flags |= rs_packet_received_flag;
195   if (pkt->conn->bev)
196     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
197   else
198     event_del (pkt->conn->rev);
199 }
200
201 /* Special function used in libradsec blocking dispatching mode,
202    i.e. with socket set to block on read/write and with no libradsec
203    callbacks registered.
204
205    For any other use of libradsec, a the received_cb callback should
206    be registered in the callbacks member of struct rs_connection.
207
208    On successful reception of a RADIUS message it will be verified
209    against REQ_MSG, if !NULL.
210
211    If PKT_OUT is !NULL it will upon return point at a pointer to a
212    struct rs_packet containing the message.
213
214    If anything goes wrong or if the read times out (TODO: explain),
215    PKT_OUT will not be changed and one or more errors are pushed on
216    the connection (available through rs_err_conn_pop()).  */
217 int
218 rs_conn_receive_packet (struct rs_connection *conn,
219                         struct rs_packet *req_msg,
220                         struct rs_packet **pkt_out)
221 {
222   int err = 0;
223   struct rs_packet *pkt = NULL;
224
225   assert (conn);
226   assert (conn->realm);
227   assert (!conn_user_dispatch_p (conn)); /* Dispatching mode only.  */
228
229   if (rs_packet_create (conn, &pkt))
230     return -1;
231
232   assert (conn->evb);
233   assert (conn->fd >= 0);
234
235   conn->callbacks.received_cb = _rcb;
236   conn->user_data = pkt;
237   pkt->flags &= ~rs_packet_received_flag;
238
239   if (conn->bev)                /* TCP.  */
240     {
241       bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
242       bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
243       bufferevent_enable (conn->bev, EV_READ);
244     }
245   else                          /* UDP.  */
246     {
247       /* Put fresh packet in user_data for the callback and enable the
248          read event.  */
249       event_assign (conn->rev, conn->evb, event_get_fd (conn->rev),
250                     EV_READ, event_get_callback (conn->rev), pkt);
251       err = event_add (conn->rev, NULL);
252       if (err < 0)
253         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
254                                     "event_add: %s",
255                                     evutil_gai_strerror (err));
256
257       /* Activae retransmission timer.  */
258       conn_activate_timeout (pkt->conn);
259     }
260
261   rs_debug (("%s: entering event loop\n", __func__));
262   err = event_base_dispatch (conn->evb);
263   conn->callbacks.received_cb = NULL;
264   if (err < 0)
265     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
266                                 "event_base_dispatch: %s",
267                                 evutil_gai_strerror (err));
268   rs_debug (("%s: event loop done\n", __func__));
269
270   if ((pkt->flags & rs_packet_received_flag) == 0
271       || (req_msg
272           && packet_verify_response (pkt->conn, pkt, req_msg) != RSE_OK))
273     {
274       assert (rs_err_conn_peek_code (pkt->conn));
275       return rs_err_conn_peek_code (conn);
276     }
277
278   if (pkt_out)
279     *pkt_out = pkt;
280   return RSE_OK;
281 }
282
283 void
284 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)
285 {
286   assert (conn);
287   assert (tv);
288   conn->timeout = *tv;
289 }
290
291 int
292 conn_activate_timeout (struct rs_connection *conn)
293 {
294   assert (conn);
295   assert (conn->tev);
296   assert (conn->evb);
297   if (conn->timeout.tv_sec || conn->timeout.tv_usec)
298     {
299       rs_debug (("%s: activating timer: %d.%d\n", __func__,
300                  conn->timeout.tv_sec, conn->timeout.tv_usec));
301       if (evtimer_add (conn->tev, &conn->timeout))
302         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
303                                     "evtimer_add: %d", errno);
304     }
305   return RSE_OK;
306 }