// ZMQ_SOCKS_PROXY: NOT DOCUMENTED
//
// Returns ErrorNotImplemented41 with ZeroMQ version < 4.1
975285
func (soc *Socket) SetSocksProxy(value string) error {
	if minor < 1 {
		return ErrorNotImplemented41
	}
	if value == "" {
		return soc.setNullString(C.ZMQ_SOCKS_PROXY)
	}
	return soc.setString(C.ZMQ_SOCKS_PROXY, value)
}
274500
func Emulate(d data.SeccompWorkingMemory, filters []unix.SockFilter) uint32 {
	e := &emulator{data: d, filters: filters, pointer: 0}
	for {
		val, finished := e.next()
		if finished {
			return val
		}
	}
}
299803
func DialSocksProxy(socksType int, proxy string) func(string, string) (net.Conn, error) {
	return (&Config{Proto: SOCKS5, Host: proxy}).dialFunc()
}
11588
func (c *socks5MuxBindConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
	accepter, ok := conn.(Accepter)
	if !ok {
		return nil, errors.New("wrong connection type")
	}

	return accepter.Accept()
}
35629
func socksDialerFunction(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
	var auth *proxy.Auth
	if u.User != nil {
		auth = new(proxy.Auth)
		auth.User = u.User.Username()
		if p, ok := u.User.Password(); ok {
			auth.Password = p
		}
	}

	return proxy.SOCKS5("tcp", u.Host, auth, forward)
}
307084
func QueryRequestFromProxyRequest(req *query.ProxyRequest) (*QueryRequest, error) {
	qr := new(QueryRequest)
	switch c := req.Request.Compiler.(type) {
	case lang.FluxCompiler:
		qr.Type = "flux"
		qr.Query = c.Query
	case lang.SpecCompiler:
		qr.Type = "flux"
		qr.Spec = c.Spec
	default:
		return nil, fmt.Errorf("unsupported compiler %T", c)
	}
	switch d := req.Dialect.(type) {
	case *csv.Dialect:
		var header = !d.ResultEncoderConfig.NoHeader
		qr.Dialect.Header = &header
		qr.Dialect.Delimiter = string(d.ResultEncoderConfig.Delimiter)
		qr.Dialect.CommentPrefix = "#"
		qr.Dialect.DateTimeFormat = "RFC3339"
		qr.Dialect.Annotations = d.ResultEncoderConfig.Annotations
	default:
		return nil, fmt.Errorf("unsupported dialect %T", d)
	}

	return qr, nil
}
1053438
func proxy_SOCKS5(network, addr string, auth *proxy_Auth, forward proxy_Dialer) (proxy_Dialer, error) {
	s := &proxy_socks5{
		network: network,
		addr:    addr,
		forward: forward,
	}
	if auth != nil {
		s.user = auth.User
		s.password = auth.Password
	}

	return s, nil
}
895193
func ZmqRequestSocket(ip string, request_ports []int) *zmq.Socket {
	context, _ := zmq.NewContext()
	socket, _ := context.NewSocket(zmq.REQ)
	for _, _port := range request_ports {
		socket.Connect(fmt.Sprintf("tcp://%s:%d", ip, _port))
	}
	return socket
}
438663
func socks5ReadCommand(rw *bufio.ReadWriter, req *SocksRequest) (err error) {
	sendErrResp := func(reason byte) {
		// Swallow errors that occur when writing/flushing the response,
		// connection will be closed anyway.
		sendSocks5ResponseRejected(rw, reason)
		socksFlushBuffers(rw)
	}

	// Validate the fixed parts of the command message.
	if err = socksReadByteVerify(rw.Reader, "version", socks5Version); err != nil {
		sendErrResp(SocksRepGeneralFailure)
		err = newTemporaryNetError("socks5ReadCommand: %s", err)
		return
	}
	if err = socksReadByteVerify(rw.Reader, "command", socksCmdConnect); err != nil {
		sendErrResp(SocksRepCommandNotSupported)
		err = newTemporaryNetError("socks5ReadCommand: %s", err)
		return
	}
	if err = socksReadByteVerify(rw.Reader, "reserved", socksReserved); err != nil {
		sendErrResp(SocksRepGeneralFailure)
		err = newTemporaryNetError("socks5ReadCommand: %s", err)
		return
	}

	// Read the destination address/port.
	// XXX: This should probably eventually send socks 5 error messages instead
	// of rudely closing connections on invalid addresses.
	var atype byte
	if atype, err = socksReadByte(rw.Reader); err != nil {
		err = newTemporaryNetError("socks5ReadCommand: Failed to read address type: %s", err)
		return
	}
	var host string
	switch atype {
	case socksAtypeV4:
		var addr []byte
		if addr, err = socksReadBytes(rw.Reader, net.IPv4len); err != nil {
			err = newTemporaryNetError("socks5ReadCommand: Failed to read IPv4 address: %s", err)
			return
		}
		host = net.IPv4(addr[0], addr[1], addr[2], addr[3]).String()

	case socksAtypeDomainName:
		var alen byte
		if alen, err = socksReadByte(rw.Reader); err != nil {
			err = newTemporaryNetError("socks5ReadCommand: Failed to read domain name length: %s", err)
			return
		}
		if alen == 0 {
			err = newTemporaryNetError("socks5ReadCommand: SOCKS request had domain name with 0 length")
			return
		}
		var addr []byte
		if addr, err = socksReadBytes(rw.Reader, int(alen)); err != nil {
			err = newTemporaryNetError("socks5ReadCommand: Failed to read domain name: %s", err)
			return
		}
		host = string(addr)

	case socksAtypeV6:
		var rawAddr []byte
		if rawAddr, err = socksReadBytes(rw.Reader, net.IPv6len); err != nil {
			err = newTemporaryNetError("socks5ReadCommand: Failed to read IPv6 address: %s", err)
			return
		}
		addr := make(net.IP, net.IPv6len)
		copy(addr[:], rawAddr[:])
		host = fmt.Sprintf("[%s]", addr.String())

	default:
		sendErrResp(SocksRepAddressNotSupported)
		err = newTemporaryNetError("socks5ReadCommand: SOCKS request had unsupported address type 0x%02x", atype)
		return
	}
	var rawPort []byte
	if rawPort, err = socksReadBytes(rw.Reader, 2); err != nil {
		err = newTemporaryNetError("socks5ReadCommand: Failed to read port number: %s", err)
		return
	}
	port := int(rawPort[0])<<8 | int(rawPort[1])<<0

	if err = socksFlushBuffers(rw); err != nil {
		err = newTemporaryNetError("socks5ReadCommand: Failed to flush buffers: %s", err)
		return
	}

	req.Target = fmt.Sprintf("%s:%d", host, port)
	return
}
1306908
func SOCKS4Handler(opts ...HandlerOption) Handler {
	h := &socks4Handler{}
	h.Init(opts...)

	return h
}
1069011
func NewProxyConnection() *ProxyConnection {
	nonce, err := crypto.GenerateRandomBytes(16)
	if err != nil {
		return nil
	}

	return &ProxyConnection{
		state: ClientTokenSend,
		Auth: AuthInfo{
			LocalContext: nonce,
		},
	}
}
1049278
func socketAddr() (addr [4]byte, err error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return
	}

	for _, a := range addrs {
		if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if len(ipnet.IP.To4()) == net.IPv4len {
				copy(addr[:], ipnet.IP.To4())
				return
			}
		}
	}
	err = errors.New("You do not appear to be connected to the Internet")
	return
}
1073142
func socks5Handshake(rw *bufio.ReadWriter) (req SocksRequest, err error) {
	// Negotiate the authentication method.
	var method byte
	if method, err = socks5NegotiateAuth(rw); err != nil {
		return
	}

	// Authenticate the client.
	if err = socks5Authenticate(rw, method, &req); err != nil {
		return
	}

	// Read the command.
	err = socks5ReadCommand(rw, &req)
	return
}
1134617
func (soc *Socket) SetVmciBufferMaxSize(value uint64) error {
	if minor < 2 {
		return ErrorNotImplemented42
	}
	return soc.setUInt64(C.ZMQ_VMCI_BUFFER_MAX_SIZE, value)
}
300588
func ZmqRequest(_socket *zmq.Socket, dat ...string) (string, error) {
	_msg := strings.Join(dat, " ")
	_socket.Send([]byte(_msg), 0)
	response, err := _socket.Recv(0)
	return string(response), err
}
587020
func (conn *SocksConn) Reject() error {
	if conn.socksVersion == socks4Version {
		return sendSocks4aResponseRejected(conn)
	}
	return sendSocks5ResponseRejected(conn, SocksRepGeneralFailure)
}
1002591
func (soc *Socket) GetVmciBufferMaxSize() (uint64, error) {
	if minor < 2 {
		return 0, ErrorNotImplemented42
	}
	return soc.getUInt64(C.ZMQ_VMCI_BUFFER_MAX_SIZE)
}