Exemplo n.º 1
0
static
int _vlsctlc_pack_cmd_unpost_service(struct vlsctlc* lsctlc, void* buf, int len)
{
    struct unpost_service_args* args = &lsctlc->args.unpost_service_args;
    void* len_addr = NULL;
    int tsz = 0;
    int bsz = 0;
    int ret = 0;
    int i = 0;

    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    if (lsctlc->bound_cmd != VLSCTL_UNPOST_SERVICE) {
        return 0;
    }
    set_uint16(buf + tsz, lsctlc->bound_cmd);
    tsz += sizeof(uint16_t);
    set_uint16(len_addr = buf + tsz, 0);
    tsz += sizeof(uint16_t);

    memcpy(buf + tsz, &args->hash, sizeof(vsrvcHash));
    bsz += sizeof(vsrvcHash);
    tsz += sizeof(vsrvcHash);

    for (i = 0;i < args->naddrs; i++) {
        ret = _aux_vlsctlc_pack_addr(buf + tsz, len - tsz, &args->addrs[i]);
        bsz += ret;
        tsz += ret;
    }
    set_uint16(len_addr, bsz);
    return tsz;
}
Exemplo n.º 2
0
static
int _vlsctlc_pack_cmd_probe_service(struct vlsctlc* lsctlc, void* buf, int len)
{
    struct probe_service_args* args = &lsctlc->args.probe_service_args;
    void* len_addr = NULL;
    int tsz = 0;
    int bsz = 0;

    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    if (lsctlc->bound_cmd != VLSCTL_PROBE_SERVICE) {
        return 0;
    }

    set_uint16(buf + tsz, lsctlc->bound_cmd);
    tsz += sizeof(uint16_t);
    set_uint16(len_addr = buf + tsz, 0);
    tsz += sizeof(uint16_t);

    memcpy(buf + tsz, &args->hash, sizeof(vsrvcHash));
    bsz += sizeof(vsrvcHash);
    tsz += sizeof(vsrvcHash);

    set_uint16(len_addr, bsz);
    return tsz;
}
Exemplo n.º 3
0
static
int _vlsctlc_pack_cmd_bogus_query(struct vlsctlc* lsctlc, void* buf, int len)
{
    struct bogus_query_args* args = &lsctlc->args.bogus_query_args;
    void* len_addr = NULL;
    int tsz = 0;
    int bsz = 0;
    int ret = 0;

    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    if (lsctlc->bound_cmd != VLSCTL_BOGUS_QUERY) {
        return 0;
    }
    set_uint16(buf + tsz, lsctlc->bound_cmd);
    tsz += sizeof(uint16_t);
    set_uint16(len_addr = buf + tsz, 0);
    tsz += sizeof(uint16_t);

    set_int32(buf + tsz, args->queryId);
    bsz += sizeof(int32_t);
    tsz += sizeof(int32_t);

    ret = _aux_vlsctlc_pack_addr(buf + tsz, len - tsz, &args->addr);
    bsz += ret;
    tsz += ret;

    set_uint16(len_addr, bsz);
    return tsz;
}
Exemplo n.º 4
0
static
int _vlsctlc_pack_cmd_join_node(struct vlsctlc* lsctlc, void* buf, int len)
{
    struct join_node_args* args = &lsctlc->args.join_node_args;
    void* len_addr = NULL;
    int tsz = 0;
    int bsz = 0;

    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    if (lsctlc->bound_cmd != VLSCTL_JOIN_NODE) {
        return 0;
    }
    set_uint16(buf + tsz, lsctlc->bound_cmd);
    tsz += sizeof(uint16_t);

    set_uint16(len_addr = buf + tsz, 0);
    tsz += sizeof(uint16_t);

    bsz = _aux_vlsctlc_pack_addr(buf + tsz, len - tsz, &args->addr);
    tsz += bsz;

    set_uint16(len_addr, bsz);
    return tsz;
}
Exemplo n.º 5
0
/** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
 * <b>username</b> and <b>hostname</b> as provided.  Return the number
 * of bytes in the request. */
