monotone

monotone Mtn Source Tree

Root/netcmd.hh

1#ifndef __NETCMD_HH__
2#define __NETCMD_HH__
3
4// Copyright (C) 2004 Graydon Hoare <graydon@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include <string>
14#include <vector>
15#include <utility>
16
17#include "merkle_tree.hh"
18#include "numeric_vocab.hh"
19#include "vocab.hh"
20#include "hmac.hh"
21#include "string_queue.hh"
22
23typedef enum
24 {
25 source_role = 1,
26 sink_role = 2,
27 source_and_sink_role = 3
28 }
29protocol_role;
30
31typedef enum
32 {
33 refinement_query = 0,
34 refinement_response = 1
35 }
36refinement_type;
37
38typedef enum
39 {
40 // general commands
41 error_cmd = 0,
42 bye_cmd = 1,
43
44 // authentication commands
45 hello_cmd = 2,
46 anonymous_cmd = 3,
47 auth_cmd = 4,
48 confirm_cmd = 5,
49
50 // refinement commands
51 refine_cmd = 6,
52 done_cmd = 7,
53
54 // transmission commands
55 data_cmd = 8,
56 delta_cmd = 9,
57
58 // usher commands
59 // usher_cmd is sent by a server farm (or anyone else who wants to serve
60 // from multiple databases over the same port), and the reply (containing
61 // the client's include pattern) is used to choose a server to forward the
62 // connection to.
63 // usher_cmd is never sent by the monotone server itself.
64 usher_cmd = 100,
65 usher_reply_cmd = 101
66 }
67netcmd_code;
68
69class netcmd
70{
71private:
72 u8 version;
73 netcmd_code cmd_code;
74 std::string payload;
75public:
76 netcmd();
77 netcmd_code get_cmd_code() const {return cmd_code;}
78 size_t encoded_size();
79 bool operator==(netcmd const & other) const;
80
81
82 // basic cmd i/o (including checksums)
83 void write(std::string & out,
84 chained_hmac & hmac) const;
85 bool read(string_queue & inbuf,
86 chained_hmac & hmac);
87 bool read_string(std::string & inbuf,
88 chained_hmac & hmac) {
89 // this is here only for the regression tests because they want to
90 // read and write to the same type, but we want to have reads from
91 // a string queue so that when data is read in from the network it
92 // can be processed efficiently
93 string_queue tmp(inbuf.size());
94 tmp.append(inbuf);
95 bool ret = read(tmp, hmac);
96 inbuf = tmp.substr(0,tmp.size());
97 return ret;
98 }
99 // i/o functions for each type of command payload
100 void read_error_cmd(std::string & errmsg) const;
101 void write_error_cmd(std::string const & errmsg);
102
103 void read_hello_cmd(rsa_keypair_id & server_keyname,
104 rsa_pub_key & server_key,
105 id & nonce) const;
106 void write_hello_cmd(rsa_keypair_id const & server_keyname,
107 rsa_pub_key const & server_key,
108 id const & nonce);
109
110 void read_bye_cmd(u8 & phase) const;
111 void write_bye_cmd(u8 phase);
112
113 void read_anonymous_cmd(protocol_role & role,
114 utf8 & include_pattern,
115 utf8 & exclude_pattern,
116 rsa_oaep_sha_data & hmac_key_encrypted) const;
117 void write_anonymous_cmd(protocol_role role,
118 utf8 const & include_pattern,
119 utf8 const & exclude_pattern,
120 rsa_oaep_sha_data const & hmac_key_encrypted);
121
122 void read_auth_cmd(protocol_role & role,
123 utf8 & include_pattern,
124 utf8 & exclude_pattern,
125 id & client,
126 id & nonce1,
127 rsa_oaep_sha_data & hmac_key_encrypted,
128 std::string & signature) const;
129 void write_auth_cmd(protocol_role role,
130 utf8 const & include_pattern,
131 utf8 const & exclude_pattern,
132 id const & client,
133 id const & nonce1,
134 rsa_oaep_sha_data const & hmac_key_encrypted,
135 std::string const & signature);
136
137 void read_confirm_cmd() const;
138 void write_confirm_cmd();
139
140 void read_refine_cmd(refinement_type & ty, merkle_node & node) const;
141 void write_refine_cmd(refinement_type ty, merkle_node const & node);
142
143 void read_done_cmd(netcmd_item_type & type, size_t & n_items) const;
144 void write_done_cmd(netcmd_item_type type, size_t n_items);
145
146 void read_data_cmd(netcmd_item_type & type,
147 id & item,
148 std::string & dat) const;
149 void write_data_cmd(netcmd_item_type type,
150 id const & item,
151 std::string const & dat);
152
153 void read_delta_cmd(netcmd_item_type & type,
154 id & base, id & ident,
155 delta & del) const;
156 void write_delta_cmd(netcmd_item_type & type,
157 id const & base, id const & ident,
158 delta const & del);
159
160 void read_usher_cmd(utf8 & greeting) const;
161 void write_usher_reply_cmd(utf8 const & server, utf8 const & pattern);
162
163};
164
165// Local Variables:
166// mode: C++
167// fill-column: 76
168// c-file-style: "gnu"
169// indent-tabs-mode: nil
170// End:
171// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
172
173#endif // __NETCMD_HH__

Archive Download this file

Branches

Tags

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