-
Notifications
You must be signed in to change notification settings - Fork 0
/
rri.pl
69 lines (68 loc) · 1.6 KB
/
rri.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Read answer from RRI-Server
#
# Syntax: rriReadData(object socket)
# Parameter:
# - socket: SSL connection established with rriConnect
#
# Returnvalue:
# Returns string with answer from RRI server on success or "undef", if an error
# occured.
sub rriReadData
{
my $sock=shift;
my ($head, $head2);
my ($data, $data2);
my $ret;
$head="";
$data="";
# Step 1: read 4-byte RRI-header
my $rest=4;
while ( $rest )
{
$ret=read $sock,$head2,$rest;
if (! defined($ret))
{
return (undef);
}
$head.=$head2;
$rest-=$ret;
}
my $len=unpack "N",$head;
if ($len > 65535)
{
# Should not happen, something went wrong
return (undef);
}
# Step 2: read payload
$rest=$len;
while ( $rest )
{
$ret=read $sock,$data2,$rest;
if (! defined($ret))
{
return (undef);
}
$data.=$data2;
$rest-=$ret;
}
return $data;
}
# Sends order to RRI-Server
#
#
# Syntax: rriSendData(object socket, string order)
# Parameter:
# - socket: SSL connection established with rriConnect
# - order: String, which should be send to the RRI-server
#
# Returnvalue:
# Returns 1 on success, 0 on failure
sub rriSendData
{
my ($sock,$data)=@_;
my $len=length($data); # Length of data
my $head=pack "N",$len; # convert to 4 byte value in network byteorder
return 0 if (!print $sock $head); # send 4 byte header
return 0 if (!print $sock $data); # send payload
return 1;
}