From patchwork Mon Jan 9 20:54:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Cunningham X-Patchwork-Id: 2148 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp7226352vsb; Mon, 9 Jan 2017 12:54:59 -0800 (PST) X-Received: by 10.194.146.202 with SMTP id te10mr3049149wjb.179.1483995299578; Mon, 09 Jan 2017 12:54:59 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bk9si34688120wjc.221.2017.01.09.12.54.59; Mon, 09 Jan 2017 12:54:59 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@me.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=me.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2213668A201; Mon, 9 Jan 2017 22:54:50 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from pv38p43im-ztdg05061201.me.com (pv38p43im-ztdg05061201.me.com [17.133.183.25]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5CBBD68A1CA for ; Mon, 9 Jan 2017 22:54:44 +0200 (EET) Received: from process-dkim-sign-daemon.pv38p43im-ztdg05061201.me.com by pv38p43im-ztdg05061201.me.com (Oracle Communications Messaging Server 7.0.5.38.0 64bit (built Feb 26 2016)) id <0OJJ005005UZW500@pv38p43im-ztdg05061201.me.com> for ffmpeg-devel@ffmpeg.org; Mon, 09 Jan 2017 20:54:49 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=me.com; s=4d515a; t=1483995289; bh=Z+jodQLhVj+CM4qgEX4uoFZyzlFmDLyvSRjpUsYBU9s=; h=From:Content-type:MIME-version:Subject:Message-id:Date:To; b=ckpL/KWc7e0qZ/CysaG/CLXIUZCA81aISRCSWlmW2SEvkchNgmCzFKPLajXgAdgqn JilZtx0AjAStG3MjP0I7MFgW2a95JsKpeV2YLE3pe2L/s8dXMbXdE4IMu2F3sDPvbr x95SceDw1ut/j4jA0HnmasHywiTv+RvRpt++ryNXGXYBPuz4/zIt+amxU4w2xWtimg IPrjlds7Wnv8LX+T25zG6XaJakcjj4rKuwHd7qCy+DURnmvHUYCIeM9romkSKTMBko ddjTKT9fERcRgCC33g7C7lZaCty23c4mZaPMBSz1w+7NoijiEao9MRP8tTAfEAlxu9 yzToYIRO/431w== Received: from [192.168.1.9] (75-130-6-224.dhcp.ftwo.tx.charter.com [75.130.6.224]) by pv38p43im-ztdg05061201.me.com (Oracle Communications Messaging Server 7.0.5.38.0 64bit (built Feb 26 2016)) with ESMTPSA id <0OJJ00I5G63B0S60@pv38p43im-ztdg05061201.me.com> for ffmpeg-devel@ffmpeg.org; Mon, 09 Jan 2017 20:54:48 +0000 (GMT) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-01-09_13:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1034 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1603290000 definitions=main-1701090279 From: Joel Cunningham MIME-version: 1.0 (Mac OS X Mail 10.2 \(3259\)) Message-id: Date: Mon, 09 Jan 2017 14:54:47 -0600 To: ffmpeg-devel@ffmpeg.org X-Mailer: Apple Mail (2.3259) Subject: [FFmpeg-devel] [PATCH] tcp: set socket buffer sizes before listen/connect/accept X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From e24d95c0e06a878d401ee34fd6742fcaddeeb95f Mon Sep 17 00:00:00 2001 From: Joel Cunningham Date: Mon, 9 Jan 2017 13:37:51 -0600 Subject: [PATCH] tcp: set socket buffer sizes before listen/connect/accept Attempting to set SO_RCVBUF and SO_SNDBUF on TCP sockets after connection establishment is incorrect and some stacks ignore the set call on the socket at this point. This has been observed on MacOS/iOS. Windows 7 has some peculiar behavior where setting SO_RCVBUF after applies only if the buffer is increasing from the default while decreases are ignored. This is possibly how the incorrect usage has gone unnoticed Unix Network Programming Vol. 1: The Sockets Networking API (3rd edition, seciton 7.5): "When setting the size of the TCP socket receive buffer, the ordering of the function calls is important. This is because of TCP's window scale option, which is exchanged with the peer on SYN segments when the connection is established. For a client, this means the SO_RCVBUF socket option must be set before calling connect. For a server, this means the socket option must be set for the listening socket before calling listen. Setting this option for the connected socket will have no effect whatsoever on the possible window scale option because accept does not return with the connected socket until TCP's three-way handshake is complete. This is why the option must be set on the listening socket. (The sizes of the socket buffers are always inherited from the listening socket by the newly created connected socket)" Signed-off-by: Joel Cunningham --- libavformat/tcp.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 25abafc..5f00ba7 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -140,6 +140,15 @@ static int tcp_open(URLContext *h, const char *uri, int flags) goto fail; } + /* Set the socket's send or receive buffer sizes, if specified. + If unspecified or setting fails, system default is used. */ + if (s->recv_buffer_size > 0) { + setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size)); + } + if (s->send_buffer_size > 0) { + setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size)); + } + if (s->listen == 2) { // multi-client if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0) @@ -164,14 +173,6 @@ static int tcp_open(URLContext *h, const char *uri, int flags) h->is_streamed = 1; s->fd = fd; - /* Set the socket's send or receive buffer sizes, if specified. - If unspecified or setting fails, system default is used. */ - if (s->recv_buffer_size > 0) { - setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size)); - } - if (s->send_buffer_size > 0) { - setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size)); - } freeaddrinfo(ai); return 0;