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.7 by root, Mon Aug 3 14:47:25 2009 UTC vs.
Revision 1.8 by root, Mon Aug 3 14:58:13 2009 UTC

130 130
131 { 131 {
132 Scalar::Util::weaken (my $self = $self); 132 Scalar::Util::weaken (my $self = $self);
133 133
134 if (exists $arg{connect}) { 134 if (exists $arg{connect}) {
135 $arg{tls} ||= "connect";
136 $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 }; 135 $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 };
137 } 136 }
138 137
139 $arg{secret} = AnyEvent::MP::Base::default_secret () 138 $arg{secret} = AnyEvent::MP::Base::default_secret ()
140 unless exists $arg{secret}; 139 unless exists $arg{secret};
150 peername => delete $arg{peername}, 149 peername => delete $arg{peername},
151 ; 150 ;
152 151
153 my $secret = $arg{secret}; 152 my $secret = $arg{secret};
154 my $greeting_kv = $self->{greeting} ||= {}; 153 my $greeting_kv = $self->{greeting} ||= {};
155 $greeting_kv->{"tls1.0"} ||= $arg{tls} 154 $greeting_kv->{"tls"} = "1.0"
156 if exists $arg{tls} && $arg{tls_ctx}; 155 if $arg{tls_ctx};
157 $greeting_kv->{provider} = "AE-$VERSION"; 156 $greeting_kv->{provider} = "AE-$VERSION";
158 $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; 157 $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport};
159 158
160 # send greeting 159 # send greeting
161 my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min 160 my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min
210 $self->{remote_greeting} = { 209 $self->{remote_greeting} = {
211 map /^([^=]+)(?:=(.*))?/ ? ($1 => $2) : (), 210 map /^([^=]+)(?:=(.*))?/ ? ($1 => $2) : (),
212 @kv 211 @kv
213 }; 212 };
214 213
215 if (exists $self->{tls} and $self->{tls_ctx} and exists $self->{remote_greeting}{"tls1.0"}) {
216 if ($self->{tls} ne $self->{remote_greeting}{"tls1.0"}) {
217 return $self->error ("TLS server/client mismatch");
218 }
219 $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx});
220 }
221
222 # read nonce 214 # read nonce
223 $self->{hdl}->push_read (line => sub { 215 $self->{hdl}->push_read (line => sub {
224 my $rgreeting2 = $_[1]; 216 my $rgreeting2 = $_[1];
225 217
218 if ($self->{tls_ctx} and 1 == int $self->{remote_greeting}{"tls"}) {
219 $self->{tls} = $lgreeting2 lt $rgreeting2 ? "connect" : "accept";
220 $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx});
221 }
222
226 # auth 223 # auth
227 require Digest::MD6; 224 require Digest::MD6;
228 require Digest::HMAC_MD6; 225 require Digest::HMAC_MD6;
229 226
230 my $key = Digest::MD6::md6_hex ($secret); 227 my $key = Digest::MD6::md6_hex ($secret);
322The greeting consists of two text lines that are ended by either an ASCII 319The greeting consists of two text lines that are ended by either an ASCII
323CR LF pair, or a single ASCII LF (recommended). 320CR LF pair, or a single ASCII LF (recommended).
324 321
325=head2 GREETING 322=head2 GREETING
326 323
327The first line contains strings seperated (not ended) by C<;> 324The first line contains strings separated (not ended) by C<;>
328characters. The first seven strings are fixed by the protocol, the 325characters. The first seven strings are fixed by the protocol, the
329remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;> 326remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;>
330characters themselves. 327characters themselves.
331 328
332The seven fixed strings are: 329The seven fixed strings are:
369 366
370A comma-separated list of packet encoding/framign formats understood. The 367A comma-separated list of packet encoding/framign formats understood. The
371receiving side should choose the first framing format it supports for 368receiving side should choose the first framing format it supports for
372sending packets (which might be different from the format it has to accept). 369sending packets (which might be different from the format it has to accept).
373 370
374 . ";$self->{peerhost};$self->{peerport}" 371=cut
375 . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); 372
376 my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), ""; 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
377=head1 SEE ALSO 446=head1 SEE ALSO
378 447
379L<AnyEvent>. 448L<AnyEvent>.
380 449
381=head1 AUTHOR 450=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines