Broad Network


A Simple MySQL Command Packet in PurePerl

Writing MySQL Protocol Packets in PurePerl – Part 6

Writing a Perl Module

Foreword: In this part of the series, I explain how to produce a simple MySQL Command Packet in PurePerl.

By: Chrysanthus Date Published: 28 Jan 2015

Introduction

This is part 6 of my series, Writing MySQL Protocol Packets in PurePerl. PurePerl stands for Pure Perl. It is Perl software without any C software underneath. In this part of the series, I explain how to produce a simple MySQL Command Packet in PurePerl. You should have read the previous parts of the series before reaching here, as this is a continuation.

In the MySQL authentication process, you have the socket connection from the client first. If the socket connection is successful, the server sends a greeting packet to the client. The greeting packet has a scramble string. The client combines the scramble string in a special way with the password of the user. The client software then sends this combination, the user name and other data in a packet called the Credentials packet, to the server. If the server accepts the user, it sends an OK packet to the client software. From there the client can be sending commands to the server. For each command, the server sends back an OK packet, or an Error packet or a result set. This command packet is a string of bytes. It begins with a header segment followed immediately by the body segment. All packets consist of the header segment and the body segment.

Packet Body Byte Arrangement
In the body segment of the Command packet string, the consecutive byte sequences, their lengths and their purposes are as in the following table. Each byte sequence is called a field. This table is a simple command packet table.
Table 6.1. A Very Simple Command Packet Format
Code numeric valueArgument descriptionCommand description
2A string containing the name of the database.Tells the server to change the default database for the session to the one specified by the argument.
3A string containing the query.Tells the server to run the query.

The PurePerl Code
The simple Perl package I have designed to produce the packet is called, Command in the file, Command.pm. The package is part of the client software. It has one function. The function takes as arguments, the decimal number code as first argument and a string or value as the second argument. To transmit a decimal number, you have to “pack” it into the appropriate bytes.

The code for the package is:

package Command;
our $VERSION = "1.01";

use strict;

    sub command
        {
            my $com_code_Dec = $_[0];
            my $code16 = pack('S', $com_code_Dec);
            my $com_code_B = pack('A', $code16);

            my $com_arg  = $_[1];

            my $com_arg_len = length($com_arg);

            my $packet_body_str = $com_code_B . $com_arg;

            my $body_len = $com_arg_len + 1;
            #convert decimal $body_len to 3 bytes with low-byte first
            my $body_len_16 = pack( 'S<', $body_len);  
            my $body_len_24 = $body_len_16 . pack( 'H2', "00");  
            my $seq_no = pack('H2', "00");
            my $header = $body_len_24 . $seq_no;

            my $packet_string = $header . $packet_body_str;  
        }

If you are not using Aciveperl, then you should precede the package with something like, #!/usr/bin/perl .

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Internet Sockets and Perl
Perl pack and unpack Functions
Writing MySQL Protocol Packets in PurePerl
Developing a PurePerl MySQL API
Using the PurePerl MySQL API
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message