1 /* Copyright 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
12 #include <event2/event.h>
13 #include <event2/bufferevent.h>
14 #if defined (RS_ENABLE_TLS)
15 #include <event2/bufferevent_ssl.h>
16 #include <openssl/err.h>
18 #include <radsec/radsec.h>
19 #include <radsec/radsec-impl.h>
22 #if defined (RS_ENABLE_TLS)
33 extern int _event_debug_mode_on;
37 _evlog_cb (int severity, const char *msg)
42 case _EVENT_LOG_DEBUG:
43 #if !defined (DEBUG_LEVENT)
61 fprintf (stderr, "libevent: [%s] %s\n", sevstr, msg); /* FIXME: stderr? */
65 event_conn_timeout_cb (int fd, short event, void *data)
67 struct rs_connection *conn = NULL;
70 conn = (struct rs_connection *) data;
72 if (event & EV_TIMEOUT)
74 rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
75 __func__, conn, conn->fd, conn->active_peer));
76 conn->is_connecting = 0;
77 rs_err_conn_push_fl (conn, RSE_TIMEOUT_CONN, __FILE__, __LINE__, NULL);
78 event_loopbreak (conn);
83 event_retransmit_timeout_cb (int fd, short event, void *data)
85 struct rs_connection *conn = NULL;
88 conn = (struct rs_connection *) data;
90 if (event & EV_TIMEOUT)
92 rs_debug (("%s: retransmission timeout on %p (fd %d) sending to %p\n",
93 __func__, conn, conn->fd, conn->active_peer));
94 rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
95 event_loopbreak (conn);
100 event_init_socket (struct rs_connection *conn, struct rs_peer *p)
105 if (p->addr_cache == NULL)
107 struct rs_error *err =
108 rs_resolve (&p->addr_cache, p->realm->type, p->hostname, p->service);
110 return err_conn_push_err (conn, err);
113 conn->fd = socket (p->addr_cache->ai_family, p->addr_cache->ai_socktype,
114 p->addr_cache->ai_protocol);
116 return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
118 errno, strerror (errno));
119 if (evutil_make_socket_nonblocking (conn->fd) < 0)
121 evutil_closesocket (conn->fd);
123 return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
124 "evutil_make_socket_nonblocking: %d (%s)",
125 errno, strerror (errno));
131 event_init_bufferevent (struct rs_connection *conn, struct rs_peer *peer)
136 if (conn->realm->type == RS_CONN_TYPE_TCP)
138 conn->bev = bufferevent_socket_new (conn->evb, conn->fd, 0);
140 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
141 "bufferevent_socket_new");
143 #if defined (RS_ENABLE_TLS)
144 else if (conn->realm->type == RS_CONN_TYPE_TLS)
146 if (rs_tls_init (conn))
148 /* Would be convenient to pass BEV_OPT_CLOSE_ON_FREE but things
149 seem to break when be_openssl_ctrl() (in libevent) calls
150 SSL_set_bio() after BIO_new_socket() with flag=1. */
152 bufferevent_openssl_socket_new (conn->evb, conn->fd, conn->tls_ssl,
153 BUFFEREVENT_SSL_CONNECTING, 0);
155 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
156 "bufferevent_openssl_socket_new");
158 #endif /* RS_ENABLE_TLS */
161 return rs_err_conn_push_fl (conn, RSE_INTERNAL, __FILE__, __LINE__,
162 "%s: unknown connection type: %d", __func__,
170 event_do_connect (struct rs_connection *conn)
176 assert (conn->active_peer);
177 p = conn->active_peer;
181 char host[80], serv[80];
183 getnameinfo (p->addr_cache->ai_addr,
184 p->addr_cache->ai_addrlen,
185 host, sizeof(host), serv, sizeof(serv),
186 0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
187 rs_debug (("%s: connecting to %s:%s\n", __func__, host, serv));
191 if (p->conn->bev) /* TCP */
193 conn_activate_timeout (conn); /* Connect timeout. */
194 err = bufferevent_socket_connect (p->conn->bev, p->addr_cache->ai_addr,
195 p->addr_cache->ai_addrlen);
197 rs_err_conn_push_fl (p->conn, RSE_EVENT, __FILE__, __LINE__,
198 "bufferevent_socket_connect: %s",
199 evutil_gai_strerror (err));
201 p->conn->is_connecting = 1;
205 err = connect (p->conn->fd,
206 p->addr_cache->ai_addr,
207 p->addr_cache->ai_addrlen);
210 sockerr = evutil_socket_geterror (p->conn->fd);
211 rs_debug (("%s: %d: connect: %d (%s)\n", __func__, p->conn->fd,
212 sockerr, evutil_socket_error_to_string (sockerr)));
213 rs_err_conn_push_fl (p->conn, RSE_SOCKERR, __FILE__, __LINE__,
214 "%d: connect: %d (%s)", p->conn->fd, sockerr,
215 evutil_socket_error_to_string (sockerr));
221 event_loopbreak (struct rs_connection *conn)
223 int err = event_base_loopbreak (conn->evb);
225 rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
226 "event_base_loopbreak: %s",
227 evutil_gai_strerror (err));
233 event_on_disconnect (struct rs_connection *conn)
235 conn->is_connecting = 0;
236 conn->is_connected = 0;
237 rs_debug (("%s: %p disconnected\n", __func__, conn->active_peer));
238 if (conn->callbacks.disconnected_cb)
239 conn->callbacks.disconnected_cb (conn->user_data);
242 /** Internal connect event returning 0 on success or -1 on error. */
244 event_on_connect (struct rs_connection *conn, struct rs_packet *pkt)
246 assert (!conn->is_connecting);
248 #if defined (RS_ENABLE_TLS)
249 if (conn->realm->type == RS_CONN_TYPE_TLS
250 || conn->realm->type == RS_CONN_TYPE_DTLS)
251 if (tls_verify_cert (conn) != RSE_OK)
253 rs_debug (("%s: server cert verification failed\n", __func__));
256 #endif /* RS_ENABLE_TLS */
258 conn->is_connected = 1;
259 rs_debug (("%s: %p connected\n", __func__, conn->active_peer));
261 if (conn->callbacks.connected_cb)
262 conn->callbacks.connected_cb (conn->user_data);
265 packet_do_send (pkt);
271 event_init_eventbase (struct rs_connection *conn)
278 if (!_event_debug_mode_on)
279 event_enable_debug_mode ();
281 event_set_log_callback (_evlog_cb);
282 conn->evb = event_base_new ();
284 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,