xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/inet_windowsize.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: inet_windowsize.c,v 1.2 2017/02/14 01:16:49 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	inet_windowsize 3
6 /* SUMMARY
7 /*	TCP window scaling control
8 /* SYNOPSIS
9 /*	#include <iostuff.h>
10 /*
11 /*	int	inet_windowsize;
12 /*
13 /*	void	set_inet_windowsize(sock, windowsize)
14 /*	int	sock;
15 /*	int	windowsize;
16 /* DESCRIPTION
17 /*	set_inet_windowsize() overrides the default TCP window size
18 /*	with the specified value. When called before listen() or
19 /*	accept(), this works around broken infrastructure that
20 /*	mis-handles TCP window scaling options.
21 /*
22 /*	The global inet_windowsize variable is available for other
23 /*	routines to remember that they wish to override the default
24 /*	TCP window size. The variable is not accessed by the
25 /*	set_inet_windowsize() function itself.
26 /*
27 /*	Arguments:
28 /* .IP sock
29 /*	TCP communication endpoint, before the connect(2) or listen(2) call.
30 /* .IP windowsize
31 /*	The preferred TCP window size. This must be > 0.
32 /* DIAGNOSTICS
33 /*	Panic: interface violation.
34 /*	Warnings: some error return from setsockopt().
35 /* LICENSE
36 /* .ad
37 /* .fi
38 /*	The Secure Mailer license must be distributed with this software.
39 /* AUTHOR(S)
40 /*	Wietse Venema
41 /*	IBM T.J. Watson Research
42 /*	P.O. Box 704
43 /*	Yorktown Heights, NY 10598, USA
44 /*--*/
45 
46 /* System libraries. */
47 
48 #include <sys_defs.h>
49 #include <sys/socket.h>
50 
51 /* Utility library. */
52 
53 #include <msg.h>
54 #include <iostuff.h>
55 
56 /* Application storage. */
57 
58  /*
59   * Tunable to work around broken routers.
60   */
61 int     inet_windowsize = 0;
62 
63 /* set_inet_windowsize - set TCP send/receive window size */
64 
set_inet_windowsize(int sock,int windowsize)65 void    set_inet_windowsize(int sock, int windowsize)
66 {
67 
68     /*
69      * Sanity check.
70      */
71     if (windowsize <= 0)
72 	msg_panic("inet_windowsize: bad window size %d", windowsize);
73 
74     /*
75      * Generic implementation: set the send and receive buffer size before
76      * listen() or connect().
77      */
78     if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &windowsize,
79 		   sizeof(windowsize)) < 0)
80 	msg_warn("setsockopt SO_SNDBUF %d: %m", windowsize);
81     if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *) &windowsize,
82 		   sizeof(windowsize)) < 0)
83 	msg_warn("setsockopt SO_RCVBUF %d: %m", windowsize);
84 }
85