1--TEST--
2Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP
3--SKIPIF--
4<?php
5if (!extension_loaded('sockets')) {
6    die('SKIP The sockets extension is not loaded.');
7}
8if (substr(PHP_OS, 0, 3) == 'WIN') {
9	die('skip Not valid for Windows');
10}
11require 'ipv6_skipif.inc';
12--FILE--
13<?php
14    $socket = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
15    if (!$socket) {
16        die('Unable to create AF_INET6 socket');
17    }
18    if (!socket_set_nonblock($socket)) {
19        die('Unable to set nonblocking mode for socket');
20    }
21    socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning
22    $address = '::1';
23    socket_sendto($socket, '', 1, 0, $address); // cause warning
24    if (!socket_bind($socket, $address, 1223)) {
25        die("Unable to bind to $address:1223");
26    }
27
28    $msg = "Ping!";
29    $len = strlen($msg);
30    $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223);
31    if ($bytes_sent == -1) {
32        die('An error occurred while sending to the socket');
33    } else if ($bytes_sent != $len) {
34        die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected');
35    }
36
37    $from = "";
38    $port = 0;
39    socket_recvfrom($socket, $buf, 12, 0); // cause warning
40    socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning
41    $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port);
42    if ($bytes_received == -1) {
43        die('An error occurred while receiving from the socket');
44    } else if ($bytes_received != $len) {
45        die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected');
46    }
47    echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
48
49    socket_close($socket);
50--EXPECTF--
51Warning: socket_recvfrom(): unable to recvfrom [11]: Resource temporarily unavailable in %s on line %d
52
53Warning: Wrong parameter count for socket_sendto() in %s on line %d
54
55Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d
56
57Warning: Wrong parameter count for socket_recvfrom() in %s on line %d
58Received Ping! from remote address ::1 and remote port 1223
59--CREDITS--
60Falko Menge <mail at falko-menge dot de>
61PHP Testfest Berlin 2009-05-09
62