static ssize_t
build_socks_resolve_request(char **out,
                            const char *username,
                            const char *hostname,
                            int reverse,
                            int version)
{
  size_t len = 0;
  tor_assert(out);
  tor_assert(username);
  tor_assert(hostname);

  if (version == 4) {
    len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
    *out = tor_malloc(len);
    (*out)[0] = 4;      /* SOCKS version 4 */
    (*out)[1] = '\xF0'; /* Command: resolve. */
    set_uint16((*out)+2, htons(0)); /* port: 0. */
    set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
    memcpy((*out)+8, username, strlen(username)+1);
    memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
  } else if (version == 5) {
    int is_ip_address;
    tor_addr_t addr;
    size_t addrlen;
    int ipv6;
    is_ip_address = tor_addr_parse(&addr, hostname) != -1;
    if (!is_ip_address && reverse) {
      log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
      return -1;
    }
    ipv6 = reverse && tor_addr_family(&addr) == AF_INET6;
    addrlen = reverse ? (ipv6 ? 16 : 4) : 1 + strlen(hostname);
    len = 6 + addrlen;
    *out = tor_malloc(len);
    (*out)[0] = 5; /* SOCKS version 5 */
    (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
    (*out)[2] = 0; /* reserved. */
    if (reverse) {
      (*out)[3] = ipv6 ? 4 : 1;
      if (ipv6)
        memcpy((*out)+4, tor_addr_to_in6_addr8(&addr), 16);
      else
        set_uint32((*out)+4, tor_addr_to_ipv4n(&addr));
    } else {
      (*out)[3] = 3;
      (*out)[4] = (char)(uint8_t)(addrlen - 1);
      memcpy((*out)+5, hostname, addrlen - 1);
    }
    set_uint16((*out)+4+addrlen, 0); /* port */
  } else {
    tor_assert(0);
  }

  return len;
}
void HCI_HOST_NUM_COMPLETED_PACKETS_T_PDU::set_num_completed_pkts(uint8 index,uint16 handle,uint16 pkts )
{
    if (index>= get_num_handles()) 
    {
        //TODO - throw an exception 
    }

    uint16 offset = HCI_HOST_NUM_COMPLETED_PACKETS_T_pduSize;
    offset += index *(2+2) ;
    set_uint16(offset,handle) ; offset +=2 ;
    set_uint16(offset,pkts) ;           
}
Exemplo n.º 7
0
/** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
 * <b>username</b> and <b>hostname</b> as provided.  Return the number
 * of bytes in the request. */
static int
build_socks_resolve_request(char **out,
                            const char *username,
                            const char *hostname,
                            int reverse,
                            int version)
{
  size_t len = 0;
  tor_assert(out);
  tor_assert(username);
  tor_assert(hostname);

  if (version == 4) {
    len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
    *out = tor_malloc(len);
    (*out)[0] = 4;      /* SOCKS version 4 */
    (*out)[1] = '\xF0'; /* Command: resolve. */
    set_uint16((*out)+2, htons(0)); /* port: 0. */
    set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
    memcpy((*out)+8, username, strlen(username)+1);
    memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
  } else if (version == 5) {
    int is_ip_address;
    struct in_addr in;
    size_t addrlen;
    is_ip_address = tor_inet_aton(hostname, &in);
    if (!is_ip_address && reverse) {
      log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
      return -1;
    }
    addrlen = reverse ? 4 : 1 + strlen(hostname);
    len = 6 + addrlen;
    *out = tor_malloc(len);
    (*out)[0] = 5; /* SOCKS version 5 */
    (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
    (*out)[2] = 0; /* reserved. */
    (*out)[3] = reverse ? 1 : 3;
    if (reverse) {
      set_uint32((*out)+4, in.s_addr);
    } else {
      (*out)[4] = (char)(uint8_t)(addrlen - 1);
      memcpy((*out)+5, hostname, addrlen - 1);
    }
    set_uint16((*out)+4+addrlen, 0); /* port */
  } else {
    tor_assert(0);
  }

  return len;
}
Exemplo n.º 8
0
/** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
 * <b>username</b> and <b>hostname</b> as provided.  Return the number
 * of bytes in the request. */
static int
build_socks_connect_request(char **out,
                            const char *username,
                            const char *hostname,
                            int reverse,
                            int version)
{
  size_t len = 0;
  assert(out);
  assert(username);
  assert(hostname);

  if (version == 4) {
    len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
    *out = malloc(len);
    (*out)[0] = 4;      /* SOCKS version 4 */
    (*out)[1] = '\x01'; /* Command: connect. */
    set_uint16((*out)+2, htons(80)); /* port: 80. */
    set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
    memcpy((*out)+8, username, strlen(username)+1);
    memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
  } else if (version == 5) {
    int is_ip_address;
    struct in_addr in;
    size_t addrlen;
    is_ip_address = inet_aton(hostname, &in);
    if (!is_ip_address && reverse) {
      fprintf(stderr,"Tried to do a reverse lookup on a non-IP!\n");
      return -1;
    }
    addrlen = is_ip_address ? 4 : 1 + strlen(hostname);
    len = 6 + addrlen;
    *out = malloc(len);
    (*out)[0] = 5; /* SOCKS version 5 */
    (*out)[1] = '\x01'; /* connect. */
    (*out)[2] = 0; /* reserved. */
    (*out)[3] = is_ip_address ? 1 : 3;
    if (is_ip_address) {
      set_uint32((*out)+4, in.s_addr);
    } else {
      (*out)[4] = (char)(uint8_t)(addrlen - 1);
      memcpy((*out)+5, hostname, addrlen - 1);
    }
    set_uint16((*out)+4+addrlen, htons(80)); /* port */
  } else {
    assert(0);
  }

  return len;
}
Exemplo n.º 9
0
static
int _vlsctlc_pack_cmd(struct vlsctlc* lsctlc, void* buf, int len)
{
    struct vlsctlc_pack_cmd_desc* desc = lsctlc_pack_cmd_desc;
    void* len_addr = NULL;
    int ret = 0;
    int tsz = 0;
    int bsz = 0;

    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    set_uint8(buf + tsz, VLSCTL_VERSION);
    tsz += sizeof(uint8_t);
    set_uint8(buf + tsz, (uint8_t)lsctlc->type);
    tsz += sizeof(uint8_t);
    set_uint16(len_addr = buf + tsz, 0);
    tsz += sizeof(uint16_t);
    set_uint32(buf + tsz, VLSCTL_MAGIC);
    tsz += sizeof(uint32_t);

    for (; desc->cmd; desc++) {
        ret = desc->cmd(lsctlc, buf + tsz, len - tsz);
        if (ret < 0) {
            return -1;
        }
        bsz += ret;
        tsz += ret;
    }
    *(uint16_t*)len_addr = (uint16_t)bsz;
    return tsz;
}
Exemplo n.º 10
0
/** Pack global_id and circ_id; set *tag to the result. (See note on
 * cpuworker_main for wire format.) */
static void
tag_pack(char *tag, uint64_t conn_id, circid_t circ_id)
{
  /*XXXX RETHINK THIS WHOLE MESS !!!! !NM NM NM NM*/
  set_uint64(tag, conn_id);
  set_uint16(tag+8, circ_id);
}
Exemplo n.º 11
0
Arquivo: onion.c Projeto: BwRy/Astoria
/** Fill <b>cell_out</b> with a correctly formatted version of the
 * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
 * failure. */
int
created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
{
  if (check_created_cell(cell_in) < 0)
    return -1;

  memset(cell_out->payload, 0, sizeof(cell_out->payload));
  cell_out->command = cell_in->cell_type;

  switch (cell_in->cell_type) {
  case CELL_CREATED:
  case CELL_CREATED_FAST:
    tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
    memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
    break;
  case CELL_CREATED2:
    tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
    set_uint16(cell_out->payload, htons(cell_in->handshake_len));
    memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
    break;
  default:
    return -1;
  }
  return 0;
}
Exemplo n.º 12
0
static void
test_util_format_unaligned_accessors(void *ignored)
{
  (void)ignored;
  char buf[9] = "onionsoup"; // 6f6e696f6e736f7570

  tt_u64_op(get_uint64(buf+1), OP_EQ, htonll(U64_LITERAL(0x6e696f6e736f7570)));
  tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e));
  tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69));
  tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e);

  set_uint8(buf+7, 0x61);
  tt_mem_op(buf, OP_EQ, "onionsoap", 9);

  set_uint16(buf+6, htons(0x746f));
  tt_mem_op(buf, OP_EQ, "onionstop", 9);

  set_uint32(buf+1, htonl(0x78696465));
  tt_mem_op(buf, OP_EQ, "oxidestop", 9);

  set_uint64(buf+1, htonll(U64_LITERAL(0x6266757363617465)));
  tt_mem_op(buf, OP_EQ, "obfuscate", 9);
 done:
  ;
}
Exemplo n.º 13
0
/** Pack global_id and circ_id; set *tag to the result. (See note on
 * cpuworker_main for wire format.) */
