summaryrefslogtreecommitdiff
path: root/includes/libs/UDPTransport.php
blob: 7fad882a244903b9964db53d286f68112e200641 (plain)
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
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

/**
 * A generic class to send a message over UDP
 *
 * If a message prefix is provided to the constructor or via
 * UDPTransport::newFromString(), the payload of the UDP datagrams emitted
 * will be formatted with the prefix and a single space at the start of each
 * line. This is the payload format expected by the udp2log service.
 *
 * @since 1.25
 */
class UDPTransport {
	private $host, $port, $prefix, $domain;

	/**
	 * @param string $host IP address to send to
	 * @param int $port port number
	 * @param int $domain AF_INET or AF_INET6 constant
	 * @param string|bool $prefix Prefix to use, false for no prefix
	 */
	public function __construct( $host, $port, $domain, $prefix = false ) {
		$this->host = $host;
		$this->port = $port;
		$this->domain = $domain;
		$this->prefix = $prefix;
	}

	/**
	 * @param string $info In the format of "udp://host:port/prefix"
	 * @return UDPTransport
	 * @throws InvalidArgumentException
	 */
	public static function newFromString( $info ) {
		if ( preg_match( '!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
			// IPv6 bracketed host
			$host = $m[1];
			$port = intval( $m[2] );
			$prefix = isset( $m[3] ) ? $m[3] : false;
			$domain = AF_INET6;
		} elseif ( preg_match( '!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
			$host = $m[1];
			if ( !IP::isIPv4( $host ) ) {
				$host = gethostbyname( $host );
			}
			$port = intval( $m[2] );
			$prefix = isset( $m[3] ) ? $m[3] : false;
			$domain = AF_INET;
		} else {
			throw new InvalidArgumentException( __METHOD__ . ': Invalid UDP specification' );
		}

		return new self( $host, $port, $domain, $prefix );
	}

	/**
	 * @param string $text
	 */
	public function emit( $text ) {
		// Clean it up for the multiplexer
		if ( $this->prefix !== false ) {
			$text = preg_replace( '/^/m', $this->prefix . ' ', $text );

			// Limit to 64KB
			if ( strlen( $text ) > 65506 ) {
				$text = substr( $text, 0, 65506 );
			}

			if ( substr( $text, -1 ) != "\n" ) {
				$text .= "\n";
			}
		} elseif ( strlen( $text ) > 65507 ) {
			$text = substr( $text, 0, 65507 );
		}

		$sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
		if ( !$sock ) { // @todo should this throw an exception?
			return;
		}

		socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
		socket_close( $sock );
	}
}