Ejemplo n.º 1
0
int txop_ward_init(shpeer_t *cli_peer, tx_ward_t *ward)
{
  unsigned char context[128];
  shkey_t sig_key;
  tx_context_t ctx;
  tx_t *tx;
  int err;

  if (ward->ward_stamp == SHTIME_UNDEFINED)
    ward->ward_stamp = shtime();
  if (ward->ward_expire == SHTIME_UNDEFINED)
    ward->ward_expire = shtime_adj(shtime(), MAX_SHARE_SESSION_TIME);

  memcpy(&ward->ward_tx.tx_peer, shpeer_kpriv(sharedaemon_peer()), sizeof(shpeer_t));


  memset(&ctx, 0, sizeof(ctx));
  err = inittx_context(&ctx, get_tx_key(ward), ashkey_uniq());
  if (err)
    return (err);

  err = tx_save(&ctx);
  if (err)
    return (err);

  /* store key reference to generated context */
  memcpy(&ward->ward_ctx, &ctx.ctx_tx.tx_key, sizeof(ward->ward_ctx));


  return (0);
}
Ejemplo n.º 2
0
int tx_send(shpeer_t *cli_peer, tx_t *tx)
{
  unsigned char *data = (unsigned char *)tx;
  size_t data_len;
  tx_ledger_t *l;
  int err;
  txop_t *op;

  if (!data)
    return (SHERR_INVAL);

  data_len = get_tx_size(tx);
  if (!data_len)
    return (0);

  op = get_tx_op(tx->tx_op);
  if (!op)
    return (SHERR_INVAL);

  if (op->op_send) {
    err = op->op_send(cli_peer, data);
    if (err) {
      if (err == SHERR_OPNOTSUPP)
        return (0);

      return (err);
    }
  }

  if (is_tx_stored(tx->tx_op)) {
    l = ledger_load(shpeer_kpriv(sharedaemon_peer()), shtime());
    if (l) {
      ledger_tx_add(l, (tx_t *)data);   
      ledger_close(l);
    }
  }

#if 0
  /* encapsulate for network transfer. */
  tx_wrap(shpeer_kpriv(cli_peer), (tx_t *)tx);
#endif

  if (cli_peer) {
    sched_tx_sink(shpeer_kpriv(cli_peer), data, data_len);
  } else {
    sched_tx(data, data_len);
  }

  return (0);
}
Ejemplo n.º 3
0
shkey_t *get_bond_key(shkey_t *sender, shkey_t *receiver, shkey_t *ref)
{
  static shkey_t ret_key;
  shkey_t keys[3];
  shkey_t *key;

  if (!sender)
    sender = shpeer_kpriv(sharedaemon_peer());
  else if (!receiver)
    receiver = shpeer_kpriv(sharedaemon_peer());

  if (!ref)
    ref = ashkey_blank();

  memcpy(&keys[0], sender, sizeof(shkey_t));
  memcpy(&keys[1], receiver, sizeof(shkey_t));
  memcpy(&keys[2], ref, sizeof(shkey_t));

  key = shkey_bin(keys, sizeof(shkey_t) * 3);
  memcpy(&ret_key, key, sizeof(ret_key));
  shkey_free(&key);

  return (&ret_key);
}
Ejemplo n.º 4
0
int sharedaemon_bcast_recv(void)
{
    struct sockaddr_in addr;
    socklen_t addr_len;
    struct timeval to;
    fd_set read_set;
    shpeer_t *peer;
    char dgram[512];
    ssize_t r_len;
    int err;

    err = bcast_recv_init();
    if (err) {
        return (err);
    }

    FD_ZERO(&read_set);
    FD_SET(_bcast_recv_fd, &read_set);

    /* nonblocking read */
    memset(&to, 0, sizeof(to));
    err = select(_bcast_recv_fd+1, &read_set, NULL, NULL, &to);
    if (err < 0) {
        return (-errno);
    }
    if (err == 0) {
//fprintf(stderr, "\rWaiting for select(_bcast_recv_fd)..");
//fflush(stderr);
        return (0); /* nothing to read */
    }

    addr_len = sizeof(addr);
    memset(&addr, 0, addr_len);
    r_len = recvfrom(_bcast_recv_fd,
                     dgram, sizeof dgram, 0, &addr, &addr_len);
    if (r_len < 0) {
        fprintf(stderr, "DEBUG: %d = recvfrom()\n", r_len);
        return (-errno);
    }

    /* and who are you? */
    if (r_len < sizeof(shpeer_t)) {
        fprintf(stderr, "DEBUG: <%d bytes> pending..\n", r_len);
        return (SHERR_INVAL);
    }

#if 0
    now = shtime();
    tx = (tx_t *)dgram;
    if (shtime_after(tx->tx_stamp, now) ||
            shtime_before(tx->tx_stamp, shtime_adj(now, -BROADCAST_TIMEOUT))) {
        /* broadcast message must indicate sane time-frame. */
        return (SHERR_TIME);
    }

    switch (tx->tx_op) {
    case TX_PEER:
        peer_tx = (tx_peer_t *)dgram;
        if (0 != shkey_cmp(&tx->tx_peer, shpeer_kpriv(&peer_tx->peer)))
            return (SHERR_INVAL); /* only accept self-referencing broadcast */
    }
#endif


    /* share-daemon broadcasting it's peer address. */
    peer = (shpeer_t *)dgram;

    if (!shkey_cmp(shpeer_kpub(sharedaemon_peer()), shpeer_kpub(peer))) {
        /* this is not a shared peer */
        return (0); /* all done */
    }

    if (!shkey_cmp(shpeer_kpub(sharedaemon_peer()), shpeer_kpub(peer))) {
        fprintf(stderr, "DEBUG: invalid key\n");
        /* this is a peer referencing ourselves. */
        //err = sharedaemon_netclient_alias(&addr);
    }



    switch (peer->type) {
    case SHNET_PEER_LOCAL:
    case SHNET_PEER_IPV4:

        /*
           memset(&addr, '\000', sizeof(struct sockaddr_in));
           memcpy(&addr, &peer_tx->peer.addr, sizeof(peer_tx->peer.addr));
           */
        fprintf(stderr, "DEBUG: received UDP broadcast with peer \"%s\"\n", shpeer_print(peer));
        fprintf(stderr, "DEBUG: received UDP broadcast for \"%s\" port %d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
        if (!peer->addr.sin_port)
            break; /* otay */

        addr.sin_family = AF_INET;
        err = sharedaemon_netclient_conn(peer, &addr);
        if (err)
            return (err);

        break;
    }
    fprintf(stderr, "DEBUG: processed bcast recv\n");

    return (0);
}