3 #include <event2/event.h>
11 #include <trp_internal.h>
12 #include <tr_config.h>
18 /* hold a trps instance and a config manager */
19 struct tr_trps_event_cookie {
24 /* callback to schedule event to process messages */
25 static void tr_trps_mq_cb(TR_MQ *mq, void *arg)
27 struct event *mq_ev=(struct event *)arg;
28 event_active(mq_ev, 0, 0);
31 static void msg_free_helper(void *p)
33 tr_msg_free_decoded((TR_MSG *)p);
35 /* takes a TR_MSG and puts it in a TR_MQ_MSG for processing by the main thread */
36 static TRP_RC tr_trps_msg_handler(TRPS_INSTANCE *trps,
40 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
41 TR_MQ_MSG *mq_msg=NULL;
43 /* n.b., conn is available here, but do not hold onto the reference
44 * because it may be cleaned up if the originating connection goes
45 * down before the message is processed */
46 mq_msg=tr_mq_msg_new(tmp_ctx, "tr_msg");
50 tr_mq_msg_set_payload(mq_msg, (void *)tr_msg, msg_free_helper);
51 trps_mq_append(trps, mq_msg);
52 talloc_free(tmp_ctx); /* cleans up the message if it did not get appended correctly */
57 static int tr_trps_gss_handler(gss_name_t client_name, gss_buffer_t gss_name,
61 struct tr_trps_event_cookie *cookie=(struct tr_trps_event_cookie *)cookie_in;
62 TRPS_INSTANCE *trps = cookie->trps;
63 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
64 TR_NAME name={gss_name->value, gss_name->length};
66 tr_debug("tr_trps_gss_handler()");
68 if ((!client_name) || (!gss_name) || (!trps) || (!cfg_mgr)) {
69 tr_debug("tr_trps_gss_handler: Bad parameters.");
73 /* look up the RP client matching the GSS name */
74 if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, &name)))) {
75 tr_debug("tr_trps_gss_handler: Unknown GSS name %.*s", name.len, name.buf);
79 /*trps->rp_gss = rp;*/
80 tr_debug("Client's GSS Name: %.*s", name.len, name.buf);
85 /* data passed to thread */
86 struct trps_thread_data {
90 /* thread to handle GSS connections to peers */
91 static void *tr_trps_thread(void *arg)
93 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
94 struct trps_thread_data *thread_data=talloc_get_type_abort(arg, struct trps_thread_data);
95 TRP_CONNECTION *conn=thread_data->conn;
96 TRPS_INSTANCE *trps=thread_data->trps;
99 tr_debug("tr_trps_thread: started");
100 trps_handle_connection(trps, conn);
102 msg=tr_mq_msg_new(tmp_ctx, "trps_thread_exit");
103 tr_mq_msg_set_payload(msg, (void *)conn, NULL); /* do not pass a free routine */
105 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
107 trps_mq_append(trps, msg);
109 tr_debug("tr_trps_thread: exit");
110 talloc_free(tmp_ctx);
114 /* called when a connection to the TRPS port is received */
115 static void tr_trps_event_cb(int listener, short event, void *arg)
117 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
118 TRPS_INSTANCE *trps = talloc_get_type_abort(arg, TRPS_INSTANCE); /* aborts on wrong type */
119 TRP_CONNECTION *conn=NULL;
120 TR_NAME *gssname=NULL;
122 struct trps_thread_data *thread_data=NULL;
124 if (0==(event & EV_READ)) {
125 tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
127 /* create a thread to handle this connection */
128 asprintf(&name, "trustrouter@%s", trps->hostname);
129 gssname=tr_new_name(name);
130 free(name); name=NULL;
131 conn=trp_connection_accept(tmp_ctx, listener, gssname);
133 /* need to monitor this fd and trigger events when read becomes possible */
134 thread_data=talloc(conn, struct trps_thread_data);
135 if (thread_data==NULL) {
136 tr_err("tr_trps_event_cb: unable to allocate trps_thread_data");
137 talloc_free(tmp_ctx);
140 thread_data->conn=conn;
141 thread_data->trps=trps;
142 pthread_create(trp_connection_get_thread(conn), NULL, tr_trps_thread, thread_data);
143 pthread_detach(*(trp_connection_get_thread(conn))); /* we will not rejoin the thread */
144 trps_add_connection(trps, conn); /* remember the connection */
147 talloc_free(tmp_ctx);
150 static void tr_trps_cleanup_thread(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
152 /* everything belonging to the thread is in the TRP_CONNECTION
153 * associated with it */
154 trps_remove_connection(trps, conn);
155 tr_debug("Deleted connection");
158 static void tr_trps_process_mq(int socket, short event, void *arg)
160 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
165 tr_debug("tr_trps_process_mw: starting");
166 msg=trps_mq_pop(trps);
168 s=tr_mq_msg_get_message(msg);
169 if (0==strcmp(s, "trps_thread_exit")) {
170 tr_trps_cleanup_thread(trps,
171 talloc_get_type_abort(tr_mq_msg_get_payload(msg),
174 else if (0==strcmp(s, "tr_msg")) {
175 tmp=tr_msg_encode(tr_mq_msg_get_payload(msg));
176 tr_debug("tr_msg: %s", tmp);
177 tr_msg_free_encoded(tmp);
180 tr_notice("tr_trps_process_mq: unknown message '%s' received.", tr_mq_msg_get_message(msg));
183 msg=trps_mq_pop(trps);
185 tr_debug("tr_trps_process_mw: ending");
188 static int tr_trps_events_destructor(void *obj)
190 TR_TRPS_EVENTS *ev=talloc_get_type_abort(obj, TR_TRPS_EVENTS);
192 event_free(ev->mq_ev);
195 TR_TRPS_EVENTS *tr_trps_events_new(TALLOC_CTX *mem_ctx)
197 TR_TRPS_EVENTS *ev=talloc(mem_ctx, TR_TRPS_EVENTS);
199 ev->listen_ev=talloc(ev, struct tr_socket_event);
201 if (ev->listen_ev==NULL) {
205 talloc_set_destructor((void *)ev, tr_trps_events_destructor);
210 /* Configure the trps instance and set up its event handler.
211 * Fills in trps_ev, which should be allocated by caller. */
212 TRP_RC tr_trps_event_init(struct event_base *base,
215 TR_TRPS_EVENTS *trps_ev)
217 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
218 struct tr_socket_event *listen_ev=NULL;
219 struct tr_trps_event_cookie *cookie;
220 TRP_RC retval=TRP_ERROR;
222 if (trps_ev == NULL) {
223 tr_debug("tr_trps_event_init: Null trps_ev.");
228 /* get convenient handles */
229 listen_ev=trps_ev->listen_ev;
231 /* Create the cookie for callbacks. It is part of the trps context, so it will
232 * be cleaned up when trps is freed by talloc_free. */
233 cookie=talloc(tmp_ctx, struct tr_trps_event_cookie);
234 if (cookie == NULL) {
235 tr_debug("tr_trps_event_init: Unable to allocate cookie.");
240 cookie->cfg_mgr=cfg_mgr;
241 talloc_steal(trps, cookie);
243 /* get a trps listener */
244 listen_ev->sock_fd=trps_get_listener(trps,
247 cfg_mgr->active->internal->hostname,
248 cfg_mgr->active->internal->trps_port,
250 if (listen_ev->sock_fd < 0) {
251 tr_crit("Error opening TRP server socket.");
257 listen_ev->ev=event_new(base,
262 event_add(listen_ev->ev, NULL);
264 /* now set up message queue processing event, only triggered by
266 trps_ev->mq_ev=event_new(base,
271 tr_mq_set_notify_cb(trps->mq, tr_trps_mq_cb, trps_ev->mq_ev);
276 talloc_free(tmp_ctx);
281 struct trpc_notify_cb_data {
284 pthread_mutex_t mutex;
287 static void tr_trpc_mq_cb(TR_MQ *mq, void *arg)
289 struct trpc_notify_cb_data *cb_data=(struct trpc_notify_cb_data *) arg;
290 pthread_mutex_lock(&(cb_data->mutex));
291 if (!cb_data->msg_ready) {
292 cb_data->msg_ready=1;
293 pthread_cond_signal(&(cb_data->cond));
295 pthread_mutex_unlock(&(cb_data->mutex));
298 /* data passed to thread */
299 struct trpc_thread_data {
303 static void *tr_trpc_thread(void *arg)
305 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
306 struct trpc_thread_data *thread_data=talloc_get_type_abort(arg, struct trpc_thread_data);
307 TRPC_INSTANCE *trpc=thread_data->trpc;
308 TRPS_INSTANCE *trps=thread_data->trps;
311 const char *msg_type=NULL;
312 char *encoded_msg=NULL;
314 struct trpc_notify_cb_data cb_data={0,
315 PTHREAD_COND_INITIALIZER,
316 PTHREAD_MUTEX_INITIALIZER};
318 tr_debug("tr_trpc_thread: started");
320 /* set up the mq for receiving */
321 pthread_mutex_lock(&(cb_data.mutex)); /* hold this lock until we enter the main loop */
323 tr_mq_lock(trpc->mq);
324 tr_mq_set_notify_cb(trpc->mq, tr_trpc_mq_cb, (void *) &cb_data);
325 tr_mq_unlock(trpc->mq);
327 rc=trpc_connect(trpc);
328 if (rc!=TRP_SUCCESS) {
329 tr_notice("tr_trpc_thread: failed to initiate connection to %s:%d.",
330 trpc_get_server(trpc),
331 trpc_get_port(trpc));
335 pthread_cond_wait(&(cb_data.cond), &(cb_data.mutex));
336 /* verify the condition */
337 if (cb_data.msg_ready) {
338 msg=trpc_mq_pop(trpc);
340 /* no message in the queue */
341 tr_err("tr_trpc_thread: notified of msg, but queue empty");
345 msg_type=tr_mq_msg_get_message(msg);
347 if (0==strcmp(msg_type, "trpc_abort")) {
349 break; /* exit loop */
351 else if (0==strcmp(msg_type, "trpc_send")) {
352 encoded_msg=tr_mq_msg_get_payload(msg);
353 if (encoded_msg==NULL)
354 tr_notice("tr_trpc_thread: null outgoing TRP message.");
356 rc = trpc_send_msg(trpc, encoded_msg);
357 if (rc!=TRP_SUCCESS) {
358 tr_notice("tr_trpc_thread: trpc_send_msg failed.");
365 tr_notice("tr_trpc_thread: unknown message '%s' received.", msg_type);
372 msg=tr_mq_msg_new(tmp_ctx, "trpc_thread_exit");
373 tr_mq_msg_set_payload(msg, (void *)trpc, NULL); /* do not pass a free routine */
375 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
377 trps_mq_append(trps, msg);
379 talloc_free(tmp_ctx);
383 /* starts a trpc thread to connect to server:port */
384 TRPC_INSTANCE *tr_trpc_initiate(TRPS_INSTANCE *trps, const char *server, unsigned int port)
386 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
387 TRPC_INSTANCE *trpc=NULL;
388 TRP_CONNECTION *conn=NULL;
389 struct trpc_thread_data *thread_data=NULL;
391 tr_debug("tr_trpc_initiate entered");
392 trpc=trpc_new(tmp_ctx);
394 tr_crit("tr_trpc_initiate: could not allocate TRPC_INSTANCE.");
397 tr_debug("tr_trpc_initiate: allocated trpc");
399 conn=trp_connection_new(trpc);
401 tr_crit("tr_trpc_initiate: could not allocate TRP_CONNECTION.");
404 trpc_set_conn(trpc, conn);
405 trpc_set_server(trpc, talloc_strdup(trpc, server));
406 trpc_set_port(trpc, port);
407 tr_debug("tr_trpc_initiate: allocated connection");
410 thread_data=talloc(trpc, struct trpc_thread_data);
411 if (thread_data==NULL) {
412 tr_crit("tr_trpc_initiate: could not allocate struct trpc_thread_data.");
415 thread_data->trpc=trpc;
416 thread_data->trps=trps;
418 pthread_create(trp_connection_get_thread(conn), NULL, tr_trpc_thread, thread_data);
419 pthread_detach(*(trp_connection_get_thread(conn))); /* we will not rejoin the thread */
421 tr_debug("tr_trpc_initiate: started trpc thread");
422 trps_add_trpc(trps, trpc);
424 talloc_report_full(trps, stderr);
425 talloc_report_full(tmp_ctx, stderr);
428 talloc_free(tmp_ctx);