Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Sync EN] socket_atmark: rewrite example to demonstrate OOB read
  • Loading branch information
lacatoire committed May 17, 2026
commit 5fd00495ce894cd7b626dc762abe11cad1415577
50 changes: 43 additions & 7 deletions reference/sockets/functions/socket-atmark.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: eef7fb60d16864b253aa3aa95a57f8b1cfd41451 Maintainer: PhilDaiguille Status: ready -->
<!-- EN-Revision: 2c357a0dd6cc195bce10fa974ea14a2e30eb4b5b Maintainer: lacatoire Status: ready -->
<!-- Reviewed: yes -->
<refentry xml:id="function.socket-atmark" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
Expand Down Expand Up @@ -45,18 +45,54 @@
&reftitle.examples;
<para>
<example>
<title>Uso de <function>socket_atmark</function> para definir la dirección fuente</title>
<title>Uso de <function>socket_atmark</function> para comprobar si el socket está listo para leer datos fuera de banda.</title>
<programlisting role="php">
<![CDATA[
<?php
// Crear un nuevo socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
var_dump(socket_atmark($sock));
// Cerrar
socket_close($sock);
$socketServer = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_set_option( $socketServer, SOL_SOCKET, SO_REUSEADDR, 1 );
socket_bind( $socketServer, '127.0.0.1' );
socket_listen( $socketServer );

$socketClient = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_getsockname( $socketServer, $stAddr, $uPort );
socket_connect( $socketClient, $stAddr, $uPort );

$socket = socket_accept( $socketServer );
socket_shutdown( $socket, 1 );

$st = 'Estos son datos normales.';
socket_send( $socketClient, $st, strlen( $st ), 0 );
$st = '!'; # TCP solo permite un byte de datos urgentes.
socket_send( $socketClient, $st, strlen( $st ), MSG_OOB );
$st = 'No tan urgente.';
socket_send( $socketClient, $st, strlen( $st ), 0 );
socket_shutdown( $socketClient );

do {
if ( socket_atmark( $socket ) ) {
$rc = socket_recv( $socket, $st, 65536, MSG_OOB );
echo "Datos urgentes recibidos: ({$rc}) {$st}\n";
} else {
$rc = socket_recv( $socket, $st, 1024, 0 );
echo "Datos normales recibidos: ({$rc}) {$st}\n";
}
} while ( $rc > 0 );
socket_close( $socketServer );
socket_close( $socketClient );
socket_close( $socket );
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Datos normales recibidos: (25) Estos son datos normales.
Datos urgentes recibidos: (1) !
Datos normales recibidos: (15) No tan urgente.
Datos normales recibidos: (0)
]]>
</screen>
</example>
</para>
</refsect1>
Expand Down