Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2003-2006 Constantin Kaplinsky. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // |
| 20 | // CapsContainer and associated structures - dealing with TightVNC-specific |
| 21 | // protocol capability lists. |
| 22 | // |
| 23 | |
| 24 | #ifndef __RFB_CAPSCONTAINER_H__ |
| 25 | #define __RFB_CAPSCONTAINER_H__ |
| 26 | |
| 27 | #include <rdr/types.h> |
| 28 | |
| 29 | #include <map> |
| 30 | |
| 31 | namespace rfb { |
| 32 | |
| 33 | // |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 34 | // CapabilityInfo - structure used to describe protocol options such as |
| 35 | // tunneling methods, authentication schemes, message types and encoding |
| 36 | // types (protocol versions 3.7 and 3.8 with TightVNC extensions). |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 37 | // |
| 38 | |
| 39 | struct CapabilityInfo { |
| 40 | rdr::U32 code; // numeric identifier |
| 41 | rdr::U8 vendorSignature[4]; // vendor identification |
| 42 | rdr::U8 nameSignature[8]; // abbreviated option name |
| 43 | }; |
| 44 | |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 45 | // |
| 46 | // CapsContainer - a container class to maintain a list of protocol |
| 47 | // capabilities. |
| 48 | // |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 49 | // Typical usage is as follows. First, the client creates an instance |
| 50 | // of the CapsContainer class for each type of capabilities (e.g. |
| 51 | // authentication methods). It adds information about capabilities it |
| 52 | // supports, by calling add() functions. Then, the client receives |
| 53 | // information about supported capabilities from the server, and tries |
| 54 | // to "enable" each capability advertised by the server. Particular |
| 55 | // capability becomes enabled if it is known (added by the client) and |
| 56 | // matches that one received from the server. Finally, the client can |
| 57 | // check if a given capability is enabled, and also get the list of |
Constantin Kaplinsky | 311c826 | 2006-09-12 06:23:16 +0000 | [diff] [blame] | 58 | // capabilities in the order they were listed by the server. |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 59 | // |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 60 | |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 61 | class CapsContainer |
| 62 | { |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 63 | public: |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 64 | |
| 65 | // Constructor. The maxCaps argument is the maximum number of records |
| 66 | // in the list used by getByOrder() function. Remaining functions do not |
| 67 | // impose limitations on the number of capabilities. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 68 | CapsContainer(int maxCaps = 64); |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 69 | |
| 70 | // Destructor. |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame] | 71 | virtual ~CapsContainer(); |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 72 | |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 73 | // Add information about a particular capability into the object. These |
| 74 | // functions overwrite existing capability records with the same code. |
| 75 | // NOTE: Value 0 should not be used for capability codes. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 76 | void add(const CapabilityInfo *capinfo, const char *desc = 0); |
| 77 | void add(rdr::U32 code, const char *vendor, const char *name, |
| 78 | const char *desc = 0); |
| 79 | |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 80 | // Check if a capability with the specified code was added earlier. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 81 | bool isKnown(rdr::U32 code) const; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 82 | |
| 83 | // Fill in an rfbCapabilityInfo structure with contents corresponding to |
| 84 | // the specified code. Returns true on success, false if the specified |
| 85 | // code is not known. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 86 | bool getInfo(rdr::U32 code, CapabilityInfo *capinfo) const; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 87 | |
| 88 | // Get an optional description string for the specified capability code. |
| 89 | // Returns 0 either if the code is not known, or if there is no |
| 90 | // description for the given capability. Otherwise, the return value |
| 91 | // is a pointer valid until either add() is called again for the same |
| 92 | // capability, or the CapsContaner object is destroyed. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 93 | char *getDescription(rdr::U32 code) const; |
| 94 | |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 95 | // Mark the specified capability as "enabled". This function compares |
| 96 | // "vendor" and "name" signatures in the existing record and in the |
| 97 | // argument structure and enables the capability only if both records |
| 98 | // are the same. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 99 | bool enable(const CapabilityInfo *capinfo); |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 100 | |
| 101 | // Check if the specified capability is known and enabled. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 102 | bool isEnabled(rdr::U32 code) const; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 103 | |
| 104 | // Return the number of enabled capabilities. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 105 | int numEnabled() const { return m_listSize; } |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 106 | |
| 107 | // Return the capability code at the specified index, from the list of |
| 108 | // enabled capabilities. Capabilities are indexed in the order they were |
| 109 | // enabled, index 0 points to the capability which was enabled first. |
| 110 | // If the index is not valid, this function returns 0. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 111 | rdr::U32 getByOrder(int idx) const; |
| 112 | |
| 113 | private: |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 114 | |
| 115 | // Mapping codes to corresponding CapabilityInfo structures. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 116 | std::map<rdr::U32,CapabilityInfo> m_infoMap; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 117 | // Mapping capability codes to corresponding descriptions. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 118 | std::map<rdr::U32,char*> m_descMap; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 119 | // Mapping codes to boolean flags, true for enabled capabilities. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 120 | std::map<rdr::U32,bool> m_enableMap; |
| 121 | |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 122 | // Allocated size of m_plist[]. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 123 | int m_maxSize; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 124 | // Number of valid elements in m_plist[]. |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 125 | int m_listSize; |
Constantin Kaplinsky | 56b7fa1 | 2006-09-12 05:15:44 +0000 | [diff] [blame] | 126 | // Array of enabled capabilities (allocated in constructor). |
Constantin Kaplinsky | 73fdc8c | 2006-06-03 13:18:37 +0000 | [diff] [blame] | 127 | rdr::U32 *m_plist; |
| 128 | }; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | #endif // __RFB_CAPSCONTAINER_H__ |