Ejemplo n.º 1
0
		void readFrom(const char *buffer, std::size_t length) {
			if (buffer == NULL)
				throw nrpe::nrpe_exception("No buffer.");
			if (length != get_packet_length())
				throw nrpe::nrpe_exception("Invalid packet length: " + strEx::s::xtos(length) + " != " + strEx::s::xtos(get_packet_length()) + " configured payload is: " + strEx::s::xtos(get_payload_length()));
			const nrpe::data::packet *p = reinterpret_cast<const nrpe::data::packet*>(buffer);
			type_ = swap_bytes::ntoh<int16_t>(p->packet_type);
			if ((type_ != nrpe::data::queryPacket)&&(type_ != nrpe::data::responsePacket))
				throw nrpe::nrpe_exception("Invalid packet type: " + strEx::s::xtos(type_));
			version_ = swap_bytes::ntoh<int16_t>(p->packet_version);
			if (version_ != nrpe::data::version2)
				throw nrpe::nrpe_exception("Invalid packet version." + strEx::s::xtos(version_));
			crc32_ = swap_bytes::ntoh<u_int32_t>(p->crc32_value);
			// Verify CRC32
			// @todo Fix this, currently we need a const buffer so we cannot change the CRC to 0.
			char * tb = new char[length+1];
			memcpy(tb, buffer, length);
			nrpe::data::packet *p2 = reinterpret_cast<nrpe::data::packet*>(tb);
			p2->crc32_value = 0;
			calculatedCRC32_ = calculate_crc32(tb, get_packet_length());
			delete [] tb;
			if (crc32_ != calculatedCRC32_) 
				throw nrpe::nrpe_exception("Invalid checksum in NRPE packet: " + strEx::s::xtos(crc32_) + "!=" + strEx::s::xtos(calculatedCRC32_));
			// Verify CRC32 end
			result_ = swap_bytes::ntoh<int16_t>(p->result_code);
			payload_ = fetch_payload(p);
		}
Ejemplo n.º 2
0
static int ki_isup_to_json(sip_msg_t *_m, int proto)
{
	struct isup_state isup_state = { 0, };
	const uint8_t *data;
	int opc, dpc, mtp_type, int_len, rc;
	size_t len;

	free((char *) isup_last);
	srjson_DeleteDoc(isup_json);
	isup_last = NULL;
	isup_json = NULL;
	mtp_type = 0;

	data = fetch_payload(_m, "$var(payload)", &int_len);
	if (!data)
		return -1;

	if (int_len < 0) {
		LM_ERR("Payload length low %d\n", int_len);
		return -1;
	}
	len = int_len;

	data = ss7_extract_payload(data, &len, proto, &opc, &dpc, &mtp_type);
	if (!data)
		return -1;

	if (mtp_type != MTP_ISUP) {
		LM_DBG("Non ISUP payload %d\n", mtp_type);
		return -1;
	}

	/* parse isup... */
	isup_state.json = srjson_NewDoc(NULL);
	if (!isup_state.json) {
		LM_ERR("Failed to allocate JSON document\n");
		return -1;
	}
	isup_state.json->root = srjson_CreateObject(isup_state.json);
	if (!isup_state.json->root) {
		LM_ERR("Failed to allocate JSON object\n");
		srjson_DeleteDoc(isup_state.json);
		return -1;
	}

	rc = isup_parse(data, len, &isup_state);
	if (rc != 0) {
		srjson_DeleteDoc(isup_state.json);
		return rc;
	}
	srjson_AddNumberToObject(isup_state.json, isup_state.json->root, "opc", opc);
	srjson_AddNumberToObject(isup_state.json, isup_state.json->root, "dpc", dpc);
	isup_last = srjson_PrintUnformatted(isup_state.json, isup_state.json->root);
	isup_json = isup_state.json;
	return 1;
}