+ /* If we already sent a response, don't send another no matter what. */
+ if (req->resp_sent)
+ return 0;
+
+ if (NULL == (resp = tids_create_response(req, tids, req))) {
+ tr_crit("tids_send_err_response: Can't create response.");
+ return -1;
+ }
+
+ /* mark this as an error response, and include the error message */
+ resp->result = TID_ERROR;
+ resp->err_msg = tr_new_name((char *)err_msg);
+ resp->error_path = req->path;
+
+ rc = tids_send_response(tids, req, resp);
+
+ tid_resp_free(resp);
+ return rc;
+}
+
+/**
+ * Encode/send a response
+ *
+ * Part of the public interface
+ *
+ * @param tids
+ * @param req
+ * @param resp
+ * @return
+ */
+int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp)
+{
+ int err;
+ char *resp_buf;
+
+ if ((!tids) || (!req) || (!resp)) {
+ tr_debug("tids_send_response: Invalid parameters.");
+ return -1;
+ }
+
+ /* Never send a second response if we already sent one. */
+ if (req->resp_sent)
+ return 0;
+
+ resp_buf = tids_encode_response(NULL, tids, req, resp);
+ if (resp_buf == NULL) {
+ tr_err("tids_send_response: Error encoding json response.");
+ tr_audit_req(req);
+ return -1;
+ }
+
+ tr_debug("tids_send_response: Encoded response: %s", resp_buf);
+
+ /* If external logging is enabled, fire off a message */
+ /* TODO Can be moved to end once segfault in gsscon_write_encrypted_token fixed */
+ tr_audit_resp(resp);
+