static int bsd_number( bsd_ctx_t *ctx, bsd_data_t *x, const uint8_t *buffer, int length) {
  int nread;
  CHECK_LENGTH( 1);
  uint8_t opcode = buffer[0];

  CHECK_DECODE( decodeInteger( ctx, x, buffer, length, &BS_NUMBER_INTEGER));

  /* Only go on if integer decoding failed */
  switch( opcode) {

  case BS_N_NULL:
    decodeNull( ctx, x);
    break;

  case BS_N_FLOAT32:
    CHECK_LENGTH( 5);
    x->kind = BSD_DOUBLE;
    float f;
    memcpy( &f, buffer + 1, sizeof(float));
    ntoh( &f, sizeof(float), sendian.float_);
    x->content.d = f;
    break;

  case BS_N_FLOAT64:
    CHECK_LENGTH( 9);
    x->kind = BSD_DOUBLE;
    memcpy( &(x->content.d), buffer + 1, sizeof(double));
    ntoh( &(x->content.d), sizeof(double), sendian.double_);
    break;
  default: return bsd_error( x, BSD_EINVALID);
  }

  return nread;
}
/**
 * Try to decode a number under given context.
 * This algorithm assume that all number kind are consecutive and in the same order.
 */
static int decodeInteger( bsd_ctx_t *ctx, bsd_data_t *x, const uint8_t *buffer, int length,
    const bs_integer_encoding_t *enc) {
  int nread = 0;
  uint8_t opcode = buffer[0];
  CHECK_LENGTH( 1);

  x->kind = BSD_INT;
  if( (enc->tiny_zero_opcode + enc->tiny_min) <= opcode && opcode <= (enc->tiny_zero_opcode + enc->tiny_max)) {
    /* tiny number */
    x->content.i = opcode - enc->tiny_zero_opcode;
  } else if( enc->small_pos_opcode <= opcode && opcode < enc->small_neg_opcode) {
    /* small positive number */
    CHECK_LENGTH( 2);
    x->content.i = ((opcode - enc->small_pos_opcode) << 8) + buffer[1] + enc->tiny_max + 1;
  } else if( enc->small_neg_opcode <= opcode && opcode < enc->medium_pos_opcode) {
    /* small negative number */
    CHECK_LENGTH( 2);
    x->content.i = -(((opcode - enc->small_neg_opcode) << 8) + buffer[1]) + enc->tiny_min - 1;
  } else if( enc->medium_pos_opcode <= opcode && opcode < enc->medium_neg_opcode) {
    /* medium positive number */
    CHECK_LENGTH( 3);
    x->content.i = ((opcode - enc->medium_pos_opcode) << 16) + (buffer[1] << 8) + buffer[2] + enc->small_max + 1;
  } else if( enc->medium_neg_opcode <= opcode && opcode < enc->large_pos_opcode) {
    /* medium negative number */
    CHECK_LENGTH( 3);
    x->content.i = -(((opcode - enc->medium_neg_opcode) << 16) + (buffer[1] << 8) + buffer[2]) + enc->small_min - 1;
  } else if( enc->large_pos_opcode <= opcode && opcode < enc->large_neg_opcode) {
    /* large positive opcode */
    CHECK_LENGTH( 4);
    x->content.i = ((opcode - enc->large_pos_opcode) << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] + enc->medium_max + 1;
  } else if( enc->large_neg_opcode <= opcode && opcode <= enc->last_large_neg_opcode) {
    /* large negative opcode */
    CHECK_LENGTH( 4);
    x->content.i = -(((opcode - enc->large_neg_opcode) << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]) + enc->medium_min - 1;
  } else if( enc->int32_opcode == opcode) {
    /* big endian 32 bits integer */
    CHECK_LENGTH( 5);
    int32_t val;
    memcpy( &val, buffer + 1, sizeof(int32_t));
    ntoh( &val, sizeof(int32_t), sendian.int32_);
    x->content.i = val;
  } else if( enc->int64_opcode == opcode) {
    /* big endian 64 bits integer */
    CHECK_LENGTH( 9);
    memcpy( &(x->content.i), buffer + 1, sizeof(int64_t));
    ntoh( &(x->content.i), sizeof(int64_t), sendian.int64_);
  } else {
    return 0;
  }

  return nread;
}
static int bsd_global( bsd_ctx_t *ctx, bsd_data_t *x, const uint8_t *buffer, int length) {
  int nread;
  CHECK_LENGTH( 1);
  uint8_t opcode = buffer[0];


  CHECK_DECODE( decodeInteger( ctx, x, buffer, length, &BS_GLOBAL_INTEGER));
  CHECK_DECODE( decodeString( ctx, x, buffer, length, &BS_GLOBAL_STRING));
  CHECK_DECODE( decodeCollection( ctx, x, buffer, length, NULL, &BS_GLOBAL_LIST));
  CHECK_DECODE( decodeCollection( ctx, x, buffer, length, NULL, &BS_GLOBAL_MAP));
  CHECK_DECODE( decodeClass( ctx, x, buffer, length));
  if( 0x60 <= opcode && opcode <= 0x6f) { /* object (short form) */
    CHECK_ERROR( bsd_object( ctx, x, opcode - 0x60));
    return nread;
  }
  switch( opcode) {
  case BS_G_NULL: /* null */
    decodeNull( ctx, x);
    break;
  case 0x01: /* boolean true */
    x->kind = BSD_BOOL;
    x->content.boolean = 1;
    break;
  case 0x02: /* boolean false */
    x->kind = BSD_BOOL;
    x->content.boolean = 0;
    break;
  case 0x70: /* object (long form) */
    CHECK_SUBDECODE( bsd_uis( ctx, x, buffer + nread, length - nread), BSD_INT);
    CHECK_ERROR( bsd_object( ctx, x, x->content.i + 0x10));
    break;
  case BS_G_FLOAT32: /* float */
    CHECK_LENGTH( 5);
    x->kind = BSD_DOUBLE;
    float f;
    memcpy( &f, buffer + 1, sizeof(float));
    ntoh( &f, sizeof(float), sendian.float_);
    x->content.d = f;
    break;
  case BS_G_FLOAT64: /* double */
    CHECK_LENGTH( 9);
    x->kind = BSD_DOUBLE;
    memcpy( &(x->content.d), buffer + 1, sizeof(double));
    ntoh( &(x->content.d), sizeof(double), sendian.double_);
    break;
  default: return bsd_error( x, BSD_EINVALID);
  }

  return nread;
}
Beispiel #4
0
clock_t
NTP::time()
{
  if (m_sock == NULL) return (ENOTSOCK);
  const int PACKET_MAX = 48;
  uint8_t packet[PACKET_MAX];
  int res;
  memset(packet, 0, PACKET_MAX);
  packet[0] = 0b11100011;
  packet[1] = 0;
  packet[2] = 6;
  packet[3] = 0xEC;
  packet[12] = 49;
  packet[13] = 0x4E;
  packet[14] = 49;
  packet[15] = 52;
  res = m_sock->send(packet, sizeof(packet), m_server, PORT);
  delay(TIMEOUT);
  uint8_t dest[4];
  uint16_t port;
  res = m_sock->recv(packet, sizeof(packet), dest, port);
  if (res != sizeof(packet)) return (0L);
  int32_t* tp = (int32_t*) &packet[40];
  return (ntoh(*tp) + (m_zone * 3600L));
}
Beispiel #5
0
Response& operator<<(Response& os, int64_t n)
{
    std::ostream out(os.data.get());
    n = ntoh(n);
    out.write((const char*)&n, 8);
    return os;
}
Beispiel #6
0
int
reclaim_ts(const unsigned char *p, size_t psize, intmax_t *secs, int *nsecs)
{
	const unsigned char *t = p;
	size_t tsize = psize;
	const double frac = 0.000244140625; /*2^-12*/
	const size_t billion = 1000000000;

    unsigned char marker = 0;
    memcpy(&marker, t, sizeof(unsigned char));
    t += sizeof(unsigned char);
    tsize--;


    switch (marker) {
    case CCN_MARKER_VERSION:
    	break;
    default:
    	return(-__LINE__);
    }

    if (tsize > 48) {
    	/*Timestamp is too big*/
    	return(-__LINE__);
    }

    intmax_t ts = 0;
    ntoh(t, tsize, &ts);

    *secs = ts * frac;
    *nsecs = (ts * frac - *secs) * billion;

    return(0);
}
Beispiel #7
0
typename std::enable_if<std::is_integral<T>::value>::type
deserialize(InputArchive& ar, T& value)
{
    T tmp;
    ar >> tmp;
    value = ntoh(tmp);
}
Beispiel #8
0
Response& operator<<(Response& os, std::string const& s)
{
    std::ostream out(os.data.get());
    uint16_t len = ntoh((uint16_t)s.length());
    out.write((const char*)&len, 2);
    out << s;
    return os;
}
Beispiel #9
0
Response& operator<<(Response& os, std::u16string const& s)
{
    std::ostream out(os.data.get());
    uint16_t len = ntoh((uint16_t)s.length());
    out.write((const char*)&len, 2);
    out.write((const char*)s.c_str(), s.length()*sizeof(char16_t));
    return os;
}
Beispiel #10
0
	template<class T> uint16_t read(uint16_t address, T& val)
	{
		uint8_t  buf[sizeof(T)];
		uint16_t addr = readBytes(address, buf, sizeof(T));
		if (addr != INVALID_ADDRESS)
			ntoh(buf, val);
		return addr;
	}
Beispiel #11
0
void deserialize(InputArchive& ar, boost::units::quantity<U, T>& q)
{
    static_assert(std::is_integral<T>::value,
            "Only integral based quantities are supported");
    T tmp;
    ar >> tmp;
    q = boost::units::quantity<U, T>::from_value(ntoh(tmp));
}
Beispiel #12
0
 // Post: No effect if there is no next chunk.
 void next_chunk()
 {
   if(status_ == PREFIX_CHUNK)
   {
     prefix_type prefix = ntoh(storage_.prefix_);
     new (&storage_.data_) data_type(prefix, 0);
     status_ = DATA_CHUNK;
   }
 }
Beispiel #13
0
void mld_interface::handle_mldv2_membership_report(const in6_addr &src,
						   mldv2_report *mldhdr,
						   int len) {
	m_stats.counter(ReportV2Count, RX)++;
	mld->stats().counter(ReportV2Count, RX)++;

	mldv2_mrec *mrec = mldhdr->mrecs();
	int clen = 0;

	int reccount = ntoh(mldhdr->nmrecs());

	for (int i = 0; i < reccount && clen < len; i++, mrec = mrec->next()) {
		clen += sizeof(mldv2_mrec);
		if (clen <= len)
			clen += sizeof(in6_addr) * ntoh(mrec->nsrcs);
	}

	if (clen > len) {
		if (should_log(MESSAGE_ERR))
			log().writeline("Dropped badly formed MLDv2 Membership");

		m_stats.counter(ReportV2Count, Bad)++;
		mld->stats().counter(ReportV2Count, Bad)++;
		return;
	}

	mrec = mldhdr->mrecs();
	for (int i = 0; i < reccount; i++, mrec = mrec->next()) {
		if (!IN6_IS_ADDR_MULTICAST(&mrec->mca) ||
		     IN6_IS_ADDR_MC_NODELOCAL(&mrec->mca) ||
		     IN6_IS_ADDR_MC_LINKLOCAL(&mrec->mca))
			continue;

		address_set sources;
		in6_addr *srcs = mrec->sources();
		for (uint16_t j = 0; j < ntoh(mrec->nsrcs); j++)
			sources += srcs[j];

		handle_mode_change_for_group(2, src, mrec->mca, mrec->type, sources);
	}
}
Beispiel #14
0
AmfDouble AmfDouble::deserialize(v8::const_iterator& it, v8::const_iterator end, SerializationContext&) {
	if (it == end || *it++ != AMF_DOUBLE)
		throw std::invalid_argument("AmfDouble: Invalid type marker");

	// Dates are always encoded as 64bit double in network order.
	if (end - it < 8)
		throw std::out_of_range("Not enough bytes for AmfDouble");

	double v;
	std::copy(it, it + 8, reinterpret_cast<u8 *>(&v));
	it += 8;

	return AmfDouble(ntoh(v));
}
int LocalSocketStream::recv(Head & head, void * buf, unsigned int size)
{
	if (fd < 0) return -1;
	if (!buf) return -1;
	if (size == 0) return -1;

	uint8_t buf_head[sizeof(head)];
	int rc = ::read(fd, buf_head, sizeof(buf_head));
	if (rc < 0) return -1;
	if (rc == 0) return 0;
	deserialize(head, buf_head);
	ntoh(head);

	if (head.size > size) return -2;

	rc = ::read(fd, buf, head.size);
	if (rc < 0) return -3;
	return rc;
}
Beispiel #16
0
bool RecvFile( SOCKET sock, const char* strFileName, int nBufSize)
{
	//get file header
	NETF_STAT nfstat;
	RecvNetfStat( sock, &nfstat );

	ntoh( nfstat );

	//receive the file content.
	FILE* pf = fopen( strFileName, "wb" );
	if( pf==NULL )return false;

	bool bOk = RecvFileStream( sock, pf, nfstat.nfs_size, nBufSize );

	fclose( pf );

	//change the file mode to the right mode
	bOk = bOk && (chmod( strFileName, nfstat.nfs_mode )==0);

	return bOk;
}
Beispiel #17
0
static int bsd_float( bsd_ctx_t *ctx, bsd_data_t *x, const uint8_t *buffer, int length) {
  int nread;
  union { uint32_t u; float f; } val;

  CHECK_LENGTH( 4);
  memcpy( &val.f, buffer, sizeof(float));
  ntoh( &val.f, sizeof(float), sendian.float_);
  x->content.d = val.f;
  x->kind = BSD_DOUBLE;

  // check for escape sequence
  if( 0xFFFFFFFFU == val.u) {
    CHECK_LENGTH( 5);
    switch( buffer[4]) {
    case 0x00: decodeNull( ctx, x); break;
    case 0x01: break;
    default: return bsd_error( x, BSD_EINVALID);
    }
  }

  return nread;
}
Beispiel #18
0
static int bsd_double( bsd_ctx_t *ctx, bsd_data_t *x, const uint8_t *buffer, int length) {
  int nread;
  union { uint64_t u; double d; } val;

  CHECK_LENGTH( 8);
  memcpy( &val.d, buffer, sizeof(double));
  ntoh( &val.d, sizeof(double), sendian.float_);
  x->content.d = val.d;
  x->kind = BSD_DOUBLE;

  // check for escape sequence
  if( 0xFFFFFFFFFFFFFFFFULL == val.u) {
    CHECK_LENGTH( 9);
    switch( buffer[8]) {
    case 0x00: decodeNull( ctx, x); break;
    case 0x01: break;
    default: return bsd_error( x, BSD_EINVALID);
    }
  }

  return nread;
}
Beispiel #19
0
AmfVector<T> AmfVector<T, typename VectorProperties<T>::type>::deserialize(
	v8::const_iterator& it, v8::const_iterator end, SerializationContext& ctx) {
	if (it == end || *it++ != VectorProperties<T>::marker)
		throw std::invalid_argument("AmfVector: Invalid type marker");

	int type = AmfInteger::deserializeValue(it, end);
	if ((type & 0x01) == 0)
		return ctx.getObject<AmfVector<T>>(type >> 1);

	unsigned int stride = VectorProperties<T>::size;
	size_t count = type >> 1;

	if (it == end)
		throw std::out_of_range("Not enough bytes for AmfVector");

	bool fixed = (*it++ == 0x01);

	if (static_cast<size_t>(end - it) < count * stride)
		throw std::out_of_range("Not enough bytes for AmfVector");

	std::vector<T> values(count);
	for (size_t i = 0; i < count; ++i) {
		// Convert value bytes to requested type.
		T val;
		std::copy(it, it + stride, reinterpret_cast<u8 *>(&val));
		it += stride;

		// Values are stored in network order.
		values[i] = ntoh(val);
	}

	AmfVector<T> ret(values, fixed);
	ctx.addObject<AmfVector<T>>(ret);

	return ret;
}
Beispiel #20
0
static void deserialize_value(lua_State *L, luaL_Buffer* buffer, char** frame, int* index, int* gindex, size_t* len, uint16_t* key) {
    if (!lua_checkstack(L, 2)) {
        luaL_error(L, "cannot deserialize: stack won't grow");
    }
    uint16_t size = 0x0000;
    read_object(L, buffer, frame, index, 1, len);
    uint8_t type = (*frame)[*index];
    *index = *index + 1;
    *gindex = *gindex + 1;
    switch (type) {
    case BIN_NIL: {
        // printf("\r\n\t(nil[1])[%p]", *frame);
        lua_pushnil(L);
        break;
    }

    case BIN_BOOLEAN: {
        read_object(L, buffer, frame, index, 1, len);
        // printf("\r\n\t(boolean[2][%p]", *frame);
        lua_pushboolean(L, (*frame)[*index]);
        *index = *index + 1;
        *gindex = *gindex + 1;
        break;
    }

    case BIN_DOUBLE: {
        double number = 0;
        size = sizeof(number);
        read_object(L, buffer, frame, index, size, len);
        // printf("\r\n\t(number[%d])[%p]", size, *frame);
        char* b = (char*) &number;
        int i;
        for (i = 0; i < size; i++) {
            b[i] = (*frame)[*index + i];
        }
        if (sendian.double_) ntoh(&number, sizeof(number), sendian.double_);
        lua_pushnumber(L, number);
        *index = *index + size;
        *gindex = *gindex + size;
        break;
    }

    case BIN_INTEGER: {
        int32_t number = 0;
        size = sizeof(number);
        read_object(L, buffer, frame, index, size, len);
        // printf("\r\n\t(number[%d])[%p]", size, *frame);
        char* b = (char*) &number;
        int i;
        for (i = 0; i < size; i++) {
            b[i] = (*frame)[*index + i];
        }
        if (sendian.int32_) ntoh(&number, sizeof(number), sendian.int32_);
        lua_pushinteger(L, number);
        *index = *index + size;
        *gindex = *gindex + size;
        break;
    }

    case BIN_REF: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] =(*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        // printf("\r\n\t(ref[%d])[%p]", size, *frame);
        get_from_cache(L, size);
        break;
    }

    case BIN_STRING: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] = (*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        read_object(L, buffer, frame, index, size, len);
        // printf("\r\n\t(string[%d])[%p]", size, *frame);
        lua_pushlstring(L, &((*frame)[*index]), size);
        *index = *index + size;
        *gindex = *gindex + size;
        add_to_cache(L, key);
        break;
    }

    case BIN_FUNCTION: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] = (*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        // printf("\r\n\t(function[%d])[%p]", size, *frame);
        read_object(L, buffer, frame, index, size, len);
        Dfunction dfunction;
        dfunction.counter = 0;
        dfunction.size = size;
        dfunction.buffer = &((*frame)[*index]);
        lua_load(L, f_reader, &dfunction, "function");
        *index = *index + size;
        *gindex = *gindex + size;
        add_to_cache(L, key);
        break;
    }

    case BIN_TABLE: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] = (*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        lua_newtable(L);
        // printf("\r\n\t{table[%d][%p]=", size, *frame);
        add_to_cache(L, key);
        int top = lua_gettop(L);
        while (size > 0) {
            // printf("\r\n\t|key[%d][%p]", *index, *frame);
            deserialize_value(L, buffer, frame, index, gindex, len, key);
            // printf("\r\n\t+value[%d][%p]", *index, *frame);
            deserialize_value(L, buffer, frame, index, gindex, len, key);
            lua_rawset(L, top);
            size--;
        }
        // printf("\r\n\t)");
        // printf("\r\n\t}");
        break;
    }

    default:
        luaL_error(L, "cannot deserialize: unsupported type [%d] at %d", type, *gindex);
    }
}
void deserialize(SequenceNumber& sn, InputArchive& ar)
{
    SequenceNumber::value_type tmp = 0;
    deserialize(tmp, ar);
    sn = SequenceNumber(ntoh(tmp));
}
Beispiel #22
0
}

