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);
48 conn_activate_timeout (struct rs_connection *conn)
53 if (conn->timeout.tv_sec || conn->timeout.tv_usec)
55 rs_debug (("%s: activating timer: %d.%d\n", __func__,
56 conn->timeout.tv_sec, conn->timeout.tv_usec));
57 if (evtimer_add (conn->tev, &conn->timeout))
58 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
59 "evtimer_add: %d", errno);
64 /* Public functions. */
66 rs_conn_create (struct rs_context *ctx,
67 struct rs_connection **conn,
70 struct rs_connection *c;
72 c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
74 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
76 memset (c, 0, sizeof(struct rs_connection));
81 struct rs_realm *r = rs_conf_find_realm (ctx, config);
87 c->peers = r->peers; /* FIXME: Copy instead? */
88 for (p = c->peers; p != NULL; p = p->next)
90 c->timeout.tv_sec = r->timeout;
91 c->tryagain = r->retries;
95 c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
97 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
99 memset (c->realm, 0, sizeof (struct rs_realm));
109 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
112 assert (conn->realm);
113 conn->realm->type = type;
117 rs_conn_add_listener (struct rs_connection *conn,
119 const char *hostname,
122 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
127 rs_conn_disconnect (struct rs_connection *conn)
133 err = evutil_closesocket (conn->fd);
139 rs_conn_destroy (struct rs_connection *conn)
145 /* NOTE: conn->realm is owned by context. */
146 /* NOTE: conn->peers is owned by context. */
148 if (conn->is_connected)
149 err = rs_conn_disconnect (conn);
151 #if defined (RS_ENABLE_TLS)
152 if (conn->tls_ssl) /* FIXME: Free SSL strucxt in rs_conn_disconnect? */
153 SSL_free (conn->tls_ssl);
155 SSL_CTX_free (conn->tls_ctx);
159 event_free (conn->tev);
161 bufferevent_free (conn->bev);
163 event_free (conn->rev);
165 event_free (conn->wev);
167 event_base_free (conn->evb);
169 rs_free (conn->ctx, conn);
175 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
177 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
181 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
184 memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
188 rs_conn_del_callbacks (struct rs_connection *conn)
191 memset (&conn->callbacks, 0, sizeof (conn->callbacks));
194 struct rs_conn_callbacks *
195 rs_conn_get_callbacks(struct rs_connection *conn)
198 return &conn->callbacks;
202 rs_conn_select_peer (struct rs_connection *conn, const char *name)
204 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
208 rs_conn_get_current_peer (struct rs_connection *conn,
212 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
216 rs_conn_dispatch(struct rs_connection *conn)
219 return event_base_loop (conn->evb, EVLOOP_ONCE);
224 *rs_conn_get_evb(const struct rs_connection *conn)
231 int rs_conn_get_fd (struct rs_connection *conn)
234 assert (conn->active_peer);
239 _rcb (struct rs_message *message, void *user_data)
241 struct rs_message *msg = (struct rs_message *) user_data;
245 msg->flags |= RS_MESSAGE_RECEIVED;
247 bufferevent_disable (msg->conn->bev, EV_WRITE|EV_READ);
249 event_del (msg->conn->rev);
253 rs_conn_receive_message (struct rs_connection *conn,
254 struct rs_message *req_msg,
255 struct rs_message **msg_out)
258 struct rs_message *msg = NULL;
261 assert (conn->realm);
262 assert (!conn_user_dispatch_p (conn)); /* Blocking mode only. */
264 if (rs_message_create (conn, &msg))
268 assert (conn->fd >= 0);
270 conn->callbacks.received_cb = _rcb;
271 conn->user_data = msg;
272 msg->flags &= ~RS_MESSAGE_RECEIVED;
274 if (conn->bev) /* TCP. */
276 bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
277 bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, msg);
278 bufferevent_enable (conn->bev, EV_READ);
282 /* Put fresh message in user_data for the callback and enable the
284 event_assign (conn->rev, conn->evb, event_get_fd (conn->rev),
285 EV_READ, event_get_callback (conn->rev), msg);
286 err = event_add (conn->rev, NULL);
288 return rs_err_conn_push_fl (msg->conn, RSE_EVENT, __FILE__, __LINE__,
290 evutil_gai_strerror (err));
292 /* Activate retransmission timer. */
293 conn_activate_timeout (msg->conn);
296 rs_debug (("%s: entering event loop\n", __func__));
297 err = event_base_dispatch (conn->evb);
298 conn->callbacks.received_cb = NULL;
300 return rs_err_conn_push_fl (msg->conn, RSE_EVENT, __FILE__, __LINE__,
301 "event_base_dispatch: %s",
302 evutil_gai_strerror (err));
303 rs_debug (("%s: event loop done\n", __func__));
305 if ((msg->flags & RS_MESSAGE_RECEIVED) == 0
307 && message_verify_response (msg->conn, msg, req_msg) != RSE_OK))
309 if (rs_err_conn_peek_code (msg->conn) == RSE_OK)
310 /* No message and no error on the stack _should_ mean that the
311 server hung up on us. */
312 rs_err_conn_push (msg->conn, RSE_DISCO, "no response");
313 return rs_err_conn_peek_code (conn);
322 rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv)