+enum map_coi_result {
+ MAP_COI_SUCCESS = 0,
+ MAP_COI_MAP_NOT_REQUIRED,
+ MAP_COI_ALREADY_MAPPED,
+ MAP_COI_NO_APC,
+ MAP_COI_INVALID_APC,
+ MAP_COI_UNKNOWN_COMM,
+ MAP_COI_ERROR
+};
+
+static enum map_coi_result map_coi(TR_COMM_TABLE *ctable, TID_REQ *req)
+{
+ TR_COMM *orig_comm;
+ TR_NAME *apc_name;
+ TR_COMM *apc;
+ TR_APC *apcs;
+
+ if (tid_req_get_orig_coi(req) != NULL)
+ return MAP_COI_ALREADY_MAPPED;
+
+ /* look up the community */
+ orig_comm = tr_comm_table_find_comm(ctable, tid_req_get_comm(req));
+ if (orig_comm == NULL)
+ return MAP_COI_UNKNOWN_COMM;
+
+ if (tr_comm_get_type(orig_comm) == TR_COMM_APC)
+ return MAP_COI_MAP_NOT_REQUIRED; /* it was already an APC, no mapping to do */
+
+ /* use first (only) APC. These are just APC names */
+ apcs = tr_comm_get_apcs(orig_comm);
+ if ((!apcs) || (!tr_apc_get_id(apcs)))
+ return MAP_COI_NO_APC;
+
+ /* get our own copy of the APC name */
+ apc_name = tr_dup_name(tr_apc_get_id(apcs));
+ if (apc_name == NULL) {
+ tr_err("map_coi: Error allocating apc_name");
+ return MAP_COI_ERROR;
+ }
+
+ /* Check that the APC is configured */
+ apc = tr_comm_table_find_comm(ctable, apc_name);
+ if (apc == NULL) {
+ tr_free_name(apc_name);
+ return MAP_COI_INVALID_APC;
+ }
+
+ tid_req_set_orig_coi(req, tid_req_get_comm(req)); /* was null, so no need to free anything */
+ tid_req_set_comm(req, apc_name); /* original contents will be freed via orig_coi */
+
+ return MAP_COI_SUCCESS; /* successfully mapped */
+}
+
+/**
+ * Process a TID request
+ *
+ * Return value of -1 means to send a TID_ERROR response. Fill in resp->err_msg or it will
+ * be returned as a generic error.
+ *
+ * @param tids
+ * @param orig_req
+ * @param resp
+ * @param cookie_in
+ * @return
+ */