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

Generated on Sat Jul 20 16:59:08 2002 for libicq2000 by doxygen1.2.16