Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

TLV.h

00001 /*
00002  * TLVs (Type, Length, Value)
00003  *
00004  * Copyright (C) 2001 Barnaby Gray <barnaby@beedesign.co.uk>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef TLV_H
00023 #define TLV_H
00024 
00025 #include <string>
00026 #include <map>
00027 
00028 #include <string.h>
00029 #include <stdlib.h>
00030 
00031 #include <libicq2000/Xml.h>
00032 #include <libicq2000/exceptions.h>
00033 #include <libicq2000/buffer.h>
00034 #include <libicq2000/constants.h>
00035 #include <libicq2000/ICQ.h>
00036 #include <libicq2000/Capabilities.h>
00037 
00038 namespace ICQ2000 {
00039  
00040   // ------------- TLV numerical constants ------------
00041 
00042   /*
00043    * TLV types
00044    * Originally I thought TLV types were distinct within
00045    * each channel, but in Messages it turns out they are only
00046    * distinct within each block. To complicate matters you
00047    * then get TLVs inside TLVs..
00048    * Solution: the TLV parser must be told what it is expecting
00049    * to parse so that the correct TLV types are associated
00050    */
00051 
00052   enum TLV_ParseMode { TLV_ParseMode_Channel01,
00053                        TLV_ParseMode_Channel02,
00054                        TLV_ParseMode_Channel04,
00055                        TLV_ParseMode_MessageBlock,
00056                        TLV_ParseMode_AdvMsgBlock,
00057                        TLV_ParseMode_InMessageData,
00058                        TLV_ParseMode_InAdvMsgData
00059   };
00060 
00061   // Channel 0x0001
00062   const unsigned short TLV_Screenname = 0x0001;
00063   const unsigned short TLV_Password = 0x0002;
00064   const unsigned short TLV_ClientProfile = 0x0003;
00065   const unsigned short TLV_UserInfo = 0x0005;
00066   const unsigned short TLV_Cookie = 0x0006;
00067   const unsigned short TLV_CountryCode = 0x000e;
00068   const unsigned short TLV_Language = 0x000f;
00069   const unsigned short TLV_ClientBuildMinor = 0x0014;
00070   const unsigned short TLV_ClientType = 0x0016;
00071   const unsigned short TLV_ClientVersionMajor = 0x0017;
00072   const unsigned short TLV_ClientVersionMinor = 0x0018;
00073   const unsigned short TLV_ClientICQNumber = 0x0019;
00074   const unsigned short TLV_ClientBuildMajor = 0x001a;
00075 
00076   // Channel 0x0002
00077   const unsigned short TLV_UserClass = 0x0001;
00078   const unsigned short TLV_SignupDate = 0x0002;
00079   const unsigned short TLV_SignonDate = 0x0003;
00080   const unsigned short TLV_Port = 0x0004; // ??
00081   const unsigned short TLV_UserInfoCapabilities = 0x0005;
00082   const unsigned short TLV_Status = 0x0006;
00083   const unsigned short TLV_Unknown = 0x0008; // ??
00084   const unsigned short TLV_IPAddress = 0x000a;
00085   const unsigned short TLV_WebAddress = 0x000b;
00086   const unsigned short TLV_LANDetails = 0x000c;
00087   const unsigned short TLV_Capabilities = 0x000d;
00088   const unsigned short TLV_TimeOnline = 0x000f;
00089 
00090   // Channel 0x0004
00091   // const unsigned short TLV_Screenname = 0x0001;
00092   const unsigned short TLV_ErrorURL = 0x0004;
00093   const unsigned short TLV_Redirect = 0x0005;
00094   // const unsigned short TLV_Cookie = 0x0006;
00095   const unsigned short TLV_ErrorCode = 0x0008;
00096   const unsigned short TLV_DisconnectReason = 0x0009;
00097   const unsigned short TLV_DisconnectMessage = 0x000b;
00098   const unsigned short TLV_Unknown3 = 0x000c;
00099   const unsigned short TLV_EmailAddress = 0x0011;
00100   const unsigned short TLV_RegStatus = 0x0013;
00101 
00102   // Message Block
00103   const unsigned short TLV_MessageData = 0x0002;
00104   const unsigned short TLV_ServerAckRequested = 0x0003;
00105   const unsigned short TLV_MessageIsAutoResponse = 0x0004;
00106   const unsigned short TLV_ICQData = 0x0005;
00107 
00108   // Advanced Message Block
00109   const unsigned short TLV_AdvMsgData = 0x0005;
00110 
00111   // In Message Data
00112   const unsigned short TLV_Unknown0501 = 0x0501;
00113   const unsigned short TLV_MessageText = 0x0101;
00114 
00115   // In Advanced Message Data
00116   const unsigned short TLV_AdvMsgBody = 0x2711;
00117   // loads more - but we don't parse them yet
00118 
00119   // ------------- abstract TLV classes ---------------
00120 
00121   class TLV {
00122    public:
00123     virtual ~TLV() { }
00124     
00125     virtual unsigned short Type() const = 0;
00126     virtual unsigned short Length() const = 0;
00127   };
00128 
00129   // -- Inbound TLV --
00130   class InTLV : public TLV {
00131    public:
00132     virtual void ParseValue(Buffer& b) = 0;
00133 
00134     static InTLV* ParseTLV(Buffer& b, TLV_ParseMode pm);
00135   };
00136 
00137   // -- Outbound TLV --
00138   class OutTLV : public TLV {
00139    protected:
00140     virtual void OutputHeader(Buffer& b) const;
00141     virtual void OutputValue(Buffer& b) const = 0;
00142 
00143    public:
00144     virtual void Output(Buffer& b) const;
00145   };
00146 
00147   // -------------- base classes ----------------------
00148 
00149   class ShortTLV : public OutTLV, public InTLV {
00150    protected:
00151     unsigned short m_value;
00152     
00153     virtual void OutputValue(Buffer& b) const;
00154 
00155    public:
00156     ShortTLV();
00157     ShortTLV(unsigned short n);
00158 
00159     unsigned short Length() const { return 2; }
00160 
00161     virtual void ParseValue(Buffer& b);
00162     virtual unsigned short Value() const { return m_value; }
00163   };
00164 
00165 
00166   class LongTLV : public OutTLV, public InTLV {
00167    protected:
00168     unsigned int m_value;
00169     
00170     virtual void OutputValue(Buffer& b) const;
00171 
00172    public:
00173     LongTLV();
00174     LongTLV(unsigned int n);
00175     
00176     unsigned short Length() const { return 4; }
00177 
00178     virtual void ParseValue(Buffer& b);
00179     virtual unsigned int Value() const { return m_value; }
00180   };
00181 
00182 
00183   class StringTLV : public OutTLV, public InTLV {
00184    protected:
00185     std::string m_value;
00186 
00187     virtual void OutputValue(Buffer& b) const;
00188 
00189    public:
00190     StringTLV();
00191     StringTLV(const std::string& val);
00192 
00193     unsigned short Length() const { return m_value.size(); }
00194 
00195     virtual void ParseValue(Buffer& b);
00196     virtual std::string Value() const { return m_value; }
00197   };
00198 
00199 
00200   // --------------- actual classes -------------------
00201 
00202   class ErrorURLTLV : public StringTLV {
00203    public:
00204     ErrorURLTLV() { }
00205     unsigned short Type() const { return TLV_ErrorURL; }
00206   };
00207 
00208   class ErrorCodeTLV : public ShortTLV {
00209    public:
00210     ErrorCodeTLV() { }
00211     unsigned short Type() const { return TLV_ErrorCode; }
00212   };
00213 
00214   class DisconnectReasonTLV : public ShortTLV {
00215    public:
00216     DisconnectReasonTLV() { }
00217     unsigned short Type() const { return TLV_DisconnectReason; }
00218   };
00219 
00220   class DisconnectMessageTLV : public StringTLV {
00221    public:
00222     DisconnectMessageTLV() { }
00223     unsigned short Type() const { return TLV_DisconnectMessage; }
00224   };
00225 
00226   class ScreenNameTLV : public StringTLV {
00227    public:
00228     ScreenNameTLV();
00229     ScreenNameTLV(const std::string& val);
00230 
00231     unsigned short Type() const { return TLV_Screenname; }
00232   };
00233 
00234   class PasswordTLV : public OutTLV {
00235    protected:
00236     std::string m_password;
00237 
00238     void OutputValue(Buffer& b) const;
00239 
00240    public:
00241     PasswordTLV(const std::string& pw);
00242 
00243     unsigned short Type() const { return TLV_Password; }
00244     unsigned short Length() const { return m_password.size(); }
00245   };
00246 
00247   const unsigned char ALLOWDIRECT_EVERYONE = 0x00;
00248   const unsigned char ALLOWDIRECT_AUTHORIZATION = 0x10;
00249   const unsigned char ALLOWDIRECT_CONTACTLIST = 0x20;
00250 
00251   const unsigned char WEBAWARE_NORMAL = 0x02;
00252   const unsigned char WEBAWARE_WEBAWARE = 0x03;
00253 
00254   class StatusTLV : public OutTLV, public InTLV {
00255    private:
00256     unsigned char m_allowDirect;
00257     unsigned char m_webAware;
00258     unsigned short m_status;
00259 
00260    protected:
00261     void OutputValue(Buffer& b) const;
00262     void ParseValue(Buffer& b);
00263 
00264    public:
00265     StatusTLV(unsigned char ad, unsigned char wa, unsigned short st)
00266       : m_allowDirect(ad), m_webAware(wa), m_status(st)
00267       { }
00268     StatusTLV() { }
00269 
00270     unsigned short Type() const { return TLV_Status; }
00271     unsigned short Length() const { return 4; }
00272 
00273     unsigned char getAllowDirect() { return m_allowDirect; }
00274     unsigned char getWebAware() { return m_webAware; }
00275     unsigned short getStatus() { return m_status; }
00276 
00277     void setAllowDirect(unsigned char m) { m_allowDirect = m; }
00278     void setWebAware(unsigned char m) { m_webAware = m; }
00279     void setStatus(unsigned short m) { m_status = m; }
00280   };
00281 
00282   // -- Client*TLVs --
00283 
00284   class ClientProfileTLV : public StringTLV {
00285    public:
00286     ClientProfileTLV(const std::string& val) : StringTLV(val) { }
00287     unsigned short Type() const { return TLV_ClientProfile; }
00288   };
00289 
00290   class ClientTypeTLV : public ShortTLV {
00291    public:
00292     ClientTypeTLV(unsigned short n) : ShortTLV(n) { }
00293     unsigned short Type() const { return TLV_ClientType; }
00294   };
00295 
00296   class ClientVersionMajorTLV : public ShortTLV {
00297    public:
00298     ClientVersionMajorTLV(unsigned short n) : ShortTLV(n) { }
00299     unsigned short Type() const { return TLV_ClientVersionMajor; }
00300   };
00301 
00302   class ClientVersionMinorTLV : public ShortTLV {
00303    public:
00304     ClientVersionMinorTLV(unsigned short n) : ShortTLV(n) { }
00305     unsigned short Type() const { return TLV_ClientVersionMinor; }
00306   };
00307 
00308   class ClientICQNumberTLV : public ShortTLV {
00309    public:
00310     ClientICQNumberTLV(unsigned short n) : ShortTLV(n) { }
00311     unsigned short Type() const { return TLV_ClientICQNumber; }
00312   };
00313 
00314   class ClientBuildMajorTLV : public ShortTLV {
00315    public:
00316     ClientBuildMajorTLV(unsigned short n) : ShortTLV(n) { }
00317     unsigned short Type() const { return TLV_ClientBuildMajor; }
00318   };
00319 
00320   class ClientBuildMinorTLV : public LongTLV {
00321    public:
00322     ClientBuildMinorTLV(unsigned int n) : LongTLV(n) { }
00323     unsigned short Type() const { return TLV_ClientBuildMinor; }
00324   };
00325 
00326   class CountryCodeTLV : public StringTLV {
00327    public:
00328     CountryCodeTLV(std::string val) : StringTLV(val) { }
00329     unsigned short Type() const { return TLV_CountryCode; }
00330   };
00331 
00332   class LanguageTLV : public StringTLV {
00333    public:
00334     LanguageTLV(const std::string& val) : StringTLV(val) { }
00335     unsigned short Type() const { return TLV_Language; }
00336   };
00337 
00338   // --
00339 
00340   class WebAddressTLV : public StringTLV {
00341    public:
00342     WebAddressTLV() { }
00343     unsigned short Type() const { return TLV_WebAddress; }
00344   };
00345 
00346   class UserClassTLV : public ShortTLV {
00347    public:
00348     UserClassTLV() { }
00349     unsigned short Type() const { return TLV_UserClass; }
00350   };
00351 
00352   class TimeOnlineTLV : public LongTLV {
00353    public:
00354     TimeOnlineTLV() { }
00355     unsigned short Type() const { return TLV_TimeOnline; }
00356   };
00357 
00358   class SignupDateTLV : public LongTLV {
00359    public:
00360     SignupDateTLV() { }
00361     unsigned short Type() const { return TLV_SignupDate; }
00362   };
00363 
00364   class SignonDateTLV : public LongTLV {
00365    public:
00366     SignonDateTLV() { }
00367     unsigned short Type() const { return TLV_SignonDate; }
00368   };
00369 
00370   class UnknownTLV : public ShortTLV {
00371    public:
00372     UnknownTLV() : ShortTLV(0) { }
00373     unsigned short Type() const { return TLV_Unknown; }
00374   };
00375 
00376   class IPAddressTLV : public LongTLV {
00377    public:
00378     IPAddressTLV() { }
00379     unsigned short Type() const { return TLV_IPAddress; }
00380   };
00381 
00382   class PortTLV : public ShortTLV {
00383    public:
00384     PortTLV() { }
00385     unsigned short Type() const { return TLV_Port; }
00386   };
00387 
00388   class UserInfoCapabilitiesTLV : public OutTLV {
00389    private:
00390     Capabilities m_capabilities;
00391 
00392    public:
00393     UserInfoCapabilitiesTLV();
00394     unsigned short Type() const { return TLV_UserInfoCapabilities; }
00395     unsigned short Length() const;
00396 
00397     void OutputValue(Buffer& b) const;
00398   };
00399 
00400   class CapabilitiesTLV : public InTLV {
00401    private:
00402     Capabilities m_capabilities;
00403     
00404    public:
00405     CapabilitiesTLV() { }
00406     unsigned short Type() const { return TLV_Capabilities; }
00407     unsigned short Length() const { return m_capabilities.get_length(); }
00408 
00409     Capabilities get_capabilities() const;
00410 
00411     void ParseValue(Buffer& b);
00412   };
00413 
00414   class RedirectTLV : public InTLV {
00415    private:
00416     std::string m_server;
00417     unsigned short m_port;
00418 
00419    public:
00420     RedirectTLV() { }
00421 
00422     unsigned short Length() const { return m_server.size(); }
00423     unsigned short Type() const { return TLV_Redirect; }
00424 
00425     void ParseValue(Buffer& b);
00426 
00427     std::string getHost() { return m_server; }
00428     unsigned short getPort() { return m_port; }
00429   };
00430 
00431   class CookieTLV : public InTLV, public OutTLV {
00432    private:
00433     unsigned char *m_value;
00434     unsigned short m_length;
00435 
00436    public:
00437     CookieTLV() : m_value(NULL), m_length(0) { }
00438     CookieTLV(const unsigned char *ck, unsigned short len);
00439     ~CookieTLV();
00440       
00441     unsigned short Length() const { return m_length; }
00442     unsigned short Type() const { return TLV_Cookie; }
00443 
00444     void ParseValue(Buffer& b);
00445     void OutputValue(Buffer& b) const;
00446 
00447     const unsigned char* Value() { return m_value; }
00448   };
00449 
00450   // can go out as well
00451   class LANDetailsTLV : public InTLV, public OutTLV {
00452    private:
00453     unsigned int m_lan_ip;
00454     unsigned short m_lan_port, m_firewall;
00455     unsigned char m_tcp_version;
00456     unsigned int m_dc_cookie;
00457     
00458    public:
00459     LANDetailsTLV();
00460     LANDetailsTLV(unsigned int ip, unsigned short port);
00461 
00462     unsigned short Length() const { return 0; } // varies
00463     unsigned short Type() const { return TLV_LANDetails; }
00464 
00465     unsigned int getLanIP() const { return m_lan_ip; }
00466     unsigned short getLanPort() const { return m_lan_port; }
00467     unsigned short getFirewall() const { return m_firewall; }
00468     unsigned char getTCPVersion() const { return m_tcp_version; }
00469     unsigned int getDCCookie() const { return m_dc_cookie; }
00470 
00471     void ParseValue(Buffer& b);
00472     void OutputValue(Buffer& b) const;
00473   };
00474 
00475   class RawTLV : public InTLV {
00476    protected:
00477     unsigned short m_type;
00478     unsigned short m_length;
00479 
00480    public:
00481     RawTLV(unsigned short type);
00482 
00483     unsigned short Type() const { return m_type; }
00484     unsigned short Length() const { return m_length; }
00485     void ParseValue(Buffer& b);
00486   };
00487 
00488   class MessageTextTLV : public InTLV {
00489    protected:
00490     std::string m_message;
00491     unsigned short m_flag1, m_flag2;
00492     
00493    public:
00494     MessageTextTLV()
00495       : m_message(), m_flag1(0), m_flag2(0) { }
00496 
00497     std::string getMessage() { return m_message; }
00498     unsigned short getFlag1() { return m_flag1; }
00499     unsigned short getFlag2() { return m_flag1; }
00500 
00501     void ParseValue(Buffer& b);
00502     unsigned short Type() const { return TLV_MessageText; }
00503     unsigned short Length() const { return 0; }
00504   };
00505 
00506   class MessageDataTLV : public InTLV {
00507     MessageTextTLV mttlv;
00508 
00509    public:
00510     MessageDataTLV();
00511 
00512     std::string getMessage() { return mttlv.getMessage(); }
00513     unsigned short getFlag1() { return mttlv.getFlag1(); }
00514     unsigned short getFlag2() { return mttlv.getFlag2(); }
00515 
00516     void ParseValue(Buffer& b);
00517     unsigned short Type() const { return TLV_MessageData; }
00518     unsigned short Length() const { return 0; }
00519   };
00520 
00521   class AdvMsgBodyTLV : public InTLV {
00522    protected:
00523     ICQSubType *m_icqsubtype;
00524     
00525    public:
00526     AdvMsgBodyTLV();
00527     ~AdvMsgBodyTLV();
00528 
00529     ICQSubType* grabICQSubType();
00530 
00531     void ParseValue(Buffer& b);
00532     unsigned short Type() const { return TLV_AdvMsgBody; }
00533     unsigned short Length() const { return 0; }
00534   };
00535 
00536   class AdvMsgDataTLV : public InTLV {
00537     ICQSubType *m_icqsubtype;
00538 
00539    public:
00540     AdvMsgDataTLV();
00541     ~AdvMsgDataTLV();
00542 
00543     ICQSubType* grabICQSubType();
00544 
00545     void ParseValue(Buffer& b);
00546     unsigned short Type() const { return TLV_AdvMsgData; }
00547     unsigned short Length() const { return 0; }
00548   };
00549 
00550 
00551   // --------------- ICQDataTLV ------------------
00552 
00553   class ICQDataTLV : public InTLV {
00554    private:
00555     ICQSubType *m_icqsubtype;
00556 
00557    public:
00558     ICQDataTLV();
00559     ~ICQDataTLV();
00560 
00561     ICQSubType* getICQSubType() const;
00562     ICQSubType* grabICQSubType();
00563 
00564     void ParseValue(Buffer& b);
00565     unsigned short Type() const { return TLV_ICQData; }
00566     unsigned short Length() const { return 0; }
00567 
00568   };
00569 
00570   // ---------------- TLV List -------------------
00571 
00572   class TLVList {
00573    private:
00574     std::map<unsigned short,InTLV*> tlvmap;
00575    public:
00576     TLVList();
00577     ~TLVList();
00578 
00579     void Parse(Buffer& b, TLV_ParseMode pm, unsigned short no_tlvs);
00580     bool exists(unsigned short type);
00581     InTLV* & operator[](unsigned short type);
00582 
00583   };
00584 
00585 }
00586 
00587 Buffer& operator<<(Buffer& b, const ICQ2000::OutTLV& t);
00588 
00589 #endif

Generated on Sun Jul 21 10:57:32 2002 for libicq2000 by doxygen1.2.16