static void
tag_pack(char *tag, uint64_t chan_id, circid_t circ_id)
{
  /*XXXX RETHINK THIS WHOLE MESS !!!! !NM NM NM NM*/
  /*XXXX DOUBLEPLUSTHIS!!!! AS AS AS AS*/
  set_uint64(tag, chan_id);
  set_uint16(tag+8, circ_id);
}
Exemplo n.º 14
0
int _vlsctlc_pack_cmd_host_exit(struct vlsctlc* lsctlc, void* buf, int len)
{
    int tsz = 0;
    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    if (lsctlc->bound_cmd != VLSCTL_HOST_EXIT) {
        return 0;
    }

    set_uint16(buf + tsz, lsctlc->bound_cmd);
    tsz += sizeof(uint16_t);
    set_uint16(buf + tsz, 0);
    tsz += sizeof(uint16_t);

    return tsz;
}
Exemplo n.º 15
0
int _vlsctlc_pack_cmd_cfg_dump(struct vlsctlc* lsctlc, void* buf, int len)
{
    int tsz = 0;
    vassert(lsctlc);
    vassert(buf);
    vassert(len > 0);

    if (lsctlc->bound_cmd != VLSCTL_CFG_DUMP) {
        return 0;
    }

    set_uint16(buf + tsz, lsctlc->bound_cmd);
    tsz += sizeof(uint16_t);
    set_uint16(buf + tsz, 0);
    tsz += sizeof(uint16_t);

    return tsz;
}
Exemplo n.º 16
0
/*
 * Set the named boolean in the given nvlist_t.
 *
 * @param       attrs
 *              the nvlist_t to search
 *
 * @param       which
 *              the string key for this element in the list
 *
 * @param       val
 *              the value to set
 *
 * @return      0
 *              if successful
 *
 * @return      EINVAL
 *              if there is an invalid argument
 *
 * @return      ENOMEM
 *              if there is insufficient memory
 */
