blob: cad8be9757daf16027a6c170c40f402e128a24d2 [file] [log] [blame]
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +00001/* 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
31namespace rfb {
32
33 //
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000034 // 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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000037 //
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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000045 //
46 // CapsContainer - a container class to maintain a list of protocol
47 // capabilities.
48 //
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000049 // 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
58 // capabilities in the order they were enabled.
59 //
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000060
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000061 class CapsContainer
62 {
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000063 public:
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000064
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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000068 CapsContainer(int maxCaps = 64);
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000069
70 // Destructor.
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000071 virtual ~CapsContainer();
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000072
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000073 // 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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000076 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 Kaplinsky56b7fa12006-09-12 05:15:44 +000080 // Check if a capability with the specified code was added earlier.
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000081 bool isKnown(rdr::U32 code) const;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000082
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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000086 bool getInfo(rdr::U32 code, CapabilityInfo *capinfo) const;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000087
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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000093 char *getDescription(rdr::U32 code) const;
94
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000095 // 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 Kaplinsky73fdc8c2006-06-03 13:18:37 +000099 bool enable(const CapabilityInfo *capinfo);
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000100
101 // Check if the specified capability is known and enabled.
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000102 bool isEnabled(rdr::U32 code) const;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000103
104 // Return the number of enabled capabilities.
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000105 int numEnabled() const { return m_listSize; }
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000106
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 Kaplinsky73fdc8c2006-06-03 13:18:37 +0000111 rdr::U32 getByOrder(int idx) const;
112
113 private:
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000114
115 // Mapping codes to corresponding CapabilityInfo structures.
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000116 std::map<rdr::U32,CapabilityInfo> m_infoMap;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000117 // Mapping capability codes to corresponding descriptions.
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000118 std::map<rdr::U32,char*> m_descMap;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000119 // Mapping codes to boolean flags, true for enabled capabilities.
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000120 std::map<rdr::U32,bool> m_enableMap;
121
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000122 // Allocated size of m_plist[].
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000123 int m_maxSize;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000124 // Number of valid elements in m_plist[].
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000125 int m_listSize;
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +0000126 // Array of enabled capabilities (allocated in constructor).
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000127 rdr::U32 *m_plist;
128 };
129
130}
131
132#endif // __RFB_CAPSCONTAINER_H__