示例#1
0
/* Add an operation */
void add_op_handler(unsigned opcode, op_handler h) {
    op_ptr ele = (op_ptr) malloc_or_fail(sizeof(op_ele), "add_op_handler");
    ele->opcode = opcode;
    ele->opfun = h;
    ele->next = op_list;
    op_list = ele;
}
示例#2
0
/* returns the sequence number of this message, or 0 for errors */
long long int send_data_message (int sock, char * peer,
                                 char * message, int mlen)
{
  if (mlen <= 0) {
    printf ("unable to send a data message of size %d\n", mlen);
    return 0;
  }

  int dsize = mlen + CHAT_DESCRIPTOR_SIZE;
  char * data_with_cd = malloc_or_fail (dsize, "xcommon.c send_data_message");
  struct chat_descriptor * cp = (struct chat_descriptor *) data_with_cd;
  if (! init_chat_descriptor (cp, peer)) {
    printf ("unknown contact %s\n", peer);
    free (data_with_cd);
    return 0;
  }
  uint64_t seq = readb64u (cp->counter);
  memcpy (data_with_cd + CHAT_DESCRIPTOR_SIZE, message, mlen);
  /* send_to_contact initializes the message ack in data_with_cd/cp */
#ifdef DEBUG_PRINT
  printf ("sending seq %" PRIu64 ":\n", seq);
#endif /* DEBUG_PRINT */
  send_to_contact (data_with_cd, dsize, peer, sock,
                   NULL, 16, NULL, 16, 4, ALLNET_PRIORITY_LOCAL, 1);
  free (data_with_cd);
  return seq;
}
示例#3
0
static void add_global_op(unsigned id, int fd) {
    global_op_ptr ele = malloc_or_fail(sizeof(global_op_ele), "add_global_op");
    ele->id = id;
    ele->client_fd = fd;
    ele->worker_ack_cnt = 0;
    ele->next = global_ops;
    global_ops = ele;
}
示例#4
0
/* Create a new chunk */
chunk_ptr chunk_new(size_t len) {
    size_t more_bytes = len == 0  ? 0 : WORD_BYTES * (len - 1);
    chunk_ptr cp = (chunk_ptr) malloc_or_fail(sizeof(chunk_t) + more_bytes,
					      "chunk_new");
    if (cp == NULL && chunk_check_level >= 1) {
	chunk_error("Could not allocate chunk", cp);
    }
    cp->length = len;
    return cp;
}
示例#5
0
chunk_queue_ptr chunk_queue_new() {
    chunk_queue_ptr cq = malloc_or_fail(sizeof(chunk_queue), "chunk_queue_new");
    cq->length = 0;
    cq->alloc_length = 0;
    cq->tail = 0;
    cq->head = 0;
    cq->elements = NULL;
    pthread_mutex_init(&cq->mutex,NULL);
    pthread_cond_init(&cq->cvar,NULL);
    return cq;
}
示例#6
0
static void add_deferred_operand(word_t operator_id, chunk_ptr operand,
				 unsigned offset) {
    word_t w;
    operand_ptr ele = malloc_or_fail(sizeof(operand_ele),
				     "add_deferred_operand");
    ele->operand = operand;
    ele->offset = offset;
    ele->next = NULL;
    if (keyvalue_find(deferred_operand_table, operator_id, &w)) {
	operand_ptr ls = (operand_ptr) w;
	/* Put as second element in list */
	ele->next = ls->next;
	ls->next = ele;
    } else {
	keyvalue_insert(deferred_operand_table, operator_id, (word_t) ele);
    }
}
示例#7
0
static int handle_data (int sock, struct allnet_header * hp,
                        char * data, int dsize, char ** contact, keyset * kset,
                        char ** message, char ** desc, int * verified,
                        time_t * sent, int * duplicate, int * broadcast)
{
  int verif = 0;
  char * text = NULL;
  int tsize = decrypt_verify (hp->sig_algo, data, dsize, contact, kset, &text,
                              (char *) (hp->source), hp->src_nbits,
                              (char *) (hp->destination), hp->dst_nbits, 0);
  if (tsize < 0) {
    printf ("no signature to verify, but decrypted from %s\n", *contact);
    tsize = -tsize;
  } else if (tsize > 0) {
    verif = 1;
  }
  if (tsize < CHAT_DESCRIPTOR_SIZE) {
#ifdef DEBUG_PRINT
    printf ("decrypted packet has size %d, min is %zd, dropping\n",
            tsize, CHAT_DESCRIPTOR_SIZE);
#endif /* DEBUG_PRINT */
    return 0;
  }
  if (*contact == NULL) {
#ifdef DEBUG_PRINT
    printf ("contact not known\n");
#endif /* DEBUG_PRINT */
    if (text != NULL) free (text);
    return 0;
  }
#ifdef DEBUG_PRINT
  printf ("got packet from contact %s\n", *contact);
#endif /* DEBUG_PRINT */
  struct chat_descriptor * cdp = (struct chat_descriptor *) text;

  int app = readb32u (cdp->app_media.app);
  if (app != XCHAT_ALLNET_APP_ID) {
#ifdef DEBUG_PRINT
    printf ("handle_data ignoring unknown app %08x\n", app);
    print_buffer (text, CHAT_DESCRIPTOR_SIZE, "chat descriptor", 100, 1);
#endif /* DEBUG_PRINT */
    return 0;
  }
  int media = readb32u (cdp->app_media.media);
  if ((media != ALLNET_MEDIA_TEXT_PLAIN) &&
      (media != ALLNET_MEDIA_PUBLIC_KEY)) {
#ifdef DEBUG_PRINT
    printf ("handle_data ignoring media type %08x\n", media);
    print_buffer (text, CHAT_DESCRIPTOR_SIZE, "chat descriptor", 100, 1);
#endif /* DEBUG_PRINT */
    return 0;
  }
  char * cleartext = text + CHAT_DESCRIPTOR_SIZE;
  int msize = tsize - CHAT_DESCRIPTOR_SIZE;

  long long int seq = readb64u (cdp->counter);
  if (seq == COUNTER_FLAG) {
    do_chat_control (*contact, *kset, text, tsize, sock, hp->hops + 4);
    send_ack (sock, hp, cdp->message_ack, verif, *contact, *kset);
    if (*contact != NULL) { free (*contact); *contact = NULL; }
    if (text != NULL) free (text);
    return 0;
  }

  *broadcast = 0;
  *duplicate = 0;
  if (was_received (*contact, *kset, seq))
    *duplicate = 1;

  save_incoming (*contact, *kset, cdp, cleartext, msize);

  if (media == ALLNET_MEDIA_PUBLIC_KEY) {
    cleartext = "received a key for an additional device";
    msize = strlen (cleartext);
  }

  *desc = chat_descriptor_to_string (cdp, 0, 0);
  *verified = verif;
  if (sent != NULL)
    *sent = (readb64u (cdp->timestamp) >> 16) & 0xffffffff;

  *message = malloc_or_fail (msize + 1, "handle_data message");
  memcpy (*message, cleartext, msize);
  (*message) [msize] = '\0';   /* null-terminate the message */

  /* printf ("hp->hops = %d\n", hp->hops); */

  send_ack (sock, hp, cdp->message_ack, verif, *contact, *kset);

  free (text);

  return msize;
}
示例#8
0
static int handle_clear (struct allnet_header * hp, char * data, int dsize,
                         char ** contact, char ** message,
                         int * verified, int * broadcast)
{
  if (hp->sig_algo == ALLNET_SIGTYPE_NONE) {
#ifdef DEBUG_PRINT
    printf ("ignoring unsigned clear packet of size %d\n", dsize);
#endif /* DEBUG_PRINT */
    return 0;
  }
  struct allnet_app_media_header * amhp =
    (struct allnet_app_media_header *) data;
  char * verif = data;
  int media = 0;
  if (dsize >= sizeof (struct allnet_app_media_header) + 2)
    media = readb32u (amhp->media);
  if ((media != ALLNET_MEDIA_TEXT_PLAIN) &&
      (media != ALLNET_MEDIA_TIME_TEXT_BIN)) {
#ifdef DEBUG_PRINT
    printf ("handle_clear ignoring unknown media type %08x, dsize %d\n",
            media, dsize);
#endif /* DEBUG_PRINT */
    return 0;
  }
  int ssize = readb16 (data + (dsize - 2)) + 2;  /* size of the signature */
  if ((ssize <= 2) || (dsize <= ssize)) {
    printf ("data packet size %d less than sig %d, dropping\n", dsize, ssize);
    return 0;
  }
  data += sizeof (struct allnet_app_media_header);
  int text_size = dsize - sizeof (struct allnet_app_media_header) - ssize;
  char * sig = data + text_size;
#ifdef DEBUG_PRINT
  printf ("data size %d, text %d + sig %d\n", dsize, text_size, ssize);
#endif /* DEBUG_PRINT */
  struct bc_key_info * keys;
  int nkeys = get_other_keys (&keys);
  int i;
/* print_buffer (verif, dsize - ssize, "verifying BC message", dsize, 1); */
  for (i = 0; i < nkeys; i++) {
    if ((matches ((unsigned char *) (keys [i].address), ADDRESS_BITS,
                  hp->source, hp->src_nbits) > 0) &&
        (allnet_verify (verif, dsize - ssize, sig, ssize - 2,
                        keys [i].pub_key))) {
      *contact = strcpy_malloc (keys [i].identifier,
                                "handle_message broadcast contact");
      *message = malloc_or_fail (text_size + 1, "handle_clear message");
      memcpy (*message, data, text_size);
      (*message) [text_size] = '\0';   /* null-terminate the message */
      *broadcast = 1;
      *verified = 1;
#ifdef DEBUG_PRINT
      printf ("verified bc message, contact %s, %d bytes\n",
              keys [i].identifier, text_size);
#endif /* DEBUG_PRINT */
      return text_size;
    } 
#ifdef DEBUG_PRINT
      else {
      printf ("matches (%02x%02x, %02x%02x/%d) == %d\n",
              keys [i].address [0] & 0xff, keys [i].address [1] & 0xff,
              hp->source [0] & 0xff, hp->source [1] & 0xff, hp->src_nbits,
              matches (keys [i].address, ADDRESS_BITS,
                       hp->source, hp->src_nbits));
      printf ("verify (%p/%d, %p/%d, %p/%d) == %d\n",
              data, dsize - ssize, sig, ssize - 2,
              keys [i].pub_key, keys [i].pub_klen,
              verify (data, dsize - ssize, sig, ssize - 2,
                      keys [i].pub_key, keys [i].pub_klen));
    }
#endif /* DEBUG_PRINT */
  }
  printf ("unable to verify bc message\n");
  return 0;   /* did not match */
}
示例#9
0
shadow_mgr new_shadow_mgr(bool do_cudd, bool do_local, bool do_dist, chaining_t chaining) {
    if (!(do_cudd || do_local || do_dist)) {
	err(true, "Must have at least one active evaluation mode");
    }
    shadow_mgr mgr = (shadow_mgr) malloc_or_fail(sizeof(shadow_ele),
						 "new_shadow_mgr");
    mgr->do_cudd = do_cudd;
    mgr->do_local = do_local;
    mgr->do_dist = do_dist;

    mgr->c2r_table = word_keyvalue_new();
    mgr->r2c_table = word_keyvalue_new();
    mgr->nvars = 0;
    mgr->nzvars = 0;

    ref_t r = REF_ZERO;
    DdNode *n = NULL;

    if (do_cudd) {
	/* Modified CUDD Parameters */
	unsigned int numVars = 0; /* Default 0 */
	unsigned int numVarsZ = 0; /* Default 0 */
	unsigned int numSlots = 1u<<18; /* Default 256 */
	unsigned int cacheSize = 1u<<22; /* Default 262144 */
	/* Default 67,108,864 */
	unsigned long int maxMemory = (1u<<31) + 32UL * 1024 * 1024 * 1024;
#if 0
	// Use defaults
	numSlots = 256;
	cacheSize = 262144;
	maxMemory = 67108864;
#endif
	mgr->bdd_manager = Cudd_Init(numVars, numVarsZ, numSlots, cacheSize, maxMemory);
	Cudd_AutodynDisable(mgr->bdd_manager);
	Cudd_AutodynDisableZdd(mgr->bdd_manager);
#ifndef NO_CHAINING
	Cudd_ChainingType ct = CUDD_CHAIN_NONE;
	switch (chaining) {
	case CHAIN_NONE:
	    ct = CUDD_CHAIN_NONE;
	    report(0, "No chaining enabled");
	    break;
	case CHAIN_CONSTANT:
	    ct = CUDD_CHAIN_CONSTANT;
	    report(0, "Constant chaining enabled");
	    break;
	case CHAIN_ALL:
	    ct = CUDD_CHAIN_ALL;
	    report(0, "Or chaining enabled");
	    break;
	default:
	    err(true, "Invalid chaining mode %d\n", chaining);
	}
	Cudd_SetChaining(mgr->bdd_manager, ct);
#endif	
	n = Cudd_ReadLogicZero(mgr->bdd_manager);
	report(0, "Using CUDD Version %s", CUDD_VERSION);
    }
    if (do_ref(mgr)) {
	mgr->ref_mgr = new_ref_mgr();
	if (!do_cudd) {
	    n = ref2dd(mgr, r);
	}
    } else {
	r = dd2ref(n, IS_BDD);
    }
    reference_dd(mgr, n);
    add_ref(mgr, r, n);
    return mgr;
}