int
set_boolean(
    nvlist_t *attrs,
    char *which,
    boolean_t val)
{
    /*
     * Use set_uint16 to distinguish "attr = B_FALSE" from
     * "attribute unset".
     */
    return (set_uint16(attrs, which, val == B_TRUE ? 1 : 0));
}
Exemplo n.º 17
0
Arquivo: onion.c Projeto: BwRy/Astoria
/** Fill <b>cell_out</b> with a correctly formatted version of the
 * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
 * failure.  This is a cell we didn't originate if <b>relayed</b> is true. */
static int
create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
                        int relayed)
{
  uint8_t *p;
  size_t space;
  if (check_create_cell(cell_in, relayed) < 0)
    return -1;

  memset(cell_out->payload, 0, sizeof(cell_out->payload));
  cell_out->command = cell_in->cell_type;

  p = cell_out->payload;
  space = sizeof(cell_out->payload);

  switch (cell_in->cell_type) {
  case CELL_CREATE:
    if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
      memcpy(p, NTOR_CREATE_MAGIC, 16);
      p += 16;
      space -= 16;
    }
    /* Fall through */
  case CELL_CREATE_FAST:
    tor_assert(cell_in->handshake_len <= space);
    memcpy(p, cell_in->onionskin, cell_in->handshake_len);
    break;
  case CELL_CREATE2:
    tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
    set_uint16(cell_out->payload, htons(cell_in->handshake_type));
    set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
    memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
    break;
  default:
    return -1;
  }

  return 0;
}
Exemplo n.º 18
0
static
int _aux_vlsctlc_pack_addr(void* buf, int len, struct sockaddr_in* addr)
{
    int tsz = 0;

    vassert(addr);
    vassert(buf);
    vassert(len > 0);

    tsz += sizeof(uint16_t); // skip family;
    set_uint16(buf + tsz, addr->sin_port);
    tsz += sizeof(int16_t);
    set_uint32(buf + tsz, addr->sin_addr.s_addr);
    tsz += sizeof(uint32_t);

    return tsz;
}
Exemplo n.º 19
0
static int
build_socks5_resolve_ptr_request(char **out, const void *_addr)
{
  size_t len;
  const struct in_addr *addr=_addr;

  len = 12;
  *out = malloc(len);
  (*out)[0] = 5;      /* SOCKS version 5 */
  (*out)[1] = '\xF1'; /* Command: reverse resolve.
                         see doc/socks-extensions.txt*/
  (*out)[2] = '\x00'; /* RSV */
  (*out)[3] = '\x01'; /* ATYP: IP V4 address: X'01' */

  set_uint32((*out)+4, addr->s_addr);/*IP*/
  set_uint16((*out)+4+4, 0); /* port */

  return len;
}
Exemplo n.º 20
0
// Return NULL on error
pico_data * get_pico_data(picoboard p) {
	char request_char[] = {0x01};
	DWORD bytes_written;
	// Send data request
	if(!WriteFile(p, request_char, sizeof(request_char), &bytes_written, NULL) 
		|| bytes_written != sizeof(request_char)) return NULL;
	
	pico_data *data = malloc(sizeof(pico_data));
	int err_tracker = 1;
	err_tracker *= set_uint16(p, NULL); // ID data
	err_tracker *= set_uint16(p, &(data->D));
	err_tracker *= set_uint16(p, &(data->C));
	err_tracker *= set_uint16(p, &(data->B));
	err_tracker *= set_bool(p, &(data->button));
	err_tracker *= set_uint16(p, &(data->A));
	err_tracker *= set_uint16(p, &(data->light));
	err_tracker *= set_uint16(p, &(data->sound));
	err_tracker *= set_uint16(p, &(data->slider));
	if(!err_tracker) return free(data), NULL;
	return data;
}
Exemplo n.º 21
0
/*
 * Helper function to setup and run a SCSI inquiry command.
 */
