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

Archive Download this file

Branches

Tags

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