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

Generated on Tue Jan 15 12:24:36 2002 for libicq2000 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001