3 #include <event2/event.h>
12 #include <trp_internal.h>
13 #include <tr_config.h>
19 /* hold a trps instance and a config manager */
20 struct tr_trps_event_cookie {
25 /* callback to schedule event to process messages */
26 static void tr_trps_mq_cb(TR_MQ *mq, void *arg)
28 struct event *mq_ev=(struct event *)arg;
29 event_active(mq_ev, 0, 0);
32 static void msg_free_helper(void *p)
34 tr_msg_free_decoded((TR_MSG *)p);
36 /* takes a TR_MSG and puts it in a TR_MQ_MSG for processing by the main thread */
37 static TRP_RC tr_trps_msg_handler(TRPS_INSTANCE *trps,
41 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
42 TR_MQ_MSG *mq_msg=NULL;
44 /* n.b., conn is available here, but do not hold onto the reference
45 * because it may be cleaned up if the originating connection goes
46 * down before the message is processed */
47 mq_msg=tr_mq_msg_new(tmp_ctx, "tr_msg");
51 tr_mq_msg_set_payload(mq_msg, (void *)tr_msg, msg_free_helper);
52 trps_mq_append(trps, mq_msg);
53 talloc_free(tmp_ctx); /* cleans up the message if it did not get appended correctly */
58 static int tr_trps_gss_handler(gss_name_t client_name, gss_buffer_t gss_name,
62 struct tr_trps_event_cookie *cookie=(struct tr_trps_event_cookie *)cookie_in;
63 TRPS_INSTANCE *trps = cookie->trps;
64 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
65 TR_NAME name={gss_name->value, gss_name->length};
67 tr_debug("tr_trps_gss_handler()");
69 if ((!client_name) || (!gss_name) || (!trps) || (!cfg_mgr)) {
70 tr_debug("tr_trps_gss_handler: Bad parameters.");
74 /* look up the RP client matching the GSS name */
75 if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, &name)))) {
76 tr_debug("tr_trps_gss_handler: Unknown GSS name %.*s", name.len, name.buf);
80 /*trps->rp_gss = rp;*/
81 tr_debug("Client's GSS Name: %.*s", name.len, name.buf);
86 /* data passed to thread */
87 struct trps_thread_data {
91 /* thread to handle GSS connections to peers */
92 static void *tr_trps_thread(void *arg)
94 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
95 struct trps_thread_data *thread_data=talloc_get_type_abort(arg, struct trps_thread_data);
96 TRP_CONNECTION *conn=thread_data->conn;
97 TRPS_INSTANCE *trps=thread_data->trps;
100 tr_debug("tr_trps_thread: started");
101 trps_handle_connection(trps, conn);
103 msg=tr_mq_msg_new(tmp_ctx, "trps_thread_exit");
104 tr_mq_msg_set_payload(msg, (void *)conn, NULL); /* do not pass a free routine */
106 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
108 trps_mq_append(trps, msg);
110 tr_debug("tr_trps_thread: exit");
111 talloc_free(tmp_ctx);
115 /* called when a connection to the TRPS port is received */
116 static void tr_trps_event_cb(int listener, short event, void *arg)
118 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
119 TRPS_INSTANCE *trps = talloc_get_type_abort(arg, TRPS_INSTANCE); /* aborts on wrong type */
120 TRP_CONNECTION *conn=NULL;
121 TR_NAME *gssname=NULL;
123 struct trps_thread_data *thread_data=NULL;
125 if (0==(event & EV_READ)) {
126 tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
128 /* create a thread to handle this connection */
129 asprintf(&name, "trustrouter@%s", trps->hostname);
130 gssname=tr_new_name(name);
131 free(name); name=NULL;
132 conn=trp_connection_accept(tmp_ctx, listener, gssname);
134 /* need to monitor this fd and trigger events when read becomes possible */
135 thread_data=talloc(conn, struct trps_thread_data);
136 if (thread_data==NULL) {
137 tr_err("tr_trps_event_cb: unable to allocate trps_thread_data");
138 talloc_free(tmp_ctx);
141 thread_data->conn=conn;
142 thread_data->trps=trps;
143 pthread_create(trp_connection_get_thread(conn), NULL, tr_trps_thread, thread_data);
144 pthread_detach(*(trp_connection_get_thread(conn))); /* we will not rejoin the thread */
145 trps_add_connection(trps, conn); /* remember the connection */
148 talloc_free(tmp_ctx);
151 static void tr_trps_cleanup_thread(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
153 /* everything belonging to the thread is in the TRP_CONNECTION
154 * associated with it */
155 trps_remove_connection(trps, conn);
156 tr_debug("Deleted connection");
159 static void tr_trps_print_route_table(TRPS_INSTANCE *trps, FILE *f)
161 char *table=trp_rtable_to_str(NULL, trps->rtable, " | ", NULL);
163 fprintf(f, "Unable to print route table.\n");
165 fprintf(f, "%s\n", table);
170 static void tr_trps_process_mq(int socket, short event, void *arg)
172 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
176 msg=trps_mq_pop(trps);
178 s=tr_mq_msg_get_message(msg);
179 if (0==strcmp(s, "trps_thread_exit")) {
180 tr_trps_cleanup_thread(trps,
181 talloc_get_type_abort(tr_mq_msg_get_payload(msg),
184 else if (0==strcmp(s, "tr_msg")) {
185 if (trps_handle_tr_msg(trps, tr_mq_msg_get_payload(msg))!=TRP_SUCCESS)
186 tr_notice("tr_trps_process_mq: error handling message.");
188 tr_trps_print_route_table(trps, stderr);
192 tr_notice("tr_trps_process_mq: unknown message '%s' received.", tr_mq_msg_get_message(msg));
195 msg=trps_mq_pop(trps);
199 static void tr_trps_sweep(int listener, short event, void *arg)
201 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
202 tr_debug("tr_trps_sweep: sweeping routes");
203 trps_sweep_routes(trps);
206 static int tr_trps_events_destructor(void *obj)
208 TR_TRPS_EVENTS *ev=talloc_get_type_abort(obj, TR_TRPS_EVENTS);
210 event_free(ev->mq_ev);
211 if (ev->sweep_ev!=NULL)
212 event_free(ev->sweep_ev);
215 TR_TRPS_EVENTS *tr_trps_events_new(TALLOC_CTX *mem_ctx)
217 TR_TRPS_EVENTS *ev=talloc(mem_ctx, TR_TRPS_EVENTS);
219 ev->listen_ev=talloc(ev, struct tr_socket_event);
222 if (ev->listen_ev==NULL) {
226 talloc_set_destructor((void *)ev, tr_trps_events_destructor);
231 /* Configure the trps instance and set up its event handler.
232 * Fills in trps_ev, which should be allocated by caller. */
233 TRP_RC tr_trps_event_init(struct event_base *base,
236 TR_TRPS_EVENTS *trps_ev)
238 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
239 struct tr_socket_event *listen_ev=NULL;
240 struct tr_trps_event_cookie *cookie;
241 struct timeval two_secs={2, 0};
242 TRP_RC retval=TRP_ERROR;
244 if (trps_ev == NULL) {
245 tr_debug("tr_trps_event_init: Null trps_ev.");
250 /* get convenient handles */
251 listen_ev=trps_ev->listen_ev;
253 /* Create the cookie for callbacks. It is part of the trps context, so it will
254 * be cleaned up when trps is freed by talloc_free. */
255 cookie=talloc(tmp_ctx, struct tr_trps_event_cookie);
256 if (cookie == NULL) {
257 tr_debug("tr_trps_event_init: Unable to allocate cookie.");
262 cookie->cfg_mgr=cfg_mgr;
263 talloc_steal(trps, cookie);
265 /* get a trps listener */
266 listen_ev->sock_fd=trps_get_listener(trps,
269 cfg_mgr->active->internal->hostname,
270 cfg_mgr->active->internal->trps_port,
272 if (listen_ev->sock_fd < 0) {
273 tr_crit("Error opening TRP server socket.");
279 listen_ev->ev=event_new(base,
284 event_add(listen_ev->ev, NULL);
286 /* now set up message queue processing event, only triggered by
288 trps_ev->mq_ev=event_new(base,
293 tr_mq_set_notify_cb(trps->mq, tr_trps_mq_cb, trps_ev->mq_ev);
295 /* now set up the route table sweep timer event */
296 trps_ev->sweep_ev=event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_trps_sweep, (void *)trps);
297 /* todo: event_add(trps_ev->sweep_ev, &(cfg_mgr->active->internal->route_sweep_interval)); */
298 event_add(trps_ev->sweep_ev, &two_secs);
303 talloc_free(tmp_ctx);
308 struct trpc_notify_cb_data {
311 pthread_mutex_t mutex;
314 static void tr_trpc_mq_cb(TR_MQ *mq, void *arg)
316 struct trpc_notify_cb_data *cb_data=(struct trpc_notify_cb_data *) arg;
317 pthread_mutex_lock(&(cb_data->mutex));
318 if (!cb_data->msg_ready) {
319 cb_data->msg_ready=1;
320 pthread_cond_signal(&(cb_data->cond));
322 pthread_mutex_unlock(&(cb_data->mutex));
325 /* data passed to thread */
326 struct trpc_thread_data {
330 static void *tr_trpc_thread(void *arg)
332 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
333 struct trpc_thread_data *thread_data=talloc_get_type_abort(arg, struct trpc_thread_data);
334 TRPC_INSTANCE *trpc=thread_data->trpc;
335 TRPS_INSTANCE *trps=thread_data->trps;
338 const char *msg_type=NULL;
339 char *encoded_msg=NULL;
341 struct trpc_notify_cb_data cb_data={0,
342 PTHREAD_COND_INITIALIZER,
343 PTHREAD_MUTEX_INITIALIZER};
345 tr_debug("tr_trpc_thread: started");
347 /* set up the mq for receiving */
348 pthread_mutex_lock(&(cb_data.mutex)); /* hold this lock until we enter the main loop */
350 tr_mq_lock(trpc->mq);
351 tr_mq_set_notify_cb(trpc->mq, tr_trpc_mq_cb, (void *) &cb_data);
352 tr_mq_unlock(trpc->mq);
354 rc=trpc_connect(trpc);
355 if (rc!=TRP_SUCCESS) {
356 tr_notice("tr_trpc_thread: failed to initiate connection to %s:%d.",
357 trpc_get_server(trpc),
358 trpc_get_port(trpc));
362 pthread_cond_wait(&(cb_data.cond), &(cb_data.mutex));
363 /* verify the condition */
364 if (cb_data.msg_ready) {
365 msg=trpc_mq_pop(trpc);
367 /* no message in the queue */
368 tr_err("tr_trpc_thread: notified of msg, but queue empty");
372 msg_type=tr_mq_msg_get_message(msg);
374 if (0==strcmp(msg_type, "trpc_abort")) {
376 break; /* exit loop */
378 else if (0==strcmp(msg_type, "trpc_send")) {
379 encoded_msg=tr_mq_msg_get_payload(msg);
380 if (encoded_msg==NULL)
381 tr_notice("tr_trpc_thread: null outgoing TRP message.");
383 rc = trpc_send_msg(trpc, encoded_msg);
384 if (rc!=TRP_SUCCESS) {
385 tr_notice("tr_trpc_thread: trpc_send_msg failed.");
392 tr_notice("tr_trpc_thread: unknown message '%s' received.", msg_type);
399 msg=tr_mq_msg_new(tmp_ctx, "trpc_thread_exit");
400 tr_mq_msg_set_payload(msg, (void *)trpc, NULL); /* do not pass a free routine */
402 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
404 trps_mq_append(trps, msg);
406 talloc_free(tmp_ctx);
410 /* starts a trpc thread to connect to server:port */
411 TRPC_INSTANCE *tr_trpc_initiate(TRPS_INSTANCE *trps, const char *server, unsigned int port)
413 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
414 TRPC_INSTANCE *trpc=NULL;
415 TRP_CONNECTION *conn=NULL;
416 struct trpc_thread_data *thread_data=NULL;
418 tr_debug("tr_trpc_initiate entered");
419 trpc=trpc_new(tmp_ctx);
421 tr_crit("tr_trpc_initiate: could not allocate TRPC_INSTANCE.");
424 tr_debug("tr_trpc_initiate: allocated trpc");
426 conn=trp_connection_new(trpc);
428 tr_crit("tr_trpc_initiate: could not allocate TRP_CONNECTION.");
431 trpc_set_conn(trpc, conn);
432 trpc_set_server(trpc, talloc_strdup(trpc, server));
433 trpc_set_port(trpc, port);
434 tr_debug("tr_trpc_initiate: allocated connection");
437 thread_data=talloc(trpc, struct trpc_thread_data);
438 if (thread_data==NULL) {
439 tr_crit("tr_trpc_initiate: could not allocate struct trpc_thread_data.");
442 thread_data->trpc=trpc;
443 thread_data->trps=trps;
445 pthread_create(trp_connection_get_thread(conn), NULL, tr_trpc_thread, thread_data);
446 pthread_detach(*(trp_connection_get_thread(conn))); /* we will not rejoin the thread */
448 tr_debug("tr_trpc_initiate: started trpc thread");
449 trps_add_trpc(trps, trpc);
451 talloc_report_full(trps, stderr);
452 talloc_report_full(tmp_ctx, stderr);
455 talloc_free(tmp_ctx);