shlist

share and manage lists between multiple people
Log | Files | Refs

commit 6c0941aa013adc6f195b312fa2f10876441a0e03
parent d416e846f821ef70398cad7bca64a48ac4adcc15
Author: kyle <kyle@getaddrinfo.net>
Date:   Sat, 30 May 2015 17:24:45 -0400

sl: clean up the code

- add comments
- add more error checking
- add new statement handle for device insertions
- clean up diagnostic messages

Diffstat:
Msl | 72+++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 51 insertions(+), 21 deletions(-)

diff --git a/sl b/sl @@ -18,7 +18,6 @@ my $dbh = DBI->connect( $dbh->do(qq{create table if not exists devices( phone_num int not null primary key, - name text not null, first_seen int not null) }) or die $DBI::errstr; @@ -70,87 +69,118 @@ my $sql = qq{insert into lists (list_id, phone_num, name, first_created, last_up values (?, ?, ?, ?, ?)}; my $new_list_sth = $dbh->prepare($sql); +$sql = qq{insert into devices (phone_num, first_seen) values (?, ?)}; +my $new_device_sth = $dbh->prepare($sql); + print "info: ready for connections\n"; while (my ($new_sock, $bin_addr) = $sock->accept()) { + # don't try and resolve ip address or port my ($err, $addr, $port) = getnameinfo($bin_addr, NI_NUMERICHOST | NI_NUMERICSERV); print "warn: getnameinfo() failed: $err\n" if ($err); - print "info: new connection from $addr:$port\n"; - my $hdr = $addr; + print "info: $addr: new connection on port $port\n"; + # put socket into binary mode binmode($new_sock); + # read and unpack message type my $bread = read $new_sock, my $raw_msg_type, 2; my ($msg_type) = unpack("n", $raw_msg_type); + # validate message type if (!defined $msg_type) { - print "warn: error unpacking msg_type\n"; + print "warn: $addr: error unpacking msg type\n"; close $new_sock; next; } - if ($msg_type > 5) { - print "warn: unknown msg type " . sprintf "%x\n", $msg_type; + print "warn: $addr: unknown message type " . sprintf "0x%x\n", $msg_type; close $new_sock; next; } - print "info: received msg type $msg_type\n"; + print "info: $addr: message type $msg_type\n"; + # read and unpack message size read($new_sock, my $raw_msg_size, 2); my ($msg_size) = unpack("n", $raw_msg_size); + # validate message size if ($msg_size == 0) { - print "warn: empty message received\n"; + print "warn: $addr: size zero message\n"; + close($new_sock); + next; + } + if ($msg_size > 1024) { + print "warn: $addr: message too large: $msg_size\n"; + close($new_sock); + next; } - print "info: msg size = $msg_size\n"; + print "info: $addr: message size = $msg_size\n"; + # read message read($new_sock, my $msg, $msg_size); if ($msg_type == 1) { + # new list + + # expecting two fields delimited by null my ($phone_num, $list_name) = split("\0", $msg); - unless ($list_name && $list_name ne "") { - print "warn: $hdr: name missing or empty, skipping\n"; + unless ($phone_num && $phone_num ne "") { + print "warn: $addr: phone number missing or empty\n"; close($new_sock); next; } - unless ($phone_num && $phone_num ne "") { - print "warn: $hdr: phone number missing, skipping\n"; + unless ($list_name && $list_name ne "") { + print "warn: $addr: name missing or empty\n"; close($new_sock); next; } if (!looks_like_number($phone_num)) { - print "warn: $hdr: $phone_num is not a number, skipping\n"; + print "warn: $addr: $phone_num is not a number\n"; close($new_sock); next; } - print "info: $hdr: phone number = $phone_num\n"; - print "info: $hdr: list name = $list_name\n"; + print "info: $addr: phone number = $phone_num\n"; + print "info: $addr: list name = $list_name\n"; my $time = time; my $list_id = sha256_hex($msg . $time); - print "info: $hdr: list id = $list_id\n"; + print "info: $addr: list id = $list_id\n"; $new_list_sth->execute($list_id, $phone_num, $list_name, $time, $time); print $new_sock $list_id; } elsif ($msg_type == 2) { - # update friend visibility map + # update friend map + + # users phone number followed by 0 or more friends numbers my ($device_ph_num, @friends) = split("\0", $msg); if (!looks_like_number($device_ph_num)) { - print "warn: device phone number $device_ph_num invalid, skipping\n"; + print "warn: $addr: device phone number $device_ph_num invalid\n"; close $new_sock; next; } - print "info: device $device_ph_num, " . @friends . " friends\n"; + print "info: $addr: device $device_ph_num, " . @friends . " friends\n"; + } elsif ($msg_type == 3) { # new device - my ($device_ph_num, $name) = split("\0", $msg); + + # single field + my $device_ph_num = $msg; + + if (!looks_like_number($device_ph_num)) { + print "warn: $addr: device phone number $device_ph_num invalid\n"; + close $new_sock; + next; + } + + $new_device_sth->execute($device_ph_num, time); } close($new_sock);