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.8 by root, Mon Aug 3 14:58:13 2009 UTC vs.
Revision 1.18 by root, Tue Aug 4 18:33:30 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::MP::Transport - actual transport protocol 3AnyEvent::MP::Transport - actual transport protocol handler
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::MP::Transport; 7 use AnyEvent::MP::Transport;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This is the superclass for MP transports, most of which is considered an 11This implements the actual transport protocol for MP (it represents a
12implementation detail. 12single link), most of which is considered an implementation detail.
13 13
14See the "PROTOCOL" section below if you want to write another client for 14See the "PROTOCOL" section below if you want to write another client for
15this protocol. 15this protocol.
16 16
17=head1 FUNCTIONS/METHODS 17=head1 FUNCTIONS/METHODS
43Creates a listener on the given host/port using 43Creates a listener on the given host/port using
44C<AnyEvent::Socket::tcp_server>. 44C<AnyEvent::Socket::tcp_server>.
45 45
46See C<new>, below, for constructor arguments. 46See C<new>, below, for constructor arguments.
47 47
48Defaults for peerhost, peerport, fh and tls are provided. 48Defaults for peerhost, peerport and fh are provided.
49 49
50=cut 50=cut
51 51
52sub mp_server($$@) { 52sub mp_server($$@) {
53 my $cb = pop; 53 my $cb = pop;
58 58
59 $cb->(new AnyEvent::MP::Transport 59 $cb->(new AnyEvent::MP::Transport
60 fh => $fh, 60 fh => $fh,
61 peerhost => $host, 61 peerhost => $host,
62 peerport => $port, 62 peerport => $port,
63 tls => "accept",
64 @args, 63 @args,
65 ); 64 );
66 } 65 }
67} 66}
68 67
82 $cb->(new AnyEvent::MP::Transport 81 $cb->(new AnyEvent::MP::Transport
83 fh => $fh, 82 fh => $fh,
84 peername => $host, 83 peername => $host,
85 peerhost => $nhost, 84 peerhost => $nhost,
86 peerport => $nport, 85 peerport => $nport,
87 tls => "accept",
88 @args, 86 @args,
89 ); 87 );
90 } 88 }
91} 89}
92 90
105 on_eof => sub { clean-close-callback }, 103 on_eof => sub { clean-close-callback },
106 on_connect => sub { successful-connect-callback }, 104 on_connect => sub { successful-connect-callback },
107 greeting => { key => value }, 105 greeting => { key => value },
108 106
109 # tls support 107 # tls support
110 tls => "accept|connect",
111 tls_ctx => AnyEvent::TLS, 108 tls_ctx => AnyEvent::TLS,
112 peername => $peername, # for verification 109 peername => $peername, # for verification
113 ; 110 ;
114 111
115=cut 112=cut
116 113
117our @FRAMINGS = qw(json storable); # the framing types we accept and send, in order of preference 114our @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 115our @AUTH_SND = qw(hmac_md6_64_256); # auth types we send
119our @AUTH_RCV = (@AUTH_SND, qw(hex_secret)); # auth types we accept 116our @AUTH_RCV = (@AUTH_SND, qw(cleartext)); # auth types we accept
120 117
121#AnyEvent::Handle::register_write_type mp_record => sub { 118#AnyEvent::Handle::register_write_type mp_record => sub {
122#}; 119#};
123 120
124sub new { 121sub new {
129 $self->{queue} = []; 126 $self->{queue} = [];
130 127
131 { 128 {
132 Scalar::Util::weaken (my $self = $self); 129 Scalar::Util::weaken (my $self = $self);
133 130
134 if (exists $arg{connect}) { 131 $arg{tls_ctx_disabled} ||= {
135 $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 }; 132 sslv2 => 0,
133 sslv3 => 0,
134 tlsv1 => 1,
135 verify => 1,
136 cert_file => "secret.pem",
137 ca_file => "secret.pem",
138 verify_require_client_cert => 1,
136 } 139 };
137 140
138 $arg{secret} = AnyEvent::MP::Base::default_secret () 141 $arg{secret} = AnyEvent::MP::Base::default_secret ()
139 unless exists $arg{secret}; 142 unless exists $arg{secret};
140 143
141 $self->{hdl} = new AnyEvent::Handle 144 $self->{hdl} = new AnyEvent::Handle
142 fh => delete $arg{fh}, 145 fh => delete $arg{fh},
143 rbuf_max => 64 * 1024,
144 autocork => 1, 146 autocork => 1,
145 no_delay => 1, 147 no_delay => 1,
146 on_error => sub { 148 on_error => sub {
147 $self->error ($_[2]); 149 $self->error ($_[2]);
148 }, 150 },
155 if $arg{tls_ctx}; 157 if $arg{tls_ctx};
156 $greeting_kv->{provider} = "AE-$VERSION"; 158 $greeting_kv->{provider} = "AE-$VERSION";
157 $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; 159 $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport};
158 160
159 # send greeting 161 # send greeting
160 my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min 162 my $lgreeting1 = "aemp;$PROTOCOL_VERSION"
161 . ";$AnyEvent::MP::Base::UNIQ" 163 . ";$AnyEvent::MP::Base::UNIQ"
162 . ";$AnyEvent::MP::Base::NODE" 164 . ";$AnyEvent::MP::Base::NODE"
163 . ";" . (join ",", @AUTH_RCV) 165 . ";" . (join ",", @AUTH_RCV)
164 . ";" . (join ",", @FRAMINGS) 166 . ";" . (join ",", @FRAMINGS)
165 . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); 167 . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv);
168
166 my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), ""; 169 my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), "";
167 170
168 $self->{hdl}->push_write ("$lgreeting1\012$lgreeting2\012"); 171 $self->{hdl}->push_write ("$lgreeting1\012$lgreeting2\012");
169 172
170 # expect greeting 173 # expect greeting
174 $self->{hdl}->rbuf_max (4 * 1024);
171 $self->{hdl}->push_read (line => sub { 175 $self->{hdl}->push_read (line => sub {
172 my $rgreeting1 = $_[1]; 176 my $rgreeting1 = $_[1];
173 177
174 my ($aemp, $version, $version_min, $uniq, $rnode, $auths, $framings, @kv) = split /;/, $rgreeting1; 178 my ($aemp, $version, $uniq, $rnode, $auths, $framings, @kv) = split /;/, $rgreeting1;
175 179
176 if ($aemp ne "aemp") { 180 if ($aemp ne "aemp") {
177 return $self->error ("unparsable greeting"); 181 return $self->error ("unparsable greeting");
178 } elsif ($version_min > $PROTOCOL_VERSION) { 182 } elsif ($version != $PROTOCOL_VERSION) {
179 return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version_min .. $version)"); 183 return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version)");
180 } 184 }
181 185
182 my $s_auth; 186 my $s_auth;
183 for my $auth_ (split /,/, $auths) { 187 for my $auth_ (split /,/, $auths) {
184 if (grep $auth_ eq $_, @AUTH_SND) { 188 if (grep $auth_ eq $_, @AUTH_SND) {
213 217
214 # read nonce 218 # read nonce
215 $self->{hdl}->push_read (line => sub { 219 $self->{hdl}->push_read (line => sub {
216 my $rgreeting2 = $_[1]; 220 my $rgreeting2 = $_[1];
217 221
218 if ($self->{tls_ctx} and 1 == int $self->{remote_greeting}{"tls"}) { 222 if ($self->{tls_ctx} and 1 == int $self->{remote_greeting}{tls}) {
219 $self->{tls} = $lgreeting2 lt $rgreeting2 ? "connect" : "accept"; 223 $self->{tls} = $lgreeting2 lt $rgreeting2 ? "connect" : "accept";
220 $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx}); 224 $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx});
221 } 225 }
222 226
223 # auth 227 # auth
224 require Digest::MD6; 228 require Digest::MD6;
225 require Digest::HMAC_MD6; 229 require Digest::HMAC_MD6;
226 230
227 my $key = Digest::MD6::md6_hex ($secret); 231 my $key = Digest::MD6::md6 ($secret);
228 my $lauth = Digest::HMAC_MD6::hmac_md6_base64 ($key, "$lgreeting1\012$lgreeting2\012$rgreeting1\012$rgreeting2\012", 64, 256); 232 my $lauth = Digest::HMAC_MD6::hmac_md6_hex ($key, "$lgreeting1\012$lgreeting2\012$rgreeting1\012$rgreeting2\012", 64, 256);
229 233
230 my $rauth = 234 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) 235 $s_auth eq "hmac_md6_64_256" ? Digest::HMAC_MD6::hmac_md6_hex ($key, "$rgreeting1\012$rgreeting2\012$lgreeting1\012$lgreeting2\012", 64, 256)
232 : $s_auth eq "hex_secret" ? unpack "H*", $secret 236 : $s_auth eq "cleartext" ? unpack "H*", $secret
233 : die; 237 : die;
234 238
235 $lauth ne $rauth # echo attack? 239 $lauth ne $rauth # echo attack?
236 or return $self->error ("authentication error"); 240 or return $self->error ("authentication error");
237 241
238 $self->{hdl}->push_write ("$s_auth;$lauth;$s_framing\012"); 242 $self->{hdl}->push_write ("$s_auth;$lauth;$s_framing\012");
239 243
240 $self->{hdl}->rbuf_max (64); # enough for 44 reply bytes or so 244 # reasd the authentication response
241 $self->{hdl}->push_read (line => sub { 245 $self->{hdl}->push_read (line => sub {
242 my ($hdl, $rline) = @_; 246 my ($hdl, $rline) = @_;
243 247
244 my ($auth_method, $rauth2, $r_framing) = split /;/, $rline; 248 my ($auth_method, $rauth2, $r_framing) = split /;/, $rline;
245 249
252 $hdl->rbuf_max (undef); 256 $hdl->rbuf_max (undef);
253 my $queue = delete $self->{queue}; # we are connected 257 my $queue = delete $self->{queue}; # we are connected
254 258
255 $self->connected; 259 $self->connected;
256 260
261 my $src_node = $self->{node};
262
257 $hdl->push_write ($self->{s_framing} => $_) 263 $hdl->push_write ($self->{s_framing} => $_)
258 for @$queue; 264 for @$queue;
259 265
260 my $rmsg; $rmsg = sub { 266 my $rmsg; $rmsg = sub {
261 $_[0]->push_read ($r_framing => $rmsg); 267 $_[0]->push_read ($r_framing => $rmsg);
262 268
269 local $AnyEvent::MP::Base::SRCNODE = $src_node;
263 AnyEvent::MP::Base::_inject ($_[1]); 270 AnyEvent::MP::Base::_inject (@{ $_[1] });
264 }; 271 };
265 $hdl->push_read ($r_framing => $rmsg); 272 $hdl->push_read ($r_framing => $rmsg);
266 }); 273 });
267 }); 274 });
268 }); 275 });
273 280
274sub error { 281sub error {
275 my ($self, $msg) = @_; 282 my ($self, $msg) = @_;
276 283
277 if ($self->{node} && $self->{node}{transport} == $self) { 284 if ($self->{node} && $self->{node}{transport} == $self) {
285 $self->{node}->fail (transport_error => $msg);
278 $self->{node}->clr_transport; 286 $self->{node}->clr_transport;
279 } 287 }
280 $AnyEvent::MP::Base::WARN->("$self->{peerhost}:$self->{peerport}: $msg"); 288 $AnyEvent::MP::Base::WARN->("$self->{peerhost}:$self->{peerport}: $msg");
281 $self->destroy; 289 $self->destroy;
282} 290}
319The greeting consists of two text lines that are ended by either an ASCII 327The greeting consists of two text lines that are ended by either an ASCII
320CR LF pair, or a single ASCII LF (recommended). 328CR LF pair, or a single ASCII LF (recommended).
321 329
322=head2 GREETING 330=head2 GREETING
323 331
332All the lines until after authentication must not exceed 4kb in length,
333including delimiter. Afterwards there is no limit on the packet size that
334can be received.
335
336=head3 First Greeting Line
337
338Example:
339
340 aemp;0;fec.4a7720fc;127.0.0.1:1235,[::1]:1235;hmac_md6_64_256;json,storable;provider=AE-0.0
341
324The first line contains strings separated (not ended) by C<;> 342The first line contains strings separated (not ended) by C<;>
325characters. The first seven strings are fixed by the protocol, the 343characters. The first even ixtrings are fixed by the protocol, the
326remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;> 344remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;>
327characters themselves. 345characters themselves.
328 346
329The seven fixed strings are: 347The fixed strings are:
330 348
331=over 4 349=over 4
332 350
333=item C<aemp> 351=item protocol identification
334 352
335The constant C<aemp> to identify the protocol. 353The constant C<aemp> to identify the protocol.
336 354
337=item protocol version 355=item protocol version
338 356
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>. 357The protocol version supported by this end, currently C<0>. If the
358versions don't match then no communication is possible. Minor extensions
359are supposed to be handled through additional key-value pairs.
344 360
345=item a token uniquely identifying the current node instance 361=item a token uniquely identifying the current node instance
346 362
347This is a string that must change between restarts. It usually contains 363This is a string that must change between restarts. It usually contains
348things like the current time, the (OS) process id or similar values, but 364things like the current time, the (OS) process id or similar values, but
366 382
367A comma-separated list of packet encoding/framign formats understood. The 383A comma-separated list of packet encoding/framign formats understood. The
368receiving side should choose the first framing format it supports for 384receiving side should choose the first framing format it supports for
369sending packets (which might be different from the format it has to accept). 385sending packets (which might be different from the format it has to accept).
370 386
371=cut 387=back
372 388
373The remaining arguments are C<KEY=VALUE> pairs. The following key-value 389The remaining arguments are C<KEY=VALUE> pairs. The following key-value
374pairs are known at this time: 390pairs are known at this time:
375 391
376=over 4 392=over 4
389 405
390Indicates that the other side supports TLS (version should be 1.0) and 406Indicates that the other side supports TLS (version should be 1.0) and
391wishes to do a TLS handshake. 407wishes to do a TLS handshake.
392 408
393=back 409=back
410
411=head3 Second Greeting Line
394 412
395After this greeting line there will be a second line containing a 413After this greeting line there will be a second line containing a
396cryptographic nonce, i.e. random data of high quality. To keep the 414cryptographic nonce, i.e. random data of high quality. To keep the
397protocol text-only, these are usually 32 base64-encoded octets, but 415protocol text-only, these are usually 32 base64-encoded octets, but
398it could be anything that doesn't contain any ASCII CR or ASCII LF 416it could be anything that doesn't contain any ASCII CR or ASCII LF
399characters. 417characters.
400 418
401Example of the two lines of greeting: 419I<< The two nonces B<must> be different, and an aemp implementation
420B<must> check and fail when they are identical >>.
402 421
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 422Example of a nonce line:
404 XntegV2Guvss0qNn7phCPnoU87xqxV+4Mqm/5y4iQm6a 423
424 p/I122ql7kJR8lumW3lXlXCeBnyDAvz8NQo3x5IFowE4
405 425
406=head2 TLS handshake 426=head2 TLS handshake
407 427
408If, after the handshake, both sides indicate interest in TLS, then the 428I<< If, after the handshake, both sides indicate interest in TLS, then the
409connection I<must> use TLS, or fail. 429connection B<must> use TLS, or fail.>>
410 430
411Both sides compare their nonces, and the side who sent the lower nonce 431Both sides compare their nonces, and the side who sent the lower nonce
412value ("string" comparison on the raw octet values) becomes the client, 432value ("string" comparison on the raw octet values) becomes the client,
413and the one with the higher nonce the server. 433and the one with the higher nonce the server.
414 434
425 445
426=item the authentication method chosen 446=item the authentication method chosen
427 447
428This must be one of the methods offered by the other side in the greeting. 448This must be one of the methods offered by the other side in the greeting.
429 449
450The currently supported authentication methods are:
451
452=over 4
453
454=item cleartext
455
456This is simply the shared secret, lowercase-hex-encoded. This method is of
457course very insecure, unless TLS is used, which is why this module will
458accept, but not generate, cleartext auth replies.
459
460=item hmac_md6_64_256
461
462This method uses an MD6 HMAC with 64 bit blocksize and 256 bit hash. First, the shared secret
463is hashed with MD6:
464
465 key = MD6 (secret)
466
467This secret is then used to generate the "local auth reply", by taking
468the two local greeting lines and the two remote greeting lines (without
469line endings), appending \012 to all of them, concatenating them and
470calculating the MD6 HMAC with the key.
471
472 lauth = HMAC_MD6 key, "lgreeting1\012lgreeting2\012rgreeting1\012rgreeting2\012"
473
474This authentication token is then lowercase-hex-encoded and sent to the
475other side.
476
477Then the remote auth reply is generated using the same method, but local
478and remote greeting lines swapped:
479
480 rauth = HMAC_MD6 key, "rgreeting1\012rgreeting2\012lgreeting1\012lgreeting2\012"
481
482This is the token that is expected from the other side.
483
484=back
485
430=item the authentication data 486=item the authentication data
431 487
432The authentication data itself, usually base64 or hex-encoded data. 488The authentication data itself, usually base64 or hex-encoded data, see
489above.
433 490
434=item the framing protocol chosen 491=item the framing protocol chosen
435 492
436This must be one of the framing protocols offered by the other side in the 493This 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. 494greeting. Each side must accept the choice of the other side.
438 495
439=back 496=back
440 497
498Example of an authentication reply:
499
500 hmac_md6_64_256;363d5175df38bd9eaddd3f6ca18aa1c0c4aa22f0da245ac638d048398c26b8d3;json
501
441=head2 DATA PHASE 502=head2 DATA PHASE
442 503
443After this, packets get exchanged using the chosen framing protocol. It is 504After this, packets get exchanged using the chosen framing protocol. It is
444quite possible that both sides use a different framing protocol. 505quite possible that both sides use a different framing protocol.
445 506
507=head2 FULL EXAMPLE
508
509This is an actual protocol dump of a handshake, followed by a single data
510packet. The greater than/less than lines indicate the direction of the
511transfer only.
512
513 > aemp;0;nndKd+gn;10.0.0.1:4040;hmac_md6_64_256,cleartext;json,storable;provider=AE-0.0;peeraddr=127.0.0.1:1235
514 > sRG8bbc4TDbkpvH8FTP4HBs87OhepH6VuApoZqXXskuG
515 < aemp;0;nmpKd+gh;127.0.0.1:1235,[::1]:1235;hmac_md6_64_256,cleartext;json,storable;provider=AE-0.0;peeraddr=127.0.0.1:58760
516 < dCEUcL/LJVSTJcx8byEsOzrwhzJYOq+L3YcopA5T6EAo
517 > hmac_md6_64_256;9513d4b258975accfcb2ab7532b83690e9c119a502c612203332a591c7237788;json
518 < hmac_md6_64_256;0298d6ba2240faabb2b2e881cf86b97d70a113ca74a87dc006f9f1e9d3010f90;json
519 > ["","lookup","pinger","10.0.0.1:4040#nndKd+gn.a","resolved"]
520
446=head1 SEE ALSO 521=head1 SEE ALSO
447 522
448L<AnyEvent>. 523L<AnyEvent>.
449 524
450=head1 AUTHOR 525=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines