Annotation of embedaddon/iperf/src/tcp_window_size.c, revision 1.1
1.1 ! misho 1: /*---------------------------------------------------------------
! 2: * Copyright (c) 1999,2000,2001,2002,2003
! 3: * The Board of Trustees of the University of Illinois
! 4: * All Rights Reserved.
! 5: *---------------------------------------------------------------
! 6: * Permission is hereby granted, free of charge, to any person
! 7: * obtaining a copy of this software (Iperf) and associated
! 8: * documentation files (the "Software"), to deal in the Software
! 9: * without restriction, including without limitation the
! 10: * rights to use, copy, modify, merge, publish, distribute,
! 11: * sublicense, and/or sell copies of the Software, and to permit
! 12: * persons to whom the Software is furnished to do
! 13: * so, subject to the following conditions:
! 14: *
! 15: *
! 16: * Redistributions of source code must retain the above
! 17: * copyright notice, this list of conditions and
! 18: * the following disclaimers.
! 19: *
! 20: *
! 21: * Redistributions in binary form must reproduce the above
! 22: * copyright notice, this list of conditions and the following
! 23: * disclaimers in the documentation and/or other materials
! 24: * provided with the distribution.
! 25: *
! 26: *
! 27: * Neither the names of the University of Illinois, NCSA,
! 28: * nor the names of its contributors may be used to endorse
! 29: * or promote products derived from this Software without
! 30: * specific prior written permission.
! 31: *
! 32: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
! 33: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
! 34: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
! 35: * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
! 36: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
! 37: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
! 38: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
! 39: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
! 40: * ________________________________________________________________
! 41: * National Laboratory for Applied Network Research
! 42: * National Center for Supercomputing Applications
! 43: * University of Illinois at Urbana-Champaign
! 44: * http://www.ncsa.uiuc.edu
! 45: * ________________________________________________________________
! 46: *
! 47: * tcp_window_size.c
! 48: * by Mark Gates <mgates@nlanr.net>
! 49: * -------------------------------------------------------------------
! 50: * set/getsockopt
! 51: * ------------------------------------------------------------------- */
! 52:
! 53: /*
! 54: * imported into iperfjd branch: 3 Feb 2009 jdugan
! 55: *
! 56: * made variable names more sane
! 57: * removed some cruft
! 58: */
! 59:
! 60: #include <stdio.h>
! 61: #include <sys/socket.h>
! 62: #include <assert.h>
! 63:
! 64: /* -------------------------------------------------------------------
! 65: * If bufsize > 0, set the TCP window size (via the socket buffer
! 66: * sizes) for sock. Otherwise leave it as the system default.
! 67: *
! 68: * This must be called prior to calling listen() or connect() on
! 69: * the socket, for TCP window sizes > 64 KB to be effective.
! 70: *
! 71: * This now works on UNICOS also, by setting TCP_WINSHIFT.
! 72: * This now works on AIX, by enabling RFC1323.
! 73: * returns -1 on error, 0 on no error.
! 74: * -------------------------------------------------------------------
! 75: */
! 76:
! 77: int
! 78: set_tcp_windowsize(int sock, int bufsize, int dir)
! 79: {
! 80: int rc;
! 81: int newbufsize;
! 82:
! 83: assert(sock >= 0);
! 84:
! 85: if (bufsize > 0)
! 86: {
! 87: /*
! 88: * note: results are verified after connect() or listen(), since
! 89: * some OS's don't show the corrected value until then.
! 90: */
! 91: // printf("Setting TCP buffer to size: %d\n", bufsize);
! 92: newbufsize = bufsize;
! 93: rc = setsockopt(sock, SOL_SOCKET, dir, (char *) &newbufsize, sizeof(newbufsize));
! 94: if (rc < 0)
! 95: return rc;
! 96: } else {
! 97: // printf(" Using default TCP buffer size and assuming OS will do autotuning\n");
! 98: }
! 99:
! 100: return 0;
! 101: }
! 102:
! 103: /* -------------------------------------------------------------------
! 104: * returns the TCP window size (on the sending buffer, SO_SNDBUF),
! 105: * or -1 on error.
! 106: * ------------------------------------------------------------------- */
! 107:
! 108: int
! 109: get_tcp_windowsize(int sock, int dir)
! 110: {
! 111: int bufsize = 0;
! 112:
! 113: int rc;
! 114: socklen_t len;
! 115:
! 116: /* send buffer -- query for buffer size */
! 117: len = sizeof bufsize;
! 118: rc = getsockopt(sock, SOL_SOCKET, dir, (char *) &bufsize, &len);
! 119:
! 120: if (rc < 0)
! 121: return rc;
! 122:
! 123: return bufsize;
! 124: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>