Commit 385ee95a authored by baire's avatar baire
Browse files

basic support for MessageHeader

parent fd4b66c9
Loading
Loading
Loading
Loading
+17 −1
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ namespace t3devlib {
namespace gen {

// enumerated types
typedef t3devlib::Boolean FieldName;
typedef t3devlib::Boolean Method;


@@ -31,6 +30,23 @@ typedef t3devlib::Charstring Raw;

typedef t3devlib::Undef XmlBody;


// enumerated types
class FieldName : public t3devlib::Enum
{
private:
	static const char* msFields[];
public:
	FieldName() : Enum (msFields) {}

	const char* GetModuleName() const { return "LibSip_SIPTypesAndValues"; }
	const char* GetTypeName() const   { return "FieldName"; }

	void Encode (Buffer& buffer) throw (EncodeError);
	void Decode (Buffer& buffer) throw (DecodeError);
};


}} //namespaces

#endif   // ----- #ifndef CODEC_INC  -----
+164 −16
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ public:
	bool Match (Buffer& buffer) {

		mStart =  buffer.GetValue().c_str() + (buffer.GetPosition() / 8);

		//TODO: check that the regex is not match past the end marker of the buffer
		return !regexec (&mRegex, mStart, 16, mMatches, 0);
	}

@@ -294,33 +294,88 @@ void GenericParam::PreDecodeField (int id, Buffer& buffer) throw (DecodeError)

class SipHeaderMap {
public:
	struct Entry {
		Entry (const char* name, const char* abbrev, int id_msg_hdr, const char* id_fdn)
		 : mName (name), mAbbrev (abbrev), mIdMessageHeader (id_msg_hdr), mIdFieldName (id_fdn)
		{}
		const std::string	mName;
		const std::string	mAbbrev;
		const int		mIdMessageHeader;
		const std::string	mIdFieldName;
	};

	static const Entry& GetByName (const std::string& key)
	{
		const std::map<std::string, Entry*>& m = msInstance.mMapName;
		std::map <std::string, Entry*>::const_iterator it = m.find (key);
		if (it != m.end()) {
			return *it->second;
		} else {
			return *msInstance.mUndef;
		}
	}
	
	static int Find (const std::string& header)
	static const Entry& GetByIdFieldName (const std::string& key)
	{
		std::map <std::string, int>::const_iterator it = mInstance.mMap.find (header);
		if (it != mInstance.mMap.end()) {
			return it->second;
		const std::map<std::string, Entry*>& m = msInstance.mMapIdFieldName;
		std::map <std::string, Entry*>::const_iterator it = m.find (key);
		if (it != m.end()) {
			return *it->second;
		} else {
			return MessageHeader::id_undefinedHeader_List;
			return *msInstance.mUndef;
		}
	}
	
	static const Entry& GetByIdMessageHeader (int key)
	{
		const std::map<int, Entry*>& m = msInstance.mMapIdMessageHeader;
		std::map <int, Entry*>::const_iterator it = m.find (key);
		if (it != m.end()) {
			return *it->second;
		} else {
			return *msInstance.mUndef;
		}
	}


private:
	void AddEntry (const Entry& entry) {
		mEntries.push_back(entry);
		Entry& e = *mEntries.rbegin();
		
		//TODO: check unicity
		mMapName[e.mName] = &e;
		mMapName[e.mAbbrev] = &e;
		mMapIdMessageHeader[e.mIdMessageHeader] = &e;
		mMapIdFieldName[e.mIdFieldName] = &e;
	}

	SipHeaderMap() {
		mMap["From"]	= MessageHeader::id_fromField;
		mMap["f"]	= MessageHeader::id_fromField;
		mMap["Via"]	= MessageHeader::id_via;
		mMap["v"]	= MessageHeader::id_via;

#define SIP_HEADER_ADD(name, abbr, msghdr, fdname)	AddEntry (Entry (#name, #abbr, MessageHeader::id_ ## msghdr, #fdname));

		SIP_HEADER_ADD (From,	f,	fromField,	FROM_E);
		SIP_HEADER_ADD (Via,	v,	via,		VIA_E);
		{
			mEntries.push_back(Entry("", "", MessageHeader::id_undefinedHeader_List, ""));
			Entry& e = *mEntries.rbegin();
			mMapIdMessageHeader[e.mIdMessageHeader] = &e;
		}
	}

	static SipHeaderMap		mInstance;
	static SipHeaderMap		msInstance;

	std::list<Entry>		mEntries;
	Entry*				mUndef;


	std::map <std::string, Entry*>	mMapName;
	std::map <std::string, Entry*>	mMapIdFieldName;
	std::map <int, Entry*>		mMapIdMessageHeader;

	std::map <std::string, int>	mMap;
};

SipHeaderMap SipHeaderMap::mInstance;
SipHeaderMap SipHeaderMap::msInstance;


void MessageHeader::PreDecodeField (Buffer& buffer) throw (DecodeError)
@@ -329,14 +384,107 @@ void MessageHeader::PreDecodeField (Buffer& buffer) throw (DecodeError)