int
do_inquiry(int fd, int evpd, unsigned int codepage,
	   void *resp, int resplen, unsigned int timeout)
{
	struct inquiry_command	cmd;
	struct sg_io_hdr	hdr;
	unsigned char		sense[SENSE_BUFF_LEN];

	memset(&cmd, 0, sizeof(cmd));
	cmd.op = OPERATION_CODE_INQUIRY;
	if (evpd) {
		inquiry_command_set_evpd(&cmd);
		cmd.page = codepage;
	}
	set_uint16(cmd.length, resplen);
	PRINT_HEX((unsigned char *) &cmd, sizeof(cmd));

	memset(&hdr, 0, sizeof(hdr));
	hdr.interface_id	= 'S';
	hdr.cmdp		= (unsigned char *) &cmd;
	hdr.cmd_len		= sizeof(cmd);
	hdr.dxfer_direction	= SG_DXFER_FROM_DEV;
	hdr.dxferp		= resp;
	hdr.dxfer_len		= resplen;
	hdr.sbp			= sense;
	hdr.mx_sb_len		= sizeof(sense);
	hdr.timeout		= get_prio_timeout(timeout, SGIO_TIMEOUT);

	if (ioctl(fd, SG_IO, &hdr) < 0) {
		PRINT_DEBUG("do_inquiry: IOCTL failed!\n");
		return -RTPG_INQUIRY_FAILED;
	}

	if (scsi_error(&hdr)) {
		PRINT_DEBUG("do_inquiry: SCSI error!\n");
		return -RTPG_INQUIRY_FAILED;
	}
	PRINT_HEX((unsigned char *) resp, resplen);

	return 0;
}
Exemplo n.º 22
0
Arquivo: onion.c Projeto: BwRy/Astoria
/** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
 * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
 * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
 * RELAY_PAYLOAD_SIZE bytes available.  Return 0 on success, -1 on failure. */
