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