| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Sep-10 13:37 (EDT) | ||
| 6 | # Function: | ||
| 7 | # | ||
| 8 | # $Id: Protocol.pm,v 1.18 2011/01/08 16:24:49 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::DC::Protocol; | ||
| 11 | use Carp qw(croak confess); | ||
| 12 | use Digest::SHA1; | ||
| 13 | use Fcntl; | ||
| 14 | use POSIX; | ||
| 15 | use Socket; | ||
| 16 | use Time::HiRes 'time'; | ||
| 17 | use strict; | ||
| 18 | |||
| 19 | # header: | ||
| 20 | # proto version(32) | ||
| 21 | # message type(32) | ||
| 22 | # auth length(32) | ||
| 23 | # data length(32) | ||
| 24 | # content length(32) | ||
| 25 | # msgidno(32) | ||
| 26 | # flags(32): is-reply(0), want-reply(1), is-error(2), data-encrypted(3), content-encrypted(4) | ||
| 27 | # | ||
| 28 | # followed by: | ||
| 29 | # Auth PB(auth-length) | ||
| 30 | # Data PB(data-length) | ||
| 31 | # Content(content-length) | ||
| 32 | |||
| 33 | |||
| 34 | my $VERSION = 0x41433032; | ||
| 35 | my $BUFSIZ = 65536; | ||
| 36 | |||
| 37 | my %MSGTYPE; | ||
| 38 | my %MSGREV; | ||
| 39 | # status => { num => 0, reqc => '', resc => 'ACPStdReply' }, | ||
| 40 | |||
| 41 | |||
| 42 | sub header_size { return 28 } | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | return bless { @_ }, $class; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub add_msg { | ||
| 50 | my $class = shift; | ||
| 51 | my $name = shift; | ||
| 52 | my $num = shift; | ||
| 53 | my $reqc = shift; | ||
| 54 | my $resc = shift; | ||
| 55 | |||
| 56 | my $d = { | ||
| 57 | name => $name, | ||
| 58 | num => $num, | ||
| 59 | reqc => $reqc, | ||
| 60 | resc => $resc, | ||
| 61 | }; | ||
| 62 | |||
| 63 | $MSGTYPE{$name} = $d; | ||
| 64 | $MSGREV{$num} = $name; | ||
| 65 | |||
| 66 | } | ||
| 67 | |||
| 68 | ################################################################ | ||
| 69 | |||
| 70 | sub encode_header { | ||
| 71 | my $me = shift; | ||
| 72 | my %p = @_; | ||
| 73 | # type, auth_length, data_length, content_length, msgidno, | ||
| 74 | # is_reply, want_reply, is_error | ||
| 75 | |||
| 76 | my $mt = $MSGTYPE{ $p{type} }; | ||
| 77 | confess "unknown message type $p{type}\n" unless defined $mt; | ||
| 78 | |||
| 79 | my $flags = ( $p{is_reply} ? 1 : 0 ) | ||
| 80 | | ( $p{want_reply} ? 2 : 0 ) | ||
| 81 | | ( $p{is_error} ? 4 : 0 ) | ||
| 82 | | ( $p{data_encrypted} ? 8 : 0 ) | ||
| 83 | | ( $p{content_encrypted} ? 16 : 0 ); | ||
| 84 | |||
| 85 | return pack( "NNNNNNN", | ||
| 86 | $VERSION, $mt->{num}, $p{auth_length}, $p{data_length}, $p{content_length}, $p{msgidno}, $flags ); | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | sub decode_header { | ||
| 91 | my $me = shift; | ||
| 92 | my $headr = shift; | ||
| 93 | |||
| 94 | my( $ver, $mt, $al, $dl, $cl, $id, $fl ) | ||
| 95 | = unpack("NNNNNNN", $headr); | ||
| 96 | |||
| 97 | my %p = ( | ||
| 98 | auth_length => $al, | ||
| 99 | data_length => $dl, | ||
| 100 | content_length => $cl, | ||
| 101 | msgidno => $id, | ||
| 102 | type => $MSGREV{$mt}, | ||
| 103 | ); | ||
| 104 | |||
| 105 | confess "unknown protocol version $ver\n" unless $ver == $VERSION; | ||
| 106 | confess "unknown protocol message $mt\n" unless $p{type}; | ||
| 107 | |||
| 108 | $p{is_reply} = ($fl & 1) ? 1 : 0; | ||
| 109 | $p{want_reply} = ($fl & 2) ? 1 : 0; | ||
| 110 | $p{is_error} = ($fl & 4) ? 1 : 0; | ||
| 111 | $p{data_encrypted} = ($fl & 8) ? 1 : 0; | ||
| 112 | $p{content_encrypted} = ($fl & 16) ? 1 : 0; | ||
| 113 | |||
| 114 | return \%p; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub encrypt { | ||
| 118 | my $me = shift; | ||
| 119 | # NYI - placeholder | ||
| 120 | } | ||
| 121 | |||
| 122 | sub decrypt { | ||
| 123 | my $me = shift; | ||
| 124 | my $auth = shift; | ||
| 125 | my $buf = shift; | ||
| 126 | # NYI - placeholder | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _encode_common { | ||
| 130 | my $me = shift; | ||
| 131 | my $how = shift; | ||
| 132 | my $proto = shift; | ||
| 133 | my $data = shift; | ||
| 134 | my $cont = shift; # reference | ||
| 135 | my $auth = shift; # NYI | ||
| 136 | |||
| 137 | my $mt = $MSGTYPE{ $proto->{type} }; | ||
| 138 | confess "unknown message type $proto->{type}\n" unless defined $mt; | ||
| 139 | |||
| 140 | my $apb = $auth ? ACPAuth->encode( $auth ) : ''; | ||
| 141 | my $gpb = $data ? $mt->{$how}->encode( $data ) : ''; | ||
| 142 | |||
| 143 | if( $proto->{data_encrypted} && $gpb ){ | ||
| 144 | $gpb = $me->encrypt( $auth, $gpb ); | ||
| 145 | } | ||
| 146 | |||
| 147 | my $hdr = $me->encode_header( | ||
| 148 | type => $proto->{type}, | ||
| 149 | want_reply => $proto->{want_reply}, | ||
| 150 | is_reply => $proto->{is_reply}, | ||
| 151 | msgidno => $proto->{msgidno}, | ||
| 152 | data_encrypted => $proto->{data_encrypted}, | ||
| 153 | content_encrypted => $proto->{content_encrypted}, | ||
| 154 | auth_length => length($apb), | ||
| 155 | data_length => length($gpb), | ||
| 156 | content_length => ($cont ? length($$cont) : 0), | ||
| 157 | ); | ||
| 158 | |||
| 159 | # caller needs to add content. (to avoid large copy) | ||
| 160 | return $hdr . $apb . $gpb; | ||
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | sub _decode_common { | ||
| 165 | my $me = shift; | ||
| 166 | my $how = shift; | ||
| 167 | my $reply = shift; | ||
| 168 | my $data = shift; | ||
| 169 | |||
| 170 | my $mt = $MSGTYPE{ $reply->{type} }; | ||
| 171 | confess "unknown message type $reply->{type}\n" unless defined $mt; | ||
| 172 | |||
| 173 | return unless $data || $reply->{data}; | ||
| 174 | my $res = $mt->{$how}->decode( $data || $reply->{data} || '' ); | ||
| 175 | return $res; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub encode_request { | ||
| 179 | my $me = shift; | ||
| 180 | |||
| 181 | return $me->_encode_common( 'reqc', @_ ); | ||
| 182 | } | ||
| 183 | sub encode_reply { | ||
| 184 | my $me = shift; | ||
| 185 | |||
| 186 | return $me->_encode_common( 'resc', @_ ); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub decode_request { | ||
| 190 | my $me = shift; | ||
| 191 | |||
| 192 | return $me->_decode_common( 'reqc', @_ ); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub decode_reply { | ||
| 196 | my $me = shift; | ||
| 197 | |||
| 198 | return $me->_decode_common( 'resc', @_ ); | ||
| 199 | } | ||
| 200 | |||
| 201 | ################################################################ | ||
| 202 | |||
| 203 | sub _try_to_connect { | ||
| 204 | my $s = shift; | ||
| 205 | my $sa = shift; | ||
| 206 | my $to = shift; | ||
| 207 | |||
| 208 | my $fn = fileno($s); | ||
| 209 | my $wfd = "\0\0\0\0"; | ||
| 210 | vec($wfd, $fn, 1) = 1; | ||
| 211 | |||
| 212 | my $i = connect($s, $sa); | ||
| 213 | return if $i; # connected | ||
| 214 | return unless $! == EISCONN || $! == EALREADY || $! == EINPROGRESS; | ||
| 215 | |||
| 216 | # wait until connected or timeout | ||
| 217 | my $is = select(undef, $wfd, undef, $to); | ||
| 218 | return if $is == -1; | ||
| 219 | return 1 if vec($wfd, $fn, 1); | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub connect_to_server { | ||
| 224 | my $me = shift; | ||
| 225 | my $ipn = shift; | ||
| 226 | my $port = shift; | ||
| 227 | my $timeo = shift; | ||
| 228 | |||
| 229 | my $s; | ||
| 230 | socket($s, PF_INET, SOCK_STREAM, 6) || confess "cannot create socket: $!\n"; | ||
| 231 | setsockopt($s, Socket::IPPROTO_TCP(), Socket::TCP_NODELAY(), 1); | ||
| 232 | |||
| 233 | # set non-blocking | ||
| 234 | my $fl = fcntl($s, F_GETFL, 0); | ||
| 235 | fcntl($s, F_SETFL, O_NDELAY); | ||
| 236 | |||
| 237 | my $sa = sockaddr_in($port, $ipn); | ||
| 238 | my $to = $timeo ? $timeo / 2 : 0.25; | ||
| 239 | |||
| 240 | # try connecting up to 3 times | ||
| 241 | for (1..3){ | ||
| 242 | # print STDERR "connecting\n"; | ||
| 243 | my $ok = _try_to_connect($s, $sa, $to); | ||
| 244 | |||
| 245 | if( $ok ){ | ||
| 246 | # reset non-blocking | ||
| 247 | fcntl($s, F_SETFL, $fl); | ||
| 248 | return $s; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | my $ipa = inet_ntoa($ipn); | ||
| 253 | confess "connect failed to $ipa:$port\n"; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub write_request { | ||
| 257 | my $me = shift; | ||
| 258 | my $s = shift; | ||
| 259 | my $req = shift; | ||
| 260 | my $timeo = shift; | ||
| 261 | |||
| 262 | $timeo ||= 1; | ||
| 263 | |||
| 264 | # set non-blocking | ||
| 265 | my $fl = fcntl($s, F_GETFL, 0); | ||
| 266 | fcntl($s, F_SETFL, O_NDELAY); | ||
| 267 | my $fn = fileno($s); | ||
| 268 | |||
| 269 | my $tlen = length($req); | ||
| 270 | my $slen = 0; | ||
| 271 | |||
| 272 | while($tlen){ | ||
| 273 | my $wfd = "\0\0\0\0"; | ||
| 274 | vec($wfd, $fn, 1) = 1; | ||
| 275 | my $to = $timeo; | ||
| 276 | |||
| 277 | my $si = select(undef, $wfd, undef, $to); | ||
| 278 | confess "write data failed: $!\n" if $si == -1; | ||
| 279 | confess "write timeout\n" unless vec($wfd, $fn, 1); | ||
| 280 | |||
| 281 | my $l = $tlen > $BUFSIZ ? $BUFSIZ : $tlen; | ||
| 282 | my $i = syswrite($s, $req, $l, $slen); | ||
| 283 | confess "write failed $!\n" unless $i >= 1; | ||
| 284 | $tlen -= $i; | ||
| 285 | $slen += $i; | ||
| 286 | } | ||
| 287 | |||
| 288 | fcntl($s, F_SETFL, $fl); | ||
| 289 | return $slen; | ||
| 290 | |||
| 291 | } | ||
| 292 | |||
| 293 | sub read_data { | ||
| 294 | my $me = shift; | ||
| 295 | my $s = shift; | ||
| 296 | my $size = shift; | ||
| 297 | my $timeo = shift; | ||
| 298 | |||
| 299 | $timeo ||= 1; | ||
| 300 | |||
| 301 | # set non-blocking | ||
| 302 | my $fl = fcntl($s, F_GETFL, 0); | ||
| 303 | fcntl($s, F_SETFL, O_NDELAY); | ||
| 304 | my $fn = fileno($s); | ||
| 305 | |||
| 306 | my $data; | ||
| 307 | my $start = time(); | ||
| 308 | while( my $len = $size - length($data) ){ | ||
| 309 | $len = $BUFSIZ if $len > $BUFSIZ; | ||
| 310 | my $rfd = "\0\0\0\0"; | ||
| 311 | vec($rfd, $fn, 1) = 1; | ||
| 312 | my $to = $start + $timeo - time(); | ||
| 313 | my $t0 = time(); | ||
| 314 | |||
| 315 | my $si = select($rfd, undef, undef, $to); | ||
| 316 | next if $si == -1 && $! == EINTR; | ||
| 317 | confess "read data failed: $!\n" if $si == -1; | ||
| 318 | confess "read timeout " . (time() - $t0) . "\n" unless vec($rfd, $fn, 1); | ||
| 319 | |||
| 320 | my $i = sysread($s, $data, $len, length($data)); | ||
| 321 | next if !defined($i) && $! == EINTR; | ||
| 322 | confess "read failed: connection closed (read " . length($data) . " of $len)\n" if $i == 0; | ||
| 323 | } | ||
| 324 | |||
| 325 | fcntl($s, F_SETFL, $fl); | ||
| 326 | return $data; | ||
| 327 | } | ||
| 328 | |||
| 329 | ################################################################ | ||
| 330 | |||
| 331 | # stream fd to other fd | ||
| 332 | # return hash | ||
| 333 | sub sendfile { | ||
| 334 | my $me = shift; | ||
| 335 | my $out = shift; | ||
| 336 | my $in = shift; | ||
| 337 | my $size = shift; | ||
| 338 | my $timeo = shift; | ||
| 339 | |||
| 340 | # NB: sendfile(2) only supports file=>socket + file=>file | ||
| 341 | # not socket=>file, ... | ||
| 342 | # RSN - elastic buffering? | ||
| 343 | |||
| 344 | my $sha1 = Digest::SHA1->new(); | ||
| 345 | |||
| 346 | while($size){ | ||
| 347 | my $len = $size > $BUFSIZ ? $BUFSIZ : $size; | ||
| 348 | my $buf = $me->read_data($in, $len, $timeo); | ||
| 349 | my $i = length $buf; | ||
| 350 | confess "read failed: $!\n" unless $i > 0; | ||
| 351 | my $w = $me->write_request($out, $buf, $timeo); | ||
| 352 | $size -= $i; | ||
| 353 | $sha1->add($buf); | ||
| 354 | } | ||
| 355 | |||
| 356 | return $sha1->b64digest(); | ||
| 357 | } | ||
| 358 | |||
| 359 | sub send_request { | ||
| 360 | my $me = shift; | ||
| 361 | my $ipn = shift; | ||
| 362 | my $port = shift; | ||
| 363 | my $req = shift; | ||
| 364 | my $debug = shift; | ||
| 365 | my $timeo = shift; | ||
| 366 | |||
| 367 | $debug ||= sub {}; | ||
| 368 | $timeo ||= 0.5; | ||
| 369 | local $SIG{ALRM} = sub{ $debug->("timeout") }; | ||
| 370 | |||
| 371 | my $s = $me->connect_to_server($ipn, $port, $timeo); | ||
| 372 | |||
| 373 | # send request | ||
| 374 | $debug->("sending request"); | ||
| 375 | $me->write_request($s, $req, $timeo); | ||
| 376 | |||
| 377 | # get response or timeout | ||
| 378 | $debug->("reading header"); | ||
| 379 | my $buf = $me->read_data($s, header_size(), $timeo); | ||
| 380 | |||
| 381 | my $p = $me->decode_header($buf); | ||
| 382 | |||
| 383 | # get auth | ||
| 384 | if( $p->{auth_length} ){ | ||
| 385 | # read gpb | ||
| 386 | $debug->("reading auth $p->{auth_length}"); | ||
| 387 | my $data = $me->read_data($s, $p->{auth_length}, $timeo); | ||
| 388 | $p->{auth} = $data; | ||
| 389 | } | ||
| 390 | |||
| 391 | # get data | ||
| 392 | if( $p->{data_length} ){ | ||
| 393 | # read gpb | ||
| 394 | $debug->("reading data $p->{data_length}"); | ||
| 395 | my $data = $me->read_data($s, $p->{data_length}, $timeo); | ||
| 396 | $p->{data} = $data; | ||
| 397 | } | ||
| 398 | |||
| 399 | # get content | ||
| 400 | if( $p->{content_length} ){ | ||
| 401 | $debug->("reading content $p->{content_length}"); | ||
| 402 | my $data = $me->read_data($s, $p->{content_length}, $timeo); | ||
| 403 | $p->{content} = $data; | ||
| 404 | } | ||
| 405 | |||
| 406 | return $p; | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; |