1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2 See LICENSE for licensing information. */
4 #if defined HAVE_CONFIG_H
12 #include <event2/event.h>
13 #include <event2/bufferevent.h>
14 #include <radsec/radsec.h>
15 #include <radsec/radsec-impl.h>
23 conn_close (struct rs_connection **connp)
28 if ((*connp)->is_connected)
29 r = rs_conn_disconnect (*connp);
36 conn_user_dispatch_p (const struct rs_connection *conn)
40 return (conn->callbacks.connected_cb ||
41 conn->callbacks.disconnected_cb ||
42 conn->callbacks.received_cb ||
43 conn->callbacks.sent_cb);
47 rs_conn_create (struct rs_context *ctx,
48 struct rs_connection **conn,
51 struct rs_connection *c;
53 c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
55 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
57 memset (c, 0, sizeof(struct rs_connection));
62 struct rs_realm *r = rs_conf_find_realm (ctx, config);
68 c->peers = r->peers; /* FIXME: Copy instead? */
69 for (p = c->peers; p; p = p->next)
71 c->timeout.tv_sec = r->timeout;
72 c->tryagain = r->retries;
76 c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
78 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
80 memset (c->realm, 0, sizeof (struct rs_realm));
90 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
94 conn->realm->type = type;
98 rs_conn_add_listener (struct rs_connection *conn,
100 const char *hostname,
103 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
108 rs_conn_disconnect (struct rs_connection *conn)
114 err = evutil_closesocket (conn->fd);
120 rs_conn_destroy (struct rs_connection *conn)
126 /* NOTE: conn->realm is owned by context. */
127 /* NOTE: conn->peers is owned by context. */
129 if (conn->is_connected)
130 err = rs_conn_disconnect (conn);
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);
136 SSL_CTX_free (conn->tls_ctx);
140 event_free (conn->tev);
142 bufferevent_free (conn->bev);
144 event_free (conn->rev);
146 event_free (conn->wev);
148 event_base_free (conn->evb);
150 rs_free (conn->ctx, conn);
156 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
158 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
162 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
165 memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
169 rs_conn_del_callbacks (struct rs_connection *conn)
172 memset (&conn->callbacks, 0, sizeof (conn->callbacks));
175 struct rs_conn_callbacks *
176 rs_conn_get_callbacks(struct rs_connection *conn)
179 return &conn->callbacks;
183 rs_conn_select_peer (struct rs_connection *conn, const char *name)
185 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
189 rs_conn_get_current_peer (struct rs_connection *conn,
193 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
197 rs_conn_dispatch(struct rs_connection *conn)
200 return event_base_loop (conn->evb, EVLOOP_ONCE);
205 *rs_conn_get_evb(const struct rs_connection *conn)
212 int rs_conn_get_fd (struct rs_connection *conn)
215 assert (conn->active_peer);
220 _rcb (struct rs_packet *packet, void *user_data)
222 struct rs_packet *pkt = (struct rs_packet *) user_data;
226 pkt->flags |= RS_PACKET_RECEIVED;
228 bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
230 event_del (pkt->conn->rev);
234 rs_conn_receive_packet (struct rs_connection *conn,
235 struct rs_packet *req_msg,
236 struct rs_packet **pkt_out)
239 struct rs_packet *pkt = NULL;
242 assert (conn->realm);
243 assert (!conn_user_dispatch_p (conn)); /* Blocking mode only. */
245 if (rs_packet_create (conn, &pkt))
249 assert (conn->fd >= 0);
251 conn->callbacks.received_cb = _rcb;
252 conn->user_data = pkt;
253 pkt->flags &= ~RS_PACKET_RECEIVED;
255 if (conn->bev) /* TCP. */
257 bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
258 bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
259 bufferevent_enable (conn->bev, EV_READ);
263 /* Put fresh packet in user_data for the callback and enable the
265 event_assign (conn->rev, conn->evb, event_get_fd (conn->rev),
266 EV_READ, event_get_callback (conn->rev), pkt);
267 err = event_add (conn->rev, NULL);
269 return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
271 evutil_gai_strerror (err));
273 /* Activate retransmission timer. */
274 conn_activate_timeout (pkt->conn);
277 rs_debug (("%s: entering event loop\n", __func__));
278 err = event_base_dispatch (conn->evb);
279 conn->callbacks.received_cb = NULL;
281 return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
282 "event_base_dispatch: %s",
283 evutil_gai_strerror (err));
284 rs_debug (("%s: event loop done\n", __func__));
286 if ((pkt->flags & RS_PACKET_RECEIVED) == 0
288 && packet_verify_response (pkt->conn, pkt, req_msg) != RSE_OK))
290 if (rs_err_conn_peek_code (pkt->conn) == RSE_OK)
291 /* No packet and no error on the stack _should_ mean that the
292 server hung up on us. */
293 rs_err_conn_push (pkt->conn, RSE_DISCO, "no response");
294 return rs_err_conn_peek_code (conn);
303 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)
311 conn_activate_timeout (struct rs_connection *conn)
316 if (conn->timeout.tv_sec || conn->timeout.tv_usec)
318 rs_debug (("%s: activating timer: %d.%d\n", __func__,
319 conn->timeout.tv_sec, conn->timeout.tv_usec));
320 if (evtimer_add (conn->tev, &conn->timeout))
321 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
322 "evtimer_add: %d", errno);