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