	if (reg_header_name.Match (buffer))
	{
		int id = SipHeaderMap::Find(reg_header_name.GetMatchedString (1));
		int id = SipHeaderMap::GetByName(reg_header_name.GetMatchedString (1)).mIdMessageHeader;
	
		SetHypNextField (id);
	}
}


const char* FieldName::msFields[] = { 
        "ACCEPT_E",
        "ACCEPT_ENCODING_E",
        "ACCEPT_LANGUAGE_E",
        "ALERT_INFO_E",
        "ALLOW_E",
        "AUTHENTICATION_INFO_E",
        "AUTHORIZATION_E",
        "CALL_ID_E",
        "CALL_INFO_E",
        "CONTACT_E",
        "CONTENT_DISPOSITION_E",
        "CONTENT_ENCODING_E",
        "CONTENT_LANGUAGE_E",
        "CONTENT_LENGTH_E",
        "CONTENT_TYPE_E",
        "CSEQ_E",
        "DATE_E",
        "ERROR_INFO_E",
        "EXPIRES_E",
        "FROM_E",
        "IN_REPLY_TO_E",
        "MAX_FORWARDS_E",
        "MIME_VERSION_E",
        "MIN_EXPIRES_E",
        "ORGANIZATION_E",
        "PRIORITY_E",
        "PROXY_AUTHENTICATE_E",
        "PROXY_AUTHORIZATION_E",
        "PROXY_REQUIRE_E",
        "RECORD_ROUTE_E",
        "REPLY_TO_E",
        "REQUIRE_E",
        "RETRY_AFTER_E",
        "ROUTE_E",
        "SERVER_E",
        "SUBJECT_E",
        "SUPPORTED_E",
        "TIMESTAMP_E",
        "TO_E",
        "UNSUPPORTED_E",
        "USER_AGENT_E",
        "VIA_E",
        "WARNING_E",
        "WWW_AUTHENTICATE_E",
	"RACK_E",
	"RSEQ_E",
	"ALLOW_EVENTS_E",
	"EVENT_E",
	"SUBSCRIPTION_STATE_E",
	"P_MEDIA_AUTHORIZATION_E",
	"PRIVACY_E",
	"P_ASSERTED_ID_E",
	"P_PREFERRED_ID_E", 
	"REASON_E",
	"REFER_TO_E",
	"REFERRED_BY_E",
	"HISTORY_INFO_E",
	"P_MEDIA_AUTH_E",
	"PATH_E",	
	"SECURITY_CLIENT_E",	
	"SECURITY_SERVER_E",
	"SECURITY_VERIFY_E",  
	"P_ACCESS_NETWORK_INFO_E",
	"P_ASSOCIATED_URI_E",
	"P_CALLED_PARTY_E",
	"P_CHARGING_FUNCTION_ADDRESSES_E",
	"P_CHARGING_VECTOR_E",
	"P_VISITED_NETWORK_E",
	"SERVICE_ROUTE_E",
	"ACCEPT_CONTACT_E",
	"MIN_SE_E",
	"SESSION_EXPIRES_E",
	"P_ASSERTED_SERVICE_E",
	"P_EARLY_MEDIA_E"
	, "" };

void FieldName::Encode (Buffer& buffer) throw (EncodeError)
{
	Charstring c;

	c.SetValue ((SipHeaderMap::GetByIdFieldName(GetValueString()).mName + ':').c_str());
	c.Encode(buffer);
}

void FieldName::Decode (Buffer& buffer) throw (DecodeError)
{
	static Regex reg_header_name ("^([A-Za-z\\-]+):", REG_ICASE);

	reg_header_name.AssertMatch (buffer, this);
	SetValueString (SipHeaderMap::GetByName(reg_header_name.GetMatchedString (1)).mIdFieldName.c_str());
	
	buffer.SetPosition(buffer.GetPosition() + reg_header_name.GetMatchedLength());
}