int
extended_cell_format(uint8_t *command_out, uint16_t *len_out,
                     uint8_t *payload_out, const extended_cell_t *cell_in)
{
  uint8_t *p;
  if (check_extended_cell(cell_in) < 0)
    return -1;

  p = payload_out;
  memset(p, 0, RELAY_PAYLOAD_SIZE);

  switch (cell_in->cell_type) {
  case RELAY_COMMAND_EXTENDED:
    {
      *command_out = RELAY_COMMAND_EXTENDED;
      *len_out = TAP_ONIONSKIN_REPLY_LEN;
      memcpy(payload_out, cell_in->created_cell.reply,
             TAP_ONIONSKIN_REPLY_LEN);
    }
    break;
  case RELAY_COMMAND_EXTENDED2:
    {
      *command_out = RELAY_COMMAND_EXTENDED2;
      *len_out = 2 + cell_in->created_cell.handshake_len;
      set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
      if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
        return -1;
      memcpy(payload_out+2, cell_in->created_cell.reply,
             cell_in->created_cell.handshake_len);
    }
    break;
  default:
    return -1;
  }

  return 0;
}
Exemplo n.º 23
0
Arquivo: onion.c Projeto: BwRy/Astoria
/** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
 * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
 * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
 * RELAY_PAYLOAD_SIZE bytes available.  Return 0 on success, -1 on failure. */
int
extend_cell_format(uint8_t *command_out, uint16_t *len_out,
                   uint8_t *payload_out, const extend_cell_t *cell_in)
{
  uint8_t *p, *eop;
  if (check_extend_cell(cell_in) < 0)
    return -1;

  p = payload_out;
  eop = payload_out + RELAY_PAYLOAD_SIZE;

  memset(p, 0, RELAY_PAYLOAD_SIZE);

  switch (cell_in->cell_type) {
  case RELAY_COMMAND_EXTEND:
    {
      *command_out = RELAY_COMMAND_EXTEND;
      *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
      set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
      set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
      if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
        memcpy(p+6, NTOR_CREATE_MAGIC, 16);
        memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
      } else {
        memcpy(p+6, cell_in->create_cell.onionskin,
               TAP_ONIONSKIN_CHALLENGE_LEN);
      }
      memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
    }
    break;
  case RELAY_COMMAND_EXTEND2:
    {
      uint8_t n = 2;
      *command_out = RELAY_COMMAND_EXTEND2;

      *p++ = n; /* 2 identifiers */
      *p++ = SPECTYPE_IPV4; /* First is IPV4. */
      *p++ = 6; /* It's 6 bytes long. */
      set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
      set_uint16(p+4, htons(cell_in->orport_ipv4.port));
      p += 6;
      *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
      *p++ = 20; /* It's 20 bytes long */
      memcpy(p, cell_in->node_id, DIGEST_LEN);
      p += 20;

      /* Now we can send the handshake */
      set_uint16(p, htons(cell_in->create_cell.handshake_type));
      set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
      p += 4;

      if (cell_in->create_cell.handshake_len > eop - p)
        return -1;

      memcpy(p, cell_in->create_cell.onionskin,
             cell_in->create_cell.handshake_len);

      p += cell_in->create_cell.handshake_len;
      *len_out = p - payload_out;
    }
    break;
  default:
    return -1;
  }

  return 0;
}
Exemplo n.º 24
0
void BNEP_HCI_SWITCH_ROLE_RSP_T_PDU::set_phandle( uint16 value )
{
    set_uint16 ( BNEP_HCI_SWITCH_ROLE_RSP_T_phandle , value );
}
Exemplo n.º 25
0
void BNEP_CONNECT_IND_T_PDU::set_loc_uuid( uint16 value )
{
    set_uint16 ( BNEP_CONNECT_IND_T_loc_uuid , value );
}
Exemplo n.º 26
0
void BNEP_REGISTER_REQ_T_PDU::set_phandle( uint16 value )
{
    set_uint16 ( BNEP_REGISTER_REQ_T_phandle , value );
}
void HCICommandCompletePDU::set_op_code ( uint16 opcode )
{
    set_uint16(3,opcode) ;
} 
Exemplo n.º 28
0
Arquivo: onion.c Projeto: ageis/tor
/** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
 * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
 * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
 * RELAY_PAYLOAD_SIZE bytes available.  Return 0 on success, -1 on failure. */
