Kamrul Khan Kamrul Khan - 9 months ago 134
Perl Question

Issues with connecting with apple APNS using perl

I am using a library(Net::APNS::Persistent) that uses Net::SSLeay. This library helps to communicate with Apples APNS servers. I found if I send any bad data for example wrong token or poorly formatted payload, the program terminates after three tries. And it shows no error message.

As per my debugging this issue is occurring from line number 1895 of SSLeay.pm

$wrote = write_partial($ssl, $written, $to_write, $$data_ref);

I tried to catch if any exception, term signal were sent but no luck. If there is a network issue or any sort of error I expect it should show an error message/ exception that can be handled. But it simply kills the program without saying anything. Is there a way to sort this out? My code is as below:

package apnsClient;

use Net::APNS::Persistent;
use Data::Dumper;
use Test::Trap;

my $cfg;
my $apns;

sub load {
my $class = shift;
my $self = {
_cfg => shift,
};

$cfg = $self->{_cfg};

bless $self, $class;
return $self;
}

sub connect {
my ($sandbox, $cert, $key, $pass) = $cfg->getAPNSServer();

print "[x] Cert $cert Key $key \n";

$apns = Net::APNS::Persistent->new({
sandbox => $sandbox,
cert => $cert,
key => $key,
}) or die("[-] Unable to connect to APNS server");
}

sub push {
my ($class,$token,$payload) = @_;

print "[+] Sending push with token: $token and Data: \n ".Dumper($payload);

$apns->queue_notification(
$token,
$payload
);
print "[+] Sending apns queue \n";
$apns->send_queue;
print "[+] Sent \n";
}

sub disconnect {
$apns->disconnect;
}

1;

Answer Source

Figured out the issue finally. Apple server drops a session if invalid tokens are supplied and the write_partial function sends a termination signal when fails to write to the socket. To prevent the script from terminating we need to use

$SIG{'PIPE'} = 'IGNORE';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download