| 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. | #!/usr/bin/python # ^----- change this if necessary! # # PTF - Python TCP Forwarder # Version: 0.4 # (C) 1999 T. Klausmann <[email protected]> # # This program is covered by the GNU public license (GPL) # # The old "Header Docs" will no longer be updated, read # the included file "README" instead. # import SocketServer from SocketServer import TCPServer, BaseRequestHandler from time import sleep from socket import * from os import fork from posix import waitpid, getppid, WNOHANG, kill from signal import SIGTERM from errno import * from sys import * from string import * # If someone can tell me how to avoid these # global vars, go ahead! global foreign_host global foreign_port global local_port class MyTCPServer(SocketServer.TCPServer): def server_bind(self): self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.socket.bind(self.server_address) class myhandler(BaseRequestHandler): global foreign_host global foreign_port global local_port # the core request handler 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() # End of class myhandler # Main program def main(): global foreign_host global foreign_port global local_port # This sanity check is way too simple # future versions should have more pedantic checks if (len(argv)<4): print "Usage:" print "%s foreign_host foreign_port local_port" % argv[0] exit (1) foreign_host=argv[1] foreign_port=atoi(argv[2]) local_port=atoi(argv[3]) # Fork off daemon process and exit firstpid=fork() if (firstpid>0): return (0) elif (firstpid<0): print ("Could not fork off server process") exit (1) # try to init the server try: myserver=MyTCPServer(('localhost',local_port),myhandler) except: print ("Could not start TCPServer. Adress already bound?") exit (1) # start the server to run forever myserver.serve_forever() if __name__ == "__main__": main() |