File: Connector.pm

package info (click to toggle)
libnet-proxy-perl 0.12-5
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 304 kB
  • ctags: 66
  • sloc: perl: 777; sh: 84; makefile: 44
file content (378 lines) | stat: -rw-r--r-- 8,839 bytes parent folder | download | duplicates (5)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package Net::Proxy::Connector;
use strict;
use warnings;
use Carp;
use Scalar::Util qw( refaddr );
use Net::Proxy;

my %PROXY_OF;
my $BUFFSIZE = 4096;

#
# the most basic possible constructor
#
sub new {
    my ( $class, $args ) = @_;
    my $self = bless $args ? {%$args} : {}, $class;
    $self->init() if $self->can('init');
    delete $self->{_proxy_}; # this link back is now unnecessary
    return $self;
}

#
# Each Connector is managed by a Net::Proxy object
#
sub set_proxy {
    my ( $self, $proxy ) = @_;
    croak "$proxy is not a Net::Proxy object"
        if !UNIVERSAL::isa( $proxy, 'Net::Proxy' );
    return $PROXY_OF{ refaddr $self } = $proxy;
}

sub get_proxy { return $PROXY_OF{ refaddr $_[0] }; }

sub is_in {
    my $id = refaddr $_[0];
    return $id == refaddr $PROXY_OF{$id}->in_connector();
}

sub is_out {
    my $id = refaddr $_[0];
    return $id == refaddr $PROXY_OF{$id}->out_connector();
}

#
# the method that creates all the sockets
#
sub new_connection_on {
    my ( $self, $listener ) = @_;
    Net::Proxy->notice(
        'New connection on ' . Net::Proxy->get_nick($listener) );

    # call the actual Connector method
    my $sock = eval { $self->accept_from($listener); };
    if( $@ ) {
        Net::Proxy->error( $@ );
        return;
    }

    Net::Proxy->set_connector( $sock, $self );
    Net::Proxy->set_buffer( $sock, '' );
    Net::Proxy->set_callback( $sock, $self->{hook} ) if $self->{hook};
    Net::Proxy->watch_reader_sockets($sock);

    # connect to the destination
    my $out = $self->get_proxy()->out_connector();
    $out->_out_connect_from($sock);

    # update the stats
    $self->get_proxy()->stat_inc_opened();
    return;
}

sub _out_connect_from {
    my ( $self, $sock ) = @_;

    my $peer = eval { $self->connect(); };
    if ($@) {    # connect() dies if the connection fails
        $@ =~ s/ at .*?\z//s;
        warn "connect() failed with error '$@'\n";
        Net::Proxy->close_sockets($sock);
        return;
    }
    if ($peer) {    # $peer is undef for Net::Proxy::Connector::dummy
        Net::Proxy->watch_reader_sockets($peer);
        Net::Proxy->set_connector( $peer, $self );
        Net::Proxy->set_buffer( $peer, '' );
        Net::Proxy->set_callback( $peer, $self->{hook} ) if $self->{hook};
        Net::Proxy->set_nick( $peer,
                  $peer->sockhost() . ':'
                . $peer->sockport() . ' -> '
                . $peer->peerhost() . ':'
                . $peer->peerport() );
        Net::Proxy->notice( 'Connected ' . Net::Proxy->get_nick( $peer ) );

        Net::Proxy->set_peer( $peer, $sock );
        Net::Proxy->set_peer( $sock, $peer );
        Net::Proxy->notice( 'Peered '
                . Net::Proxy->get_nick($sock) . ' with '
                . Net::Proxy->get_nick($peer) );
    }

    return;
}

#
# base methods for exchanging raw data
#

# return raw data from the socket
sub raw_read_from {
    my ( $self, $sock ) = @_;

    # low level read on the socket
    my $close = 0;
    my $buffer;
    my $read = $sock->sysread( $buffer, $BUFFSIZE );

    ## Net::Proxy->debug("Read $read bytes from " . Net::Proxy->get_nick($sock));

    # check for errors
    if ( not defined $read ) {
        warn sprintf( "Read undef from %s:%s (Error %d: %s)\n",
            $sock->sockhost(), $sock->sockport(), $!, "$!" );
        $close = 1;
    }

    # connection closed
    if ( $close || $read == 0 ) {
        my $peer = Net::Proxy->get_peer($sock);
        $self->get_proxy()->close_sockets( $sock, $peer );
        return;
    }

    return $buffer;
}

# send raw data to the socket
sub raw_write_to {
    my ($self, $sock) = @_;
    my $data = Net::Proxy->get_buffer( $sock );

    ## Net::Proxy->debug("Writing @{[length $data]} bytes (max $BUFFSIZE) to " . Net::Proxy->get_nick($sock));

    my $written = $sock->syswrite( $data, $BUFFSIZE );

    ## Net::Proxy->debug("Wrote $written bytes to " . Net::Proxy->get_nick($sock));
    if( ! defined $written ) {
        warn sprintf("Read undef from %s:%s (Error %d: %s)\n",
                     $sock->sockhost(), $sock->sockport(), $!, "$!");
    }
    elsif ( $written == length $data ) {
        Net::Proxy->remove_writer_sockets( $sock );
        Net::Proxy->set_buffer( $sock, '' );
    }
    else { # there is some data left to write
        Net::Proxy->set_buffer( $sock, substr( $data, $written ) );
    }
    return;
}

