| 1 | package # ignore CPAN .. | ||
| 2 | POE::Component::Server::MySQL::Client; | ||
| 3 | use Moose; | ||
| 4 | use MooseX::MethodAttributes (); | ||
| 5 | |||
| 6 | my $VERSION = '0.01_01'; | ||
| 7 | |||
| 8 | use POE; | ||
| 9 | use POE::Kernel; | ||
| 10 | use Module::Find; | ||
| 11 | use Data::Dumper; | ||
| 12 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 13 | use DBI; | ||
| 14 | |||
| 15 | has 'database' => (is => 'rw', isa => 'Any'); | ||
| 16 | has 'username' => (is => 'rw', isa => 'Any'); | ||
| 17 | has 'password' => (is => 'rw', isa => 'Any'); | ||
| 18 | |||
| 19 | has 'wheel' => (is => 'rw', isa => 'Any'); | ||
| 20 | has 'session_id' => (is => 'rw', isa => 'Any'); | ||
| 21 | has 'session' => (is => 'rw', isa => 'Any'); | ||
| 22 | has 'banner' => (is => 'rw', isa => 'Any'); | ||
| 23 | |||
| 24 | has 'salt' => (is => 'rw', isa => 'Any'); | ||
| 25 | has 'charset' => (is => 'rw', isa => 'Any'); | ||
| 26 | has 'tid' => (is => 'rw', isa => 'Any'); | ||
| 27 | has 'packet_count' => (is => 'rw', isa => 'Any'); | ||
| 28 | |||
| 29 | has 'scramble' => (is => 'rw', isa => 'Any'); | ||
| 30 | has 'authenticated' => (is => 'rw', isa => 'Any'); | ||
| 31 | |||
| 32 | has 'server_class' => (is => 'rw', isa => 'Any'); | ||
| 33 | |||
| 34 | has 'local_dsn' => (is => 'rw', isa => 'Str'); | ||
| 35 | has 'local_user' => (is => 'rw', isa => 'Str'); | ||
| 36 | has 'local_password' => (is => 'rw', isa => 'Str'); | ||
| 37 | |||
| 38 | has 'local_dbh' => (is => 'rw', isa => 'Any'); | ||
| 39 | |||
| 40 | has 'dispatchers' => (is => 'rw', isa => 'Any'); | ||
| 41 | has 'default' => (is => 'rw', isa => 'Any'); | ||
| 42 | |||
| 43 | has 'type_info' => (is => 'rw', isa => 'Any'); | ||
| 44 | |||
| 45 | has '_buffer' => (is => 'rw', isa => 'Any'); | ||
| 46 | has 'buffer' => (is => 'rw', isa => 'Any'); | ||
| 47 | |||
| 48 | use constant FIELD_CATALOG => 0; | ||
| 49 | use constant FIELD_DB => 1; | ||
| 50 | use constant FIELD_TABLE => 2; | ||
| 51 | use constant FIELD_ORG_TABLE => 3; | ||
| 52 | use constant FIELD_NAME => 4; | ||
| 53 | use constant FIELD_ORG_NAME => 5; | ||
| 54 | use constant FIELD_LENGTH => 6; | ||
| 55 | use constant FIELD_TYPE => 7; | ||
| 56 | use constant FIELD_FLAGS => 8; | ||
| 57 | use constant FIELD_DECIMALS => 9; | ||
| 58 | use constant FIELD_DEFAULT => 10; | ||
| 59 | |||
| 60 | use constant CLIENT_LONG_PASSWORD => 1; | ||
| 61 | use constant CLIENT_FOUND_ROWS => 2; | ||
| 62 | use constant CLIENT_LONG_FLAG => 4; | ||
| 63 | use constant CLIENT_CONNECT_WITH_DB => 8; | ||
| 64 | use constant CLIENT_NO_SCHEMA => 16; | ||
| 65 | use constant CLIENT_COMPRESS => 32; | ||
| 66 | use constant CLIENT_ODBC => 64; | ||
| 67 | use constant CLIENT_LOCAL_FILES => 128; | ||
| 68 | use constant CLIENT_IGNORE_SPACE => 256; | ||
| 69 | use constant CLIENT_PROTOCOL_41 => 512; | ||
| 70 | use constant CLIENT_INTERACTIVE => 1024; | ||
| 71 | use constant CLIENT_SSL => 2048; | ||
| 72 | use constant CLIENT_IGNORE_SIGPIPE => 4096; | ||
| 73 | use constant CLIENT_TRANSACTIONS => 8192; | ||
| 74 | use constant CLIENT_RESERVED => 16384; | ||
| 75 | use constant CLIENT_SECURE_CONNECTION => 32768; | ||
| 76 | use constant CLIENT_MULTI_STATEMENTS => 1 << 16; | ||
| 77 | use constant CLIENT_MULTI_RESULTS => 1 << 17; | ||
| 78 | use constant CLIENT_SSL_VERIFY_SERVER_CERT => 1 << 30; | ||
| 79 | use constant CLIENT_REMEMBER_OPTIONS => 1 << 31; | ||
| 80 | |||
| 81 | use constant SERVER_STATUS_IN_TRANS => 1; | ||
| 82 | use constant SERVER_STATUS_AUTOCOMMIT => 2; | ||
| 83 | use constant SERVER_MORE_RESULTS_EXISTS => 8; | ||
| 84 | use constant SERVER_QUERY_NO_GOOD_INDEX_USED => 16; | ||
| 85 | use constant SERVER_QUERY_NO_INDEX_USED => 32; | ||
| 86 | use constant SERVER_STATUS_CURSOR_EXISTS => 64; | ||
| 87 | use constant SERVER_STATUS_LAST_ROW_SENT => 128; | ||
| 88 | use constant SERVER_STATUS_DB_DROPPED => 256; | ||
| 89 | use constant SERVER_STATUS_NO_BACKSLASH_ESCAPES => 512; | ||
| 90 | |||
| 91 | use constant COM_SLEEP => 0; | ||
| 92 | use constant COM_QUIT => 1; | ||
| 93 | use constant COM_INIT_DB => 2; | ||
| 94 | use constant COM_QUERY => 3; | ||
| 95 | use constant COM_FIELD_LIST => 4; | ||
| 96 | use constant COM_CREATE_DB => 5; | ||
| 97 | use constant COM_DROP_DB => 6; | ||
| 98 | use constant COM_REFRESH => 7; | ||
| 99 | use constant COM_SHUTDOWN => 8; | ||
| 100 | use constant COM_STATISTICS => 9; | ||
| 101 | use constant COM_PROCESS_INFO => 10; | ||
| 102 | use constant COM_CONNECT => 11; | ||
| 103 | use constant COM_PROCESS_KILL => 12; | ||
| 104 | use constant COM_DEBUG => 13; | ||
| 105 | use constant COM_PING => 14; | ||
| 106 | use constant COM_TIME => 15; | ||
| 107 | use constant COM_DELAYED_INSERT => 16; | ||
| 108 | use constant COM_CHANGE_USER => 17; | ||
| 109 | use constant COM_BINLOG_DUMP => 18; | ||
| 110 | use constant COM_TABLE_DUMP => 19; | ||
| 111 | use constant COM_CONNECT_OUT => 20; | ||
| 112 | use constant COM_REGISTER_SLAVE => 21; | ||
| 113 | use constant COM_STMT_PREPARE => 22; | ||
| 114 | use constant COM_STMT_EXECUTE => 23; | ||
| 115 | use constant COM_STMT_SEND_LONG_DATA => 24; | ||
| 116 | use constant COM_STMT_CLOSE => 25; | ||
| 117 | use constant COM_STMT_RESET => 26; | ||
| 118 | use constant COM_SET_OPTION => 27; | ||
| 119 | use constant COM_STMT_FETCH => 28; | ||
| 120 | use constant COM_END => 29; | ||
| 121 | |||
| 122 | use constant MYSQL_TYPE_DECIMAL => 0; | ||
| 123 | use constant MYSQL_TYPE_TINY => 1; | ||
| 124 | use constant MYSQL_TYPE_SHORT => 2; | ||
| 125 | use constant MYSQL_TYPE_LONG => 3; | ||
| 126 | use constant MYSQL_TYPE_FLOAT => 4; | ||
| 127 | use constant MYSQL_TYPE_DOUBLE => 5; | ||
| 128 | use constant MYSQL_TYPE_NULL => 6; | ||
| 129 | use constant MYSQL_TYPE_TIMESTAMP => 7; | ||
| 130 | use constant MYSQL_TYPE_LONGLONG => 8; | ||
| 131 | use constant MYSQL_TYPE_INT24 => 9; | ||
| 132 | use constant MYSQL_TYPE_DATE => 10; | ||
| 133 | use constant MYSQL_TYPE_TIME => 11; | ||
| 134 | use constant MYSQL_TYPE_DATETIME => 12; | ||
| 135 | use constant MYSQL_TYPE_YEAR => 13; | ||
| 136 | use constant MYSQL_TYPE_NEWDATE => 14; | ||
| 137 | use constant MYSQL_TYPE_VARCHAR => 15; | ||
| 138 | use constant MYSQL_TYPE_BIT => 16; | ||
| 139 | use constant MYSQL_TYPE_NEWDECIMAL => 246; | ||
| 140 | use constant MYSQL_TYPE_ENUM => 247; | ||
| 141 | use constant MYSQL_TYPE_SET => 248; | ||
| 142 | use constant MYSQL_TYPE_TINY_BLOB => 249; | ||
| 143 | use constant MYSQL_TYPE_MEDIUM_BLOB => 250; | ||
| 144 | use constant MYSQL_TYPE_LONG_BLOB => 251; | ||
| 145 | use constant MYSQL_TYPE_BLOB => 252; | ||
| 146 | use constant MYSQL_TYPE_VAR_STRING => 253; | ||
| 147 | use constant MYSQL_TYPE_STRING => 254; | ||
| 148 | use constant MYSQL_TYPE_GEOMETRY => 255; | ||
| 149 | |||
| 150 | use constant NOT_NULL_FLAG => 1; | ||
| 151 | use constant PRI_KEY_FLAG => 2; | ||
| 152 | use constant UNIQUE_KEY_FLAG => 4; | ||
| 153 | use constant MULTIPLE_KEY_FLAG => 8; | ||
| 154 | use constant BLOB_FLAG => 16; | ||
| 155 | use constant UNSIGNED_FLAG => 32; | ||
| 156 | use constant ZEROFILL_FLAG => 64; | ||
| 157 | use constant BINARY_FLAG => 128; | ||
| 158 | use constant ENUM_FLAG => 256; | ||
| 159 | use constant AUTO_INCREMENT_FLAG => 512; | ||
| 160 | use constant TIMESTAMP_FLAG => 1024; | ||
| 161 | use constant SET_FLAG => 2048; | ||
| 162 | use constant NO_DEFAULT_VALUE_FLAG => 4096; | ||
| 163 | use constant NUM_FLAG => 32768; | ||
| 164 | |||
| 165 | |||
| 166 | sub BUILD { | ||
| 167 | my ($self) = @_; | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | sub _authenticate { | ||
| 174 | my ($self, $data) = @_; | ||
| 175 | |||
| 176 | my $database; | ||
| 177 | |||
| 178 | my $ptr = 0; | ||
| 179 | my $header_flags = substr($data, $ptr, 4); | ||
| 180 | $ptr = $ptr + 4; | ||
| 181 | |||
| 182 | eval { | ||
| 183 | my $client_flags = substr($data, $ptr, 4); | ||
| 184 | $ptr = $ptr + 4; | ||
| 185 | |||
| 186 | my $max_packet_size = substr($data, $ptr, 4); | ||
| 187 | $ptr = $ptr + 4; | ||
| 188 | |||
| 189 | my $charset_number = substr($data, $ptr, 1); | ||
| 190 | $self->charset(ord($charset_number)); | ||
| 191 | $ptr++; | ||
| 192 | |||
| 193 | my $filler1 = substr($data, $ptr, 23); | ||
| 194 | $ptr = $ptr + 23; | ||
| 195 | |||
| 196 | my $username_end = index($data, "\0", $ptr); | ||
| 197 | my $username = substr($data, $ptr, $username_end - $ptr); | ||
| 198 | $ptr = $username_end + 1; | ||
| 199 | |||
| 200 | my $scramble_buff; | ||
| 201 | |||
| 202 | my $scramble_length = ord(substr($data, $ptr, 1)); | ||
| 203 | $ptr++; | ||
| 204 | |||
| 205 | if ($scramble_length > 0) { | ||
| 206 | $self->scramble(substr($data, $ptr, $scramble_length)); | ||
| 207 | $ptr = $ptr + $scramble_length; | ||
| 208 | } | ||
| 209 | |||
| 210 | my $database_end = index( $data, "\0", $ptr); | ||
| 211 | if ($database_end != -1 ) { | ||
| 212 | $database = substr($data, $ptr, $database_end - $ptr); | ||
| 213 | } | ||
| 214 | |||
| 215 | $self->database($database); | ||
| 216 | $self->username($username); | ||
| 217 | |||
| 218 | }; | ||
| 219 | |||
| 220 | if ($@) { | ||
| 221 | print $@; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | if ($database) { | ||
| 226 | $self->database($database); | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | $self->database('mysql'); | ||
| 230 | } | ||
| 231 | |||
| 232 | $self->authenticated(1); | ||
| 233 | $self->send_ok; | ||
| 234 | |||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | sub send_error { | ||
| 239 | my ($self, $message, $errno, $sqlstate) = @_; | ||
| 240 | |||
| 241 | $message = 'Unknown MySQL error' if not defined $message; | ||
| 242 | $errno = 2000 if not defined $errno; | ||
| 243 | $sqlstate = 'HY000' if not defined $sqlstate; | ||
| 244 | |||
| 245 | my $payload = chr(0xff); | ||
| 246 | $payload .= pack('v', $errno); | ||
| 247 | $payload .= '#'; | ||
| 248 | $payload .= $sqlstate; | ||
| 249 | $payload .= $message."\0"; | ||
| 250 | |||
| 251 | $self->_write_to_client( $payload, 1); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub client_input { | ||
| 255 | my ( $kernel, $session, $heap, $self ) = @_[ KERNEL, SESSION, HEAP, OBJECT]; | ||
| 256 | my $data = $_[ARG0]; | ||
| 257 | |||
| 258 | # print "client_input $data on $$ \n"; | ||
| 259 | |||
| 260 | unless ($self->local_dbh) { | ||
| 261 | |||
| 262 | unless ($self->local_dsn) { | ||
| 263 | $self->send_error('No local_dsn'); | ||
| 264 | return; | ||
| 265 | } | ||
| 266 | |||
| 267 | $self->local_dbh( | ||
| 268 | DBI->connect($self->local_dsn, $self->local_user, $self->local_password) | ||
| 269 | ); | ||
| 270 | |||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | if (length($data) > 1) { | ||
| 275 | $self->packet_count($self->packet_count + 1); | ||
| 276 | } | ||
| 277 | else { | ||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | unless ( $self->authenticated) { | ||
| 282 | $self->_authenticate($data); | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | my $header_flags = substr($data, 0, 4); | ||
| 286 | my $command = unpack('C', substr($data, 3, 1)); | ||
| 287 | |||
| 288 | $data = substr($data, 4); | ||
| 289 | |||
| 290 | if ($command == COM_INIT_DB) { | ||
| 291 | $data = 'USE '.$data; | ||
| 292 | } | ||
| 293 | |||
| 294 | unless (length($data)) { | ||
| 295 | $self->send_ok; | ||
| 296 | return; | ||
| 297 | } | ||
| 298 | |||
| 299 | my $event; | ||
| 300 | my @placeholders; | ||
| 301 | |||
| 302 | foreach my $dispatcher (@{$self->dispatchers}) { | ||
| 303 | if (ref($dispatcher->{regexp}) eq 'Regexp') { | ||
| 304 | if (@placeholders = $data =~ $dispatcher->{regexp}) { | ||
| 305 | $event = $dispatcher->{method}; | ||
| 306 | last; | ||
| 307 | } | ||
| 308 | } | ||
| 309 | elsif (exists($dispatcher->{match})) { | ||
| 310 | if ($data eq $dispatcher->{match}) { | ||
| 311 | $event = $dispatcher->{method}; | ||
| 312 | last; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | unless ($event) { | ||
| 319 | $event = $self->default->{method} if $self->default; | ||
| 320 | } | ||
| 321 | |||
| 322 | unless ($event) { | ||
| 323 | $event = 'relay'; | ||
| 324 | } | ||
| 325 | |||
| 326 | # print '$event = '.$event." \n"; | ||
| 327 | |||
| 328 | if ($event) { | ||
| 329 | $self->$event($data, @placeholders); | ||
| 330 | } | ||
| 331 | else { | ||
| 332 | $self->send_error; | ||
| 333 | } | ||
| 334 | |||
| 335 | # POE::Kernel->post( | ||
| 336 | # $self->session_id, | ||
| 337 | # $event, | ||
| 338 | # $data | ||
| 339 | # ); | ||
| 340 | |||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | sub relay { | ||
| 346 | my ($self, $query) = @_; | ||
| 347 | |||
| 348 | $self->local_dbh->{'mysql_use_result'} = 1; | ||
| 349 | |||
| 350 | if ($query =~ qr{use `(.*)`}io) { | ||
| 351 | $self->database($1); | ||
| 352 | } | ||
| 353 | |||
| 354 | my $err; | ||
| 355 | |||
| 356 | my $sth = $self->local_dbh->prepare($query); | ||
| 357 | if ($DBI::err) { | ||
| 358 | $err = $DBI::errstr; | ||
| 359 | } | ||
| 360 | |||
| 361 | my $affected_rows = $sth->execute; | ||
| 362 | if ($DBI::err) { | ||
| 363 | $err = $DBI::errstr; | ||
| 364 | } | ||
| 365 | |||
| 366 | if (defined $err) { | ||
| 367 | $self->send_error($err); | ||
| 368 | } | ||
| 369 | elsif ((not defined $sth->{NUM_OF_FIELDS}) || ($sth->{NUM_OF_FIELDS} == 0)) { | ||
| 370 | $self->send_ok($self->local_dbh->{'mysql_info'}, $affected_rows, $sth->{mysql_insertid}, $sth->{'mysql_warning_count'}); | ||
| 371 | } | ||
| 372 | else { | ||
| 373 | |||
| 374 | unless ($self->type_info) { | ||
| 375 | |||
| 376 | my $infos = (); | ||
| 377 | |||
| 378 | my @type_info = @{$self->local_dbh->type_info_all()}; | ||
| 379 | |||
| 380 | my $sql_col = $type_info[0]->{DATA_TYPE}; | ||
| 381 | my $mysql_col = $type_info[0]->{mysql_native_type}; | ||
| 382 | |||
| 383 | foreach my $type (@type_info[1..$#type_info]) { | ||
| 384 | my $sql_value = $type->[$sql_col]; | ||
| 385 | my $mysql_value = $type->[$mysql_col]; | ||
| 386 | |||
| 387 | $infos->{$sql_value} = $mysql_value if exists $infos->{$sql_value}; | ||
| 388 | } | ||
| 389 | |||
| 390 | $self->type_info($infos); | ||
| 391 | |||
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | my @definitions = map { | ||
| 396 | my $flags = 0; | ||
| 397 | $flags = $flags | NOT_NULL_FLAG if not $sth->{NULLABLE}->[$_]; | ||
| 398 | $flags = $flags | BLOB_FLAG if $sth->{mysql_is_blob}->[$_]; | ||
| 399 | $flags = $flags | UNIQUE_KEY_FLAG if $sth->{mysql_is_key}->[$_]; | ||
| 400 | $flags = $flags | PRI_KEY_FLAG if $sth->{mysql_is_pri_key}->[$_]; | ||
| 401 | $flags = $flags | AUTO_INCREMENT_FLAG if $sth->{mysql_is_auto_increment}->[$_]; | ||
| 402 | |||
| 403 | my $type = $self->type_info->{$sth->{TYPE}->[$_]}; | ||
| 404 | |||
| 405 | my $def_length = $sth->{mysql_length}->[$_]; | ||
| 406 | |||
| 407 | $self->definition({ | ||
| 408 | name => $sth->{NAME}->[$_], | ||
| 409 | type => $type, | ||
| 410 | length => $def_length, | ||
| 411 | flags => $flags | ||
| 412 | }); | ||
| 413 | } (0..$sth->{NUM_OF_FIELDS}-1); | ||
| 414 | |||
| 415 | |||
| 416 | my @result = @{ $sth->fetchall_arrayref() }; | ||
| 417 | $affected_rows = scalar(@result); | ||
| 418 | |||
| 419 | $self->_send_header_packet($sth->{NUM_OF_FIELDS}, $affected_rows); | ||
| 420 | $self->_send_definitions(\@definitions); | ||
| 421 | $self->_send_eof; | ||
| 422 | |||
| 423 | my @rows; | ||
| 424 | my $i = 1; | ||
| 425 | foreach my $row_ref (@result) { | ||
| 426 | my @row = @{$row_ref}; | ||
| 427 | |||
| 428 | $rows[$i] = (); | ||
| 429 | @{ $rows[$i] } = @row; | ||
| 430 | |||
| 431 | if ($i == 10000) { | ||
| 432 | $self->_send_rows(\@rows, { | ||
| 433 | chunked => 1, | ||
| 434 | }); | ||
| 435 | @rows = undef; | ||
| 436 | $i = 1; | ||
| 437 | } | ||
| 438 | else { | ||
| 439 | $i++; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | $self->_send_rows(\@rows); | ||
| 444 | $self->_send_eof(undef,undef,1); | ||
| 445 | |||
| 446 | } | ||
| 447 | |||
| 448 | $self->local_dbh->{'mysql_use_result'} = 0; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub _send_definitions { | ||
| 452 | my ($self, $definitions, $skip_envelope) = @_; | ||
| 453 | |||
| 454 | my $last_send_result; | ||
| 455 | |||
| 456 | foreach my $definition (@{$definitions}) { | ||
| 457 | $definition = $self->definition({ name => $definition }) | ||
| 458 | unless ref($definition) eq 'POE::Component::Server::MySQL::Definition'; | ||
| 459 | $self->_send_definition($definition); | ||
| 460 | } | ||
| 461 | |||
| 462 | } | ||
| 463 | |||
| 464 | sub _send_definition { | ||
| 465 | my ($self, $definition) = @_; | ||
| 466 | |||
| 467 | my ( | ||
| 468 | $field_catalog, $field_db, $field_table, | ||
| 469 | $field_org_table, $field_name, $field_org_name, | ||
| 470 | $field_length, $field_type, $field_flags, | ||
| 471 | $field_decimals, $field_default | ||
| 472 | ) = ( | ||
| 473 | $definition->[FIELD_CATALOG], $definition->[FIELD_DB], | ||
| 474 | $definition->[FIELD_TABLE], $definition->[FIELD_ORG_TABLE], | ||
| 475 | $definition->[FIELD_NAME], $definition->[FIELD_ORG_NAME], | ||
| 476 | $definition->[FIELD_LENGTH], $definition->[FIELD_TYPE], | ||
| 477 | $definition->[FIELD_FLAGS], $definition->[FIELD_DECIMALS], | ||
| 478 | $definition->[FIELD_DEFAULT] | ||
| 479 | ); | ||
| 480 | |||
| 481 | my $payload = join('', map { $self->_length_coded_string($_) } ( | ||
| 482 | $field_catalog, | ||
| 483 | $field_db, | ||
| 484 | $field_table, | ||
| 485 | $field_org_table, | ||
| 486 | $field_name, | ||
| 487 | $field_org_name | ||
| 488 | )); | ||
| 489 | |||
| 490 | $payload .= chr(0x0c); | ||
| 491 | $payload .= pack('v', 11); | ||
| 492 | $payload .= pack('V', $field_length); | ||
| 493 | $payload .= chr($field_type); | ||
| 494 | $payload .= defined $field_flags ? pack('v', $field_flags) : pack('v', 0); | ||
| 495 | $payload .= defined $field_decimals ? chr($field_decimals) : pack('v', 0); | ||
| 496 | $payload .= pack('v', 0); | ||
| 497 | |||
| 498 | $self->_write_to_client($payload); | ||
| 499 | } | ||
| 500 | |||
| 501 | sub _send_eof { | ||
| 502 | my ($self, $warning_count, $server_status, $reinit) = @_; | ||
| 503 | |||
| 504 | my $payload; | ||
| 505 | |||
| 506 | $server_status = SERVER_STATUS_AUTOCOMMIT | | ||
| 507 | SERVER_QUERY_NO_INDEX_USED if not defined $server_status; | ||
| 508 | |||
| 509 | $warning_count = 0 if !defined $warning_count; | ||
| 510 | |||
| 511 | $payload .= chr(0xfe); | ||
| 512 | $payload .= pack('v', $warning_count) if defined $warning_count; | ||
| 513 | $payload .= pack('v', $server_status) if defined $server_status; | ||
| 514 | |||
| 515 | $self->_write_to_client($payload, $reinit); | ||
| 516 | } | ||
| 517 | |||
| 518 | sub send_ok { | ||
| 519 | my ($self, $message, $affected_rows, $insert_id, $printing_count) = @_; | ||
| 520 | |||
| 521 | my $data; | ||
| 522 | |||
| 523 | $affected_rows = 0 if not defined $affected_rows; | ||
| 524 | $printing_count = 0 if not defined $printing_count; | ||
| 525 | |||
| 526 | $data .= "\0"; | ||
| 527 | $data .= $self->_length_coded_binary($affected_rows); | ||
| 528 | $data .= $self->_length_coded_binary($insert_id); | ||
| 529 | $data .= pack('v', SERVER_STATUS_AUTOCOMMIT); | ||
| 530 | $data .= pack('v', $printing_count); | ||
| 531 | $data .= $self->_length_coded_string($message); | ||
| 532 | |||
| 533 | $self->_write_to_client( $data, 1); | ||
| 534 | } | ||
| 535 | |||
| 536 | |||
| 537 | sub _add_header { | ||
| 538 | my ($self, $message, $reinit) = @_; | ||
| 539 | |||
| 540 | my $header; | ||
| 541 | $header .= substr(pack('V',length($message)),0,3); | ||
| 542 | $header .= chr($self->packet_count % 256); | ||
| 543 | |||
| 544 | if ($reinit) { | ||
| 545 | $self->packet_count(0); | ||
| 546 | } | ||
| 547 | else { | ||
| 548 | $self->packet_count($self->packet_count + 1); | ||
| 549 | } | ||
| 550 | |||
| 551 | return $header.$message; | ||
| 552 | } | ||
| 553 | |||
| 554 | sub _write_to_client { | ||
| 555 | my ($self, $message, $reinit) = @_; | ||
| 556 | |||
| 557 | $message = $self->_add_header($message, $reinit); | ||
| 558 | $self->wheel->put($message); | ||
| 559 | |||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | |||
| 564 | |||
| 565 | sub _length_coded_string { | ||
| 566 | my ($self, $string) = @_; | ||
| 567 | return chr(0) if (not defined $string or $string eq ''); | ||
| 568 | return $self->_length_coded_binary(length($string)).$string; | ||
| 569 | } | ||
| 570 | |||
| 571 | sub _length_coded_binary { | ||
| 572 | my ($self, $number) = @_; | ||
| 573 | |||
| 574 | if (not defined $number) { | ||
| 575 | return chr(251); | ||
| 576 | } | ||
| 577 | elsif ($number < 251) { | ||
| 578 | return chr($number); | ||
| 579 | } | ||
| 580 | elsif ($number < 0x10000) { | ||
| 581 | return chr(252).pack('v', $number); | ||
| 582 | } | ||
| 583 | elsif ($number < 0x1000000) { | ||
| 584 | return chr(253).substr(pack('V', $number), 0, 3); | ||
| 585 | } | ||
| 586 | else { | ||
| 587 | return chr(254).pack('V', $number >> 32).pack('V', $number & 0xffffffff); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | sub shutdown { | ||
| 592 | |||
| 593 | } | ||
| 594 | |||
| 595 | sub client_error { | ||
| 596 | my ($self) = shift; | ||
| 597 | my ($kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP]; | ||
| 598 | my ($operation, $errnum, $errstr, $wheel_id) = @_[ARG0..ARG3]; | ||
| 599 | |||
| 600 | } | ||
| 601 | |||
| 602 | sub client_connect { | ||
| 603 | my ( $self, $kernel, $session, $heap ) = @_[OBJECT, KERNEL, SESSION, HEAP]; | ||
| 604 | |||
| 605 | $self->banner("5.1.40-log\0"); | ||
| 606 | $self->salt(join('',map { chr(int(rand(255))) } (1..20))); | ||
| 607 | $self->charset(0x21); | ||
| 608 | $self->tid($$); | ||
| 609 | $self->packet_count(0); | ||
| 610 | |||
| 611 | #[root@daef-0004 mysqlsniffer]# clear ; ./mysqlsniffer --port 3306 --verbose --tcp-ctrl eth0 | ||
| 612 | #mysqlsniffer listening for MySQL on interface eth0 port 3306 | ||
| 613 | #129.195.12.105.2908 > server: SYN | ||
| 614 | #server > 129.195.12.105.2908: SYN ACK | ||
| 615 | #129.195.12.105.2908 > server: ACK | ||
| 616 | #server > 129.195.12.105.2908: ID 0 len 56 Handshake <proto 10 ver 5.1.40-log thd 514> (Caps: Long password, Found rows, Get all column flags, Connect w/DB, No schema, Compression, ODBC client, LOAD DATA LOCAL, ) | ||
| 617 | #129.195.12.105.2908 > server: ID 1 len 58 Handshake (new auth) <user root db (null) max pkt 16777216> (Caps: Long password, Get all column flags, LOAD DATA LOCAL, 4.1 protocol, Interactive, Transactions, 4.1 authentication, Multi-statements, Multi-results) | ||
| 618 | #server > 129.195.12.105.2908: ACK | ||
| 619 | #server > 129.195.12.105.2908: ID 2 len 7 OK <fields 0 affected rows 0 insert id 0 warnings 0> (Status: Auto-commit, ) | ||
| 620 | #129.195.12.105.2908 > server: ID 0 len 33 COM_QUERY: select @@version_comment limit 1 | ||
| 621 | #server > 129.195.12.105.2908: ID 1 len 1 1 Fields | ||
| 622 | # ID 2 len 39 Field: ..@@version_comment <type var string (253) size 48> | ||
| 623 | # ID 3 len 5 End <warnings 0> (Status: Auto-commit, ) | ||
| 624 | # ID 4 len 49 || build number (revision)=IB_3.4.2_r8940_9191(ice) || | ||
| 625 | # ID 5 len 5 End <warnings 0> (Status: Auto-commit, ) | ||
| 626 | #129.195.12.105.2908 > server: ACK | ||
| 627 | |||
| 628 | |||
| 629 | |||
| 630 | my $payload = chr(10); | ||
| 631 | $payload .= $self->banner; | ||
| 632 | $payload .= pack('V', $self->tid); | ||
| 633 | $payload .= substr($self->salt,0,8)."\0"; | ||
| 634 | $payload .= pack('v', CLIENT_LONG_PASSWORD | | ||
| 635 | CLIENT_CONNECT_WITH_DB | | ||
| 636 | CLIENT_LONG_FLAG | | ||
| 637 | CLIENT_PROTOCOL_41 | | ||
| 638 | CLIENT_FOUND_ROWS | | ||
| 639 | CLIENT_NO_SCHEMA | | ||
| 640 | CLIENT_MULTI_RESULTS | | ||
| 641 | CLIENT_ODBC | | ||
| 642 | CLIENT_LOCAL_FILES | | ||
| 643 | CLIENT_INTERACTIVE | | ||
| 644 | CLIENT_TRANSACTIONS | | ||
| 645 | CLIENT_SECURE_CONNECTION); | ||
| 646 | $payload .= $self->charset; | ||
| 647 | $payload .= pack('v', SERVER_STATUS_AUTOCOMMIT); | ||
| 648 | $payload .= "\0" x 13; | ||
| 649 | $payload .= substr($self->salt,8)."\0"; | ||
| 650 | |||
| 651 | $self->_write_to_client($payload); | ||
| 652 | } | ||
| 653 | |||
| 654 | sub client_disconnect { | ||
| 655 | my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP]; | ||
| 656 | |||
| 657 | print "handle_client_disconnect"."\n"; | ||
| 658 | } | ||
| 659 | |||
| 660 | sub _send_header_packet { | ||
| 661 | my ($self, $n_fields, $n_rows) = @_; | ||
| 662 | |||
| 663 | # print '$n_fields = '.$n_fields."\n"; | ||
| 664 | |||
| 665 | my $packet = $self->_length_coded_binary($n_fields); | ||
| 666 | # $packet .= $self->_length_coded_binary($n_rows); | ||
| 667 | |||
| 668 | $self->_write_to_client($packet); | ||
| 669 | } | ||
| 670 | |||
| 671 | sub _send_rows { | ||
| 672 | my ($self, $rows, $opt) = @_; | ||
| 673 | # $self->_send_eof if not defined $rows; | ||
| 674 | |||
| 675 | foreach my $row (@$rows) { | ||
| 676 | |||
| 677 | my $small_data; | ||
| 678 | if (ref($row) eq 'ARRAY') { | ||
| 679 | foreach (@$row) { | ||
| 680 | if (not defined $_) { | ||
| 681 | $small_data .= chr(251); | ||
| 682 | } | ||
| 683 | else { | ||
| 684 | $small_data .= $self->_length_coded_string($_); | ||
| 685 | } | ||
| 686 | } | ||
| 687 | } | ||
| 688 | elsif (ref($row) eq 'HASH') { | ||
| 689 | foreach (values %{ $row }) { | ||
| 690 | if (not defined $_) { | ||
| 691 | $small_data .= chr(251); | ||
| 692 | } | ||
| 693 | else { | ||
| 694 | $small_data .= $self->_length_coded_string($_); | ||
| 695 | } | ||
| 696 | } | ||
| 697 | } | ||
| 698 | |||
| 699 | if (defined $small_data) { | ||
| 700 | $self->_write_to_client($small_data); | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | # $self->_send_eof unless $opt->{chunked}; | ||
| 705 | } | ||
| 706 | |||
| 707 | sub send_results { | ||
| 708 | my ($self, $definitions, $data, $opt) = @_; | ||
| 709 | |||
| 710 | unless (ref($data) eq 'ARRAY') { | ||
| 711 | $self->send_error('Internal error .. F**K !'); | ||
| 712 | return; | ||
| 713 | } | ||
| 714 | |||
| 715 | $self->_send_header_packet(scalar(@{$data}), $opt); | ||
| 716 | $self->_send_definitions($definitions, $opt); | ||
| 717 | $self->_send_eof; | ||
| 718 | $self->_send_rows($data, $opt); | ||
| 719 | $self->_send_eof(undef,undef,1); | ||
| 720 | } | ||
| 721 | |||
| 722 | sub definition { | ||
| 723 | my ($self, $params) = @_; | ||
| 724 | # | ||
| 725 | #System.IndexOutOfRangeException | ||
| 726 | #Index was outside the bounds of the array. | ||
| 727 | #Stack Trace: | ||
| 728 | # at Quest.FastData.FastRow.get_Item(Int32 column) | ||
| 729 | # at Quest.Toad.MySQL.Trl.ColumnListTrl.CreateChildTrl(ITrl parent, FastRow row) | ||
| 730 | |||
| 731 | no strict 'refs'; | ||
| 732 | my $definition = bless([], 'POE::Component::Server::MySQL::Definition'); | ||
| 733 | $definition->[FIELD_CATALOG] = $params->{catalog} ? $params->{catalog} : 'def'; | ||
| 734 | $definition->[FIELD_DB] = $params->{db} ? $params->{db} : ''; | ||
| 735 | $definition->[FIELD_TABLE] = $params->{table} ? $params->{table} : 'COLUMNS'; | ||
| 736 | $definition->[FIELD_ORG_TABLE] = $params->{org_table}; | ||
| 737 | $definition->[FIELD_NAME] = $params->{name}; | ||
| 738 | $definition->[FIELD_ORG_NAME] = $params->{org_name} ? $params->{table} : 'COLUMN_NAME'; | ||
| 739 | $definition->[FIELD_LENGTH] = defined $params->{length} ? $params->{length} : 0; | ||
| 740 | $definition->[FIELD_TYPE] = defined $params->{type} ? $params->{type} : MYSQL_TYPE_STRING; | ||
| 741 | $definition->[FIELD_FLAGS] = defined $params->{flags} ? $params->{flags} : 0; | ||
| 742 | $definition->[FIELD_DECIMALS] = $params->{decimals}; | ||
| 743 | $definition->[FIELD_DEFAULT] = $params->{default}; | ||
| 744 | return $definition; | ||
| 745 | } | ||
| 746 | |||
| 747 | 1; |