Don't touch TLS specific things w/o RS_ENABLE_TLS.
[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 = 0;
24   assert (connp);
25   assert (*connp);
26   if ((*connp)->is_connected)
27     r = rs_conn_disconnect (*connp);
28   if (r == RSE_OK)
29     *connp = NULL;
30   return r;
31 }
32
33 int
34 conn_user_dispatch_p (const struct rs_connection *conn)
35 {
36   assert (conn);
37
38   return (conn->callbacks.connected_cb ||
39           conn->callbacks.disconnected_cb ||
40           conn->callbacks.received_cb ||
41           conn->callbacks.sent_cb);
42 }
43
44 int
45 rs_conn_create (struct rs_context *ctx,
46                 struct rs_connection **conn,
47                 const char *config)
48 {
49   struct rs_connection *c;
50
51   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
52   if (!c)
53     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
54
55   memset (c, 0, sizeof(struct rs_connection));
56   c->ctx = ctx;
57   c->fd = -1;
58   if (config)
59     {
60       struct rs_realm *r = rs_conf_find_realm (ctx, config);
61       if (r)
62         {
63           struct rs_peer *p;
64
65           c->realm = r;
66           c->peers = r->peers;  /* FIXME: Copy instead?  */
67           for (p = c->peers; p; p = p->next)
68             p->conn = c;
69           c->timeout.tv_sec = r->timeout;
70           c->tryagain = r->retries;
71         }
72       else
73         {
74           c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
75           if (!c->realm)
76             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
77                                        NULL);
78           memset (c->realm, 0, sizeof (struct rs_realm));
79         }
80     }
81
82   if (conn)
83     *conn = c;
84   return RSE_OK;
85 }
86
87 void
88 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
89 {
90   assert (conn);
91   assert (conn->realm);
92   conn->realm->type = type;
93 }
94
95 int
96 rs_conn_add_listener (struct rs_connection *conn,
97                       rs_conn_type_t type,
98                       const char *hostname,
99                       int port)
100 {
101   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
102 }
103
104
105 int
106 rs_conn_disconnect (struct rs_connection *conn)
107 {
108   int err = 0;
109
110   assert (conn);
111
112   err = evutil_closesocket (conn->fd);
113   conn->fd = -1;
114   return err;
115 }
116
117 int
118 rs_conn_destroy (struct rs_connection *conn)
119 {
120   int err = 0;
121
122   assert (conn);
123
124   /* NOTE: conn->realm is owned by context.  */
125   /* NOTE: conn->peers is owned by context.  */
126
127   if (conn->is_connected)
128     err = rs_conn_disconnect (conn);
129
130 #if defined (RS_ENABLE_TLS)
131   if (conn->tls_ssl) /* FIXME: Free SSL strucxt in rs_conn_disconnect?  */
132     SSL_free (conn->tls_ssl);
133   if (conn->tls_ctx)
134     SSL_CTX_free (conn->tls_ctx);
135 #endif
136
137   if (conn->tev)
138     event_free (conn->tev);
139   if (conn->bev)
140     bufferevent_free (conn->bev);
141   if (conn->rev)
142     event_free (conn->rev);
143   if (conn->wev)
144     event_free (conn->wev);
145   if (conn->evb)
146     event_base_free (conn->evb);
147
148   rs_free (conn->ctx, conn);
149
150   return err;
151 }
152
153 int
154 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
155 {
156   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
157 }
158
159 void
160 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
161 {
162   assert (conn);
163   memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
164 }
165
166 void
167 rs_conn_del_callbacks (struct rs_connection *conn)
168 {
169   assert (conn);
170   memset (&conn->callbacks, 0, sizeof (conn->callbacks));
171 }
172
173 struct rs_conn_callbacks *
174 rs_conn_get_callbacks(struct rs_connection *conn)
175 {
176   assert (conn);
177   return &conn->callbacks;
178 }
179
180 int
181 rs_conn_select_peer (struct rs_connection *conn, const char *name)
182 {
183   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
184 }
185
186 int
187 rs_conn_get_current_peer (struct rs_connection *conn,
188                           const char *name,
189                           size_t buflen)
190 {
191   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
192 }
193
194 int rs_conn_fd (struct rs_connection *conn)
195 {
196   assert (conn);
197   assert (conn->active_peer);
198   return conn->fd;
199 }
200
201 static void
202 _rcb (struct rs_packet *packet, void *user_data)
203 {
204   struct rs_packet *pkt = (struct rs_packet *) user_data;
205   assert (pkt);
206   assert (pkt->conn);
207
208   pkt->flags |= rs_packet_received_flag;
209   if (pkt->conn->bev)
210     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
211   else
212     event_del (pkt->conn->rev);
213 }
214
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
230   assert (conn->evb);
231   assert (conn->fd >= 0);
232
233   conn->callbacks.received_cb = _rcb;
234   conn->user_data = pkt;
235   pkt->flags &= ~rs_packet_received_flag;
236
237   if (conn->bev)                /* TCP.  */
238     {
239       bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
240       bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
241       bufferevent_enable (conn->bev, EV_READ);
242     }
243   else                          /* UDP.  */
244     {
245       /* Put fresh packet in user_data for the callback and enable the
246          read event.  */
247       event_assign (conn->rev, conn->evb, event_get_fd (conn->rev),
248                     EV_READ, event_get_callback (conn->rev), pkt);
249       err = event_add (conn->rev, NULL);
250       if (err < 0)
251         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
252                                     "event_add: %s",
253                                     evutil_gai_strerror (err));
254
255       /* Activae retransmission timer.  */
256       conn_activate_timeout (pkt->conn);
257     }
258
259   rs_debug (("%s: entering event loop\n", __func__));
260   err = event_base_dispatch (conn->evb);
261   conn->callbacks.received_cb = NULL;
262   if (err < 0)
263     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
264                                 "event_base_dispatch: %s",
265                                 evutil_gai_strerror (err));
266   rs_debug (("%s: event loop done\n", __func__));
267
268   if ((pkt->flags & rs_packet_received_flag) == 0
269       || (req_msg
270           && packet_verify_response (pkt->conn, pkt, req_msg) != RSE_OK))
271     {
272       assert (rs_err_conn_peek_code (pkt->conn));
273       return rs_err_conn_peek_code (conn);
274     }
275
276   if (pkt_out)
277     *pkt_out = pkt;
278   return RSE_OK;
279 }
280
281 void
282 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)
283 {
284   assert (conn);
285   assert (tv);
286   conn->timeout = *tv;
287 }
288
289 int
290 conn_activate_timeout (struct rs_connection *conn)
291 {
292   assert (conn);
293   assert (conn->tev);
294   assert (conn->evb);
295   if (conn->timeout.tv_sec || conn->timeout.tv_usec)
296     {
297       rs_debug (("%s: activating timer: %d.%d\n", __func__,
298                  conn->timeout.tv_sec, conn->timeout.tv_usec));
299       if (evtimer_add (conn->tev, &conn->timeout))
300         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
301                                     "evtimer_add: %d", errno);
302     }
303   return RSE_OK;
304 }