Developing the Connect Function for PurePerl MySQL API
Developing a PurePerl MySQL API – Part 2
Writing a Perl Module
Foreword: In this part of the series I develop the connect function for PurePerl MySQL API.
By: Chrysanthus Date Published: 28 Jan 2015
Introduction
The Function Call
The syntax to call the function in its package is:
Boolean connect(user, password, host, port, [database])
where user is the user name, password is the password, host is the domain name of the MySQL server, port is the port of the server and database is the database to use. The function returns 1 for true or 0 for false. true means the connection was successful, and false means the connection was not successful. The database name is optional.
If your application script and the MySQL server are in the same computer, then host is “localhost”. The default MySQL port is 3306. You type the port number without quotes.
The function code is:
sub connect
{#return 1 on success or 0 on failure
#Credentials
my $user = $_[0];
my $password = $_[1];
$host = $_[2];
$port = $_[3];
my $db = $_[4];
my ($iaddr, $paddr, $proto);
$proto = getprotobyname("tcp");
$iaddr = gethostbyname($host);
$paddr = sockaddr_in($port, $iaddr);
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die print "socket: $!";
connect(SOCK, $paddr) || die print "connect: $!";
my $greet_pket;
recv (SOCK, $greet_pket, 200, 0);
#Do token = SHA1(scramble + SHA1(stage1_hash)) XOR stage1_hash
#stage1_hash = SHA1(password)
#Note SHA1(A+B) is the SHA1 of the concatenation of A with B.
my $stage1_hash = Sha1::sha1($password);
my $stage2_hash = Sha1::sha1($stage1_hash);
my %ha_grt_pkt = Greeting::greet($greet_pket);
my $scramble = $ha_grt_pkt{'scramble'};
my $addition = $scramble . $stage2_hash;
my $sha1_add = Sha1::sha1($addition);
my $token = $sha1_add ^ $stage1_hash;
#develop and bring here, the Credentials Packet as a string
my $credentials_pkt = Credentials::credentials($user, $token);
#send the credentials packet
send (SOCK, $credentials_pkt, 0);
#receive the reply
my $credentials_reply;
recv (SOCK, $credentials_reply, 200, 0);
#determine and return 1 or 0
my ($dummy0,$OKHex) = unpack("H8H2", $credentials_reply);
if ($OKHex eq "ff")
{
my %ha = Error::error($credentials_reply);
$Error_msg .= $ha{'error_msg'};
return 0;
}
if ($OKHex eq "00")
{
return 1;
}
}
The first code segment assigns the arguments (credentials) to variables. The second code segment creates a socket and does the connection. The next code segment receives the greeting packet from the server. The next code segment (two sub segments) combines the scramble message from the greeting pack and the password in a special way and assigns the result to the variable, $token. The next code segment brings in the credentials packet to the script.
The code segment that follows sends the credentials packet with the user name and $token to the server. This segment also receives the reply, which is either an error packet or an OK packet. The next code segment obtains a byte from the received packet that determines whether the packet is an OK packet or an error packet. This segment goes on to return 1 (for true) or 0 for false. True means an OK packet was received. False means an Error packet was received. True means that the connection was successful. In the case of false an error message is produced.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Internet Sockets and PerlPerl 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