ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Transport.pm
(Generate patch)

Comparing AnyEvent-MP/MP/Transport.pm (file contents):
Revision 1.6 by root, Sun Aug 2 14:52:41 2009 UTC vs.
Revision 1.8 by root, Mon Aug 3 14:58:13 2009 UTC

9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This is the superclass for MP transports, most of which is considered an 11This is the superclass for MP transports, most of which is considered an
12implementation detail. 12implementation detail.
13 13
14Future versions might document the actual protocol. 14See the "PROTOCOL" section below if you want to write another client for
15this protocol.
15 16
16=head1 FUNCTIONS/METHODS 17=head1 FUNCTIONS/METHODS
17 18
18=over 4 19=over 4
19 20
111 peername => $peername, # for verification 112 peername => $peername, # for verification
112 ; 113 ;
113 114
114=cut 115=cut
115 116
116our @FRAMING_WANT = qw(json storable);#d##TODO# 117our @FRAMINGS = qw(json storable); # the framing types we accept and send, in order of preference
118our @AUTH_SND = qw(hmac_md6_64_256); # auth types we send
119our @AUTH_RCV = (@AUTH_SND, qw(hex_secret)); # auth types we accept
120
121#AnyEvent::Handle::register_write_type mp_record => sub {
122#};
117 123
118sub new { 124sub new {
119 my ($class, %arg) = @_; 125 my ($class, %arg) = @_;
120 126
121 my $self = bless \%arg, $class; 127 my $self = bless \%arg, $class;
124 130
125 { 131 {
126 Scalar::Util::weaken (my $self = $self); 132 Scalar::Util::weaken (my $self = $self);
127 133
128 if (exists $arg{connect}) { 134 if (exists $arg{connect}) {
129 $arg{tls} ||= "connect";
130 $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 }; 135 $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 };
131 } 136 }
132 137
133 $arg{secret} = AnyEvent::MP::Base::default_secret () 138 $arg{secret} = AnyEvent::MP::Base::default_secret ()
134 unless exists $arg{secret}; 139 unless exists $arg{secret};
144 peername => delete $arg{peername}, 149 peername => delete $arg{peername},
145 ; 150 ;
146 151
147 my $secret = $arg{secret}; 152 my $secret = $arg{secret};
148 my $greeting_kv = $self->{greeting} ||= {}; 153 my $greeting_kv = $self->{greeting} ||= {};
149 $greeting_kv->{"tls1.0"} ||= $arg{tls} 154 $greeting_kv->{"tls"} = "1.0"
150 if exists $arg{tls} && $arg{tls_ctx}; 155 if $arg{tls_ctx};
151 $greeting_kv->{provider} = "AE-$VERSION"; 156 $greeting_kv->{provider} = "AE-$VERSION";
157 $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport};
152 158
153 # send greeting 159 # send greeting
154 my $lgreeting = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min 160 my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min
155 . ";$AnyEvent::MP::Base::UNIQ" 161 . ";$AnyEvent::MP::Base::UNIQ"
156 . ";$AnyEvent::MP::Base::NODE" 162 . ";$AnyEvent::MP::Base::NODE"
157 . ";" . (MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), "") 163 . ";" . (join ",", @AUTH_RCV)
158 . ";hmac_md6_64_256" # hardcoded atm. 164 . ";" . (join ",", @FRAMINGS)
159 . ";json" # hardcoded atm.
160 . ";$self->{peerhost};$self->{peerport}"
161 . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); 165 . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv);
166 my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), "";
162 167
163 $self->{hdl}->push_write ("$lgreeting\012"); 168 $self->{hdl}->push_write ("$lgreeting1\012$lgreeting2\012");
164 169
165 # expect greeting 170 # expect greeting
166 $self->{hdl}->push_read (line => sub { 171 $self->{hdl}->push_read (line => sub {
167 my $rgreeting = $_[1]; 172 my $rgreeting1 = $_[1];
168 173
169 my ($aemp, $version, $version_min, $uniq, $rnode, undef, $auth, $framing, $peerport, $peerhost, @kv) = split /;/, $rgreeting; 174 my ($aemp, $version, $version_min, $uniq, $rnode, $auths, $framings, @kv) = split /;/, $rgreeting1;
170 175
171 if ($aemp ne "aemp") { 176 if ($aemp ne "aemp") {
172 return $self->error ("unparsable greeting"); 177 return $self->error ("unparsable greeting");
173 } elsif ($version_min > $PROTOCOL_VERSION) { 178 } elsif ($version_min > $PROTOCOL_VERSION) {
174 return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version_min .. $version)"); 179 return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version_min .. $version)");
175 } elsif ($auth ne "hmac_md6_64_256") {
176 return $self->error ("unsupported auth method ($auth)");
177 } elsif ($framing ne "json") {
178 return $self->error ("unsupported framing method ($auth)");
179 } 180 }
181
182 my $s_auth;
183 for my $auth_ (split /,/, $auths) {
184 if (grep $auth_ eq $_, @AUTH_SND) {
185 $s_auth = $auth_;
186 last;
187 }
188 }
189
190 defined $s_auth
191 or return $self->error ("$auths: no common auth type supported");
192
193 die unless $s_auth eq "hmac_md6_64_256"; # hardcoded atm.
194
195 my $s_framing;
196 for my $framing_ (split /,/, $framings) {
197 if (grep $framing_ eq $_, @FRAMINGS) {
198 $s_framing = $framing_;
199 last;
200 }
201 }
202
203 defined $s_framing
204 or return $self->error ("$framings: no common framing method supported");
180 205
181 $self->{remote_uniq} = $uniq; 206 $self->{remote_uniq} = $uniq;
182 $self->{remote_node} = $rnode; 207 $self->{remote_node} = $rnode;
183 208
184 $self->{remote_greeting} = { 209 $self->{remote_greeting} = {
185 map /^([^=]+)(?:=(.*))?/ ? ($1 => $2) : (), 210 map /^([^=]+)(?:=(.*))?/ ? ($1 => $2) : (),
186 @kv 211 @kv
187 }; 212 };
188 213
189 if (exists $self->{tls} and $self->{tls_ctx} and exists $self->{remote_greeting}{"tls1.0"}) { 214 # read nonce
215 $self->{hdl}->push_read (line => sub {
216 my $rgreeting2 = $_[1];
217
190 if ($self->{tls} ne $self->{remote_greeting}{"tls1.0"}) { 218 if ($self->{tls_ctx} and 1 == int $self->{remote_greeting}{"tls"}) {
191 return $self->error ("TLS server/client mismatch"); 219 $self->{tls} = $lgreeting2 lt $rgreeting2 ? "connect" : "accept";
220 $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx});
192 } 221 }
193 $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx});
194 } 222
195
196 # auth 223 # auth
197 require Digest::MD6; 224 require Digest::MD6;
198 require Digest::HMAC_MD6; 225 require Digest::HMAC_MD6;
199 226
200 my $key = Digest::MD6::md6_hex ($secret); 227 my $key = Digest::MD6::md6_hex ($secret);
201 my $lauth = Digest::HMAC_MD6::hmac_md6_base64 ($key, "$lgreeting\012$rgreeting", 64, 256); 228 my $lauth = Digest::HMAC_MD6::hmac_md6_base64 ($key, "$lgreeting1\012$lgreeting2\012$rgreeting1\012$rgreeting2\012", 64, 256);
202 my $rauth = Digest::HMAC_MD6::hmac_md6_base64 ($key, "$rgreeting\012$lgreeting", 64, 256);
203 229
230 my $rauth =
231 $s_auth eq "hmac_md6_64_256" ? Digest::HMAC_MD6::hmac_md6_base64 ($key, "$rgreeting1\012$rgreeting2\012$lgreeting1\012$lgreeting2\012", 64, 256)
232 : $s_auth eq "hex_secret" ? unpack "H*", $secret
233 : die;
234
204 $lauth ne $rauth # echo attack? 235 $lauth ne $rauth # echo attack?
205 or return $self->error ("authentication error"); 236 or return $self->error ("authentication error");
206 237
207 $self->{hdl}->push_write ("$auth;$lauth;$framing\012"); 238 $self->{hdl}->push_write ("$s_auth;$lauth;$s_framing\012");
208 239
209 $self->{hdl}->rbuf_max (64); # enough for 44 reply bytes or so 240 $self->{hdl}->rbuf_max (64); # enough for 44 reply bytes or so
210 $self->{hdl}->push_read (line => sub { 241 $self->{hdl}->push_read (line => sub {
211 my ($hdl, $rline) = @_; 242 my ($hdl, $rline) = @_;
212 243
213 my ($auth_method, $rauth2, $r_framing) = split /;/, $rline; 244 my ($auth_method, $rauth2, $r_framing) = split /;/, $rline;
214 245
215 if ($rauth2 ne $rauth) { 246 if ($rauth2 ne $rauth) {
216 return $self->error ("authentication failure/shared secret mismatch"); 247 return $self->error ("authentication failure/shared secret mismatch");
217 } 248 }
218 249
219 $self->{s_framing} = "json";#d# 250 $self->{s_framing} = $s_framing;
220 251
221 $hdl->rbuf_max (undef); 252 $hdl->rbuf_max (undef);
222 my $queue = delete $self->{queue}; # we are connected 253 my $queue = delete $self->{queue}; # we are connected
223 254
224 $self->connected; 255 $self->connected;
225 256
226 $hdl->push_write ($self->{s_framing} => $_) 257 $hdl->push_write ($self->{s_framing} => $_)
227 for @$queue; 258 for @$queue;
228 259
229 my $rmsg; $rmsg = sub { 260 my $rmsg; $rmsg = sub {
230 $_[0]->push_read ($r_framing => $rmsg); 261 $_[0]->push_read ($r_framing => $rmsg);
231 262
232 AnyEvent::MP::Base::_inject ($_[1]); 263 AnyEvent::MP::Base::_inject ($_[1]);
233 }; 264 };
234 $hdl->push_read ($r_framing => $rmsg); 265 $hdl->push_read ($r_framing => $rmsg);
266 });
235 }); 267 });
236 }); 268 });
237 } 269 }
238 270
239 $self 271 $self
243 my ($self, $msg) = @_; 275 my ($self, $msg) = @_;
244 276
245 if ($self->{node} && $self->{node}{transport} == $self) { 277 if ($self->{node} && $self->{node}{transport} == $self) {
246 $self->{node}->clr_transport; 278 $self->{node}->clr_transport;
247 } 279 }
248# $self->{on_error}($self, $msg); 280 $AnyEvent::MP::Base::WARN->("$self->{peerhost}:$self->{peerport}: $msg");
249 $self->destroy; 281 $self->destroy;
250} 282}
251 283
252sub connected { 284sub connected {
253 my ($self) = @_; 285 my ($self) = @_;
274 $self->destroy; 306 $self->destroy;
275} 307}
276 308
277=back 309=back
278 310
311=head1 PROTOCOL
312
313The protocol is relatively simple, and consists of three phases which are
314symmetrical for both sides: greeting (followed by optionally switching to
315TLS mode), authentication and packet exchange.
316
317the protocol is designed to allow both full-text and binary streams.
318
319The greeting consists of two text lines that are ended by either an ASCII
320CR LF pair, or a single ASCII LF (recommended).
321
322=head2 GREETING
323
324The first line contains strings separated (not ended) by C<;>
325characters. The first seven strings are fixed by the protocol, the
326remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;>
327characters themselves.
328
329The seven fixed strings are:
330
331=over 4
332
333=item C<aemp>
334
335The constant C<aemp> to identify the protocol.
336
337=item protocol version
338
339The (maximum) protocol version supported by this end, currently C<0>.
340
341=item minimum protocol version
342
343The minimum protocol version supported by this end, currently C<0>.
344
345=item a token uniquely identifying the current node instance
346
347This is a string that must change between restarts. It usually contains
348things like the current time, the (OS) process id or similar values, but
349no meaning of the contents are assumed.
350
351=item the node endpoint descriptors
352
353for public nodes, this is a comma-separated list of protocol endpoints,
354i.e., the noderef. For slave nodes, this is a unique identifier.
355
356=item the acceptable authentication methods
357
358A comma-separated list of authentication methods supported by the
359node. Note that AnyEvent::MP supports a C<hex_secret> authentication
360method that accepts a cleartext password (hex-encoded), but will not use
361this auth method itself.
362
363The receiving side should choose the first auth method it supports.
364
365=item the acceptable framing formats
366
367A comma-separated list of packet encoding/framign formats understood. The
368receiving side should choose the first framing format it supports for
369sending packets (which might be different from the format it has to accept).
370
371=cut
372
373The remaining arguments are C<KEY=VALUE> pairs. The following key-value
374pairs are known at this time:
375
376=over 4
377
378=item provider=<module-version>
379
380The software provider for this implementation. For AnyEvent::MP, this is
381C<AE-0.0> or whatever version it currently is at.
382
383=item peeraddr=<host>:<port>
384
385The peer address (socket address of the other side) as seen locally, in the same format
386as noderef endpoints.
387
388=item tls=<major>.<minor>
389
390Indicates that the other side supports TLS (version should be 1.0) and
391wishes to do a TLS handshake.
392
393=back
394
395After this greeting line there will be a second line containing a
396cryptographic nonce, i.e. random data of high quality. To keep the
397protocol text-only, these are usually 32 base64-encoded octets, but
398it could be anything that doesn't contain any ASCII CR or ASCII LF
399characters.
400
401Example of the two lines of greeting:
402
403 aemp;0;0;e7d.4a76f48f;10.0.0.1:4040;hmac_md6_64_256,hex_secret;json,storable;provider=AE-0.0;peeraddr=127.0.0.1:1235
404 XntegV2Guvss0qNn7phCPnoU87xqxV+4Mqm/5y4iQm6a
405
406=head2 TLS handshake
407
408If, after the handshake, both sides indicate interest in TLS, then the
409connection I<must> use TLS, or fail.
410
411Both sides compare their nonces, and the side who sent the lower nonce
412value ("string" comparison on the raw octet values) becomes the client,
413and the one with the higher nonce the server.
414
415=head2 AUTHENTICATION PHASE
416
417After the greeting is received (and the optional TLS handshake),
418the authentication phase begins, which consists of sending a single
419C<;>-separated line with three fixed strings and any number of
420C<KEY=VALUE> pairs.
421
422The three fixed strings are:
423
424=over 4
425
426=item the authentication method chosen
427
428This must be one of the methods offered by the other side in the greeting.
429
430=item the authentication data
431
432The authentication data itself, usually base64 or hex-encoded data.
433
434=item the framing protocol chosen
435
436This must be one of the framing protocols offered by the other side in the
437greeting. Each side must accept the choice of the other side.
438
439=back
440
441=head2 DATA PHASE
442
443After this, packets get exchanged using the chosen framing protocol. It is
444quite possible that both sides use a different framing protocol.
445
279=head1 SEE ALSO 446=head1 SEE ALSO
280 447
281L<AnyEvent>. 448L<AnyEvent>.
282 449
283=head1 AUTHOR 450=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines