]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/libs/UDPTransport.php
MediaWiki 1.30.2-scripts
[autoinstalls/mediawiki.git] / includes / libs / UDPTransport.php
1 <?php
2 /**
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  * http://www.gnu.org/copyleft/gpl.html
17  *
18  * @file
19  */
20
21 /**
22  * A generic class to send a message over UDP
23  *
24  * If a message prefix is provided to the constructor or via
25  * UDPTransport::newFromString(), the payload of the UDP datagrams emitted
26  * will be formatted with the prefix and a single space at the start of each
27  * line. This is the payload format expected by the udp2log service.
28  *
29  * @since 1.25
30  */
31 class UDPTransport {
32         private $host, $port, $prefix, $domain;
33
34         /**
35          * @param string $host IP address to send to
36          * @param int $port port number
37          * @param int $domain AF_INET or AF_INET6 constant
38          * @param string|bool $prefix Prefix to use, false for no prefix
39          */
40         public function __construct( $host, $port, $domain, $prefix = false ) {
41                 $this->host = $host;
42                 $this->port = $port;
43                 $this->domain = $domain;
44                 $this->prefix = $prefix;
45         }
46
47         /**
48          * @param string $info In the format of "udp://host:port/prefix"
49          * @return UDPTransport
50          * @throws InvalidArgumentException
51          */
52         public static function newFromString( $info ) {
53                 if ( preg_match( '!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
54                         // IPv6 bracketed host
55                         $host = $m[1];
56                         $port = intval( $m[2] );
57                         $prefix = isset( $m[3] ) ? $m[3] : false;
58                         $domain = AF_INET6;
59                 } elseif ( preg_match( '!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
60                         $host = $m[1];
61                         if ( !IP::isIPv4( $host ) ) {
62                                 $host = gethostbyname( $host );
63                         }
64                         $port = intval( $m[2] );
65                         $prefix = isset( $m[3] ) ? $m[3] : false;
66                         $domain = AF_INET;
67                 } else {
68                         throw new InvalidArgumentException( __METHOD__ . ': Invalid UDP specification' );
69                 }
70
71                 return new self( $host, $port, $domain, $prefix );
72         }
73
74         /**
75          * @param string $text
76          */
77         public function emit( $text ) {
78                 // Clean it up for the multiplexer
79                 if ( $this->prefix !== false ) {
80                         $text = preg_replace( '/^/m', $this->prefix . ' ', $text );
81
82                         // Limit to 64KB
83                         if ( strlen( $text ) > 65506 ) {
84                                 $text = substr( $text, 0, 65506 );
85                         }
86
87                         if ( substr( $text, -1 ) != "\n" ) {
88                                 $text .= "\n";
89                         }
90                 } elseif ( strlen( $text ) > 65507 ) {
91                         $text = substr( $text, 0, 65507 );
92                 }
93
94                 $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
95                 if ( !$sock ) { // @todo should this throw an exception?
96                         return;
97                 }
98
99                 socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
100                 socket_close( $sock );
101         }
102 }