int
extend_cell_format(uint8_t *command_out, uint16_t *len_out,
                   uint8_t *payload_out, const extend_cell_t *cell_in)
{
  uint8_t *p;
  if (check_extend_cell(cell_in) < 0)
    return -1;

  p = payload_out;

  memset(p, 0, RELAY_PAYLOAD_SIZE);

  switch (cell_in->cell_type) {
  case RELAY_COMMAND_EXTEND:
    {
      *command_out = RELAY_COMMAND_EXTEND;
      *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
      set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
      set_uint16(p+4, htons(cell_in->orport_ipv4.port));
      if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
        memcpy(p+6, NTOR_CREATE_MAGIC, 16);
        memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
      } else {
        memcpy(p+6, cell_in->create_cell.onionskin,
               TAP_ONIONSKIN_CHALLENGE_LEN);
      }
      memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
    }
    break;
  case RELAY_COMMAND_EXTEND2:
    {
      uint8_t n_specifiers = 2;
      *command_out = RELAY_COMMAND_EXTEND2;
      extend2_cell_body_t *cell = extend2_cell_body_new();
      link_specifier_t *ls;
      {
        /* IPv4 specifier first. */
        ls = link_specifier_new();
        extend2_cell_body_add_ls(cell, ls);
        ls->ls_type = LS_IPV4;
        ls->ls_len = 6;
        ls->un_ipv4_addr = tor_addr_to_ipv4h(&cell_in->orport_ipv4.addr);
        ls->un_ipv4_port = cell_in->orport_ipv4.port;
      }
      {
        /* Then RSA id */
        ls = link_specifier_new();
        extend2_cell_body_add_ls(cell, ls);
        ls->ls_type = LS_LEGACY_ID;
        ls->ls_len = DIGEST_LEN;
        memcpy(ls->un_legacy_id, cell_in->node_id, DIGEST_LEN);
      }
      if (should_include_ed25519_id_extend_cells(NULL, get_options()) &&
          !ed25519_public_key_is_zero(&cell_in->ed_pubkey)) {
        /* Then, maybe, the ed25519 id! */
        ++n_specifiers;
        ls = link_specifier_new();
        extend2_cell_body_add_ls(cell, ls);
        ls->ls_type = LS_ED25519_ID;
        ls->ls_len = 32;
        memcpy(ls->un_ed25519_id, cell_in->ed_pubkey.pubkey, 32);
      }
      cell->n_spec = n_specifiers;

      /* Now, the handshake */
      cell->create2 = create2_cell_body_new();
      cell->create2->handshake_type = cell_in->create_cell.handshake_type;
      cell->create2->handshake_len = cell_in->create_cell.handshake_len;
      create2_cell_body_setlen_handshake_data(cell->create2,
                                         cell_in->create_cell.handshake_len);
      memcpy(create2_cell_body_getarray_handshake_data(cell->create2),
             cell_in->create_cell.onionskin,
             cell_in->create_cell.handshake_len);

      ssize_t len_encoded = extend2_cell_body_encode(
                             payload_out, RELAY_PAYLOAD_SIZE,
                             cell);
      extend2_cell_body_free(cell);
      if (len_encoded < 0 || len_encoded > UINT16_MAX)
        return -1;
      *len_out = (uint16_t) len_encoded;
    }
    break;
  default:
    return -1;
  }

  return 0;
}
Exemplo n.º 29
0
void BNEP_CONNECT_REQ_T_PDU::set_flags( uint16 value )
{
    set_uint16 ( BNEP_CONNECT_REQ_T_flags , value );
}
Exemplo n.º 30
0
void BNEP_REGISTER_REQ_T_PDU::set_flags( uint16 value )
{
    set_uint16 ( BNEP_REGISTER_REQ_T_flags , value );
}