… | |
… | |
141 | $arg{secret} = AnyEvent::MP::Base::default_secret () |
141 | $arg{secret} = AnyEvent::MP::Base::default_secret () |
142 | unless exists $arg{secret}; |
142 | unless exists $arg{secret}; |
143 | |
143 | |
144 | $self->{hdl} = new AnyEvent::Handle |
144 | $self->{hdl} = new AnyEvent::Handle |
145 | fh => delete $arg{fh}, |
145 | fh => delete $arg{fh}, |
146 | rbuf_max => 64 * 1024, |
|
|
147 | autocork => 1, |
146 | autocork => 1, |
148 | no_delay => 1, |
147 | no_delay => 1, |
149 | on_error => sub { |
148 | on_error => sub { |
150 | $self->error ($_[2]); |
149 | $self->error ($_[2]); |
151 | }, |
150 | }, |
… | |
… | |
158 | if $arg{tls_ctx}; |
157 | if $arg{tls_ctx}; |
159 | $greeting_kv->{provider} = "AE-$VERSION"; |
158 | $greeting_kv->{provider} = "AE-$VERSION"; |
160 | $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; |
159 | $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; |
161 | |
160 | |
162 | # send greeting |
161 | # send greeting |
163 | my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min |
162 | my $lgreeting1 = "aemp;$PROTOCOL_VERSION" |
164 | . ";$AnyEvent::MP::Base::UNIQ" |
163 | . ";$AnyEvent::MP::Base::UNIQ" |
165 | . ";$AnyEvent::MP::Base::NODE" |
164 | . ";$AnyEvent::MP::Base::NODE" |
166 | . ";" . (join ",", @AUTH_RCV) |
165 | . ";" . (join ",", @AUTH_RCV) |
167 | . ";" . (join ",", @FRAMINGS) |
166 | . ";" . (join ",", @FRAMINGS) |
168 | . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); |
167 | . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); |
|
|
168 | |
169 | my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), ""; |
169 | my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), ""; |
170 | |
170 | |
171 | $self->{hdl}->push_write ("$lgreeting1\012$lgreeting2\012"); |
171 | $self->{hdl}->push_write ("$lgreeting1\012$lgreeting2\012"); |
172 | |
172 | |
173 | # expect greeting |
173 | # expect greeting |
|
|
174 | $self->{hdl}->rbuf_max (4 * 1024); |
174 | $self->{hdl}->push_read (line => sub { |
175 | $self->{hdl}->push_read (line => sub { |
175 | my $rgreeting1 = $_[1]; |
176 | my $rgreeting1 = $_[1]; |
176 | |
177 | |
177 | my ($aemp, $version, $version_min, $uniq, $rnode, $auths, $framings, @kv) = split /;/, $rgreeting1; |
178 | my ($aemp, $version, $uniq, $rnode, $auths, $framings, @kv) = split /;/, $rgreeting1; |
178 | |
179 | |
179 | if ($aemp ne "aemp") { |
180 | if ($aemp ne "aemp") { |
180 | return $self->error ("unparsable greeting"); |
181 | return $self->error ("unparsable greeting"); |
181 | } elsif ($version_min > $PROTOCOL_VERSION) { |
182 | } elsif ($version != $PROTOCOL_VERSION) { |
182 | return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version_min .. $version)"); |
183 | return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version)"); |
183 | } |
184 | } |
184 | |
185 | |
185 | my $s_auth; |
186 | my $s_auth; |
186 | for my $auth_ (split /,/, $auths) { |
187 | for my $auth_ (split /,/, $auths) { |
187 | if (grep $auth_ eq $_, @AUTH_SND) { |
188 | if (grep $auth_ eq $_, @AUTH_SND) { |
… | |
… | |
238 | $lauth ne $rauth # echo attack? |
239 | $lauth ne $rauth # echo attack? |
239 | or return $self->error ("authentication error"); |
240 | or return $self->error ("authentication error"); |
240 | |
241 | |
241 | $self->{hdl}->push_write ("$s_auth;$lauth;$s_framing\012"); |
242 | $self->{hdl}->push_write ("$s_auth;$lauth;$s_framing\012"); |
242 | |
243 | |
243 | $self->{hdl}->rbuf_max (64); # enough for 44 reply bytes or so |
244 | # reasd the authentication response |
244 | $self->{hdl}->push_read (line => sub { |
245 | $self->{hdl}->push_read (line => sub { |
245 | my ($hdl, $rline) = @_; |
246 | my ($hdl, $rline) = @_; |
246 | |
247 | |
247 | my ($auth_method, $rauth2, $r_framing) = split /;/, $rline; |
248 | my ($auth_method, $rauth2, $r_framing) = split /;/, $rline; |
248 | |
249 | |
… | |
… | |
255 | $hdl->rbuf_max (undef); |
256 | $hdl->rbuf_max (undef); |
256 | my $queue = delete $self->{queue}; # we are connected |
257 | my $queue = delete $self->{queue}; # we are connected |
257 | |
258 | |
258 | $self->connected; |
259 | $self->connected; |
259 | |
260 | |
|
|
261 | my $src_node = $self->{node}; |
|
|
262 | |
260 | $hdl->push_write ($self->{s_framing} => $_) |
263 | $hdl->push_write ($self->{s_framing} => $_) |
261 | for @$queue; |
264 | for @$queue; |
262 | |
265 | |
263 | my $rmsg; $rmsg = sub { |
266 | my $rmsg; $rmsg = sub { |
264 | $_[0]->push_read ($r_framing => $rmsg); |
267 | $_[0]->push_read ($r_framing => $rmsg); |
265 | |
268 | |
|
|
269 | local $AnyEvent::MP::Base::SRCNODE = $src_node; |
266 | AnyEvent::MP::Base::_inject ($_[1]); |
270 | AnyEvent::MP::Base::_inject (@{ $_[1] }); |
267 | }; |
271 | }; |
268 | $hdl->push_read ($r_framing => $rmsg); |
272 | $hdl->push_read ($r_framing => $rmsg); |
269 | }); |
273 | }); |
270 | }); |
274 | }); |
271 | }); |
275 | }); |
… | |
… | |
323 | CR LF pair, or a single ASCII LF (recommended). |
327 | CR LF pair, or a single ASCII LF (recommended). |
324 | |
328 | |
325 | =head2 GREETING |
329 | =head2 GREETING |
326 | |
330 | |
327 | The first line contains strings separated (not ended) by C<;> |
331 | The first line contains strings separated (not ended) by C<;> |
328 | characters. The first seven strings are fixed by the protocol, the |
332 | characters. The first even ixtrings are fixed by the protocol, the |
329 | remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;> |
333 | remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;> |
330 | characters themselves. |
334 | characters themselves. |
331 | |
335 | |
|
|
336 | All the lines until after authentication must not exceed 4kb in length, including delimiter. |
|
|
337 | |
332 | The seven fixed strings are: |
338 | The fixed strings are: |
333 | |
339 | |
334 | =over 4 |
340 | =over 4 |
335 | |
341 | |
336 | =item C<aemp> |
342 | =item C<aemp> |
337 | |
343 | |
338 | The constant C<aemp> to identify the protocol. |
344 | The constant C<aemp> to identify the protocol. |
339 | |
345 | |
340 | =item protocol version |
346 | =item protocol version |
341 | |
347 | |
342 | The (maximum) protocol version supported by this end, currently C<0>. |
|
|
343 | |
|
|
344 | =item minimum protocol version |
|
|
345 | |
|
|
346 | The minimum protocol version supported by this end, currently C<0>. |
348 | The protocol version supported by this end, currently C<0>. If the |
|
|
349 | versions don't match then no communication is possible. Minor extensions |
|
|
350 | are supposed to be handled by addign additional key-value pairs. |
347 | |
351 | |
348 | =item a token uniquely identifying the current node instance |
352 | =item a token uniquely identifying the current node instance |
349 | |
353 | |
350 | This is a string that must change between restarts. It usually contains |
354 | This is a string that must change between restarts. It usually contains |
351 | things like the current time, the (OS) process id or similar values, but |
355 | things like the current time, the (OS) process id or similar values, but |
… | |
… | |
401 | it could be anything that doesn't contain any ASCII CR or ASCII LF |
405 | it could be anything that doesn't contain any ASCII CR or ASCII LF |
402 | characters. |
406 | characters. |
403 | |
407 | |
404 | Example of the two lines of greeting: |
408 | Example of the two lines of greeting: |
405 | |
409 | |
406 | 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 |
410 | aemp;0;fec.4a7720fc;127.0.0.1:1235,[::1]:1235;hmac_md6_64_256;json,storable;provider=AE-0.0 |
407 | XntegV2Guvss0qNn7phCPnoU87xqxV+4Mqm/5y4iQm6a |
411 | p/I122ql7kJR8lumW3lXlXCeBnyDAvz8NQo3x5IFowE4 |
408 | |
412 | |
409 | =head2 TLS handshake |
413 | =head2 TLS handshake |
410 | |
414 | |
411 | If, after the handshake, both sides indicate interest in TLS, then the |
415 | If, after the handshake, both sides indicate interest in TLS, then the |
412 | connection I<must> use TLS, or fail. |
416 | connection I<must> use TLS, or fail. |
… | |
… | |
439 | This must be one of the framing protocols offered by the other side in the |
443 | This must be one of the framing protocols offered by the other side in the |
440 | greeting. Each side must accept the choice of the other side. |
444 | greeting. Each side must accept the choice of the other side. |
441 | |
445 | |
442 | =back |
446 | =back |
443 | |
447 | |
444 | Example (the actual reply matching the previous example): |
448 | Example: |
445 | |
449 | |
446 | hmac_md6_64_256;wIlLedBY956UCGSISG9mBZRDTG8xUi73/sVse2DSQp0;json |
450 | hmac_md6_64_256;wIlLedBY956UCGSISG9mBZRDTG8xUi73/sVse2DSQp0;json |
447 | |
451 | |
448 | =head2 DATA PHASE |
452 | =head2 DATA PHASE |
449 | |
453 | |