| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package DR::Tnt::Proto; | ||
| 6 | use base qw(Exporter); | ||
| 7 | our @EXPORT_OK = qw( | ||
| 8 | call_lua | ||
| 9 | response | ||
| 10 | insert | ||
| 11 | replace | ||
| 12 | del | ||
| 13 | update | ||
| 14 | select | ||
| 15 | auth | ||
| 16 | handshake | ||
| 17 | ping | ||
| 18 | ); | ||
| 19 | use Carp; | ||
| 20 | $Carp::Internal{ (__PACKAGE__) }++; | ||
| 21 | use DR::Tnt::Msgpack; | ||
| 22 | use Digest::SHA; | ||
| 23 | use Scalar::Util 'looks_like_number'; | ||
| 24 | use Digest::SHA 'sha1'; | ||
| 25 | use MIME::Base64; | ||
| 26 | use Data::Dumper; | ||
| 27 | |||
| 28 | sub parse_greeting { | ||
| 29 | my ($str) = @_; | ||
| 30 | croak "strlen is not 128 bytes" unless $str and 128 == length $str; | ||
| 31 | |||
| 32 | my $salt = eval { substr decode_base64(substr $str, 64, 44), 0, 20; } || undef; | ||
| 33 | my $grstr = substr $str, 0, 64; | ||
| 34 | |||
| 35 | my ($title, $v, $pt, $uid) = split /\s+/, $grstr, 5; | ||
| 36 | |||
| 37 | return { | ||
| 38 | salt => $salt, | ||
| 39 | gr => $grstr, | ||
| 40 | title => $title, | ||
| 41 | version => $v, | ||
| 42 | uuid => $uid, | ||
| 43 | proto => $pt, | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | my (%resolve, %tresolve); | ||
| 48 | |||
| 49 | my %iter = ( | ||
| 50 | EQ => 0, | ||
| 51 | REQ => 1, | ||
| 52 | ALL => 2, | ||
| 53 | LT => 3, | ||
| 54 | LE => 4, | ||
| 55 | GE => 5, | ||
| 56 | GT => 6, | ||
| 57 | BITS_ALL_SET => 7, | ||
| 58 | BITS_ANY_SET => 8, | ||
| 59 | BITS_ALL_NOT_SET => 9 | ||
| 60 | ); | ||
| 61 | |||
| 62 | my %riter = reverse %iter; | ||
| 63 | |||
| 64 | BEGIN { | ||
| 65 | my %types = ( | ||
| 66 | IPROTO_SELECT => 1, | ||
| 67 | IPROTO_INSERT => 2, | ||
| 68 | IPROTO_REPLACE => 3, | ||
| 69 | IPROTO_UPDATE => 4, | ||
| 70 | IPROTO_DELETE => 5, | ||
| 71 | IPROTO_CALL => 6, | ||
| 72 | IPROTO_AUTH => 7, | ||
| 73 | IPROTO_EVAL => 8, | ||
| 74 | IPROTO_PING => 64, | ||
| 75 | ); | ||
| 76 | my %attrs = ( | ||
| 77 | IPROTO_CODE => 0x00, | ||
| 78 | IPROTO_SYNC => 0x01, | ||
| 79 | IPROTO_SERVER_ID => 0x02, | ||
| 80 | IPROTO_LSN => 0x03, | ||
| 81 | IPROTO_TIMESTAMP => 0x04, | ||
| 82 | IPROTO_SCHEMA_ID => 0x05, | ||
| 83 | IPROTO_SPACE_ID => 0x10, | ||
| 84 | IPROTO_INDEX_ID => 0x11, | ||
| 85 | IPROTO_LIMIT => 0x12, | ||
| 86 | IPROTO_OFFSET => 0x13, | ||
| 87 | IPROTO_ITERATOR => 0x14, | ||
| 88 | IPROTO_KEY => 0x20, | ||
| 89 | IPROTO_TUPLE => 0x21, | ||
| 90 | IPROTO_FUNCTION_NAME => 0x22, | ||
| 91 | IPROTO_USER_NAME => 0x23, | ||
| 92 | IPROTO_EXPRESSION => 0x27, | ||
| 93 | IPROTO_DATA => 0x30, | ||
| 94 | IPROTO_ERROR => 0x31, | ||
| 95 | ); | ||
| 96 | |||
| 97 | use constant; | ||
| 98 | while (my ($n, $v) = each %types) { | ||
| 99 | constant->import($n => $v); | ||
| 100 | $n =~ s/^IPROTO_//; | ||
| 101 | $tresolve{$v} = $n; | ||
| 102 | } | ||
| 103 | while (my ($n, $v) = each %attrs) { | ||
| 104 | constant->import($n => $v); | ||
| 105 | $n =~ s/^IPROTO_//; | ||
| 106 | $resolve{$v} = $n; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub raw_response($$) { | ||
| 112 | my ($response, $utf8) = @_; | ||
| 113 | |||
| 114 | my $len; | ||
| 115 | { | ||
| 116 | return unless defined $response; | ||
| 117 | my $lenheader = length $response > 10 ? | ||
| 118 | substr $response, 0, 10 : $response; | ||
| 119 | return unless my $lenlen = DR::Tnt::Msgpack::msgunpack_check $lenheader; | ||
| 120 | |||
| 121 | $len = DR::Tnt::Msgpack::msgunpack $lenheader; | ||
| 122 | croak 'Unexpected msgpack object ' . ref($len) if ref $len; | ||
| 123 | $len += $lenlen; | ||
| 124 | } | ||
| 125 | |||
| 126 | return if length $response < $len; | ||
| 127 | |||
| 128 | my @r; | ||
| 129 | my $off = 0; | ||
| 130 | |||
| 131 | for (1 .. 3) { | ||
| 132 | my $sp = $off ? substr $response, $off : $response; | ||
| 133 | my $len_item = DR::Tnt::Msgpack::msgunpack_check $sp; | ||
| 134 | croak sprintf('Broken %s section of response', $_) | ||
| 135 | unless $len_item and $len_item + $off <= length $response; | ||
| 136 | if ($utf8) { | ||
| 137 | push @r => DR::Tnt::Msgpack::msgunpack_utf8 $sp; | ||
| 138 | } else { | ||
| 139 | push @r => DR::Tnt::Msgpack::msgunpack $sp; | ||
| 140 | } | ||
| 141 | $off += $len_item; | ||
| 142 | |||
| 143 | if ($_ eq 2 and $off == length $response) { | ||
| 144 | push @r => {}; | ||
| 145 | last; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | croak 'Broken response header' unless 'HASH' eq ref $r[1]; | ||
| 150 | croak 'Broken response body' unless 'HASH' eq ref $r[2]; | ||
| 151 | |||
| 152 | return [ $r[1], $r[2] ], substr $response, $off; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub response($;$) { | ||
| 156 | |||
| 157 | my ($buffer, $utf8) = @_; | ||
| 158 | my ($resp, $tail) = raw_response($buffer => $utf8); | ||
| 159 | return unless $resp; | ||
| 160 | my ($h, $b) = @$resp; | ||
| 161 | |||
| 162 | my $res = {}; | ||
| 163 | |||
| 164 | while(my ($k, $v) = each %$h) { | ||
| 165 | my $name = $resolve{$k}; | ||
| 166 | $name = $k unless defined $name; | ||
| 167 | $res->{$name} = $v; | ||
| 168 | } | ||
| 169 | while(my ($k, $v) = each %$b) { | ||
| 170 | my $name = $resolve{$k}; | ||
| 171 | $name = $k unless defined $name; | ||
| 172 | $res->{$name} = $v; | ||
| 173 | } | ||
| 174 | |||
| 175 | if (defined $res->{CODE}) { | ||
| 176 | my $n = $tresolve{ $res->{CODE} }; | ||
| 177 | $res->{CODE} = $n if defined $n; | ||
| 178 | } | ||
| 179 | |||
| 180 | if (defined $res->{ITERATOR}) { | ||
| 181 | my $n = $riter{ $res->{ITERATOR} }; | ||
| 182 | $res->{ITERATOR} = $n if defined $n; | ||
| 183 | } | ||
| 184 | |||
| 185 | return $res, $tail; | ||
| 186 | |||
| 187 | } | ||
| 188 | |||
| 189 | sub request($$) { | ||
| 190 | my ($header, $body) = @_; | ||
| 191 | my $pkt = msgpack($header) . msgpack($body); | ||
| 192 | |||
| 193 | return join '', | ||
| 194 | msgpack(length $pkt), | ||
| 195 | $pkt; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _mk_header($$$) { | ||
| 199 | my ($code, $sync, $schema_id) = @_; | ||
| 200 | |||
| 201 | |||
| 202 | return { | ||
| 203 | IPROTO_SYNC, $sync, | ||
| 204 | IPROTO_CODE, $code, | ||
| 205 | } unless defined $schema_id; | ||
| 206 | |||
| 207 | return { | ||
| 208 | IPROTO_SYNC, $sync, | ||
| 209 | IPROTO_CODE, $code, | ||
| 210 | IPROTO_SCHEMA_ID, $schema_id | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub _call_lua($$$$) { | ||
| 215 | my ($sync, $schema_id, $proc, $tuple) = @_; | ||
| 216 | request | ||
| 217 | _mk_header(IPROTO_CALL, $sync, $schema_id), | ||
| 218 | { | ||
| 219 | IPROTO_FUNCTION_NAME, $proc, | ||
| 220 | IPROTO_TUPLE, $tuple, | ||
| 221 | } | ||
| 222 | ; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub call_lua($$$@) { | ||
| 226 | my ($sync, $schema_id, $proc, @args) = @_; | ||
| 227 | return _call_lua($sync, $schema_id, $proc, \@args); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | sub eval_lua($$$@) { | ||
| 232 | my ($sync, $schema_id, $lua, @args) = @_; | ||
| 233 | request | ||
| 234 | _mk_header(IPROTO_EVAL, $sync, $schema_id), | ||
| 235 | { | ||
| 236 | IPROTO_EXPRESSION, $lua, | ||
| 237 | IPROTO_TUPLE, \@args, | ||
| 238 | } | ||
| 239 | ; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub insert($$$$) { | ||
| 243 | my ($sync, $schema_id, $space, $tuple) = @_; | ||
| 244 | |||
| 245 | $tuple = [ $tuple ] unless ref $tuple; | ||
| 246 | croak "Cant convert HashRef to tuple" if 'HASH' eq ref $tuple; | ||
| 247 | |||
| 248 | if (looks_like_number $space) { | ||
| 249 | return request | ||
| 250 | _mk_header(IPROTO_INSERT, $sync, $schema_id), | ||
| 251 | { | ||
| 252 | IPROTO_SPACE_ID, $space, | ||
| 253 | IPROTO_TUPLE, $tuple, | ||
| 254 | } | ||
| 255 | ; | ||
| 256 | } | ||
| 257 | |||
| 258 | # HACK | ||
| 259 | _call_lua($sync, $schema_id, "box.space.$space:insert", $tuple); | ||
| 260 | } | ||
| 261 | |||
| 262 | sub replace($$$$) { | ||
| 263 | my ($sync, $schema_id, $space, $tuple) = @_; | ||
| 264 | |||
| 265 | $tuple = [ $tuple ] unless ref $tuple; | ||
| 266 | croak "Cant convert HashRef to tuple" if 'HASH' eq ref $tuple; | ||
| 267 | |||
| 268 | if (looks_like_number $space) { | ||
| 269 | return request | ||
| 270 | _mk_header(IPROTO_REPLACE, $sync, $schema_id), | ||
| 271 | { | ||
| 272 | IPROTO_SPACE_ID, $space, | ||
| 273 | IPROTO_TUPLE, $tuple, | ||
| 274 | } | ||
| 275 | ; | ||
| 276 | } | ||
| 277 | # HACK | ||
| 278 | _call_lua($sync, $schema_id, "box.space.$space:replace", $tuple); | ||
| 279 | } | ||
| 280 | sub del($$$$) { | ||
| 281 | my ($sync, $schema_id, $space, $key) = @_; | ||
| 282 | |||
| 283 | $key = [ $key ] unless ref $key; | ||
| 284 | croak "Cant convert HashRef to key" if 'HASH' eq ref $key; | ||
| 285 | |||
| 286 | if (looks_like_number $space) { | ||
| 287 | return request | ||
| 288 | _mk_header(IPROTO_DELETE, $sync, $schema_id), | ||
| 289 | { | ||
| 290 | IPROTO_SPACE_ID, $space, | ||
| 291 | IPROTO_KEY, $key, | ||
| 292 | } | ||
| 293 | ; | ||
| 294 | } | ||
| 295 | # HACK | ||
| 296 | _call_lua($sync, $schema_id, "box.space.$space:delete", $key); | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | sub update($$$$$) { | ||
| 301 | my ($sync, $schema_id, $space, $key, $ops) = @_; | ||
| 302 | croak 'Oplist must be Arrayref' unless 'ARRAY' eq ref $ops; | ||
| 303 | $key = [ $key ] unless ref $key; | ||
| 304 | croak "Cant convert HashRef to key" if 'HASH' eq ref $key; | ||
| 305 | |||
| 306 | if (looks_like_number $space) { | ||
| 307 | return request | ||
| 308 | _mk_header(IPROTO_UPDATE, $sync, $schema_id), | ||
| 309 | { | ||
| 310 | IPROTO_SPACE_ID, $space, | ||
| 311 | IPROTO_KEY, $key, | ||
| 312 | IPROTO_TUPLE, $ops, | ||
| 313 | } | ||
| 314 | ; | ||
| 315 | } | ||
| 316 | # HACK | ||
| 317 | _call_lua($sync, $schema_id, "box.space.$space:update", [ $key, $ops ]); | ||
| 318 | } | ||
| 319 | |||
| 320 | sub select($$$$$;$$$) { | ||
| 321 | my ($sync, $schema_id, $space, $index, $key, $limit, $offset, $iterator) = @_; | ||
| 322 | $iterator = 'EQ' unless defined $iterator; | ||
| 323 | $offset ||= 0; | ||
| 324 | $limit = 0xFFFF_FFFF unless defined $limit; | ||
| 325 | $key = [ $key ] unless ref $key; | ||
| 326 | croak "Cant convert HashRef to key" if 'HASH' eq ref $key; | ||
| 327 | |||
| 328 | unless(looks_like_number $iterator) { | ||
| 329 | my $i = $iter{$iterator}; | ||
| 330 | croak "Wrong iterator type: $iterator" unless defined $i; | ||
| 331 | $iterator = $i; | ||
| 332 | } | ||
| 333 | |||
| 334 | if (looks_like_number $space and looks_like_number $index) { | ||
| 335 | return request | ||
| 336 | _mk_header(IPROTO_SELECT, $sync, $schema_id), | ||
| 337 | { | ||
| 338 | IPROTO_KEY, $key, | ||
| 339 | IPROTO_SPACE_ID, $space, | ||
| 340 | IPROTO_OFFSET, $offset, | ||
| 341 | IPROTO_INDEX_ID, $index, | ||
| 342 | IPROTO_LIMIT, $limit, | ||
| 343 | IPROTO_ITERATOR, $iterator, | ||
| 344 | } | ||
| 345 | ; | ||
| 346 | } | ||
| 347 | |||
| 348 | # HACK | ||
| 349 | _call_lua($sync, $schema_id, "box.space.$space.index.$index:select", [ | ||
| 350 | $key, | ||
| 351 | { | ||
| 352 | offset => $offset, | ||
| 353 | limit => $limit, | ||
| 354 | iterator => $iterator | ||
| 355 | } | ||
| 356 | ] | ||
| 357 | ); | ||
| 358 | } | ||
| 359 | |||
| 360 | sub ping($$) { | ||
| 361 | my ($sync, $schema_id) = @_; | ||
| 362 | request | ||
| 363 | _mk_header(IPROTO_PING, $sync, $schema_id), | ||
| 364 | { | ||
| 365 | } | ||
| 366 | ; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | sub strxor($$) { | ||
| 371 | my ($x, $y) = @_; | ||
| 372 | |||
| 373 | my @x = unpack 'C*', $x; | ||
| 374 | my @y = unpack 'C*', $y; | ||
| 375 | $x[$_] ^= $y[$_] for 0 .. $#x; | ||
| 376 | return pack 'C*', @x; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub auth($$$$$) { | ||
| 380 | my ($sync, $schema_id, $user, $password, $salt) = @_; | ||
| 381 | |||
| 382 | my $hpasswd = sha1 $password; | ||
| 383 | my $hhpasswd = sha1 $hpasswd; | ||
| 384 | my $scramble = sha1 $salt . $hhpasswd; | ||
| 385 | |||
| 386 | my $hash = strxor $hpasswd, $scramble; | ||
| 387 | request | ||
| 388 | _mk_header(IPROTO_AUTH, $sync, $schema_id), | ||
| 389 | { | ||
| 390 | IPROTO_USER_NAME, $user, | ||
| 391 | IPROTO_TUPLE, [ 'chap-sha1', $hash ], | ||
| 392 | } | ||
| 393 | ; | ||
| 394 | } | ||
| 395 | |||
| 396 | 1; |