// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

import 

func kernelVersion() ( int,  int) {
	var  syscall.Utsname
	if  := syscall.Uname(&);  != nil {
		return
	}

	 := .Release
	var  [2]int
	 := 0
	 := 0
	for ,  := range  {
		if  >= '0' &&  <= '9' {
			 = ( * 10) + int(-'0')
		} else {
			// Note that we're assuming N.N.N here.  If we see anything else we are likely to
			// mis-parse it.
			[] = 
			++
			if  >= len() {
				break
			}
			 = 0
		}
	}
	switch  {
	case 0:
		return 0, 0
	case 1:
		return [0], 0
	case 2:
		return [0], [1]
	}
	return
}

// Linux stores the backlog as:
//
//  - uint16 in kernel version < 4.1,
//  - uint32 in kernel version >= 4.1
//
// Truncate number to avoid wrapping.
//
// See issue 5030 and 41470.
func maxAckBacklog( int) int {
	,  := kernelVersion()
	 := 16
	if  > 4 || ( == 4 &&  >= 1) {
		 = 32
	}

	var  uint = 1<< - 1
	if uint() >  {
		 = int()
	}
	return 
}

func maxListenerBacklog() int {
	,  := open("/proc/sys/net/core/somaxconn")
	if  != nil {
		return syscall.SOMAXCONN
	}
	defer .close()
	,  := .readLine()
	if ! {
		return syscall.SOMAXCONN
	}
	 := getFields()
	, ,  := dtoi([0])
	if  == 0 || ! {
		return syscall.SOMAXCONN
	}

	if  > 1<<16-1 {
		return maxAckBacklog()
	}
	return 
}