#
# base methods for listen() and accept_from()
#

# the most basic possible listen()
sub raw_listen {
    my $self = shift;
    my $sock = IO::Socket::INET->new(
        Listen    => 1,
        LocalAddr => $self->{host},
        LocalPort => $self->{port},
        Proto     => 'tcp',
        ReuseAddr => $^O eq 'MSWin32' ? 0 : 1,
    );

    # this exception is not catched by Net::Proxy
    die "Can't listen on $self->{host} port $self->{port}: $!" unless $sock;

    Net::Proxy->set_nick( $sock,
        'listener ' . $sock->sockhost() . ':' . $sock->sockport() );

    return $sock;
}

# accept on a socket and return the new connected socket
sub raw_accept_from {
    my ($self, $listen) = @_;
    my $sock = $listen->accept();
    die $! unless $sock;

    Net::Proxy->set_nick( $sock,
              $sock->peerhost() . ':'
            . $sock->peerport() . ' -> '
            . $sock->sockhost() . ':'
            . $sock->sockport() );
    Net::Proxy->notice( 'Accepted ' . Net::Proxy->get_nick( $sock ) );

    return $sock;
}

1;

__END__

=head1 NAME

Net::Proxy::Connector - Base class for Net::Proxy protocols

=head1 SYNOPSIS

    #
    # template for the zlonk connector
    #
    package Net::Proxy::Connector::zlonk;

    use strict;
    use Net::Proxy::Connector;
    our @ISA = qw( Net::Proxy::Connector );

    # here are the methods you need to write for your connector

    # if it can be used as an 'in' connector
    sub listen { }
    sub accept_from { }

    # if it can be used as an 'out' connector
    sub connect { }

    # to process data
    sub read_from { }
    sub write_to { }

    1;

=head1 DESCRIPTION

C<Net::Proxy::Connector> is the base class for all specialised
protocols used by C<Net::Proxy>.

=head1 METHODS

=head2 Class methods

The base class provides the following methods:

=over 4

=item new()

=back

=head2 Instance methods

=over 4

=item set_proxy( $proxy )

Define the proxy that "owns" the connector.

=item get_proxy()

Return the C<Net::Proxy> object that "owns" the connector.

=item is_in()

Return a boolean value indicating if the C<Net::Proxy::Connector>
object is the C<in> connector of its proxy.

=item is_out()

Return a boolean value indicating if the C<Net::Proxy::Connector>
object is the C<out> connector of its proxy.

=item new_connection_on( $socket )

This method is called by C<Net::Proxy> to handle incoming connections,
and in turn call C<accept_from()> on the 'in' connector and
C<connect()> on the 'out' connector.

=item raw_read_from( $socket )

This method can be used by C<Net::Proxy::Connector> subclasses in their
C<read_from()> methods, to fetch raw data on a socket.

=item raw_write_to( $socket, $data )

This method can be used by C<Net::Proxy::Connector> subclasses in their
C<write_to()> methods, to send raw data on a socket.

=item raw_listen( )

This method can be used by C<Net::Proxy::Connector> subclasses in their
C<listen()> methods, to create a listening socket on their C<host>
and C<port> parameters.

=item raw_accept_from( $socket )

This method can be used internaly by C<Net::Proxy::Connector> subclasses
in their C<accept_from()> methods, to accept a newly connected socket.

=back

=head1 Subclass methods

The following methods should be defined in C<Net::Proxy::Connector>
subclasses:

=head2 Initialisation

=over 4

=item init()

This method initalise the connector.

=back

=head2 Processing incoming/outgoing data

=over 4

=item read_from( $socket )

Return the data that was possibly decapsulated by the connector.

=item write_to( $socket, $data )

Write C<$data> to the given C<$socket>, according to the connector
scheme.

=back

=head2 C<in> connector

=over 4

=item listen()

Initiate listening sockets and return them.

This method can use the C<raw_listen()> method to do the low-level
listen call.

=item accept_from( $socket )

C<$socket> is a listening socket created by C<listen()>.
This method returns the connected socket.

This method can use the C<raw_accept_from()> method to do the low-level
accept call.

=back

=head2 C<out> connector

=over 4

=item connect()

Return a socket connected to the remote server.

=back

=head1 AUTHOR

Philippe 'BooK' Bruhat, C<< <book@cpan.org> >>.

=head1 COPYRIGHT

Copyright 2006 Philippe 'BooK' Bruhat, All Rights Reserved.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut