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