- int rc = 0;
- int conn = -1;
- union {
- struct sockaddr_storage storage;
- struct sockaddr_in in4;
- } addr;
- struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
-
- saddr->sin_port = htons (port);
- saddr->sin_family = AF_INET;
- saddr->sin_addr.s_addr = INADDR_ANY;
-
- if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
- return conn;
-
- if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
- return rc;
-
- if (0 > (rc = listen(conn, 512)))
- return rc;
+ TID_RESP *resp=NULL;
+ int success=0;
+
+ if (NULL == (resp = tid_resp_new(mem_ctx))) {
+ tr_crit("tids_create_response: Error allocating response structure.");
+ return NULL;
+ }
+
+ resp->result = TID_SUCCESS; /* presume success */
+ if ((NULL == (resp->rp_realm = tr_dup_name(req->rp_realm))) ||
+ (NULL == (resp->realm = tr_dup_name(req->realm))) ||
+ (NULL == (resp->comm = tr_dup_name(req->comm)))) {
+ tr_crit("tids_create_response: Error allocating fields in response.");
+ goto cleanup;
+ }
+ if (req->orig_coi) {
+ if (NULL == (resp->orig_coi = tr_dup_name(req->orig_coi))) {
+ tr_crit("tids_create_response: Error allocating fields in response.");
+ goto cleanup;
+ }
+ }
+ if (req->request_id) {
+ if (NULL == (resp->request_id = tr_dup_name(req->request_id))) {
+ tr_crit("tids_create_response: Error allocating fields in response.");
+ goto cleanup;
+ }
+ }
+
+ success=1;
+
+cleanup:
+ if ((!success) && (resp!=NULL)) {
+ talloc_free(resp);
+ resp=NULL;
+ }
+ return resp;
+}
+
+static int tids_handle_request(TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp)
+{
+ int rc=-1;
+
+ /* Check that this is a valid TID Request. If not, send an error return. */
+ if ((!req) ||
+ (!(req->rp_realm)) ||
+ (!(req->realm)) ||
+ (!(req->comm))) {
+ tr_notice("tids_handle_request(): Not a valid TID Request.");
+ tid_resp_set_result(resp, TID_ERROR);
+ tid_resp_set_err_msg(resp, tr_new_name("Bad request format"));
+ return -1;
+ }
+
+ tr_debug("tids_handle_request: adding self to req path.");
+ tid_req_add_path(req, tids->hostname, tids->tids_port);
+
+ /* Call the caller's request handler */
+ /* TBD -- Handle different error returns/msgs */
+ if (0 > (rc = (*tids->req_handler)(tids, req, resp, tids->cookie))) {
+ /* set-up an error response */
+ tr_debug("tids_handle_request: req_handler returned error.");
+ tid_resp_set_result(resp, TID_ERROR);
+ if (!tid_resp_get_err_msg(resp)) /* Use msg set by handler, if any */
+ tid_resp_set_err_msg(resp, tr_new_name("Internal processing error"));
+ } else {
+ /* set-up a success response */
+ tr_debug("tids_handle_request: req_handler returned success.");
+ tid_resp_set_result(resp, TID_SUCCESS);
+ resp->err_msg = NULL; /* No error msg on successful return */
+ }