Exemplo n.º 1
0
TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
{
  TR_MSG *msg=NULL;
  json_t *jmsg = NULL;
  json_error_t rc;
  json_t *jtype=NULL;
  json_t *jbody=NULL;
  const char *mtype = NULL;

  if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
    tr_debug("tr_msg_decode(): error loading object");
    return NULL;
  }

  if (!(msg = malloc(sizeof(TR_MSG)))) {
    tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
    json_decref(jmsg);
    return NULL;
  }
 
  memset(msg, 0, sizeof(TR_MSG));

  if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
      (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
    tr_debug("tr_msg_decode(): Error parsing message header.");
    json_decref(jmsg);
    tr_msg_free_decoded(msg);
    return NULL;
  }

  mtype = json_string_value(jtype);

  if (0 == strcmp(mtype, "tid_request")) {
    msg->msg_type = TID_REQUEST;
    tr_msg_set_req(msg, tr_msg_decode_tidreq(jbody));
  }
  else if (0 == strcmp(mtype, "tid_response")) {
    msg->msg_type = TID_RESPONSE;
    tr_msg_set_resp(msg, tr_msg_decode_tidresp(jbody));
  }
  else if (0 == strcmp(mtype, "trp_update")) {
    msg->msg_type = TRP_UPDATE;
    tr_msg_set_trp_upd(msg, tr_msg_decode_trp_upd(NULL, jbody)); /* null talloc context for now */
  }
  else if (0 == strcmp(mtype, "trp_request")) {
    msg->msg_type = TRP_UPDATE;
    tr_msg_set_trp_req(msg, tr_msg_decode_trp_req(NULL, jbody)); /* null talloc context for now */
  }
  else {
    msg->msg_type = TR_UNKNOWN;
    msg->msg_rep = NULL;
  }
  return msg;
}
Exemplo n.º 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;
}