monotone

monotone Mtn Source Tree

Root/src/netcmd.hh

1// Copyright (C) 2004 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#ifndef __NETCMD_HH__
11#define __NETCMD_HH__
12
13#include "vector.hh"
14#include <list>
15#include <utility>
16#include <iostream>
17
18#include "globish.hh"
19#include "merkle_tree.hh"
20#include "numeric_vocab.hh"
21#include "uri.hh"
22#include "vocab.hh"
23#include "hmac.hh"
24#include "string_queue.hh"
25
26struct globish;
27class database;
28class project_t;
29class key_store;
30class lua_hooks;
31class options;
32
33class app_state;
34
35namespace error_codes {
36 static const int no_error = 200;
37 static const int partial_transfer = 211;
38 static const int no_transfer = 212;
39
40 static const int not_permitted = 412;
41 static const int unknown_key = 422;
42 static const int mixing_versions = 432;
43
44 static const int role_mismatch = 512;
45 static const int bad_command = 521;
46
47 static const int failed_identification = 532;
48 //static const int bad_data = 541;
49}
50
51typedef enum
52 {
53 server_voice,
54 client_voice
55 }
56protocol_voice;
57
58typedef enum
59 {
60 source_role = 1,
61 sink_role = 2,
62 source_and_sink_role = 3
63 }
64protocol_role;
65
66typedef enum
67 {
68 refinement_query = 0,
69 refinement_response = 1
70 }
71refinement_type;
72
73typedef enum
74 {
75 // general commands
76 error_cmd = 0,
77 bye_cmd = 1,
78
79 // authentication commands
80 hello_cmd = 2,
81 anonymous_cmd = 3,
82 auth_cmd = 4,
83 confirm_cmd = 5,
84
85 // refinement commands
86 refine_cmd = 6,
87 done_cmd = 7,
88
89 // transmission commands
90 data_cmd = 8,
91 delta_cmd = 9,
92
93 // automation commands
94 automate_cmd = 10,
95 automate_headers_request_cmd = 11,
96 automate_headers_reply_cmd = 12,
97 automate_command_cmd = 13,
98 automate_packet_cmd = 14,
99
100 // usher commands
101 // usher_cmd is sent either by a proxy that needs to know where
102 // to forward a connection (the reply gives the desired hostname and
103 // include pattern), or by a server performing protocol
104 // version negotiation.
105 usher_cmd = 100,
106 usher_reply_cmd = 101
107 }
108netcmd_code;
109
110class netcmd
111{
112private:
113 u8 version;
114 netcmd_code cmd_code;
115 std::string payload;
116public:
117 explicit netcmd(u8 ver);
118 netcmd_code get_cmd_code() const {return cmd_code;}
119 u8 get_version() const { return version; }
120 size_t encoded_size() const;
121 bool operator==(netcmd const & other) const;
122
123
124 // basic cmd i/o (including checksums)
125 void write(std::string & out,
126 chained_hmac & hmac) const;
127 bool read(u8 min_version, u8 max_version,
128 string_queue & inbuf,
129 chained_hmac & hmac);
130 bool read_string(std::string & inbuf,
131 chained_hmac & hmac) {
132 // this is here only for the regression tests because they want to
133 // read and write to the same type, but we want to have reads from
134 // a string queue so that when data is read in from the network it
135 // can be processed efficiently
136 string_queue tmp(inbuf.size());
137 tmp.append(inbuf);
138 // allow any version
139 bool ret = read(0, 255, tmp, hmac);
140 inbuf = tmp.substr(0,tmp.size());
141 return ret;
142 }
143 // i/o functions for each type of command payload
144 void read_error_cmd(std::string & errmsg) const;
145 void write_error_cmd(std::string const & errmsg);
146
147 void read_hello_cmd(u8 & server_version,
148 key_name & server_keyname,
149 rsa_pub_key & server_key,
150 id & nonce) const;
151 void write_hello_cmd(key_name const & server_keyname,
152 rsa_pub_key const & server_key,
153 id const & nonce);
154
155 void read_bye_cmd(u8 & phase) const;
156 void write_bye_cmd(u8 phase);
157
158 void read_anonymous_cmd(protocol_role & role,
159 globish & include_pattern,
160 globish & exclude_pattern,
161 rsa_oaep_sha_data & hmac_key_encrypted) const;
162 void write_anonymous_cmd(protocol_role role,
163 globish const & include_pattern,
164 globish const & exclude_pattern,
165 rsa_oaep_sha_data const & hmac_key_encrypted);
166
167 void read_auth_cmd(protocol_role & role,
168 globish & include_pattern,
169 globish & exclude_pattern,
170 key_id & client,
171 id & nonce1,
172 rsa_oaep_sha_data & hmac_key_encrypted,
173 rsa_sha1_signature & signature) const;
174 void write_auth_cmd(protocol_role role,
175 globish const & include_pattern,
176 globish const & exclude_pattern,
177 key_id const & client,
178 id const & nonce1,
179 rsa_oaep_sha_data const & hmac_key_encrypted,
180 rsa_sha1_signature const & signature);
181
182 void read_confirm_cmd() const;
183 void write_confirm_cmd();
184
185 void read_refine_cmd(refinement_type & ty, merkle_node & node) const;
186 void write_refine_cmd(refinement_type ty, merkle_node const & node);
187
188 void read_done_cmd(netcmd_item_type & type, size_t & n_items) const;
189 void write_done_cmd(netcmd_item_type type, size_t n_items);
190
191 void read_data_cmd(netcmd_item_type & type,
192 id & item,
193 std::string & dat) const;
194 void write_data_cmd(netcmd_item_type type,
195 id const & item,
196 std::string const & dat);
197
198 void read_delta_cmd(netcmd_item_type & type,
199 id & base, id & ident,
200 delta & del) const;
201 void write_delta_cmd(netcmd_item_type & type,
202 id const & base, id const & ident,
203 delta const & del);
204
205 void read_automate_cmd(key_id & client,
206 id & nonce1,
207 rsa_oaep_sha_data & hmac_key_encrypted,
208 rsa_sha1_signature & signature) const;
209 void write_automate_cmd(key_id const & client,
210 id const & nonce1,
211 rsa_oaep_sha_data & hmac_key_encrypted,
212 rsa_sha1_signature & signature);
213 void read_automate_headers_request_cmd() const;
214 void write_automate_headers_request_cmd();
215 void read_automate_headers_reply_cmd(std::vector<std::pair<std::string, std::string> > & headers) const;
216 void write_automate_headers_reply_cmd(std::vector<std::pair<std::string, std::string> > const & headers);
217 void read_automate_command_cmd(std::vector<std::string> & args,
218 std::vector<std::pair<std::string, std::string> > & opts) const;
219 void write_automate_command_cmd(std::vector<std::string> const & args,
220 std::vector<std::pair<std::string, std::string> > const & opts);
221 void read_automate_packet_cmd(int & command_num,
222 char & stream,
223 std::string & packet_data) const;
224 void write_automate_packet_cmd(int command_num,
225 char stream,
226 std::string const & packet_data);
227
228 void read_usher_cmd(utf8 & greeting) const;
229 void write_usher_cmd(utf8 const & greeting);
230 void read_usher_reply_cmd(u8 & version, utf8 & server, std::string & pattern) const;
231 void write_usher_reply_cmd(utf8 const & server, std::string const & pattern);
232
233};
234
235#endif // __NETCMD_HH__
236
237// Local Variables:
238// mode: C++
239// fill-column: 76
240// c-file-style: "gnu"
241// indent-tabs-mode: nil
242// End:
243// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status