1 /* Copyright 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
9 #include <event2/event.h>
10 #include <event2/bufferevent.h>
11 #include <radsec/radsec.h>
12 #include <radsec/radsec-impl.h>
21 _conn_open (struct rs_connection *conn, struct rs_packet *pkt)
23 if (event_init_eventbase (conn))
26 if (!conn->active_peer)
27 peer_pick_peer (conn);
28 if (!conn->active_peer)
29 return rs_err_conn_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
31 if (event_init_socket (conn, conn->active_peer))
34 if (conn->realm->type == RS_CONN_TYPE_TCP
35 || conn->realm->type == RS_CONN_TYPE_TLS)
37 if (event_init_bufferevent (conn, conn->active_peer))
42 if (udp_init (conn, pkt))
46 if (!conn->is_connected)
47 if (!conn->is_connecting)
48 event_do_connect (conn);
54 _conn_is_open_p (struct rs_connection *conn)
56 return conn->active_peer && conn->is_connected;
59 /* User callback used when we're dispatching for user. */
61 _wcb (void *user_data)
63 struct rs_packet *pkt = (struct rs_packet *) user_data;
65 pkt->written_flag = 1;
67 bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
69 event_del (pkt->conn->wev);
73 rs_packet_send (struct rs_packet *pkt, void *user_data)
75 struct rs_connection *conn = NULL;
82 if (_conn_is_open_p (conn))
85 if (_conn_open (conn, pkt))
89 assert (conn->active_peer);
90 assert (conn->fd >= 0);
92 conn->user_data = user_data;
94 if (conn->bev) /* TCP */
96 bufferevent_setcb (conn->bev, NULL, tcp_write_cb, tcp_event_cb, pkt);
97 bufferevent_enable (conn->bev, EV_WRITE);
101 err = event_add (conn->wev, NULL);
103 return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
105 evutil_gai_strerror (err));
108 /* Do dispatch, unless the user wants to do it herself. */
109 if (!conn->user_dispatch_flag)
111 conn->callbacks.sent_cb = _wcb;
112 conn->user_data = pkt;
113 rs_debug (("%s: entering event loop\n", __func__));
114 err = event_base_dispatch (conn->evb);
116 return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
117 "event_base_dispatch: %s",
118 evutil_gai_strerror (err));
119 rs_debug (("%s: event loop done\n", __func__));
120 conn->callbacks.sent_cb = NULL;
121 conn->user_data = NULL;
123 if (!pkt->written_flag)