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