uint8_t ntoh(uint8_t t) {
	return t;
}

template<typename T>
struct ReadWrapper {
	ReadWrapper(T& t) : t_(t) {}
	T& t_;
};

template<typename T>
std::istream& operator >> (std::istream& is, ReadWrapper<T> const& r) {
	is.read(reinterpret_cast<char*>(&r.t_), sizeof(r.t_));
	r.t_ = ntoh(r.t_);
	return is;
}

template<typename T>
ReadWrapper<T> read(T& t) {
	return ReadWrapper<T>(t);
}

struct NitSectionHeader {
	uint8_t tableid_;
	uint16_t length_;

	uint16_t network_id_;
	
	uint8_t version_;
uint32_t AuthTokenTable::Entry::timestamp_host_order() const {
    return ntoh(token_->timestamp);
}
hw_authenticator_type_t AuthTokenTable::Entry::authenticator_type() const {
    hw_authenticator_type_t result = static_cast<hw_authenticator_type_t>(
        ntoh(static_cast<uint32_t>(token_->authenticator_type)));
    return result;
}
Beispiel #25
0
void pim_interface::handle_assert(const sockaddr_in6 *from, pim_assert_message *msg, uint16_t length) {
	m_stats.counter(AssertCount, RX)++;

	if (should_log(MESSAGE_CONTENT)) {
		base_stream &os = log();
		os.inc_level();
		_debug_pim_dump(os, *msg);
		os.dec_level();
	}

	if (!get_neighbour(from->sin6_addr)) {
		m_stats.counter(AssertCount, Bad)++;
		return;
	}

	inet6_addr grpaddr(msg->gaddr.addr, msg->gaddr.masklen);
	pim_group_node *node = pim->get_group(grpaddr);

	bool rpt = msg->rpt();
	uint32_t metric_pref = msg->metric_pref();
	uint32_t metric = ntoh(msg->metric);

	if (node) {
		if (!IN6_IS_ADDR_UNSPECIFIED(&msg->saddr.addr)) {
			pim_group_source_state *state = node->get_state(msg->saddr.addr);
			if (state) {
				pim_common_oif::assert_state prev = pim_common_oif::AssertNoInfo;
				bool existed = false;

				pim_common_oif *oif = (pim_common_oif *)state->get_oif(owner());
				if (oif) {
					prev = oif->current_assert_state();
					existed = true;
				}

				state->handle_assert(owner(), from->sin6_addr,
						     rpt, metric, metric_pref);

				/* for some future reason the oif may be released meanwhile */
				oif = (pim_common_oif *)state->get_oif(owner());

				if (!oif && existed) {
					/* transitioned somehow */
					return;
				}

				pim_common_oif::assert_state current = oif ?
					oif->current_assert_state() : pim_common_oif::AssertNoInfo;

				if (current != pim_common_oif::AssertNoInfo || current != prev) {
					/* (S,G) Assert state machine is not NoInfo
					 * or a transition occurred: no (*,G) handling */
					return;
				}
			}
		}

		if (node->has_wildcard()) {
			node->wildcard()->handle_assert(owner(), from->sin6_addr,
						        rpt, metric, metric_pref);
		}
	}
}
Beispiel #26
0
void pim_interface::handle_hello(const sockaddr_in6 *from,
				 pim_hello_message *msg, uint16_t len) {
	m_stats.counter(HelloCount, RX)++;

	/* rejected by configuration */
	if (!conf()->neigh_acl_accepts(from->sin6_addr))
		return;

	uint16_t holdtime = 0;
	bool has_dr_priority = false;
	uint32_t dr_priority = 0;
	bool has_genid = false;
	uint32_t genid = mrd::get_randu32();
	bool has_lan_delay = false;
	uint16_t propdelay = 0, overrinter = 0;
	bool trackbit = false;

	int address_count = 0;
	pim_encoded_unicast_address *addresses = 0;
	int old_address_count = 0;
	pim_encoded_unicast_address *old_addresses = 0;

	int slen = sizeof(pim_hello_message);

	pim_hello_option *opt = msg->options();

	while (slen < len) {
		uint16_t optlen = ntoh(opt->length);

		switch (ntoh(opt->type)) {
		case pim_hello_option::holdtime:
			if (optlen == 2)
				holdtime = ntoh(opt->data16()[0]);
			break;
		case pim_hello_option::lan_prune_delay:
			if (optlen == 4) {
				has_lan_delay = true;
				propdelay = ntoh(opt->data16()[0]);
				overrinter = ntoh(opt->data16()[1]);
				trackbit = (propdelay & 0x8000) != 0;
				propdelay &= 0x7fff;
			}
			break;
		case pim_hello_option::dr_priority:
			if (optlen == 4) {
				has_dr_priority = true;
				dr_priority = ntoh(opt->data32()[0]);
			}
			break;
		case pim_hello_option::genid:
			if (optlen == 4) {
				has_genid = true;
				genid = ntoh(opt->data32()[0]);
			}
			break;
		case pim_hello_option::addrlist:
			if ((optlen % sizeof(pim_encoded_unicast_address)) == 0) {
				address_count = optlen / sizeof(pim_encoded_unicast_address);
				addresses = (pim_encoded_unicast_address *)opt->data();
			}
			break;
		case pim_hello_option::cisco_old_addrlist:
			if ((optlen % sizeof(pim_encoded_unicast_address)) == 0) {
				old_address_count = optlen / sizeof(pim_encoded_unicast_address);
				old_addresses = (pim_encoded_unicast_address *)opt->data();
			}
			break;
		}

		slen += sizeof(pim_hello_option) + optlen;
		opt = opt->next();
	}

	pim_neighbour *neigh;

	if ((neigh = get_neighbour(from->sin6_addr))) {
		if (holdtime == 0) {
			neighbour_timed_out(neigh);
			return;
		}

		if (!neigh->compare_genid(genid)) {
			if (should_log(NORMAL))
				neigh->log().writeline("Had different GenID, forcing timeout.");
			remove_neighbour(neigh, false);
			neigh = 0;
		}
	}

	bool is_new = false;

	if (!neigh) {
		if (!(neigh = allocate_neighbour(from->sin6_addr))) {
			if (should_log(DEBUG))
				log().writeline("Failed to allocate neighbor state.");
			return;
		}

		is_new = true;
	}

	if (!conf()->support_old_cisco_addrlist()) {
		old_addresses = 0;
		old_address_count = 0;
	}

	neigh->update_from_hello(addresses, address_count,
				 old_addresses, old_address_count, holdtime);

	if (has_dr_priority)
		neigh->set_dr_priority(dr_priority);
	if (has_genid)
		neigh->set_genid(genid);
	if (has_lan_delay)
		neigh->set_lan_delay(propdelay, overrinter, trackbit);

	if (is_new)
		found_new_neighbour(neigh);

	check_lan_delay();
	elect_subnet_dr();
}
Beispiel #27
0
 operator host_type() const { return host_cast(ntoh(BASE::get())); }
Beispiel #28
0
int main(int argc, char *argv[]) {
	std::string inp_file = argv[1];
	std::string out_file = argv[2];
	std::cout << "input: " << inp_file << "; output: " << out_file << std::endl;

	struct stat st;
	stat(inp_file.c_str(), &st);
	auto inp_fd = open(inp_file.c_str(), O_RDONLY, 0); 
	assert(inp_fd != -1);
	auto inp_bof = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, inp_fd, 0);
	auto inp_data = inp_bof;
	assert(inp_data != MAP_FAILED);
	auto inp_eof = inp_data + st.st_size;
	
