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