# Path: sshuttle/helpers.py
# def b(s):
#     return s.encode("ASCII")
# 
# def log(s):
#     global logprefix
#     try:
#         sys.stdout.flush()
#         # Put newline at end of string if line doesn't have one.
#         if not s.endswith("\n"):
#             s = s+"\n"
# 
#         prefix = logprefix
#         s = s.rstrip("\n")
#         for line in s.split("\n"):
#             # We output with \r\n instead of \n because when we use
#             # sudo with the use_pty option, the firewall process, the
#             # other processes printing to the terminal will have the
#             # \n move to the next line, but they will fail to reset
#             # cursor to the beginning of the line. Printing output
#             # with \r\n endings fixes that problem and does not appear
#             # to cause problems elsewhere.
#             sys.stderr.write(prefix + line + "\r\n")
#             prefix = "    "
#         sys.stderr.flush()
#     except IOError:
#         # this could happen if stderr gets forcibly disconnected, eg. because
#         # our tty closes.  That sucks, but it's no reason to abort the program.
#         pass
# 
# def debug1(s):
#     if verbose >= 1:
#         log(s)
# 
# def debug2(s):
#     if verbose >= 2:
#         log(s)
# 
# def debug3(s):
#     if verbose >= 3:
#         log(s)
# 
# class Fatal(Exception):
#     pass


import sys
import struct
import socket
import errno
import select
import os
import fcntl
from sshuttle.helpers import b, log, debug1, debug2, debug3, Fatal
                # automatically, so this code won't run.
                realerr = self.rsock.getsockopt(socket.SOL_SOCKET,
                                                socket.SO_ERROR)
                e = socket.error(realerr, os.strerror(realerr))
                debug3('%r: fixed connect result: %s' % (self, e))
            if e.args[0] in [errno.EINPROGRESS, errno.EALREADY]:
                pass  # not connected yet
            elif e.args[0] == 0:
                # connected successfully (weird Linux bug?)
                # Sometimes Linux seems to return EINVAL when it isn't
                # invalid.  This *may* be caused by a race condition
                # between connect() and getsockopt(SO_ERROR) (ie. it
                # finishes connecting in between the two, so there is no
                # longer an error).  However, I'm not sure of that.
                #
                # I did get at least one report that the problem went away
                # when we added this, however.
                self.connect_to = None
            elif e.args[0] == errno.EISCONN:
                # connected successfully (BSD)
                self.connect_to = None
            elif e.args[0] in NET_ERRS + [errno.EACCES, errno.EPERM]:
                # a "normal" kind of error
                self.connect_to = None
                self.seterr(e)
            else:
                raise  # error we've never heard of?!  barf completely.

    def noread(self):
        if not self.shut_read:

# Path: sshuttle/helpers.py
# def b(s):
#     return s.encode("ASCII")
# 
# def log(s):
#     global logprefix
#     try:
#         sys.stdout.flush()
#         # Put newline at end of string if line doesn't have one.
#         if not s.endswith("\n"):
#             s = s+"\n"
# 
#         prefix = logprefix
#         s = s.rstrip("\n")
#         for line in s.split("\n"):
#             # We output with \r\n instead of \n because when we use
#             # sudo with the use_pty option, the firewall process, the
#             # other processes printing to the terminal will have the
#             # \n move to the next line, but they will fail to reset
#             # cursor to the beginning of the line. Printing output
#             # with \r\n endings fixes that problem and does not appear
#             # to cause problems elsewhere.
#             sys.stderr.write(prefix + line + "\r\n")
#             prefix = "    "
#         sys.stderr.flush()
#     except IOError:
#         # this could happen if stderr gets forcibly disconnected, eg. because
#         # our tty closes.  That sucks, but it's no reason to abort the program.
#         pass
# 
# def debug1(s):
#     if verbose >= 1:
#         log(s)
# 
# def debug2(s):
#     if verbose >= 2:
#         log(s)
# 
# def debug3(s):
#     if verbose >= 3:
#         log(s)
# 
# class Fatal(Exception):
#     pass


# Path: sshuttle/ssnet.py
import sys
import struct
import socket
import errno
import select
import os
import fcntl
from sshuttle.helpers import b, log, debug1, debug2, debug3, Fatal

                # automatically, so this code won't run.
                realerr = self.rsock.getsockopt(socket.SOL_SOCKET,
                                                socket.SO_ERROR)
                e = socket.error(realerr, os.strerror(realerr))
                debug3('%r: fixed connect result: %s' % (self, e))
            if e.args[0] in [errno.EINPROGRESS, errno.EALREADY]:
                pass  # not connected yet
            elif e.args[0] == 0:
                # connected successfully (weird Linux bug?)
                # Sometimes Linux seems to return EINVAL when it isn't
                # invalid.  This *may* be caused by a race condition
                # between connect() and getsockopt(SO_ERROR) (ie. it
                # finishes connecting in between the two, so there is no
                # longer an error).  However, I'm not sure of that.
                #
                # I did get at least one report that the problem went away
                # when we added this, however.
                self.connect_to = None
            elif e.args[0] == errno.EISCONN:
                # connected successfully (BSD)
                self.connect_to = None
            elif e.args[0] in NET_ERRS + [errno.EACCES, errno.EPERM]:
                # a "normal" kind of error
                self.connect_to = None
                self.seterr(e)
            else:
                raise  # error we've never heard of?!  barf completely.

    def noread(self):
        if not self.shut_read:

            debug2('%r: done reading' % self)