Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UDPTransport
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 newFromString
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 emit
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia;
8
9use InvalidArgumentException;
10
11/**
12 * A generic class to send a message over UDP
13 *
14 * If a message prefix is provided to the constructor or via
15 * UDPTransport::newFromString(), the payload of the UDP datagrams emitted
16 * will be formatted with the prefix and a single space at the start of each
17 * line. This is the payload format expected by the udp2log service.
18 *
19 * @since 1.25
20 */
21class UDPTransport {
22    // Limit to 64 KiB
23    public const MAX_PAYLOAD_SIZE = 65507;
24    private string $host;
25    private int $port;
26    /** @var bool|string */
27    private $prefix;
28    private int $domain;
29
30    /**
31     * @param string $host IP address to send to
32     * @param int $port port number
33     * @param int $domain AF_INET or AF_INET6 constant
34     * @param string|bool $prefix Prefix to use, false for no prefix
35     */
36    public function __construct( $host, $port, $domain, $prefix = false ) {
37        $this->host = $host;
38        $this->port = $port;
39        $this->domain = $domain;
40        $this->prefix = $prefix;
41    }
42
43    /**
44     * @param string $info In the format of "udp://host:port/prefix"
45     * @return UDPTransport
46     */
47    public static function newFromString( $info ) {
48        if ( preg_match( '!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
49            // IPv6 bracketed host
50            $host = $m[1];
51            $port = intval( $m[2] );
52            $prefix = $m[3] ?? false;
53            $domain = AF_INET6;
54        } elseif ( preg_match( '!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
55            $host = $m[1];
56            if ( !IPUtils::isIPv4( $host ) ) {
57                $host = gethostbyname( $host );
58            }
59            $port = intval( $m[2] );
60            $prefix = $m[3] ?? false;
61            $domain = AF_INET;
62        } else {
63            throw new InvalidArgumentException( __METHOD__ . ': Invalid UDP specification' );
64        }
65
66        return new self( $host, $port, $domain, $prefix );
67    }
68
69    /**
70     * @param string $text
71     */
72    public function emit( $text ): void {
73        // Clean it up for the multiplexer
74        if ( $this->prefix !== false ) {
75            $text = preg_replace( '/^/m', $this->prefix . ' ', $text );
76
77            if ( strlen( $text ) > self::MAX_PAYLOAD_SIZE - 1 ) {
78                $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE - 1 );
79            }
80
81            if ( !str_ends_with( $text, "\n" ) ) {
82                $text .= "\n";
83            }
84        } elseif ( strlen( $text ) > self::MAX_PAYLOAD_SIZE ) {
85            $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE );
86        }
87
88        $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
89        if ( !$sock ) { // @todo should this throw an exception?
90            return;
91        }
92
93        socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
94        socket_close( $sock );
95    }
96}
97
98/** @deprecated class alias since 1.47 */
99class_alias( UDPTransport::class, 'UDPTransport' );