Timeout implemented in request objects, supported by TCP.
[libradsec.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 <debug.h>
11 #include <event2/event.h>
12 #include <event2/bufferevent.h>
13 #include <radsec/radsec.h>
14 #include <radsec/radsec-impl.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
94 struct rs_error *          /* FIXME: Return int as all the others?  */
95 _rs_resolv (struct evutil_addrinfo **addr, rs_conn_type_t type,
96             const char *hostname, const char *service)
97 {
98   int err;
99   struct evutil_addrinfo hints, *res = NULL;
100
101   memset (&hints, 0, sizeof(struct evutil_addrinfo));
102   hints.ai_family = AF_INET;   /* IPv4 only.  TODO: Set AF_UNSPEC.  */
103   hints.ai_flags = AI_ADDRCONFIG;
104   switch (type)
105     {
106     case RS_CONN_TYPE_NONE:
107       return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
108     case RS_CONN_TYPE_TCP:
109       /* Fall through.  */
110     case RS_CONN_TYPE_TLS:
111       hints.ai_socktype = SOCK_STREAM;
112       hints.ai_protocol = IPPROTO_TCP;
113       break;
114     case RS_CONN_TYPE_UDP:
115       /* Fall through.  */
116     case RS_CONN_TYPE_DTLS:
117       hints.ai_socktype = SOCK_DGRAM;
118       hints.ai_protocol = IPPROTO_UDP;
119       break;
120     default:
121       return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
122     }
123   err = evutil_getaddrinfo (hostname, service, &hints, &res);
124   if (err)
125     return _rs_err_create (RSE_BADADDR, __FILE__, __LINE__,
126                            "%s:%s: bad host name or service name (%s)",
127                            hostname, service, evutil_gai_strerror(err));
128   *addr = res;                  /* Simply use first result.  */
129   return NULL;
130 }
131
132 int
133 rs_conn_add_listener (struct rs_connection *conn, rs_conn_type_t type,
134                       const char *hostname, int port)
135 {
136   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
137 }
138
139
140 int
141 rs_conn_disconnect (struct rs_connection *conn)
142 {
143   int err = 0;
144
145   assert (conn);
146
147   err = evutil_closesocket (conn->fd);
148   conn->fd = -1;
149   return err;
150 }
151
152 int
153 rs_conn_destroy (struct rs_connection *conn)
154 {
155   int err = 0;
156
157   assert (conn);
158
159   /* NOTE: conn->realm is owned by context.  */
160   /* NOTE: conn->peers is owned by context.  */
161
162   if (conn->is_connected)
163     err = rs_conn_disconnect (conn);
164   if (conn->tev)
165     event_free (conn->tev);
166   if (conn->bev)
167     bufferevent_free (conn->bev);
168   if (conn->evb)
169     event_base_free (conn->evb);
170
171   /* TODO: free tls_ctx  */
172   /* TODO: free tls_ssl  */
173
174   return err;
175 }
176
177 int
178 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
179 {
180   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
181 }
182
183 void
184 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
185 {
186   assert (conn);
187   memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
188 }
189
190 void
191 rs_conn_del_callbacks (struct rs_connection *conn)
192 {
193   assert (conn);
194   memset (&conn->callbacks, 0, sizeof (conn->callbacks));
195 }
196
197 struct rs_conn_callbacks *
198 rs_conn_get_callbacks(struct rs_connection *conn)
199 {
200   assert (conn);
201   return &conn->callbacks;
202 }
203
204 int
205 rs_conn_select_peer (struct rs_connection *conn, const char *name)
206 {
207   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
208 }
209
210 int
211 rs_conn_get_current_peer (struct rs_connection *conn, const char *name,
212                           size_t buflen)
213 {
214   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
215 }
216
217 int rs_conn_fd (struct rs_connection *conn)
218 {
219   assert (conn);
220   assert (conn->active_peer);
221   return conn->fd;
222 }
223
224 static void
225 _rcb (struct rs_packet *packet, void *user_data)
226 {
227   struct rs_packet *pkt = (struct rs_packet *) user_data;
228   assert (pkt);
229   assert (pkt->conn);
230
231   pkt->flags |= rs_packet_received_flag;
232   if (pkt->conn->bev)
233     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
234   else
235     event_del (pkt->conn->rev);
236 }
237
238 /* Special function used in libradsec blocking dispatching mode,
239    i.e. with socket set to block on read/write and with no libradsec
240    callbacks registered.
241
242    For any other use of libradsec, a the received_cb callback should
243    be registered in the callbacks member of struct rs_connection.
244
245    On successful reception of a RADIUS message it will be verified
246    against REQ_MSG, if !NULL.
247
248    If PKT_OUT is !NULL it will upon return point at a pointer to a
249    struct rs_packet containing the message.
250
251    If anything goes wrong or if the read times out (TODO: explain),
252    PKT_OUT will not be changed and one or more errors are pushed on
253    the connection (available through rs_err_conn_pop()).  */
254 int
255 rs_conn_receive_packet (struct rs_connection *conn,
256                         struct rs_packet *req_msg,
257                         struct rs_packet **pkt_out)
258 {
259   int err = 0;
260   struct rs_packet *pkt = NULL;
261
262   assert (conn);
263   assert (conn->realm);
264   assert (!conn_user_dispatch_p (conn)); /* Dispatching mode only.  */
265
266   if (rs_packet_create (conn, &pkt))
267     return -1;
268   pkt->conn = conn;
269
270   assert (conn->evb);
271   assert (conn->bev);
272   assert (conn->active_peer);
273   assert (conn->fd >= 0);
274
275   conn->callbacks.received_cb = _rcb;
276   conn->user_data = pkt;
277   pkt->flags &= ~rs_packet_received_flag;
278
279   if (conn->bev)
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
286     {
287       err = event_add (conn->rev, NULL);
288       if (err < 0)
289         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
290                                     "event_add: %s",
291                                     evutil_gai_strerror (err));
292     }
293
294
295   rs_debug (("%s: entering event loop\n", __func__));
296   err = event_base_dispatch (conn->evb);
297   conn->callbacks.received_cb = NULL;
298   if (err < 0)
299     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
300                                 "event_base_dispatch: %s",
301                                 evutil_gai_strerror (err));
302   rs_debug (("%s: event loop done\n", __func__));
303
304   if ((pkt->flags & rs_packet_received_flag) == 0
305       || (req_msg
306           && packet_verify_response (pkt->conn, pkt, req_msg) != RSE_OK))
307     {
308       assert (rs_err_conn_peek_code (pkt->conn));
309       return rs_err_conn_peek_code (conn);
310     }
311
312   if (pkt_out)
313     *pkt_out = pkt;
314   return RSE_OK;
315 }
316
317 void
318 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)
319 {
320   assert (conn);
321   assert (tv);
322   conn->timeout = *tv;
323 }