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

Archive Download this file

Branches

Tags

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