	const uint64_t max_out_size = 1024ULL * 1024ULL * 1024ULL * 1024ULL;
	const uint64_t chunk_size = 8 * 1024 * 1024;
	
	uint64_t out_len = 0;
	auto out_fd = open(out_file.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
	assert(out_fd != -1);
	auto out_bof = (char *)mmap(NULL, max_out_size, PROT_READ | PROT_WRITE, MAP_SHARED, out_fd, 0);
	auto out_data = out_bof;
	if (out_bof == MAP_FAILED) {
		std::cout << strerror(errno) << std::endl;
		return EXIT_FAILURE;
	}
	assert(out_data != MAP_FAILED);
	
	int add_order_cnt = 0;

	for (; inp_data < inp_eof - 3;) {
		if (out_data - out_bof + 1024 > out_len) { // grow output
			out_len += chunk_size;
			ftruncate(out_fd, out_len);
		}

		auto len = ntohs(*reinterpret_cast<uint16_t *>(inp_data));
		inp_data += sizeof(uint16_t);

		*reinterpret_cast<uint16_t *>(out_data) = len;
		out_data += sizeof(uint16_t);
		std::memcpy(out_data, inp_data, len);

		switch (*inp_data) {
		case (char)espeed::itch_msg_type::timestamp_seconds: {
			auto timestamp_seconds = reinterpret_cast<struct espeed::itch_timestamp_seconds *>(out_data);
			timestamp_seconds->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::order_book_directory: {
			auto order_book_directory = reinterpret_cast<struct espeed::itch_order_book_directory *>(out_data);
			order_book_directory->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::combination_order_book_directory: {
			auto combination_order_book_directory = reinterpret_cast<struct espeed::itch_combination_order_book_directory *>(out_data);
			combination_order_book_directory->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::system_event: {
			auto system_event = reinterpret_cast<struct espeed::itch_system_event *>(out_data);
			system_event->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::trade_state_information: {
			auto trade_state_information = reinterpret_cast<struct espeed::itch_trade_state_information *>(out_data);
			trade_state_information->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::add_order: {
			auto add_order = reinterpret_cast<struct espeed::itch_add_order *>(out_data);
			add_order->ntoh();
			add_order_cnt++;
			break;
		}
		case (char)espeed::itch_msg_type::order_executed: {
			auto order_executed = reinterpret_cast<struct espeed::itch_order_executed *>(out_data);
			order_executed->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::order_executed_with_price: {
			auto order_executed_with_price = reinterpret_cast<struct espeed::itch_order_executed_with_price *>(out_data);
			order_executed_with_price->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::two_sided_order_executed: {
			auto two_sided_order_executed = reinterpret_cast<struct espeed::itch_two_sided_order_executed *>(out_data);
			two_sided_order_executed->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::order_cancel: {
			auto order_cancel = reinterpret_cast<struct espeed::itch_order_cancel *>(out_data);
			order_cancel->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::order_replace: {
			auto order_replace = reinterpret_cast<struct espeed::itch_order_replace *>(out_data);
			order_replace->ntoh();
			break;
		}
		case (char)espeed::itch_msg_type::order_trade: {
			auto order_trade = reinterpret_cast<struct espeed::itch_order_trade *>(out_data);
			order_trade->ntoh();
			break;
		}
		default:
			std::cout << "break" << std::endl;
			goto eof_label;
		}
		
		inp_data += len;
		out_data += len;
	}

eof_label:
	munmap(inp_bof, st.st_size);
	close(inp_fd);

	munmap(out_bof, max_out_size);
	ftruncate(out_fd, out_data - out_bof);
	close(out_fd);
	
	std::cout << "add order: " << add_order_cnt << std::endl;
	
	return EXIT_SUCCESS;
}
Beispiel #29
0
	static inline bool decompress(I begin,I end,O out)
	{
		volatile char i32c[4];
		void *const i32cp = (void *)i32c;
		unsigned int bufLen = LZ4_compressBound(ZT_COMPRESSION_BLOCK_SIZE);
		char *buf = new char[bufLen * 2];
		char *buf2 = buf + bufLen;

		try {
			I inp(begin);
			while (inp != end) {
				i32c[0] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				i32c[1] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				i32c[2] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				i32c[3] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				unsigned int originalSize = ntoh(*((const uint32_t *)i32cp));
				i32c[0] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				i32c[1] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				i32c[2] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				i32c[3] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
				uint32_t _compressedSize = ntoh(*((const uint32_t *)i32cp));
				unsigned int compressedSize = _compressedSize & 0x7fffffff;

				if (compressedSize) {
					if (compressedSize > bufLen) {
						delete [] buf;
						return false;
					}
					unsigned int readLen = 0;
					while ((readLen < compressedSize)&&(inp != end)) {
						buf[readLen++] = (char)*inp;
						++inp;
					}
					if (readLen != compressedSize) {
						delete [] buf;
						return false;
					}

					if (LZ4_uncompress_unknownOutputSize(buf,buf2,compressedSize,bufLen) != (int)originalSize) {
						delete [] buf;
						return false;
					} else out((const void *)buf2,(unsigned int)originalSize);
				} else { // stored
					if (originalSize > bufLen) {
						delete [] buf;
						return false;
					}
					unsigned int readLen = 0;
					while ((readLen < originalSize)&&(inp != end)) {
						buf[readLen++] = (char)*inp;
						++inp;
					}
					if (readLen != originalSize) {
						delete [] buf;
						return false;
					}

					out((const void *)buf,(unsigned int)originalSize);
				}
			}

			delete [] buf;
			return true;
		} catch ( ... ) {
			delete [] buf;
			throw;
		}
	}
Beispiel #30
0
int
DNS::gethostbyname(const char* hostname, uint8_t addr[4], bool progmem)
{
  if (UNLIKELY(m_sock == NULL)) return (ENOTSOCK);

  // Check if we already have a network address (as a string)
  if (INET::aton(hostname, addr, progmem) == 0) return (0);

  // Convert hostname to a path
  char path[INET::PATH_MAX];
  int len = INET::nametopath(hostname, path, progmem);
  if (UNLIKELY(len <= 0)) return (EFAULT);

  // Construct request header
  header_t request;
  request.ID = hton((int16_t) ID);
  request.FC = hton(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG);
  request.QC = hton(1);
  request.ANC = 0;
  request.NSC = 0;
  request.ARC = 0;

  // And query attributes
  attr_t attr;
  attr.TYPE = hton(TYPE_A);
  attr.CLASS = hton(CLASS_IN);

  // Send request and wait for reply
  for (int8_t retry = 0; retry < RETRY_MAX; retry++) {
    m_sock->datagram(m_server, PORT);
    m_sock->write(&request, sizeof(request));
    m_sock->write(path, len);
    m_sock->write(&attr, sizeof(attr));
    m_sock->flush();

    // Wait for a reply
    int res;
    for (uint16_t i = 0; i < TIMEOUT; i += 32) {
      if ((res = m_sock->available()) != 0) break;
      delay(32);
    }
    if (res == 0) continue;

    // Receive the DNS response
    uint8_t response[128];
    uint8_t dest[4];
    uint16_t port;
    res = m_sock->recv(response, sizeof(response), dest, port);
    if (UNLIKELY(res <= 0)) continue;

    // The response header
    header_t* header = (header_t*) response;
    ntoh((int16_t*) header, (int16_t*) header, sizeof(header_t) / 2);
    if (header->ID != ID) continue;
    uint8_t* ptr = &response[sizeof(header_t)];

    // The query; Path and attributes
    uint8_t n;
    while ((n = *ptr++) != 0) ptr += n;
    ptr += sizeof(attr_t);

    // The answer; domain name, attributes and data (address)
    for (uint16_t i = 0; i < header->ANC; i++) {
      do {
	n = *ptr++;
	if ((n & LABEL_COMPRESSION_MASK) == 0) {
	  if ((n & 0x80) == 0) {
	    ptr += n;
	  }
	}
	else {
	  ptr += 1;
	  n = 0;
	}
      } while (n != 0);
      rec_t* rec = (rec_t*) ptr;
      ntoh((int16_t*) rec, (int16_t*) rec, sizeof(rec_t) / 2);
      ptr += sizeof(rec_t);
      ptr += rec->RDL;
      if (rec->TYPE != TYPE_A) continue;
      if (rec->CLASS != CLASS_IN) continue;
      if (rec->RDL != INET::IP_MAX) continue;
      memcpy(addr, rec->RD, INET::IP_MAX);
      return (0);
    }
  }
  return (EIO);
}