Ejemplo n.º 1
0
def tcp4_to_unix(local_port, unix_path):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                           socket.IPPROTO_TCP)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        server.bind(('127.0.0.1', local_port))
    except socket.error as e:
        sys.stderr.write('remote cant grab port %d\n' % local_port)
        # let other end time to connect to maintain ssh up
        time.sleep(10)
        sys.exit(0)
    server.listen(32)

    while True:
        (rl, wl, el) = select.select([server], [], [server], 1)
        if server in rl:
            (client, _) = server.accept()
            if not posix.fork():
                unix = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                try:
                    unix.connect(unix_path)
                except socket.error as e:
                    print 'Unable to grab %s: %s' % (unix_path, e)
                pipe = FdPipe(client, client, unix, unix)
                while pipe.one_loop():
                    pass
                return
            client.close()
        try:
            posix.waitpid(-1, posix.WNOHANG)
        except OSError:
            pass
Ejemplo n.º 2
0
def tcp4_to_unix(local_port, unix_path):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                           socket.IPPROTO_TCP)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        server.bind(('127.0.0.1', local_port))
    except socket.error as e:
        sys.stderr.write('remote cant grab port %d\n' % local_port)
        # let other end time to connect to maintain ssh up
        time.sleep(10)
        sys.exit(0)
    server.listen(32)

    while True:
        (rl, wl, el) = select.select([server], [], [server], 1)
        if server in rl:
            (client, _) = server.accept()
            if not posix.fork():
                unix = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                try:
                    unix.connect(unix_path)
                except socket.error as e:
                    print('Unable to grab %s: %s' % (unix_path, e))
                pipe = FdPipe(client, client, unix, unix)
                while pipe.one_loop():
                    pass
                return
            client.close()
        try:
            posix.waitpid(-1, posix.WNOHANG)
        except OSError:
            pass
Ejemplo n.º 3
0
 def test_exit(self):
     try:
         import posix, _rawffi
     except ImportError:
         skip("requires posix.fork() to test")
     #
     pid = posix.fork()
     if pid == 0:
         _rawffi.exit(5)   # in the child
     pid, status = posix.waitpid(pid, 0)
     assert posix.WIFEXITED(status)
     assert posix.WEXITSTATUS(status) == 5
Ejemplo n.º 4
0
    def wait():
        """ wait() -> (pid, status)

        Wait for completion of a child process.
        """
        return posix.waitpid(-1, 0)
Ejemplo n.º 5
0
 def handle (self):

	# Init server data variable (for while())
	server_input=client_input="DEADBEEF"

	# Get FD from listener
	client_fd=self.request

	# Open socket to serving machine and connect
	server_fd=socket(AF_INET,SOCK_STREAM)
	server_fd.connect (foreign_host,foreign_port)

	# Now, we fork and handle data in both directions
	CPID=fork()

	# Where are we?
	if (CPID<0):
 	  # oops, somehting went wrong
	  print "Could not fork."
	  return(0)

	if (CPID==0):
	  # This is the child, get parent pid
	  PPID=getppid()

	  # infinite loop with break condition
	  # XXX this is not elegant all
	  while(1):
	   # read 1024 bytes from FD
	   server_input=server_fd.recv(1024)

	   # if we couldn't read, back out
	   if not server_input: break;

	   # write data to client
	   client_fd.send(server_input)

	  # If we were disconnected, kill away the parent
	  kill (PPID, SIGTERM);

        else:
	  # This is the parent, child PID is CPID

	  # infinite loop with break condition 
          # XXX this is not elegant

	  while (1):
	   # Fetch signals and back out if kid is dead
	   if (not waitpid(CPID,WNOHANG)): break;

	   # Read from client FD
	   client_input=client_fd.recv(1024)

	   # Back out if we couldn't read anything
	   if not client_input: break; 

	   # Write data to server
	   server_fd.send(client_input)

	  # Get rid of the kid
	  kill (CPID, SIGTERM);

	# This actually should be a "can't happen",
	# but I am not sure
	# Close remaining FDs
	client_fd.close()
	server_fd.close()
Ejemplo n.º 6
0
    def wait():
        """ wait() -> (pid, status)

        Wait for completion of a child process.
        """
        return posix.waitpid(-1, 0)
Ejemplo n.º 7
0
        (rl, wl, el) = select.select([server], [], [server], 1)
        if server in rl:
            (client, _) = server.accept()
            if not posix.fork():
                unix = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                try:
                    unix.connect(unix_path)
                except socket.error, e:
                    print "Unable to grab %s: %s" % (unix_path, e)
                pipe = FdPipe(client, client, unix, unix)
                while pipe.one_loop():
                    pass
                return
            client.close()
        try:
            posix.waitpid(-1, posix.WNOHANG)
        except OSError:
            pass


def find_port(addr, user):
    """Find local port in existing tunnels"""
    home = pwd.getpwuid(os.getuid()).pw_dir
    for name in os.listdir("%s/.ssh/" % home):
        if name.startswith("unixpipe_%s@%s_" % (user, addr)):
            return int(name.split("_")[2])


def new_port():
    """Find a free local port and allocate it"""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)