5afba98be3c0daa02ddd22feca1ad045970f1f5e
[radsecproxy.git] / lib / event.c
1 /* Copyright 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 <assert.h>
9 #include <event2/event.h>
10 #include <event2/bufferevent.h>
11 #if defined (RS_ENABLE_TLS)
12 #include <event2/bufferevent_ssl.h>
13 #include <openssl/err.h>
14 #endif
15 #include <radsec/radsec.h>
16 #include <radsec/radsec-impl.h>
17 #include "tcp.h"
18 #include "udp.h"
19 #if defined (RS_ENABLE_TLS)
20 #include "tls.h"
21 #endif
22 #include "event.h"
23 #include "packet.h"
24 #include "conn.h"
25 #include "debug.h"
26
27 static void
28 _evlog_cb (int severity, const char *msg)
29 {
30   const char *sevstr;
31   switch (severity)
32     {
33     case _EVENT_LOG_DEBUG:
34 #if !defined (DEBUG_LEVENT)
35       return;
36 #endif
37       sevstr = "debug";
38       break;
39     case _EVENT_LOG_MSG:
40       sevstr = "msg";
41       break;
42     case _EVENT_LOG_WARN:
43       sevstr = "warn";
44       break;
45     case _EVENT_LOG_ERR:
46       sevstr = "err";
47       break;
48     default:
49       sevstr = "???";
50       break;
51     }
52   fprintf (stderr, "libevent: [%s] %s\n", sevstr, msg); /* FIXME: stderr?  */
53 }
54
55 void
56 event_conn_timeout_cb (int fd, short event, void *data)
57 {
58   struct rs_connection *conn = NULL;
59
60   assert (data);
61   conn = (struct rs_connection *) data;
62
63   if (event & EV_TIMEOUT)
64     {
65       rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
66                  __func__, conn, conn->fd, conn->active_peer));
67       conn->is_connecting = 0;
68       rs_err_conn_push_fl (conn, RSE_TIMEOUT_CONN, __FILE__, __LINE__, NULL);
69       event_loopbreak (conn);
70     }
71 }
72
73 void
74 event_retransmit_timeout_cb (int fd, short event, void *data)
75 {
76   struct rs_connection *conn = NULL;
77
78   assert (data);
79   conn = (struct rs_connection *) data;
80
81   if (event & EV_TIMEOUT)
82     {
83       rs_debug (("%s: retransmission timeout on %p (fd %d) sending to %p\n",
84                  __func__, conn, conn->fd, conn->active_peer));
85       rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
86       event_loopbreak (conn);
87     }
88 }
89
90 int
91 event_init_socket (struct rs_connection *conn, struct rs_peer *p)
92 {
93   if (conn->fd != -1)
94     return RSE_OK;
95
96   assert (p->addr);
97   conn->fd = socket (p->addr->ai_family, p->addr->ai_socktype,
98                      p->addr->ai_protocol);
99   if (conn->fd < 0)
100     return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
101                                 "socket: %d (%s)",
102                                 errno, strerror (errno));
103   if (evutil_make_socket_nonblocking (conn->fd) < 0)
104     {
105       evutil_closesocket (conn->fd);
106       conn->fd = -1;
107       return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
108                                   "evutil_make_socket_nonblocking: %d (%s)",
109                                   errno, strerror (errno));
110     }
111   return RSE_OK;
112 }
113
114 int
115 event_init_bufferevent (struct rs_connection *conn, struct rs_peer *peer)
116 {
117   if (conn->bev)
118     return RSE_OK;
119
120   if (conn->realm->type == RS_CONN_TYPE_TCP)
121     {
122       conn->bev = bufferevent_socket_new (conn->evb, conn->fd, 0);
123       if (!conn->bev)
124         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
125                                     "bufferevent_socket_new");
126     }
127 #if defined (RS_ENABLE_TLS)
128   else if (conn->realm->type == RS_CONN_TYPE_TLS)
129     {
130       if (rs_tls_init (conn))
131         return -1;
132       /* Would be convenient to pass BEV_OPT_CLOSE_ON_FREE but things
133          seem to break when be_openssl_ctrl() (in libevent) calls
134          SSL_set_bio() after BIO_new_socket() with flag=1.  */
135       conn->bev =
136         bufferevent_openssl_socket_new (conn->evb, conn->fd, conn->tls_ssl,
137                                         BUFFEREVENT_SSL_CONNECTING, 0);
138       if (!conn->bev)
139         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
140                                     "bufferevent_openssl_socket_new");
141     }
142 #endif  /* RS_ENABLE_TLS */
143   else
144     {
145       return rs_err_conn_push_fl (conn, RSE_INTERNAL, __FILE__, __LINE__,
146                                   "%s: unknown connection type: %d", __func__,
147                                   conn->realm->type);
148     }
149
150   return RSE_OK;
151 }
152
153 void
154 event_do_connect (struct rs_connection *conn)
155 {
156   struct rs_peer *p;
157   int err, sockerr;
158
159   assert (conn);
160   assert (conn->active_peer);
161   p = conn->active_peer;
162
163 #if defined (DEBUG)
164   {
165     char host[80], serv[80];
166
167     getnameinfo (p->addr->ai_addr,
168                  p->addr->ai_addrlen,
169                  host, sizeof(host), serv, sizeof(serv),
170                  0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
171     rs_debug (("%s: connecting to %s:%s\n", __func__, host, serv));
172   }
173 #endif
174
175   if (p->conn->bev)             /* TCP */
176     {
177       conn_activate_timeout (conn); /* Connect timeout.  */
178       err = bufferevent_socket_connect (p->conn->bev, p->addr->ai_addr,
179                                         p->addr->ai_addrlen);
180       if (err < 0)
181         rs_err_conn_push_fl (p->conn, RSE_EVENT, __FILE__, __LINE__,
182                              "bufferevent_socket_connect: %s",
183                              evutil_gai_strerror (err));
184       else
185         p->conn->is_connecting = 1;
186     }
187   else                          /* UDP */
188     {
189       err = connect (p->conn->fd, p->addr->ai_addr, p->addr->ai_addrlen);
190       if (err < 0)
191         {
192           sockerr = evutil_socket_geterror (p->conn->fd);
193           rs_debug (("%s: %d: connect: %d (%s)\n", __func__, p->conn->fd,
194                      sockerr, evutil_socket_error_to_string (sockerr)));
195           rs_err_conn_push_fl (p->conn, RSE_SOCKERR, __FILE__, __LINE__,
196                                "%d: connect: %d (%s)", p->conn->fd, sockerr,
197                                evutil_socket_error_to_string (sockerr));
198         }
199     }
200 }
201
202 int
203 event_loopbreak (struct rs_connection *conn)
204 {
205   int err = event_base_loopbreak (conn->evb);
206   if (err < 0)
207     rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
208                          "event_base_loopbreak: %s",
209                          evutil_gai_strerror (err));
210   return err;
211 }
212
213
214 void
215 event_on_disconnect (struct rs_connection *conn)
216 {
217   conn->is_connecting = 0;
218   conn->is_connected = 0;
219   rs_debug (("%s: %p disconnected\n", __func__, conn->active_peer));
220   if (conn->callbacks.disconnected_cb)
221     conn->callbacks.disconnected_cb (conn->user_data);
222 }
223
224 void
225 event_on_connect (struct rs_connection *conn, struct rs_packet *pkt)
226 {
227   assert (!conn->is_connecting);
228   conn->is_connected = 1;
229   rs_debug (("%s: %p connected\n", __func__, conn->active_peer));
230
231   if (conn->callbacks.connected_cb)
232     conn->callbacks.connected_cb (conn->user_data);
233
234   if (pkt)
235     packet_do_send (pkt);
236 }
237
238 int
239 event_init_eventbase (struct rs_connection *conn)
240 {
241   assert (conn);
242   if (conn->evb)
243     return RSE_OK;
244
245 #if defined (DEBUG)
246   event_enable_debug_mode ();
247 #endif
248   event_set_log_callback (_evlog_cb);
249   conn->evb = event_base_new ();
250   if (!conn->evb)
251     return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
252                                 "event_base_new");
253
254   return RSE_OK;
255 }