3981f6a10714eaa35872634beba61cb55e0e687e
[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   return err;
136 }
137
138 int
139 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
140 {
141   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
142 }
143
144 void
145 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
146 {
147   assert (conn);
148   memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
149 }
150
151 void
152 rs_conn_del_callbacks (struct rs_connection *conn)
153 {
154   assert (conn);
155   memset (&conn->callbacks, 0, sizeof (conn->callbacks));
156 }
157
158 struct rs_conn_callbacks *
159 rs_conn_get_callbacks(struct rs_connection *conn)
160 {
161   assert (conn);
162   return &conn->callbacks;
163 }
164
165 int
166 rs_conn_select_peer (struct rs_connection *conn, const char *name)
167 {
168   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
169 }
170
171 int
172 rs_conn_get_current_peer (struct rs_connection *conn, const char *name,
173                           size_t buflen)
174 {
175   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
176 }
177
178 int rs_conn_fd (struct rs_connection *conn)
179 {
180   assert (conn);
181   assert (conn->active_peer);
182   return conn->fd;
183 }
184
185 static void
186 _rcb (struct rs_packet *packet, void *user_data)
187 {
188   struct rs_packet *pkt = (struct rs_packet *) user_data;
189   assert (pkt);
190   assert (pkt->conn);
191
192   pkt->flags |= rs_packet_received_flag;
193   if (pkt->conn->bev)
194     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
195   else
196     event_del (pkt->conn->rev);
197 }
198
199 /* Special function used in libradsec blocking dispatching mode,
200    i.e. with socket set to block on read/write and with no libradsec
201    callbacks registered.
202
203    For any other use of libradsec, a the received_cb callback should
204    be registered in the callbacks member of struct rs_connection.
205
206    On successful reception of a RADIUS message it will be verified
207    against REQ_MSG, if !NULL.
208
209    If PKT_OUT is !NULL it will upon return point at a pointer to a
210    struct rs_packet containing the message.
211
212    If anything goes wrong or if the read times out (TODO: explain),
213    PKT_OUT will not be changed and one or more errors are pushed on
214    the connection (available through rs_err_conn_pop()).  */
215 int
216 rs_conn_receive_packet (struct rs_connection *conn,
217                         struct rs_packet *req_msg,
218                         struct rs_packet **pkt_out)
219 {
220   int err = 0;
221   struct rs_packet *pkt = NULL;
222
223   assert (conn);
224   assert (conn->realm);
225   assert (!conn_user_dispatch_p (conn)); /* Dispatching mode only.  */
226
227   if (rs_packet_create (conn, &pkt))
228     return -1;
229   pkt->conn = conn;
230
231   assert (conn->evb);
232   assert (conn->bev);
233   assert (conn->active_peer);
234   assert (conn->fd >= 0);
235
236   conn->callbacks.received_cb = _rcb;
237   conn->user_data = pkt;
238   pkt->flags &= ~rs_packet_received_flag;
239
240   if (conn->bev)
241     {
242       bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
243       bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
244       bufferevent_enable (conn->bev, EV_READ);
245     }
246   else
247     {
248       err = event_add (conn->rev, NULL);
249       if (err < 0)
250         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
251                                     "event_add: %s",
252                                     evutil_gai_strerror (err));
253     }
254
255
256   rs_debug (("%s: entering event loop\n", __func__));
257   err = event_base_dispatch (conn->evb);
258   conn->callbacks.received_cb = NULL;
259   if (err < 0)
260     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
261                                 "event_base_dispatch: %s",
262                                 evutil_gai_strerror (err));
263   rs_debug (("%s: event loop done\n", __func__));
264
265   if ((pkt->flags & rs_packet_received_flag) == 0
266       || (req_msg
267           && packet_verify_response (pkt->conn, pkt, req_msg) != RSE_OK))
268     {
269       assert (rs_err_conn_peek_code (pkt->conn));
270       return rs_err_conn_peek_code (conn);
271     }
272
273   if (pkt_out)
274     *pkt_out = pkt;
275   return RSE_OK;
276 }
277
278 void
279 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)
280 {
281   assert (conn);
282   assert (tv);
283   conn->timeout = *tv;
284 }
285
286 int
287 conn_activate_timeout (struct rs_connection *conn)
288 {
289   assert (conn);
290   assert (conn->tev);
291   assert (conn->evb);
292   if (conn->timeout.tv_sec || conn->timeout.tv_usec)
293     {
294       rs_debug (("%s: activating timer: %d.%d\n", __func__,
295                  conn->timeout.tv_sec, conn->timeout.tv_usec));
296       if (evtimer_add (conn->tev, &conn->timeout))
297         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
298                                     "evtimer_add: %d", errno);
299     }
300   return RSE_OK;
301 }