Пример #1
0
/**
 * Read a request from the GSS connection
 *
 * @param mem_ctx talloc context for the result
 * @param conn file descriptor for the connection
 * @param gssctx GSS context
 * @return talloc'ed string containing the request, or null on error
 */
static char *tr_gss_read_req(TALLOC_CTX *mem_ctx, int conn, gss_ctx_id_t gssctx)
{
  int err;
  char *retval = NULL;
  char *buf = NULL;
  size_t buflen = 0;

  err = gsscon_read_encrypted_token(conn, gssctx, &buf, &buflen);
  if (err || (buf == NULL)) {
    if (buf)
      free(buf);
    tr_debug("tr_gss_read_req: Error reading from connection, rc=%d", err);
    return NULL;
  }

  tr_debug("tr_gss_read_req: Read %u bytes.", (unsigned) buflen);

  // get a talloc'ed version, guaranteed to have a null termination
  retval = talloc_asprintf(mem_ctx, "%.*s", (int) buflen, buf);
  free(buf);

  return retval;
}
Пример #2
0
int tidc_fwd_request(TIDC_INSTANCE *tidc,
                     TID_REQ *tid_req,
		     TIDC_RESP_FUNC *resp_handler,
                     void *cookie)
{
  char *req_buf = NULL;
  char *resp_buf = NULL;
  size_t resp_buflen = 0;
  TR_MSG *msg = NULL;
  TR_MSG *resp_msg = NULL;
  int err;
  int rc = 0;

  /* Create and populate a TID msg structure */
  if (!(msg = talloc_zero(tid_req, TR_MSG)))
    goto error;

  msg->msg_type = TID_REQUEST;
  tr_msg_set_req(msg, tid_req);

  /* store the response function and cookie */
  // tid_req->resp_func = resp_handler;
  // tid_req->cookie = cookie;


  /* Encode the request into a json string */
  if (!(req_buf = tr_msg_encode(msg))) {
    tr_err("tidc_fwd_request: Error encoding TID request.\n");
    goto error;
  }

  tr_debug( "tidc_fwd_request: Sending TID request:\n");
  tr_debug( "%s\n", req_buf);

  /* Send the request over the connection */
  if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
					  strlen(req_buf))) {
    tr_err( "tidc_fwd_request: Error sending request over connection.\n");
    goto error;
  }

  /* TBD -- queue request on instance, read resps in separate thread */

  /* Read the response from the connection */
  /* TBD -- timeout? */
  if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
    if (resp_buf)
      free(resp_buf);
    goto error;
  }

  tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
  tr_debug( "%s\n", resp_buf);

  if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
    tr_err( "tidc_fwd_request: Error decoding response.\n");
    goto error;
  }

  /* TBD -- Check if this is actually a valid response */
  if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
    tr_err( "tidc_fwd_request: Error, no response in the response!\n");
    goto error;
  }

  if (resp_handler) {
    /* Call the caller's response function. It must copy any data it needs before returning. */
    tr_debug("tidc_fwd_request: calling response callback function.");
    (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
  }

  goto cleanup;

 error:
  rc = -1;
 cleanup:
  if (msg)
    talloc_free(msg);
  if (req_buf)
    free(req_buf);
  if (resp_buf)
    free(resp_buf);
  if (resp_msg)
    tr_msg_free_decoded(resp_msg);
  return rc;
}